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..2fff1538 100644 --- a/test/regression.txt +++ b/test/regression.txt @@ -665,3 +665,76 @@ 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 +--- +. +
+```````````````````````````````` + +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

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