Skip to content

Don't parse a definition after other text on the destination line - #477

Merged
robinst merged 1 commit into
commonmark:mainfrom
bardrop-oss:fix-ref-def-text-after-destination
Nov 21, 2026
Merged

robinst merged 1 commit into
commonmark:mainfrom
bardrop-oss:fix-ref-def-text-after-destination

Conversation

@bardrop

@bardrop bardrop commented Nov 20, 2026

Copy link
Copy Markdown
Contributor

When a link reference definition's destination is followed by other text on the same line, the line is not a definition. But if that text contains another [label]: dest, commonmark-java parses that one as a definition and the whole line disappears:

[foo]: /url [bar]: /url2

[foo] [bar]
  • commonmark.js / cmark: <p>[foo]: /url [bar]: /url2</p> <p>[foo] [bar]</p>
  • commonmark-java 0.33.0: first line gone, <p>[foo] <a href="/url2">bar</a></p>

Same for e.g. [x]: /u``_> [x]: /u (defines x as /u, line removed).

Cause: startTitle goes back to START_DEFINITION "for the same character" when no title delimiter follows. That is right when the title would be on a later line ([a]: /a then [b]: /b on the next line), but not on the destination's own line.

Fix: in destination(), if there is more text after the destination on the same line and it does not start a title (", ', (), return false. I first tried checking referenceValid in startTitle, but getDefinitions() can finish the reference between lines, so the check in destination() is safer.

Test: LinkReferenceDefinitionParserTest.testDestinationFollowedByOtherText fails before, passes after. mvn test green, mvn spotless:check clean.

Found with a differential fuzz against commonmark.js + micromark (20k random inputs where both agree). With percentEncodeUrls(true), the only other differences are fenced code info strings with a tab: ```py\tx gives class="language-py\tx" (cmark/commonmark.js stop the language at the tab). Happy to send that separately if you want it.

[foo]: /url [bar]: /url2 was read as a definition of bar and the line was
removed, because startTitle went back to START_DEFINITION on the same line.
If the destination is followed by text that is not a title on the same line,
the line is not a link reference definition.
if (c != '"' && c != '\'' && c != '(') {
// Other text after the destination on the same line, so this is not a link
// reference definition (and not the start of another one either)
return false;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This whole code is getting a bit unwieldy, might be time for a full rewrite at some point..

@robinst

robinst commented Nov 21, 2026

Copy link
Copy Markdown
Collaborator

Happy to send that separately if you want it.

Yes please!

@robinst
robinst merged commit 36b4ff0 into commonmark:main Nov 21, 2026
5 checks passed
@robinst

robinst commented Nov 21, 2026

Copy link
Copy Markdown
Collaborator

Thanks, merged! I'll wait for a PR for the other issue before doing a release.

@bardrop

bardrop commented Nov 21, 2026

Copy link
Copy Markdown
Contributor Author

Thanks! Here it is: #478.

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