Skip to content

fix: twoslash @link comments skipped when multiple appear consecutively#52

Open
tsushanth wants to merge 1 commit into
mintlify:mainfrom
tsushanth:fix/twoslash-link-splice
Open

fix: twoslash @link comments skipped when multiple appear consecutively#52
tsushanth wants to merge 1 commit into
mintlify:mainfrom
tsushanth:fix/twoslash-link-splice

Conversation

@tsushanth

Copy link
Copy Markdown

Problem

In rehypeSyntaxHighlighting.ts, the loop that strips // @link comment lines from twoslash code blocks mutates the array while iterating over it with .entries():

for (const [i, line] of splitCode.entries()) {
  const parsedLineComment = parseLineComment(line);
  if (!parsedLineComment) continue;
  linkMap.set(word, href);
  splitCode.splice(i, 1);  // shifts all subsequent elements down
}

After splice(i, 1), the element that was at index i+1 shifts to i, but the iterator advances to i+1 on the next tick — skipping it. The result: when two or more consecutive lines are // @link comments, every second one is silently dropped. The skipped line stays in the rendered code output as a visible comment and its link is never added to linkMap, so the hover popup won't contain a link for that token.

Example that triggers the bug — the second @link is silently ignored:

// @link Promise https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise
// @link fetch https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API
const result: Promise<Response> = fetch('/api/data')

Fix

Replace the mutate-in-place loop with a single filter pass that collects links and strips comment lines in one traversal, avoiding the off-by-one from in-place removal.

…comments

When multiple consecutive // @link comment lines appear in a twoslash
code block, the previous loop used splice(i, 1) while iterating with
.entries(). After each removal, subsequent elements shift down by one
index, so the iterator skips the next element. This causes alternating
@link lines to be silently ignored — neither removed from the rendered
code nor registered in the linkMap.

Replace the mutate-in-place loop with a single filter pass that collects
links and strips comment lines in one traversal.
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