Skip to content

Commit

Permalink
html: add "GFM quirks" mode.
Browse files Browse the repository at this point in the history
  • Loading branch information
kivikakk committed Jul 10, 2024
1 parent 4de32be commit 59d39df
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
5 changes: 3 additions & 2 deletions src/html.rs
Original file line number Diff line number Diff line change
Expand Up @@ -774,8 +774,9 @@ impl<'o> HtmlFormatter<'o> {
}
NodeValue::Strong => {
let parent_node = node.parent();
if parent_node.is_none()
|| !matches!(parent_node.unwrap().data.borrow().value, NodeValue::Strong)
if !self.options.render.gfm_quirks
|| (parent_node.is_none()
|| !matches!(parent_node.unwrap().data.borrow().value, NodeValue::Strong))
{
if entering {
self.output.write_all(b"<strong")?;
Expand Down
7 changes: 5 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,10 @@ struct Cli {
#[arg(long)]
full_info_string: bool,

/// Enable GitHub-flavored markdown extensions: strikethrough, tagfilter, table, autolink, and tasklist.
/// Also enables --github-pre-lang.
/// Enable GitHub-flavored markdown extensions: strikethrough, tagfilter,
/// table, autolink, and tasklist. Also enables --github-pre-lang, and
/// enables GFM-style quirks in output HTML, such as not nesting <strong>
/// tags, which otherwise breaks CommonMark compatibility.
#[arg(long)]
gfm: bool,

Expand Down Expand Up @@ -284,6 +286,7 @@ fn main() -> Result<(), Box<dyn Error>> {
.escaped_char_spans(cli.escaped_char_spans)
.ignore_setext(cli.ignore_setext)
.ignore_empty_links(cli.ignore_empty_links)
.gfm_quirks(cli.gfm)
.build()?;

let options = Options {
Expand Down
16 changes: 16 additions & 0 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -766,6 +766,22 @@ pub struct RenderOptions {
/// assert_eq!(markdown_to_html(input, &options), "<p>[]()</p>\n");
/// ```
pub ignore_empty_links: bool,

/// Enables GFM quirks in HTML output which break CommonMark compatibility.
///
/// ```rust
/// # use comrak::{markdown_to_html, Options};
/// let mut options = Options::default();
/// let input = "****abcd**** *_foo_*";
///
/// assert_eq!(markdown_to_html(input, &options),
/// "<p><strong><strong>abcd</strong></strong> <em><em>foo</em></em></p>\n");
///
/// options.render.gfm_quirks = true;
/// assert_eq!(markdown_to_html(input, &options),
/// "<p><strong>abcd</strong> <em><em>foo</em></em></p>\n");
/// ```
pub gfm_quirks: bool,
}

#[non_exhaustive]
Expand Down

0 comments on commit 59d39df

Please sign in to comment.