Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/s-expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ fn dump(source: &str) -> io::Result<()> {
.math_code(true)
.wikilinks_title_after_pipe(true)
.wikilinks_title_before_pipe(true)
.subtext(true)
.build();

let opts = Options {
Expand Down
1 change: 1 addition & 0 deletions fuzz/fuzz_targets/all_options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ fuzz_target!(|s: &str| {
image_url_rewriter: Some(Arc::new(url_rewriter)),
link_url_rewriter: Some(Arc::new(url_rewriter)),
cjk_friendly_emphasis: true,
subtext: true,
};

let cb = |link_ref: options::BrokenLinkReference| {
Expand Down
13 changes: 13 additions & 0 deletions src/cm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,7 @@ impl<'a, 'o, 'c> CommonMarkFormatter<'a, 'o, 'c> {
NodeValue::SpoileredText => self.format_spoiler()?,
NodeValue::EscapedTag(ref net) => self.format_escaped_tag(net)?,
NodeValue::Alert(ref alert) => self.format_alert(alert, entering)?,
NodeValue::Subtext => self.format_subtext(entering)?,
};
Ok(true)
}
Expand Down Expand Up @@ -998,6 +999,18 @@ impl<'a, 'o, 'c> CommonMarkFormatter<'a, 'o, 'c> {
}
Ok(())
}

fn format_subtext(&mut self, entering: bool) -> fmt::Result {
if entering {
write!(self, "-# ")?;
self.begin_content = true;
self.no_linebreaks = true;
} else {
self.no_linebreaks = false;
self.blankline();
}
Ok(())
}
}

fn longest_byte_sequence(buffer: &[u8], ch: u8) -> usize {
Expand Down
18 changes: 18 additions & 0 deletions src/html.rs
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,7 @@ pub fn format_node_default<'a, T>(
NodeValue::Superscript => render_superscript(context, node, entering),
NodeValue::Underline => render_underline(context, node, entering),
NodeValue::WikiLink(ref nwl) => render_wiki_link(context, node, entering, nwl),
NodeValue::Subtext => render_subtext(context, node, entering),
}
}

Expand Down Expand Up @@ -1443,6 +1444,23 @@ fn render_subscript<'a, T>(
Ok(ChildRendering::HTML)
}

fn render_subtext<'a, T>(
context: &mut Context<T>,
node: Node<'a>,
entering: bool,
) -> Result<ChildRendering, fmt::Error> {
if entering {
context.cr()?;
context.write_str("<p><sub")?;
render_sourcepos(context, node)?;
context.write_str(">")?;
} else {
writeln!(context, "</sub></p>")?;
}

Ok(ChildRendering::HTML)
}

fn render_superscript<'a, T>(
context: &mut Context<T>,
node: Node<'a>,
Expand Down
4 changes: 3 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ enum Extension {
Greentext,
Alerts,
CjkFriendlyEmphasis,
Subtext,
}

#[derive(Clone, Copy, Debug, ValueEnum)]
Expand Down Expand Up @@ -282,7 +283,8 @@ fn main() -> Result<(), Box<dyn Error>> {
.greentext(exts.contains(&Extension::Greentext))
.alerts(exts.contains(&Extension::Alerts))
.maybe_front_matter_delimiter(cli.front_matter_delimiter)
.cjk_friendly_emphasis(exts.contains(&Extension::CjkFriendlyEmphasis));
.cjk_friendly_emphasis(exts.contains(&Extension::CjkFriendlyEmphasis))
.subtext(exts.contains(&Extension::Subtext));

#[cfg(feature = "shortcodes")]
let extension = extension.shortcodes(cli.gemoji);
Expand Down
17 changes: 15 additions & 2 deletions src/nodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,10 @@ pub enum NodeValue {
/// **Block**. GitHub style alert boxes which uses a modified blockquote syntax.
/// Enabled with the `alerts` option.
Alert(Box<NodeAlert>),

/// **Block**. Block scoped subscript that acts similar to a header.
/// Enabled with `subtext` option.
Subtext,
}

/// Alignment of a single table cell.
Expand Down Expand Up @@ -561,14 +565,18 @@ impl NodeValue {
| NodeValue::TaskItem(..)
| NodeValue::MultilineBlockQuote(_)
| NodeValue::Alert(_)
| NodeValue::Subtext
)
}

/// Whether the type the node is of can contain inline nodes.
pub fn contains_inlines(&self) -> bool {
matches!(
*self,
NodeValue::Paragraph | NodeValue::Heading(..) | NodeValue::TableCell
NodeValue::Paragraph
| NodeValue::Heading(..)
| NodeValue::TableCell
| NodeValue::Subtext
)
}

