Skip to content

Avoid merging emphasis delimiter with preceding same character in MarkdownRenderer - #484

Open
amalsaidov wants to merge 1 commit into
commonmark:mainfrom
amalsaidov:fix-markdown-emphasis-delimiter-merge
Open

amalsaidov wants to merge 1 commit into
commonmark:mainfrom
amalsaidov:fix-markdown-emphasis-delimiter-merge

Conversation

@amalsaidov

Copy link
Copy Markdown

Problem

MarkdownRenderer can change the meaning of a document when it renders an Emphasis whose parsed opening delimiter directly follows the same character that was just written — for example a literal * (escaped from the source) or an adjacent emphasis. The emphasis delimiter then merges with the preceding character when the output is parsed again.

Example:

Parser parser = Parser.builder().build();
HtmlRenderer html = HtmlRenderer.builder().build();
MarkdownRenderer md = MarkdownRenderer.builder().build();

String src = "*****Hello*world****";
Node doc = parser.parse(src);

html.render(doc);                     // <p>**<em><strong>Hello<em>world</em></strong></em></p>
String markdown = md.render(doc);     // \*\****Hello*world****   <-- delimiters merged
html.render(parser.parse(markdown));  // <p>**<strong><em>Hello</em>world</strong>**</p>  <-- different!

The AST is Text("**"), then Emphasis(*) > StrongEmphasis(**) > .... The literal ** is correctly escaped to \*\*, but the following emphasis then reuses the parsed * delimiter right after it, so on a re-parse the runs merge and the nesting flips.

This was found by round-tripping the cmark and commonmark.js regression suites (parse → render Markdown → parse → render HTML) and comparing the HTML; it was the only failing example.

Fix

The renderer already switches between * and _ to avoid merged delimiters, but only when no parsed delimiter is available (delimiter == null). This applies the same switch when reusing a parsed delimiter would directly follow the same character (writer.getLastChar() == delimiter.charAt(0)).

With the fix, *****Hello*world**** renders to \*\*_**Hello*world***_, which round-trips to the original HTML.

Tests

  • Added testEmphasisDelimiterNextToSameLiteralCharacter to MarkdownRendererTest, asserting that the Markdown round-trip preserves the HTML. It fails before the change and passes after.
  • Full commonmark core suite passes (2307 tests, 1 pre-existing skip).
  • Re-ran the round-trip check over spec.txt, gfm-spec.txt, cmark-regression.txt and commonmark.js-regression.txt: 0 discrepancies (was 1).

…kdownRenderer

When the MarkdownRenderer rendered an Emphasis whose parsed opening
delimiter directly followed the same character already written (for
example a literal "*" escaped from the source, or an adjacent
emphasis), it reused that delimiter verbatim. The delimiter run then
merged with the preceding character on a re-parse and changed the
meaning of the output.

For example, "*****Hello*world****" parses to
"**<em><strong>Hello<em>world</em></strong></em>", but rendering it
back to Markdown produced "\*\****Hello*world****", which re-parses
to "**<strong><em>Hello</em>world</strong>**".

The existing logic already switches between "*" and "_" to avoid
this when no parsed delimiter is available; apply the same switch when
reusing a parsed delimiter would directly follow the same character.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant