From 1428013d52e87528e01503cc6cf37c74614a8c7d Mon Sep 17 00:00:00 2001 From: Noir <160358296+afifm561@users.noreply.github.com> Date: Mon, 12 Oct 2026 23:02:21 +0000 Subject: [PATCH] Don't leave an empty paragraph before a setext underline (#262) 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
. Parse the definitions on a copy of the paragraph content, so the paragraph is only changed when it actually becomes a heading. --- lib/blocks.js | 19 ++++++++----------- test/regression.txt | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 11 deletions(-) diff --git a/lib/blocks.js b/lib/blocks.js index c74a278b..b3c5b8a0 100644 --- a/lib/blocks.js +++ b/lib/blocks.js @@ -607,23 +607,20 @@ var blockStarts = [ .match(reSetextHeadingLine)) ) { parser.closeUnmatchedBlocks(); - // resolve reference link definitiosn + // resolve reference link definitions on a copy, so the paragraph + // is left untouched if this turns out not to be a heading (#262) var pos; + var content = container._string_content; while ( - peek(container._string_content, 0) === C_OPEN_BRACKET && - (pos = parser.inlineParser.parseReference( - container._string_content, - parser.refmap - )) + peek(content, 0) === C_OPEN_BRACKET && + (pos = parser.inlineParser.parseReference(content, parser.refmap)) ) { - container._string_content = container._string_content.slice( - pos - ); + content = content.slice(pos); } - if (container._string_content.length > 0) { + if (content.length > 0) { var heading = new Node("heading", container.sourcepos); heading.level = match[0][0] === "=" ? 1 : 2; - heading._string_content = container._string_content; + heading._string_content = content; container.insertAfter(heading); container.unlink(); parser.tip = heading; diff --git a/test/regression.txt b/test/regression.txt index 46b5e6bb..369d921e 100644 --- a/test/regression.txt +++ b/test/regression.txt @@ -665,3 +665,42 @@ Entities inside autolinks (#263).
````````````````````````````````
+
+Issue #262 - link reference definition followed by a setext underline
+must not leave an empty paragraph behind.
+
+```````````````````````````````` example
+[a]: b
+---
+.
+===
+```````````````````````````````` + +```````````````````````````````` example +[a]: b +--- + +[a]: b +=== +. +===
+```````````````````````````````` + +```````````````````````````````` example +[a]: b +Foo +--- + +[a] +. +