Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 36 additions & 7 deletions scripts/check-doc-refs.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,46 @@ function renderPlain(text) {
.replace(/(^|[^\p{L}\p{N}_])(__|_)(?=\S)([\s\S]*?\S)\2(?![\p{L}\p{N}_])/gu, '$1$3'));
}

function githubSlug(heading) {
const code = [];
function* unusedMarkerChars(heading, spans) {
let outside = '';
let from = 0;
for (const span of spans) {
outside += heading.slice(from, span.start);
from = span.end;
}
outside += heading.slice(from);
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) {
let protectedText = '';
let at = 0;
for (const span of codeSpans(heading)) {
protectedText += `${heading.slice(at, span.start)}${code.length}`;
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);
}
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 open = free.next(), close = free.next(); text === null && !close.done; open = free.next(), close = free.next()) {
text = renderWithMarkers(heading, spans, open.value, close.value);
}
protectedText += heading.slice(at);
const text = renderPlain(protectedText).replace(/(\d+)/g, (whole, i) => code[Number(i)]);
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, '-');
}

Expand Down
18 changes: 18 additions & 0 deletions test/check-doc-refs.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,24 @@ test('githubSlug follows GitHub: rendered text, lower case, punctuation dropped,
assert.equal(githubSlug('`a &amp; b`'), 'a-amp-b');
});

test('githubSlug picks markers absent from the decoded heading and keeps the old emphasis, link and tag behaviour', (t) => {
assert.equal(githubSlug('Use &#xE000;0&#xE001;'), 'use-0');
assert.equal(githubSlug('Use &#xE000;`x`&#xE001;'), '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](&#xE000;&#xE001;) &#xE[002](a);0&#xE[003](b);'), 'use-x-0');
assert.equal(githubSlug('Use [x](&#xE000;&#xE001;) [label]`a`(&#xE00<i></i>2;0&#xE00<i></i>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');
assert.equal(githubSlug('Use <span title="`hidden`">label</span> `shown`'), 'use-label-shown');
const root = repo(t, {
'docs/other.md': '# Use &#xE000;0&#xE001; 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`).',
Expand Down
Loading