|
11 | 11 | * |
12 | 12 | * Deliberately conservative: |
13 | 13 | * - text leaves only (code/inline-code nodes are never visited); |
14 | | - * - 1–2 digit indices, optionally comma-separated, and runs of them (`[1][2]`); |
15 | | - * - the token must be delimiter-bounded on BOTH sides (start/whitespace |
16 | | - * before; whitespace/punctuation/end after) so technical prose like |
17 | | - * `array[1]` or `foo[12]bar` is untouched. |
| 14 | + * - what counts as a citation is the chat protocol's one chip grammar |
| 15 | + * (`chat-protocol/citation-chip.ts`), the same one the server writes, so |
| 16 | + * technical prose like `array[1]` or `foo[12]bar` is untouched. |
18 | 17 | */ |
19 | 18 |
|
20 | 19 | import type { Root, Text } from 'mdast'; |
21 | 20 | import type { Plugin } from 'unified'; |
22 | 21 | import { visit } from 'unist-util-visit'; |
23 | | - |
24 | | -/** A run of citation markers (`[1]`, `[2, 3]`, `[1][2]`) bounded by whitespace or the start on the left and by |
25 | | - * whitespace, punctuation or the end on the right. */ |
26 | | -const CITATION_REGEX = /(^|\s)(?:\[\d{1,2}(?:\s*,\s*\d{1,2})*\])+(?=[\s.,;:!?)\]]|$)/g; |
27 | | - |
28 | | -/** What a removed run leaves: nothing when punctuation or the end follows it (so "command [1]." reads "command."), |
29 | | - * else the whitespace that preceded it. */ |
30 | | -function withoutRun(text: string): string { |
31 | | - return text.replace(CITATION_REGEX, (run: string, lead: string, offset: number) => { |
32 | | - const next = text.charAt(offset + run.length); |
33 | | - return next === '' || /[.,;:!?)\]]/.test(next) ? '' : lead; |
34 | | - }); |
35 | | -} |
| 22 | +import { stripCitationChips } from '../../chat-protocol/citation-chip'; |
36 | 23 |
|
37 | 24 | export const remarkStripCitations: Plugin<[], Root> = () => { |
38 | 25 | return (tree: Root) => { |
39 | 26 | visit(tree, 'text', (node: Text, index, parent) => { |
40 | 27 | if (!parent || typeof index !== 'number') return; |
41 | 28 | if (!node.value || !node.value.includes('[')) return; |
42 | | - const withoutCitations = withoutRun(node.value); |
43 | | - // No citation matched → leave the node untouched, INCLUDING its |
44 | | - // whitespace (collapsing unconditionally would rewrite deliberate |
45 | | - // spacing in prose like `array[1] value`). |
| 29 | + const withoutCitations = stripCitationChips(node.value); |
| 30 | + // No citation matched → leave the node untouched. |
46 | 31 | if (withoutCitations === node.value) return; |
47 | 32 | // Replace the text node rather than editing `node.value` in place — |
48 | 33 | // same idiom as remark-card-links / remark-mention-chips next door, and |
49 | 34 | // the node the visitor was handed is left alone. The spread keeps |
50 | 35 | // `position` so source mapping survives. Length is unchanged, so the |
51 | 36 | // walker needs no index steering: it has already captured the original |
52 | 37 | // leaf and moves on to the next sibling, never re-entering this slot. |
53 | | - parent.children.splice(index, 1, { |
54 | | - ...node, |
55 | | - // Collapse doubled spaces the removal can leave mid-sentence. |
56 | | - value: withoutCitations.replace(/ {2,}/g, ' '), |
57 | | - }); |
| 38 | + parent.children.splice(index, 1, { ...node, value: withoutCitations }); |
58 | 39 | }); |
59 | 40 | }; |
60 | 41 | }; |
0 commit comments