From 920923c98bacbe53b59e4efa30123d32b2f44076 Mon Sep 17 00:00:00 2001 From: Christopher Pruijsen Date: Mon, 12 Oct 2026 15:33:34 +0100 Subject: [PATCH 1/2] Fix precedence of link reference definitions in setext headings Link reference definitions at the start of a paragraph that becomes a setext heading were registered in the refmap when the heading underline was parsed, ahead of definitions in earlier ordinary paragraphs, which are registered only at document finalization. Per the spec, the first matching definition in the document wins. Scan the leading definitions with a throwaway refmap instead, leave them in the paragraph so the end-of-document pass registers them in document order, and put only the remaining content in the heading. --- lib/blocks.js | 49 ++++++++++++++++++++++++++++++++++++--------- test/regression.txt | 37 ++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+), 10 deletions(-) diff --git a/lib/blocks.js b/lib/blocks.js index c74a278b..79c65683 100644 --- a/lib/blocks.js +++ b/lib/blocks.js @@ -609,23 +609,52 @@ var blockStarts = [ parser.closeUnmatchedBlocks(); // resolve reference link definitiosn var pos; + var refmap = {}; // dummy map, just to determine consumed length + var refdefLength = 0; while ( - peek(container._string_content, 0) === C_OPEN_BRACKET && + peek(container._string_content, refdefLength) === + C_OPEN_BRACKET && (pos = parser.inlineParser.parseReference( - container._string_content, - parser.refmap + container._string_content.slice(refdefLength), + refmap )) ) { - container._string_content = container._string_content.slice( - pos - ); + refdefLength += pos; } - if (container._string_content.length > 0) { - var heading = new Node("heading", container.sourcepos); + if (container._string_content.length > refdefLength) { + // Leave link reference definitions in the paragraph, so + // that they are added to parser.refmap in document order + // when parsing ends (removeLinkReferenceDefinitions); + // the heading gets only the remaining content. + var refdefText = container._string_content.slice( + 0, + refdefLength + ); + var refdefLines = refdefText.split("\n"); + var heading = new Node("heading", [ + [ + container.sourcepos[0][0] + refdefLines.length - 1, + container.sourcepos[0][1] + ], + container.sourcepos[1] + ]); heading.level = match[0][0] === "=" ? 1 : 2; - heading._string_content = container._string_content; + heading._string_content = container._string_content.slice( + refdefLength + ); container.insertAfter(heading); - container.unlink(); + if (refdefLength > 0) { + container._string_content = refdefText; + container._open = false; + container.sourcepos[1] = [ + container.sourcepos[0][0] + refdefLines.length - 2, + container.sourcepos[0][1] + + refdefLines[refdefLines.length - 2].length - + 1 + ]; + } else { + container.unlink(); + } parser.tip = heading; parser.advanceOffset( parser.currentLine.length - parser.offset, diff --git a/test/regression.txt b/test/regression.txt index 46b5e6bb..65c2998b 100644 --- a/test/regression.txt +++ b/test/regression.txt @@ -665,3 +665,40 @@ Entities inside autolinks (#263).
```````````````````````````````` +Link reference definitions in setext headings do not take precedence +over earlier definitions in ordinary paragraphs (#298). + +```````````````````````````````` example +[x]: fizz + +[x]: buzz +=== +[x] +. +

=== +x

+```````````````````````````````` + +The definition is still resolved, in document order (#298). + +```````````````````````````````` example +[x]: fizz + +[x]: buzz +Title +=== +[x] +. +

Title

+

x

+```````````````````````````````` + +A paragraph containing only link reference definitions before a +thematic break leaves no empty paragraph (#262). + +```````````````````````````````` example +[a]: b +--- +. +
+```````````````````````````````` From 88d7e0f2222dce5ee283ebdb4f83552e66581805 Mon Sep 17 00:00:00 2001 From: Christopher Pruijsen Date: Tue, 13 Oct 2026 10:45:00 +0100 Subject: [PATCH 2/2] Add regression cases from #262 for definitions before setext underlines --- test/regression.txt | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/test/regression.txt b/test/regression.txt index 65c2998b..2fff1538 100644 --- a/test/regression.txt +++ b/test/regression.txt @@ -702,3 +702,39 @@ thematic break leaves no empty paragraph (#262). .
```````````````````````````````` + +A link reference definition alone cannot form a setext heading (#262). + +```````````````````````````````` example +[a]: b +=== +. +

===

+```````````````````````````````` + +Link reference definitions around a thematic break leave no empty +paragraph or setext heading (#262). + +```````````````````````````````` example +[a]: b +--- + +[a]: b +=== +. +
+

===

+```````````````````````````````` + +A link reference definition before a setext heading remains available (#262). + +```````````````````````````````` example +[a]: b +Foo +--- + +[a] +. +

Foo

+

a

+````````````````````````````````