Fix: keep trailing content out of an unmatched emphasis closer (#743) - #950
Open
dualfroz wants to merge 1 commit into
Open
Fix: keep trailing content out of an unmatched emphasis closer (#743)#950dualfroz wants to merge 1 commit into
dualfroz wants to merge 1 commit into
Conversation
When EnableTrackTrivia() is enabled, an emphasis closer whose delimiter run is only partially consumed (its leftover dropping below the minimum count) kept the inlines that follow it nested inside the emphasis. The roundtrip renderer then emitted the closing delimiters after that trailing content, corrupting the output, for example a grid table separator row using the "marked" (==) delimiter from EmphasisExtras. Move the trailing content out to the outermost enclosing emphasis when the closer can no longer be matched, mirroring the existing handling for a fully consumed opener. The leftover closer keeps its position, so cases without trailing content are unaffected. Adds a regression test.
dualfroz
force-pushed
the
fix/emphasis-trivia-roundtrip-743
branch
from
September 5, 2026 23:19
f57efa9 to
0a17da1
Compare
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
Fixes #743
Problem
Since v28.0, parsing a grid-table-like block with
EnableTrackTrivia()and thenrendering it back with
RoundtripRendererproduces invalid output. The reportedinput round-trips cleanly up to v27.0 but is corrupted from v28.0 onward:
Input:
Corrupted round-trip output (the middle separator column collapses to a single
=and a stray run of=is appended after the last line):Root cause
The block is a plain paragraph (the reporter's pipeline does not enable grid
tables). The
+===...===+separators are runs of=, which the EmphasisExtras"marked" delimiter (
==, minimum count 2) parses as emphasis.EmphasisInlineParser.ProcessEmphasismatches an opener run against a closer run.Each match embraces everything that follows the opener - including the closer and
every inline after it - and later moves the trailing content back out. That
"move out" step only runs when the closer is fully consumed (
DelimiterCount == 0)or when the opener is fully consumed. When both the opener and the closer keep
leftover delimiter characters that fall below the minimum count (e.g. a run of 15
=is 7 pairs plus one leftover), the trailing content stays nested inside theinnermost emphasis. The roundtrip renderer then writes the closing delimiters
after that trailing content, which pushes the closing run to the end of the block.
The regression was introduced by
Fix emphasis when EnableTrackTrivia() is used (#561), which made emphasis detection work under trivia and thereby exposed thispre-existing gap in the leftover-closer handling.
Fix
After the matching loop for a closer, if the closer still holds leftover delimiter
characters and is nested inside the emphasis it closed, move the inlines that
follow the closer out to the outermost enclosing emphasis. This mirrors the
existing behaviour for a fully consumed opener. The leftover closer itself keeps
its position, so cases with no trailing content (already covered by
TestEmphasisExtended, e.g.2223222->2<two-only>32</two-only>) areunchanged.
One file changed:
src/Markdig/Parsers/Inlines/EmphasisInlineParser.cs.Tests
Added
src/Markdig.Tests/TestEmphasisRoundtrip.cs:GridTableSeparatorRoundtripsWithTrackTrivia- the exact issue input round-tripsto itself.
UnbalancedMarkedRunsRoundtrip- minimal reduction (+===============+===============+).BalancedMarkedEmphasisStillParses-==bold==still renders<mark>bold</mark>,confirming emphasis detection under EmphasisExtras is preserved.
The two round-trip tests fail before the fix and pass after it. The full test
suite passes (3798 passed, 1 pre-existing skip, 0 failed).