Repository navigation
Avoid merging emphasis delimiter with preceding same character in MarkdownRenderer - #484
Open
amalsaidov wants to merge 1 commit into
Open
amalsaidov wants to merge 1 commit into
amalsaidov wants to merge 1 commit into
Conversation
…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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
MarkdownRenderercan change the meaning of a document when it renders anEmphasiswhose 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:
The AST is
Text("**"), thenEmphasis(*) > 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
cmarkandcommonmark.jsregression 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
testEmphasisDelimiterNextToSameLiteralCharactertoMarkdownRendererTest, asserting that the Markdown round-trip preserves the HTML. It fails before the change and passes after.commonmarkcore suite passes (2307 tests, 1 pre-existing skip).spec.txt,gfm-spec.txt,cmark-regression.txtandcommonmark.js-regression.txt: 0 discrepancies (was 1).