Skip to content

Don't leave an empty paragraph before a setext underline (#262) - #314

Closed
afifm561 wants to merge 1 commit into
commonmark:masterfrom
afifm561:fix/262-empty-paragraph
Closed

afifm561 wants to merge 1 commit into
commonmark:masterfrom
afifm561:fix/262-empty-paragraph

Conversation

@afifm561

Copy link
Copy Markdown

Fixes the first point of #262.

[a]: b
---

gave <p></p> followed by <hr />. It now gives only <hr />.

Cause. The setext heading block start in lib/blocks.js strips leading link reference definitions from container._string_content before it checks whether any heading text remains. When nothing remains, no heading is created, but the paragraph has already been emptied. removeLinkReferenceDefinitions only unlinks paragraphs whose definitions it parsed itself, so this paragraph survives and renders as <p></p>.

Fix. Parse the definitions on a local copy of the content. The paragraph is changed only when it actually becomes a heading. Otherwise it keeps its definitions, and removeLinkReferenceDefinitions handles it like any other paragraph.

Not changed. The second question in #262, whether --- here should be <hr /> or <p>---</p> (which cmark 0.30.2 gives), is left as is. The === case already gave <p>===</p> and still does.

Tests. I added 4 regression tests to test/regression.txt covering ---, ===, both in one document, and a definition followed by real heading text. npm test passes with 752 tests. Without the change in lib/blocks.js, 2 of the new tests fail.

Relation to #313. #313 also fixes this, as a side effect of fixing #298, the definition-order issue. This PR does not fix #298. This PR is a smaller alternative scoped to #262 only: +8/−11 in lib/blocks.js, with no change to heading sourcepos. If #313 is preferred, the extra regression cases here may still be useful there, and I'm happy to close this one.

)

When a paragraph contains only link reference definitions and is
followed by a setext heading underline, the setext block start stripped
the definitions from the paragraph before deciding not to create a
heading. The emptied paragraph was then skipped by
removeLinkReferenceDefinitions and rendered as <p></p>.

Parse the definitions on a copy of the paragraph content, so the
paragraph is only changed when it actually becomes a heading.
@jgm

jgm commented Oct 13, 2026

Copy link
Copy Markdown
Member

Superseded by #313

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.

Setext Heading LRDs take precedence over Paragraph LRDs

2 participants