Skip to content

Commit 0b30b0c

Browse files
committed
fix(webview): mask reference links and image references before mention rewriting
Address CodeRabbit finding on #1257: - Add linkReference and imageReference to MENTION_MASK_NODE_TYPES so a reference link's label and an image reference's alt are masked before prepareMentions() runs. Without it, a mention pattern inside such a label (e.g. [the @problems summary][docs]) was rewritten to an indexed placeholder that remark kept inside the label; since rehypeMentions() skips anchors, the raw placeholder (control character plus index) rendered verbatim inside the link text. Image references corrupted the alt attribute the same way. - Regression tests: a mention inside a reference link label and inside an image reference alt both stay inert, the label text and alt attribute are preserved verbatim, no mention span is rendered, and no placeholder control characters leak into the output.
1 parent 9fa1a40 commit 0b30b0c

2 files changed

Lines changed: 52 additions & 9 deletions

File tree

‎webview-ui/src/components/common/MarkdownBlock.tsx‎

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,11 @@ const MENTION_PLACEHOLDER_REGEX = new RegExp(`${MENTION_PLACEHOLDER_CHAR}(\\d+)$
2727

2828
// mdast node types whose raw source regions must never be mention-rewritten:
2929
// code blocks (fenced or indented), inline code, links, images, raw HTML, math,
30-
// and reference link definitions all render as literal or non-text content
31-
// (rewriting a definition's destination would corrupt the reference link's
32-
// href instead of producing a mention span).
30+
// reference link definitions, and reference links/images all render as literal
31+
// or non-text content. Rewriting a definition's destination would corrupt the
32+
// reference link's href; rewriting a reference label or alt would leak the raw
33+
// placeholder into the anchor text or img alt (rehypeMentions skips anchors),
34+
// instead of producing a mention span.
3335
const MENTION_MASK_NODE_TYPES = new Set([
3436
"code",
3537
"inlineCode",
@@ -39,6 +41,8 @@ const MENTION_MASK_NODE_TYPES = new Set([
3941
"inlineMath",
4042
"math",
4143
"definition",
44+
"linkReference",
45+
"imageReference",
4246
])
4347

4448
/**
@@ -55,10 +59,10 @@ const MENTION_MASK_NODE_TYPES = new Set([
5559
* Matching runs on the raw string so the shared regex's boundary rules apply
5660
* unchanged (replacing literal regions with spaces would turn a preceding `)`
5761
* or backtick into whitespace and make non-mentions actionable). Literal / non-
58-
* text regions (code, links, images, HTML, math, reference link definitions)
59-
* are marked via a throwaway mdast parse with the exact positions remark sees,
60-
* and a match whose range intersects one of them is discarded so mentions
61-
* inside such regions stay inert.
62+
* text regions (code, links, images, HTML, math, reference link definitions,
63+
* and reference links/images) are marked via a throwaway mdast parse with the
64+
* exact positions remark sees, and a match whose range intersects one of them
65+
* is discarded so mentions inside such regions stay inert.
6266
*/
6367
function prepareMentions(markdown: string): { preparedMarkdown: string; mentions: string[] } {
6468
if (!markdown) {
@@ -70,8 +74,10 @@ function prepareMentions(markdown: string): { preparedMarkdown: string; mentions
7074
// non-text regions (mdast positions carry absolute source offsets): a
7175
// mention inside any of them must stay inert, because code and links render
7276
// as literal/interactive content, images, raw HTML, and math keep their
73-
// source text unchanged, and a reference link definition's destination
74-
// becomes the link's href (rewriting it would corrupt the href).
77+
// source text unchanged, a reference link definition's destination becomes
78+
// the link's href, and a reference link/image label or alt renders as the
79+
// anchor text or img alt (rewriting any of them would corrupt the href/alt
80+
// or leak the raw placeholder into the rendered output).
7581
const tree = unified().use(remarkParse).use(remarkGfm).use(remarkMath).parse(markdown)
7682

7783
const isMasked = new Array<boolean>(markdown.length).fill(false)

‎webview-ui/src/components/common/__tests__/MarkdownBlock.spec.tsx‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,43 @@ describe("MarkdownBlock", () => {
385385
expect(anchor.getAttribute("href")).not.toContain("\u0001")
386386
})
387387

388+
it("keeps reference link labels inert and preserves the label text", async () => {
389+
// A mention inside a reference link's label renders as the anchor's
390+
// text. Rewriting it to a mention placeholder would leak the raw
391+
// placeholder (rehypeMentions skips anchors, so the control characters
392+
// would render verbatim inside the link) and a role=button span inside
393+
// <a> would be invalid nested interactive content. The whole reference
394+
// region must stay masked.
395+
const markdown = "See [the @problems summary][docs] now.\n\n[docs]: https://example.com/problems"
396+
const { container } = render(<MarkdownBlock markdown={markdown} mentions />)
397+
398+
await screen.findByText(/now/, { exact: false })
399+
400+
const anchor = container.querySelector("a")!
401+
expect(anchor).toHaveAttribute("href", "https://example.com/problems")
402+
expect(anchor.textContent).toBe("the @problems summary")
403+
expect(container.querySelectorAll("span.mention-context-highlight").length).toBe(0)
404+
405+
// No placeholder control characters leak into the rendered output.
406+
expect(container.textContent).not.toContain("\u0001")
407+
})
408+
409+
it("keeps image reference alt text inert and preserves the alt attribute", async () => {
410+
// An image reference's alt renders as the img's alt attribute. Rewriting
411+
// it to a mention placeholder would corrupt the alt instead of producing
412+
// a mention span. The whole reference region must stay masked.
413+
const markdown = "See ![a @problems screenshot][docs] now.\n\n[docs]: https://example.com/problems.png"
414+
const { container } = render(<MarkdownBlock markdown={markdown} mentions />)
415+
416+
await screen.findByText(/now/, { exact: false })
417+
418+
const img = container.querySelector("img")!
419+
expect(img).toHaveAttribute("src", "https://example.com/problems.png")
420+
expect(img).toHaveAttribute("alt", "a @problems screenshot")
421+
expect(container.querySelectorAll("span.mention-context-highlight").length).toBe(0)
422+
expect(container.textContent).not.toContain("\u0001")
423+
})
424+
388425
it("keeps the shared regex boundary rules when a mention directly follows a link or inline code", async () => {
389426
// Matching must run on the raw string so the shared regex's start boundary
390427
// sees the real characters: with no whitespace after a closing `)` or a

0 commit comments

Comments
 (0)