fix: handle bracketed spans inside link labels#288
Open
deshiknaves wants to merge 1 commit into
Open
Conversation
Any link label containing a bracketed span, e.g. `[[1] Document](#)`
(common in LLM-generated citation links), threw "inline rule didn't
increment state.pos": the comark_inline_span rule's silent branch
returned true without advancing state.pos, violating markdown-it's
inline-rule contract when parseLinkLabel -> skipToken probed the label.
Return false in silent mode instead (same pattern as emphasis /
strikethrough) so the label's own bracket-depth tracking consumes the
nested `[...]` and the outer link parses. Advancing state.pos in the
silent branch was not enough — parseLinkLabel treats a multi-char
silent match starting with `[` as a nested link and aborts the label.
Also keep bare `[text]` literal inside link labels: the span rule ate
the inner `[1]` when the label was re-tokenized, rendering link text
"1 Document" instead of the plain-markdown literal "[1] Document".
markdown-exit's link rule tokenizes label children inside
state.linkLevel++/--, so when linkLevel > 0 only match explicit
`[text]{attrs}` spans. Bare `[text]` outside links still becomes a
span (MDC behavior, per compare-parsers tests).
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
|
@deshiknaves is attempting to deploy a commit to the NuxtLabs Team on Vercel. A member of the Team first needs to authorize it. |
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.
🔗 Linked issue
Resolves #287
❓ Type of change
📚 Description
Parsing any link whose label contains a bracketed span throws:
Minimal repro:
This shape is common in LLM-generated citation links (
[[1] Source](url)), and the same input parses fine in plain markdown-it.Cause —
comark_inline_span(registeredbefore('link')) returnedtruein silent mode without advancingstate.pos. markdown-it's inline-rule contract requires every rule returningtrueto advancestate.pos, silent mode included —skipTokenenforces it. When thelinkrule callsparseLinkLabel→skipTokenon the inner[1], the span rule matched silently, leftposuntouched, andskipTokenthrew.Fix — two parts:
falsein silent mode (same pattern as the built-inemphasis/strikethroughrules), soparseLinkLabel's own bracket-depth tracking consumes the nested[...]and the outer link parses. Just advancingstate.posin the silent branch is not enough:parseLinkLabeltreats a multi-char silent match starting with[as a nested link and aborts the whole label.[text]literal inside link labels. Without this, the span rule ate the inner[1]when the label was re-tokenized, rendering link text "1 Document" instead of the plain-markdown "[1] Document". Thelinkrule tokenizes label children insidestate.linkLevel++/--, so whenlinkLevel > 0the span rule now only matches explicit[text]{attrs}spans.Behavior after the fix:
[[1] Document](#)[1] Document(previously: throw)[a [x]{.y} b](#)[content]{.class}Hello [World]compare-parsersbehavior)Tests added in
test/parse/inline-span.test.tscovering all rows above.📝 Checklist
pnpm verifyand it passes.