From d3c44e1ec6f3eb5d4dd5dc10c4395584d0a48178 Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Date: Sun, 29 Nov 2026 15:17:40 +0100 Subject: [PATCH 1/5] fix(docs): restore code spans by position in heading slugs (#1361) githubSlug swapped code spans for their index as digits and restored every digit run afterwards, so a heading holding its own digits or entity-decoded characters got the wrong text back (use-undefined for 'Use 0'). The spans are now marked with a character absent from the heading, syntax is stripped, and the text is split on that mark; entities are decoded per piece and each code span is put back by position. --- scripts/check-doc-refs.js | 12 ++++++++---- test/check-doc-refs.test.js | 14 ++++++++++++++ 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/scripts/check-doc-refs.js b/scripts/check-doc-refs.js index 5ce57ac49..1858028d9 100644 --- a/scripts/check-doc-refs.js +++ b/scripts/check-doc-refs.js @@ -34,8 +34,8 @@ function decodeEntities(text) { }); } -function renderPlain(text) { - return decodeEntities(text +function stripSyntax(text) { + return (text .replace(/!\[[^\]]*\]\([^)]*\)/g, '') .replace(/\[([^\]]*)\]\([^)]*\)/g, '$1') .replace(/<[^>]+>/g, '') @@ -45,16 +45,20 @@ function renderPlain(text) { } function githubSlug(heading) { + let marker = 0x2460; + while (heading.includes(String.fromCodePoint(marker))) marker++; + const mark = String.fromCodePoint(marker); const code = []; let protectedText = ''; let at = 0; for (const span of codeSpans(heading)) { - protectedText += `${heading.slice(at, span.start)}${code.length}`; + protectedText += `${heading.slice(at, span.start)}${mark}`; code.push(span.content); at = span.end; } protectedText += heading.slice(at); - const text = renderPlain(protectedText).replace(/(\d+)/g, (whole, i) => code[Number(i)]); + const text = stripSyntax(protectedText).split(mark) + .map((piece, i) => decodeEntities(piece) + (i < code.length ? code[i] : '')).join(''); return text.toLowerCase().replace(/[^\p{L}\p{M}\p{N}\p{Pc} -]/gu, '').replace(/ /g, '-'); } diff --git a/test/check-doc-refs.test.js b/test/check-doc-refs.test.js index b9e4742b5..254679d61 100644 --- a/test/check-doc-refs.test.js +++ b/test/check-doc-refs.test.js @@ -103,6 +103,20 @@ test('githubSlug follows GitHub: rendered text, lower case, punctuation dropped, assert.equal(githubSlug('`a & b`'), 'a-amp-b'); }); +test('githubSlug restores code spans by position whatever the heading text holds', (t) => { + assert.equal(githubSlug('Use 0'), 'use-0'); + assert.equal(githubSlug('Use `x` 2'), 'use-x-2'); + assert.equal(githubSlug('Step 2 and `a` 10'), 'step-2-and-a-10'); + assert.equal(githubSlug('Use \uE000 `x` \uE001'), 'use--x-'); + assert.equal(githubSlug('Use \u2460 `x` & `y`'), 'use-\u2460-x--y'); + assert.equal(githubSlug('`a`0`b`'), 'a0b'); + const root = repo(t, { + 'docs/other.md': '# Use 0 bar\n', + 'docs/guide.md': '[a](other.md#use-0-bar) [b](other.md#use-undefined-bar)\n', + }); + assert.deepEqual(checkDocRefs({ root, allow: [] }).failures.map(f => `${f.category} ${f.ref}`), ['anchor other.md#use-undefined-bar']); +}); + test('GIVEN an identifier qualified by a cited file WHEN the file lacks it THEN it fails, also across a line wrap', (t) => { const doc = [ '`fooBar()` in `public/app.js` and `public/app.js` `fooBar` and `spawnPty` (`src/main/terminals/pty-ops.js`).', From e385c10357ed9ae289bb0ffd6a2bb9ff4c511d3c Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Date: Sun, 29 Nov 2026 15:34:41 +0100 Subject: [PATCH 2/5] fix(docs): keep the old slug algorithm, choose markers absent from the decoded heading (#1361) Back to the original code-span markers and restore regex. The two private-use marker characters are now the first pair that appears neither in the heading nor in its entity-decoded form, so a heading written with 0 no longer collides. Emphasis, link and tag handling is unchanged. --- scripts/check-doc-refs.js | 23 +++++++++++++++-------- test/check-doc-refs.test.js | 12 +++++++----- 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/scripts/check-doc-refs.js b/scripts/check-doc-refs.js index 1858028d9..f45f08482 100644 --- a/scripts/check-doc-refs.js +++ b/scripts/check-doc-refs.js @@ -34,8 +34,8 @@ function decodeEntities(text) { }); } -function stripSyntax(text) { - return (text +function renderPlain(text) { + return decodeEntities(text .replace(/!\[[^\]]*\]\([^)]*\)/g, '') .replace(/\[([^\]]*)\]\([^)]*\)/g, '$1') .replace(/<[^>]+>/g, '') @@ -44,21 +44,28 @@ function stripSyntax(text) { .replace(/(^|[^\p{L}\p{N}_])(__|_)(?=\S)([\s\S]*?\S)\2(?![\p{L}\p{N}_])/gu, '$1$3')); } +function freeMarkers(heading) { + const used = heading + decodeEntities(heading); + const found = []; + for (let c = 0xe000; c <= 0xf8ff && found.length < 2; c++) { + if (!used.includes(String.fromCharCode(c))) found.push(String.fromCharCode(c)); + } + if (found.length < 2) throw new Error('no free private-use marker pair for heading slug'); + return found; +} + function githubSlug(heading) { - let marker = 0x2460; - while (heading.includes(String.fromCodePoint(marker))) marker++; - const mark = String.fromCodePoint(marker); + const [open, close] = freeMarkers(heading); const code = []; let protectedText = ''; let at = 0; for (const span of codeSpans(heading)) { - protectedText += `${heading.slice(at, span.start)}${mark}`; + protectedText += `${heading.slice(at, span.start)}${open}${code.length}${close}`; code.push(span.content); at = span.end; } protectedText += heading.slice(at); - const text = stripSyntax(protectedText).split(mark) - .map((piece, i) => decodeEntities(piece) + (i < code.length ? code[i] : '')).join(''); + const text = renderPlain(protectedText).replace(new RegExp(`${open}(\\d+)${close}`, 'g'), (whole, i) => code[Number(i)]); return text.toLowerCase().replace(/[^\p{L}\p{M}\p{N}\p{Pc} -]/gu, '').replace(/ /g, '-'); } diff --git a/test/check-doc-refs.test.js b/test/check-doc-refs.test.js index 254679d61..da238d103 100644 --- a/test/check-doc-refs.test.js +++ b/test/check-doc-refs.test.js @@ -103,13 +103,15 @@ test('githubSlug follows GitHub: rendered text, lower case, punctuation dropped, assert.equal(githubSlug('`a & b`'), 'a-amp-b'); }); -test('githubSlug restores code spans by position whatever the heading text holds', (t) => { +test('githubSlug picks markers absent from the decoded heading and keeps the old emphasis, link and tag behaviour', (t) => { assert.equal(githubSlug('Use 0'), 'use-0'); - assert.equal(githubSlug('Use `x` 2'), 'use-x-2'); - assert.equal(githubSlug('Step 2 and `a` 10'), 'step-2-and-a-10'); + assert.equal(githubSlug('Use `x`'), 'use-x'); assert.equal(githubSlug('Use \uE000 `x` \uE001'), 'use--x-'); - assert.equal(githubSlug('Use \u2460 `x` & `y`'), 'use-\u2460-x--y'); - assert.equal(githubSlug('`a`0`b`'), 'a0b'); + assert.equal(githubSlug('Step 2 and `a` 10'), 'step-2-and-a-10'); + assert.equal(githubSlug('`x`_y_'), 'xy'); + assert.equal(githubSlug('_x_`y`'), 'xy'); + assert.equal(githubSlug('Use [link](`target`) `shown`'), 'use-link-shown'); + assert.equal(githubSlug('Use label `shown`'), 'use-label-shown'); const root = repo(t, { 'docs/other.md': '# Use 0 bar\n', 'docs/guide.md': '[a](other.md#use-0-bar) [b](other.md#use-undefined-bar)\n', From cc2c3b420712cfea8cea5ba62f7359de5699a402 Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Date: Sun, 29 Nov 2026 15:42:56 +0100 Subject: [PATCH 3/5] fix(docs): reject slug markers that link stripping can reassemble (#1361) A marker pair is now also rejected when it can appear in the heading's text outside code spans after stripping and entity decoding, so entities split by a link no longer decode into the chosen markers. --- scripts/check-doc-refs.js | 9 ++++++++- test/check-doc-refs.test.js | 1 + 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/scripts/check-doc-refs.js b/scripts/check-doc-refs.js index f45f08482..bfaf6e42b 100644 --- a/scripts/check-doc-refs.js +++ b/scripts/check-doc-refs.js @@ -45,7 +45,14 @@ function renderPlain(text) { } function freeMarkers(heading) { - const used = heading + decodeEntities(heading); + let outside = ''; + let from = 0; + for (const span of codeSpans(heading)) { + outside += heading.slice(from, span.start); + from = span.end; + } + outside += heading.slice(from); + const used = heading + decodeEntities(heading) + renderPlain(outside); const found = []; for (let c = 0xe000; c <= 0xf8ff && found.length < 2; c++) { if (!used.includes(String.fromCharCode(c))) found.push(String.fromCharCode(c)); diff --git a/test/check-doc-refs.test.js b/test/check-doc-refs.test.js index da238d103..437dada81 100644 --- a/test/check-doc-refs.test.js +++ b/test/check-doc-refs.test.js @@ -108,6 +108,7 @@ test('githubSlug picks markers absent from the decoded heading and keeps the old assert.equal(githubSlug('Use `x`'), 'use-x'); assert.equal(githubSlug('Use \uE000 `x` \uE001'), 'use--x-'); assert.equal(githubSlug('Step 2 and `a` 10'), 'step-2-and-a-10'); + assert.equal(githubSlug('Use [x]() [002](a);0[003](b);'), 'use-x-0'); assert.equal(githubSlug('`x`_y_'), 'xy'); assert.equal(githubSlug('_x_`y`'), 'xy'); assert.equal(githubSlug('Use [link](`target`) `shown`'), 'use-link-shown'); From d3ab8cdff5947179509790fd0a755c5de507ee32 Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Date: Sun, 29 Nov 2026 15:53:27 +0100 Subject: [PATCH 4/5] fix(docs): accept a slug marker pair only if the rendered heading uses it consistently (#1361) Each candidate pair is tried against the real rendering and kept only when every marker in the output belongs to a well-formed marker, index pair with a known span used once. The pre-filter on the heading text stays as a cheap first test. --- scripts/check-doc-refs.js | 43 +++++++++++++++++++++++-------------- test/check-doc-refs.test.js | 1 + 2 files changed, 28 insertions(+), 16 deletions(-) diff --git a/scripts/check-doc-refs.js b/scripts/check-doc-refs.js index bfaf6e42b..dc70af61a 100644 --- a/scripts/check-doc-refs.js +++ b/scripts/check-doc-refs.js @@ -44,35 +44,46 @@ function renderPlain(text) { .replace(/(^|[^\p{L}\p{N}_])(__|_)(?=\S)([\s\S]*?\S)\2(?![\p{L}\p{N}_])/gu, '$1$3')); } -function freeMarkers(heading) { +function unusedMarkerChars(heading, spans) { let outside = ''; let from = 0; - for (const span of codeSpans(heading)) { + for (const span of spans) { outside += heading.slice(from, span.start); from = span.end; } outside += heading.slice(from); const used = heading + decodeEntities(heading) + renderPlain(outside); - const found = []; - for (let c = 0xe000; c <= 0xf8ff && found.length < 2; c++) { - if (!used.includes(String.fromCharCode(c))) found.push(String.fromCharCode(c)); - } - if (found.length < 2) throw new Error('no free private-use marker pair for heading slug'); - return found; + const free = []; + for (let c = 0xe000; c <= 0xf8ff; c++) if (!used.includes(String.fromCharCode(c))) free.push(String.fromCharCode(c)); + return free; } -function githubSlug(heading) { - const [open, close] = freeMarkers(heading); - const code = []; +function renderWithMarkers(heading, spans, open, close) { let protectedText = ''; let at = 0; - for (const span of codeSpans(heading)) { - protectedText += `${heading.slice(at, span.start)}${open}${code.length}${close}`; - code.push(span.content); + spans.forEach((span, i) => { + protectedText += `${heading.slice(at, span.start)}${open}${i}${close}`; at = span.end; + }); + const rendered = renderPlain(protectedText + heading.slice(at)); + const pattern = new RegExp(`${open}(\\d+)${close}`, 'g'); + const seen = new Set(); + for (const m of rendered.matchAll(pattern)) { + const index = Number(m[1]); + if (index >= spans.length || seen.has(index)) return null; + seen.add(index); } - protectedText += heading.slice(at); - const text = renderPlain(protectedText).replace(new RegExp(`${open}(\\d+)${close}`, 'g'), (whole, i) => code[Number(i)]); + const rest = rendered.replace(pattern, ''); + if (rest.includes(open) || rest.includes(close)) return null; + return rendered.replace(pattern, (whole, i) => spans[Number(i)].content); +} + +function githubSlug(heading) { + const spans = codeSpans(heading); + const free = unusedMarkerChars(heading, spans); + let text = null; + for (let k = 0; text === null && k + 1 < free.length; k += 2) text = renderWithMarkers(heading, spans, free[k], free[k + 1]); + if (text === null) throw new Error('no usable private-use marker pair for heading slug'); return text.toLowerCase().replace(/[^\p{L}\p{M}\p{N}\p{Pc} -]/gu, '').replace(/ /g, '-'); } diff --git a/test/check-doc-refs.test.js b/test/check-doc-refs.test.js index 437dada81..326cc63bb 100644 --- a/test/check-doc-refs.test.js +++ b/test/check-doc-refs.test.js @@ -109,6 +109,7 @@ test('githubSlug picks markers absent from the decoded heading and keeps the old assert.equal(githubSlug('Use \uE000 `x` \uE001'), 'use--x-'); assert.equal(githubSlug('Step 2 and `a` 10'), 'step-2-and-a-10'); assert.equal(githubSlug('Use [x]() [002](a);0[003](b);'), 'use-x-0'); + assert.equal(githubSlug('Use [x]() [label]`a`(฀2;0฀3;)'), 'use-x-labela0'); assert.equal(githubSlug('`x`_y_'), 'xy'); assert.equal(githubSlug('_x_`y`'), 'xy'); assert.equal(githubSlug('Use [link](`target`) `shown`'), 'use-link-shown'); From 2189af039e67d9f3ff3e7d39cf9c275dc675602f Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Date: Sun, 29 Nov 2026 16:09:39 +0100 Subject: [PATCH 5/5] perf(docs): search slug markers lazily against a set of used characters (#1361) --- scripts/check-doc-refs.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/scripts/check-doc-refs.js b/scripts/check-doc-refs.js index dc70af61a..03971fcb7 100644 --- a/scripts/check-doc-refs.js +++ b/scripts/check-doc-refs.js @@ -44,7 +44,7 @@ function renderPlain(text) { .replace(/(^|[^\p{L}\p{N}_])(__|_)(?=\S)([\s\S]*?\S)\2(?![\p{L}\p{N}_])/gu, '$1$3')); } -function unusedMarkerChars(heading, spans) { +function* unusedMarkerChars(heading, spans) { let outside = ''; let from = 0; for (const span of spans) { @@ -52,10 +52,8 @@ function unusedMarkerChars(heading, spans) { from = span.end; } outside += heading.slice(from); - const used = heading + decodeEntities(heading) + renderPlain(outside); - const free = []; - for (let c = 0xe000; c <= 0xf8ff; c++) if (!used.includes(String.fromCharCode(c))) free.push(String.fromCharCode(c)); - return free; + const used = new Set(heading + decodeEntities(heading) + renderPlain(outside)); + for (let c = 0xe000; c <= 0xf8ff; c++) if (!used.has(String.fromCharCode(c))) yield String.fromCharCode(c); } function renderWithMarkers(heading, spans, open, close) { @@ -82,7 +80,9 @@ function githubSlug(heading) { const spans = codeSpans(heading); const free = unusedMarkerChars(heading, spans); let text = null; - for (let k = 0; text === null && k + 1 < free.length; k += 2) text = renderWithMarkers(heading, spans, free[k], free[k + 1]); + for (let open = free.next(), close = free.next(); text === null && !close.done; open = free.next(), close = free.next()) { + text = renderWithMarkers(heading, spans, open.value, close.value); + } if (text === null) throw new Error('no usable private-use marker pair for heading slug'); return text.toLowerCase().replace(/[^\p{L}\p{M}\p{N}\p{Pc} -]/gu, '').replace(/ /g, '-'); }