Expand All @@ -595,7 +603,10 @@ impl NodeValue {
pub(crate) fn accepts_lines(&self) -> bool {
matches!(
*self,
NodeValue::Paragraph | NodeValue::Heading(..) | NodeValue::CodeBlock(..)
NodeValue::Paragraph
| NodeValue::Heading(..)
| NodeValue::CodeBlock(..)
| NodeValue::Subtext
)
}

Expand Down Expand Up @@ -644,6 +655,7 @@ impl NodeValue {
NodeValue::SpoileredText => "spoiler",
NodeValue::EscapedTag(_) => "escaped_tag",
NodeValue::Alert(_) => "alert",
NodeValue::Subtext => "subtext",
}
}
}
Expand Down Expand Up @@ -888,6 +900,7 @@ impl<'a> arena_tree::Node<'a, RefCell<Ast>> {
| NodeValue::SpoileredText
| NodeValue::Underline
| NodeValue::Subscript
| NodeValue::Subtext
// XXX: this is quite a hack: the EscapedTag _contains_ whatever was
// possibly going to fall into the spoiler. This should be fixed in
// inlines.
Expand Down
31 changes: 29 additions & 2 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,10 @@ where
break;
}
}
NodeValue::Heading(..) | NodeValue::TableRow(..) | NodeValue::TableCell => {
NodeValue::Heading(..)
| NodeValue::TableRow(..)
| NodeValue::TableCell
| NodeValue::Subtext => {
break;
}
NodeValue::FootnoteDefinition(..) => {
Expand Down Expand Up @@ -613,6 +616,7 @@ where
|| self.handle_multiline_blockquote(container, line)
|| self.handle_blockquote(container, line)
|| self.handle_atx_heading(container, line)
|| self.handle_atx_subtext(container, line)
|| self.handle_code_fence(container, line)
|| self.handle_html_block(container, line)
|| self.handle_setext_heading(container, line)
Expand Down Expand Up @@ -782,6 +786,26 @@ where
scanners::atx_heading_start(&line[self.first_nonspace..])
}

fn handle_atx_subtext(&mut self, container: &mut Node<'a>, line: &str) -> bool {
let Some(matched) = self.detect_atx_subtext(line) else {
return false;
};

let heading_startpos = self.first_nonspace;
let offset = self.offset;
self.advance_offset(line, heading_startpos + matched - offset, false);
*container = self.add_child(container, NodeValue::Subtext, heading_startpos + 1);

let container_ast = &mut container.data_mut();
container_ast.value = NodeValue::Subtext;

true
}

fn detect_atx_subtext(&self, line: &str) -> Option<usize> {
scanners::atx_subtext_start(&line[self.first_nonspace..])
}

fn handle_code_fence(&mut self, container: &mut Node<'a>, line: &str) -> bool {
let Some(matched) = self.detect_code_fence(line) else {
return false;
Expand Down Expand Up @@ -1328,7 +1352,10 @@ where

container.data_mut().last_line_blank = self.blank
&& match container.data().value {
NodeValue::BlockQuote | NodeValue::Heading(..) | NodeValue::ThematicBreak => false,
NodeValue::BlockQuote
| NodeValue::Heading(..)
| NodeValue::ThematicBreak
| NodeValue::Subtext => false,
NodeValue::CodeBlock(ref ncb) => !ncb.fenced,
NodeValue::Item(..) => {
container.first_child().is_some()
Expand Down
16 changes: 16 additions & 0 deletions src/parser/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,22 @@ pub struct Extension<'c> {
/// ```
#[cfg_attr(feature = "bon", builder(default))]
pub cjk_friendly_emphasis: bool,

/// Enables block scoped subscript that acts similar to a header.
///
/// ```md
/// -# subtext
/// ```
///
/// ```rust
/// # use comrak::{markdown_to_html, Options};
/// let mut options = Options::default();
/// options.extension.subscript = true;
///
/// assert_eq!(markdown_to_html("-# subtext", &options),
/// "<p><sub>subtext</sub></p>\n");
/// ```
pub subtext: bool,
}

impl<'c> Extension<'c> {
Expand Down
10 changes: 10 additions & 0 deletions src/scanners.re
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,16 @@ pub fn atx_heading_start(s: &str) -> Option<usize> {
*/
}

pub fn atx_subtext_start(s: &str) -> Option<usize> {
let mut cursor = 0;
let mut marker = 0;
let len = s.len();
/*!re2c
[-][#] ([ \t]+|[\r\n]) { return Some(cursor); }
* { return None; }
*/
}

pub fn html_block_end_1(s: &str) -> bool {
let mut cursor = 0;
let mut marker = 0;
Expand Down
124 changes: 123 additions & 1 deletion src/scanners.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ mod sourcepos_;
mod spoiler;
mod strikethrough;
mod subscript;
mod subtext;
mod supersubscript;
mod table;
mod tagfilter;
Expand Down
Loading
Loading