fix: keep emphasis in the text of a rejected reference link - #4059
Open
luantaraschi wants to merge 1 commit into
Open
fix: keep emphasis in the text of a rejected reference link#4059luantaraschi wants to merge 1 commit into
luantaraschi wants to merge 1 commit into
Conversation
|
@AntonioBraga88 is attempting to deploy a commit to the MarkedJS Team on Vercel. A member of the Team first needs to authorize it. |
A reference link candidate whose text already holds a link never becomes a link, but the mask em and strong read still flattened the whole span, so every emphasis marker inside it was invisible. Mask the links the text holds instead of the span, and leave images alone, since an image may hold a link. Fixes CommonMark example 533.
luantaraschi
force-pushed
the
fix/emphasis-in-rejected-link-text
branch
from
August 18, 2026 23:04
088bfc5 to
726aed0
Compare
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Marked version:
18.0.10, currentmainat e250e31Markdown flavor: CommonMark|GitHub Flavored Markdown
Description
No issue for this one. It is what is left of the group I listed in #4050 after #4051 landed: the tokenizer now refuses to nest a link inside a link, but the mask that
emStrongreads was not told, so it still treats the rejected candidate as a link and hides every emphasis marker inside it.Expectation
commonmark0.31.2, already a dev dependency here, returns exactly that.Result
The asterisks are printed as text. The reference link and the inner link are both right, only the emphasis is gone. Same with
_, with**, and when the inner link is a reference link instead of an inline one, which is CommonMark example 533.The trigger is narrow but it is ordinary prose: a reference link whose text happens to hold another link. Nothing warns the author, the markers just disappear from the output.
What was attempted
inlineTokensmasks a reference link candidate by replacing the whole span with[aaaa...], soemStrongcannot pair a delimiter outside a link with one inside it. That is the right call when the candidate really is a link, because the link text is tokenized separately.When the text already holds a link the candidate is not a link, #4051 made
outputLinksay so, and the span goes back through the loop as ordinary text. The mask built before the loop still reads[aaaa...]there, so the*never reachesemStrong.The mask now follows the same rule the tokenizer follows. A candidate whose text holds a link keeps its brackets and its emphasis markers visible, and the links inside it are masked instead. Images are left on the old path, since an image may hold a link.
linkInTextanswers the question. It returns ontext.includes('[')before touching a regex, so text without brackets costs nothing, and it skips images in both directions: an image inside link text does not disqualify the outer candidate, and an image candidate is never disqualified by what it holds.I first tried rebuilding the mask from the lexer loop whenever a candidate was rejected. It is a smaller diff and it reads better, but it is quadratic in the number of rejections in one inline run, and a paragraph with 400 of them went from 10.1 ms to 44.6 ms. Given #4040 and #4048 are open on this very block, that seemed like the wrong thing to hand you.
Scope
Fixes CommonMark example 533.
shouldFailcleared intest/specs/commonmark/and intest/specs/gfm/, Links goes from 80 to 81 of 90.512, 520 and 528 are still failing and are not touched here. They share a title with 533 in my grouping but not a cause: they need
_inlineLabelto match more than one level of nested brackets, which is a different change.Measurements
Differential run over 317 inputs, combinations of brackets, emphasis, inline links, reference links, images and code spans, plus every file in
test/specs/new, in both flavors, against the bundledcommonmark:<img ...>against<img ... />Timing on Node 22.20.0, best of five runs:
npm testis green: 1785 spec tests, 191 unit tests, plus umd, cjs, types and lint.Contributor