Skip to content

Fix fence-like lines outside the container of an open code fence - #470

Open
wleeaf wants to merge 1 commit into
mity:masterfrom
wleeaf:fix-fence-outside-container
Open

wleeaf wants to merge 1 commit into
mity:masterfrom
wleeaf:fix-fence-outside-container

Conversation

@wleeaf

@wleeaf wleeaf commented Oct 24, 2026

Copy link
Copy Markdown

When a code fence is open inside a block quote or list item, a following line that does not continue that container still goes through the closing-fence check. As a result, it either closes the fence, or it is silently dropped if it starts with fence characters but is not a valid closing fence:

$ printf '> ```\n> code\n```\nafter' | md2html
<blockquote>
<pre><code>code
</code></pre>
</blockquote>
<p>after</p>

$ printf '> ```\n> code\n``\nafter' | md2html
<blockquote>
<pre><code>code
</code></pre>
</blockquote>
<p>after</p>

In the second case the "" line disappears. cmark and commonmark.js end the block quote at the unprefixed line, as fenced code cannot be continued lazily. The "```" therefore opens a new code block (`<pre><code>after\n</code></pre>`), and "" is kept as paragraph text (<p>``\nafter</p>). The same happens with list items (- ```\n code\n```\nafter).

In md_analyze_line(), the fenced code continuation branch ran even when n_parents < ctx->n_containers. Its later n_parents == ctx->n_containers check only prevented the line from becoming code. md_is_closing_code_fence() had already turned a real closing fence into MD_LINE_BLANK, and on failure it had advanced off past the fence characters. The branch is now entered only when all containers continue, so the line is analyzed like any other line that ends the container.

Added three examples to test/regressions.txt; they fail before the change and pass after. python3 scripts/run-tests.py passes with a -Werror Debug build.

Found by differential testing against cmark.

Copilot AI balanced review requested due to automatic review settings October 24, 2026 13:27

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

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.

2 participants