markdown-parity-check 0.2.11 → 0.2.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/src/html.js CHANGED
@@ -3,7 +3,6 @@
3
3
  import { parseDocument } from 'htmlparser2';
4
4
  import { selectAll, selectOne } from 'css-select';
5
5
  import { Element, Text } from 'domhandler';
6
- import { textContent } from 'domutils';
7
6
  import { extractNumbers, looseNormalize, normalizeCode, resolveHref, strictNormalize } from './normalize.js';
8
7
  export class HtmlExtractError extends Error {
9
8
  }
@@ -51,28 +50,27 @@ function pathOf(ctx, el) {
51
50
  const parts = [];
52
51
  let cur = el;
53
52
  while (cur) {
54
- let index = 1;
55
- let sib = cur.prev;
56
- while (sib) {
57
- if (sib instanceof Element && sib.name === cur.name)
58
- index++;
59
- sib = sib.prev;
60
- }
61
- parts.unshift(index > 1 || hasLaterSameNamedSibling(cur) ? `${cur.name}:nth-of-type(${index})` : cur.name);
53
+ parts.unshift(segment(ctx, cur));
62
54
  if (cur === ctx.root)
63
55
  break;
64
56
  cur = cur.parent instanceof Element ? cur.parent : null;
65
57
  }
66
58
  return parts.join(' > ');
67
59
  }
68
- function hasLaterSameNamedSibling(el) {
69
- let sib = el.next;
70
- while (sib) {
71
- if (sib instanceof Element && sib.name === el.name)
72
- return true;
73
- sib = sib.next;
74
- }
75
- return false;
60
+ /**
61
+ * The element's own part of a path: its tag name, with :nth-of-type(n) when the parent has more than one child of
62
+ * that name. The positions of all same-named siblings are computed at once and kept in ctx. Up to 0.2.12 every
63
+ * block counted its siblings again, so a list of 20 000 items took time quadratic in its length (found by an
64
+ * independent review before 0.2.13 was released).
65
+ */
66
+ function segment(ctx, el) {
67
+ const known = ctx.segments.get(el);
68
+ if (known !== undefined)
69
+ return known;
70
+ const siblings = el.parent ? el.parent.children : [el];
71
+ const same = siblings.filter((c) => c instanceof Element && c.name === el.name);
72
+ same.forEach((c, i) => ctx.segments.set(c, same.length > 1 ? `${c.name}:nth-of-type(${i + 1})` : c.name));
73
+ return ctx.segments.get(el) ?? el.name;
76
74
  }
77
75
  function hasClass(el, name) {
78
76
  return (el.attribs.class ?? '').split(/\s+/).includes(name);
@@ -93,6 +91,25 @@ function iframeLink(el) {
93
91
  const title = el.attribs['title'] ?? '';
94
92
  return src && title ? { src, title } : null;
95
93
  }
94
+ /** The hidden and aria-hidden attributes alone, without the tag, role and page chrome rules of isSkipped. */
95
+ function hiddenAttr(el) {
96
+ return el.attribs['hidden'] !== undefined || el.attribs['aria-hidden'] === 'true';
97
+ }
98
+ /**
99
+ * Text of code as the page shows it: every text node with its whitespace, except under a hidden or aria-hidden
100
+ * element. Up to 0.2.12 code used textContent, which also returns hidden descendants, so a hidden span inside a
101
+ * code block counted as code while the same span in a paragraph did not (found by an outside review 2026-09-26).
102
+ */
103
+ function visibleCode(nodes) {
104
+ let out = '';
105
+ for (const node of nodes) {
106
+ if (node instanceof Text)
107
+ out += node.data;
108
+ else if (node instanceof Element && !hiddenAttr(node))
109
+ out += visibleCode(node.children);
110
+ }
111
+ return out;
112
+ }
96
113
  function isSkipped(el, root, profile = 'generic', ignoreHidden = false) {
97
114
  if (SKIP_TAGS.has(el.name) && !(profile === 'starlight' && el.name === 'iframe' && iframeLink(el)))
98
115
  return true;
@@ -178,12 +195,12 @@ function pushBlock(ctx, partial, node, pathEl) {
178
195
  };
179
196
  ctx.blocks.push(block);
180
197
  }
181
- function flushRun(ctx, run, container, type = 'paragraph') {
198
+ function flushRun(ctx, run, container, type = 'paragraph', list) {
182
199
  const text = strictNormalize(run.text);
183
200
  if (text === '')
184
201
  return;
185
202
  const node = run.firstNode ?? container;
186
- pushBlock(ctx, { type, text, links: run.links }, node, container);
203
+ pushBlock(ctx, { type, text, links: run.links, ...(type === 'listItem' && list ? { list } : {}) }, node, container);
187
204
  }
188
205
  function hasBlockChild(el) {
189
206
  return el.children.some((c) => c instanceof Element && BLOCK_TAGS.has(c.name) && !SKIP_TAGS.has(c.name));
@@ -232,7 +249,7 @@ function handleBlock(ctx, el) {
232
249
  return;
233
250
  }
234
251
  if (el.name === 'pre') {
235
- const code = normalizeCode(ctx.profile === 'starlight' ? starlightCode(el) : textContent(el));
252
+ const code = normalizeCode(ctx.profile === 'starlight' ? starlightCode(el) : visibleCode(el.children));
236
253
  if (code.trim() !== '')
237
254
  pushBlock(ctx, { type: 'code', text: strictNormalize(code), code, links: [] }, el, el);
238
255
  return;
@@ -242,10 +259,13 @@ function handleBlock(ctx, el) {
242
259
  return;
243
260
  }
244
261
  if (el.name === 'ul' || el.name === 'ol') {
262
+ // A hidden item or list is skipped like any other hidden element (0.2.13; up to 0.2.12 it was read).
245
263
  for (const c of el.children) {
246
- if (c instanceof Element && c.name === 'li')
264
+ if (!(c instanceof Element) || isSkipped(c, ctx.root, ctx.profile))
265
+ continue;
266
+ if (c.name === 'li')
247
267
  handleListItem(ctx, c);
248
- else if (c instanceof Element && (c.name === 'ul' || c.name === 'ol'))
268
+ else if (c.name === 'ul' || c.name === 'ol')
249
269
  handleBlock(ctx, c);
250
270
  }
251
271
  return;
@@ -259,9 +279,74 @@ function handleBlock(ctx, el) {
259
279
  // Generic container (div, section, article, blockquote, figure, details, header, footer, ...).
260
280
  walk(ctx, el);
261
281
  }
282
+ /**
283
+ * Where an item sits in its list (0.2.13). Up to 0.2.12 a list item carried its text only, so a changed start
284
+ * number, nesting level or task state passed unreported (found by an outside review 2026-09-26).
285
+ */
286
+ function listInfo(ctx, li) {
287
+ const parent = li.parent instanceof Element ? li.parent : null;
288
+ const ordered = parent?.name === 'ol';
289
+ let lists = 0;
290
+ for (let p = li.parent; p instanceof Element; p = p.parent) {
291
+ if (p.name === 'ul' || p.name === 'ol')
292
+ lists++;
293
+ if (p === ctx.root)
294
+ break;
295
+ }
296
+ const info = { ordered, depth: ctx.listBase + Math.max(0, lists - 1), checked: taskState(li) };
297
+ if (ordered && parent) {
298
+ const ordinal = listOrdinals(ctx, parent).get(li);
299
+ if (ordinal !== undefined)
300
+ info.ordinal = ordinal;
301
+ }
302
+ return info;
303
+ }
304
+ /**
305
+ * The numbers the visible items of a numbered list show, the way a browser's list counter counts them: from the
306
+ * list's start, or from the item count for a reversed list without one, and an item's value attribute sets the
307
+ * number from that item on. One pass per list, kept in ctx; the first version counted again for every item, and
308
+ * a list of 20 000 items took ten seconds (found by an independent review before 0.2.13 was released). A reversed
309
+ * list gives only its first item a number: Markdown has no reversed list and numbers every item from the first,
310
+ * so the later numbers of a faithful Markdown copy can never match.
311
+ */
312
+ function listOrdinals(ctx, ol) {
313
+ const cached = ctx.ordinals.get(ol);
314
+ if (cached)
315
+ return cached;
316
+ const out = new Map();
317
+ const items = ol.children.filter((c) => c instanceof Element && c.name === 'li' && !isSkipped(c, ctx.root, ctx.profile));
318
+ const reversed = ol.attribs['reversed'] !== undefined;
319
+ const start = Number.parseInt(ol.attribs['start'] ?? '', 10);
320
+ let n = Number.isFinite(start) ? start : reversed ? items.length : 1;
321
+ items.forEach((item, i) => {
322
+ const value = Number.parseInt(item.attribs['value'] ?? '', 10);
323
+ if (Number.isFinite(value))
324
+ n = value;
325
+ if (!reversed || i === 0)
326
+ out.set(item, n);
327
+ n += reversed ? -1 : 1;
328
+ });
329
+ ctx.ordinals.set(ol, out);
330
+ return out;
331
+ }
332
+ /** The state of the item's own checkbox, not one inside a nested list: true, false, or null without one. */
333
+ function taskState(li) {
334
+ const stack = [...li.children].reverse();
335
+ while (stack.length > 0) {
336
+ const node = stack.pop();
337
+ if (!(node instanceof Element) || node.name === 'ul' || node.name === 'ol')
338
+ continue;
339
+ if (node.name === 'input' && (node.attribs['type'] ?? '').toLowerCase() === 'checkbox')
340
+ return node.attribs['checked'] !== undefined;
341
+ for (let i = node.children.length - 1; i >= 0; i--)
342
+ stack.push(node.children[i]);
343
+ }
344
+ return null;
345
+ }
262
346
  function handleListItem(ctx, li) {
263
347
  // The item's own inline text (and inline text of a leading <p>) becomes one listItem block; nested lists
264
348
  // and other block children recurse.
349
+ const info = listInfo(ctx, li);
265
350
  let run = newRun();
266
351
  let ownFlushed = false;
267
352
  for (const child of li.children) {
@@ -276,7 +361,7 @@ function handleListItem(ctx, li) {
276
361
  continue;
277
362
  }
278
363
  if (!ownFlushed) {
279
- flushRun(ctx, run, li, 'listItem');
364
+ flushRun(ctx, run, li, 'listItem', info);
280
365
  ownFlushed = true;
281
366
  run = newRun();
282
367
  }
@@ -290,19 +375,19 @@ function handleListItem(ctx, li) {
290
375
  inlineText(ctx, child, run);
291
376
  }
292
377
  }
293
- flushRun(ctx, run, li, ownFlushed ? 'paragraph' : 'listItem');
378
+ flushRun(ctx, run, li, ownFlushed ? 'paragraph' : 'listItem', info);
294
379
  }
295
380
  /** Read only recognized direct line wrappers; never discard extra non-whitespace code children. */
296
381
  function starlightCode(pre) {
297
382
  if (!ancestor(pre, (node) => hasClass(node, 'expressive-code')))
298
- return textContent(pre);
383
+ return visibleCode(pre.children);
299
384
  const meaningful = pre.children.filter((node) => !(node instanceof Text && !node.data.trim()));
300
385
  const code = meaningful.length === 1 ? meaningful[0] : null;
301
386
  if (!(code instanceof Element) || code.name !== 'code')
302
- return textContent(pre);
387
+ return visibleCode(pre.children);
303
388
  const lines = code.children.filter((node) => !(node instanceof Text && !node.data.trim()));
304
389
  if (lines.length === 0 || !lines.every((node) => node instanceof Element && node.name === 'div' && hasClass(node, 'ec-line')))
305
- return textContent(pre);
390
+ return visibleCode(pre.children);
306
391
  // A gutter (line numbers from a plugin) sits beside the code as a direct child of the line; it is not code text.
307
392
  const gutter = (node) => node instanceof Element && node.name === 'div' && hasClass(node, 'gutter');
308
393
  // starlight-llms-txt prefixes + or - to ins and del lines when the block has a language other than diff. The page
@@ -317,16 +402,16 @@ function starlightCode(pre) {
317
402
  function starlightLineText(children, line, diffMarkers) {
318
403
  const inserted = hasClass(line, 'ins');
319
404
  if (!diffMarkers || !(inserted || hasClass(line, 'del')))
320
- return textContent(children);
405
+ return visibleCode(children);
321
406
  const target = firstNonIndentSpan(children)?.children[0];
322
407
  if (!(target instanceof Text))
323
- return textContent(children);
408
+ return visibleCode(children);
324
409
  let out = '';
325
410
  const collect = (nodes) => {
326
411
  for (const node of nodes) {
327
412
  if (node instanceof Text)
328
413
  out += (node === target ? (inserted ? '+' : '-') : '') + node.data;
329
- else if (node instanceof Element)
414
+ else if (node instanceof Element && !hiddenAttr(node))
330
415
  collect(node.children);
331
416
  }
332
417
  };
@@ -409,18 +494,38 @@ function handleStarlightTabs(ctx, component) {
409
494
  inlineText(ctx, child, label);
410
495
  if (!strictNormalize(label.text))
411
496
  throw new HtmlExtractError('Starlight tab label is empty.');
412
- flushRun(ctx, label, tab, 'listItem');
497
+ flushRun(ctx, label, tab, 'listItem', { ordered: false, depth: ctx.listBase, checked: null });
498
+ ctx.listBase++;
413
499
  walk(ctx, panel);
500
+ ctx.listBase--;
414
501
  }
415
502
  }
416
503
  function handleTable(ctx, table) {
417
- const rows = selectAll('tr', table).filter((tr) => closestTable(tr) === table);
504
+ // The caption is content: a paragraph before the table, the way a Markdown table's title has to be written. Up to
505
+ // 0.2.12 it was dropped with its text, numbers and links (found by an outside review 2026-09-26).
506
+ for (const child of table.children) {
507
+ if (child instanceof Element && child.name === 'caption' && !isSkipped(child, ctx.root, ctx.profile)) {
508
+ const run = newRun();
509
+ for (const c of child.children)
510
+ inlineText(ctx, c, run);
511
+ flushRun(ctx, run, child);
512
+ }
513
+ }
514
+ // Hidden rows, row groups and cells are skipped like any other hidden element (0.2.13; up to 0.2.12 they were read).
515
+ const shown = (tr) => {
516
+ for (let cur = tr; cur instanceof Element && cur !== table; cur = cur.parent) {
517
+ if (isSkipped(cur, ctx.root, ctx.profile))
518
+ return false;
519
+ }
520
+ return true;
521
+ };
522
+ const rows = selectAll('tr', table).filter((tr) => closestTable(tr) === table && shown(tr));
418
523
  const cells = [];
419
524
  const links = [];
420
525
  for (const tr of rows) {
421
526
  const row = [];
422
527
  for (const cell of tr.children) {
423
- if (cell instanceof Element && (cell.name === 'td' || cell.name === 'th')) {
528
+ if (cell instanceof Element && (cell.name === 'td' || cell.name === 'th') && !isSkipped(cell, ctx.root, ctx.profile)) {
424
529
  const run = newRun();
425
530
  for (const c of cell.children)
426
531
  inlineText(ctx, c, run);
@@ -445,7 +550,15 @@ function closestTable(el) {
445
550
  }
446
551
  return null;
447
552
  }
448
- function pickRoot(doc, selector, notes) {
553
+ /** Whether the page hides an element: it or an ancestor carries hidden or aria-hidden, or sits in a template. */
554
+ function hiddenInPage(el) {
555
+ for (let cur = el; cur instanceof Element; cur = cur.parent) {
556
+ if (hiddenAttr(cur) || cur.name === 'template')
557
+ return true;
558
+ }
559
+ return false;
560
+ }
561
+ function pickRoot(doc, selector, notes, issues) {
449
562
  if (selector) {
450
563
  let matches;
451
564
  try {
@@ -460,12 +573,30 @@ function pickRoot(doc, selector, notes) {
460
573
  notes.push(`Selector "${selector}" matched ${matches.length} elements; the first in document order was used.`);
461
574
  return { root: matches[0], strategy: `selector:${selector}`, confidence: 'high' };
462
575
  }
576
+ // A candidate the page hides is not its main content. Up to 0.2.12 the first candidate was taken as it was, so a
577
+ // hidden <main> before the visible one was compared in its place and the visible content not at all. Candidates
578
+ // that do not sit inside one another are separate content, and only the first is compared, so that is a warning;
579
+ // one nested inside another (an <article> inside an <article>) is part of it (found by an outside review 2026-09-26).
463
580
  for (const tag of ['main', 'article', '[role=main]']) {
464
581
  const matches = selectAll(tag, doc.children).filter((n) => n instanceof Element);
465
- if (matches.length >= 1) {
466
- if (matches.length > 1)
467
- notes.push(`${matches.length} <${tag}> elements found; the first in document order was used.`);
468
- return { root: matches[0], strategy: tag, confidence: 'high' };
582
+ const visible = matches.filter((el) => !hiddenInPage(el));
583
+ if (matches.length > visible.length)
584
+ notes.push(`${matches.length - visible.length} hidden <${tag}> element(s) were not used as the main content.`);
585
+ if (visible.length >= 1) {
586
+ const set = new Set(visible);
587
+ const outer = visible.filter((el) => {
588
+ for (let cur = el.parent; cur instanceof Element; cur = cur.parent)
589
+ if (set.has(cur))
590
+ return false;
591
+ return true;
592
+ });
593
+ if (outer.length > 1) {
594
+ issues.push({ code: 'EXTRACTION_MULTIPLE_ROOTS', severity: 'warning', message: `${outer.length} visible <${tag}> elements that do not contain one another were found; only the first in document order was compared. Use --selector to choose the content.` });
595
+ }
596
+ else if (visible.length > 1) {
597
+ notes.push(`${visible.length} <${tag}> elements found, nested inside the first; the first was used.`);
598
+ }
599
+ return { root: outer[0], strategy: tag, confidence: 'high' };
469
600
  }
470
601
  }
471
602
  const body = selectOne('body', doc.children);
@@ -486,6 +617,7 @@ export function extractHtml(source, options = {}) {
486
617
  if (depth > maxDepth)
487
618
  throw new HtmlExtractError(`HTML nesting depth ${depth} exceeds the limit of ${maxDepth} levels; the comparison was not run.`);
488
619
  const notes = [];
620
+ const issues = [];
489
621
  const baseEl = selectOne('base[href]', doc.children);
490
622
  let base = options.baseUrl ?? null;
491
623
  if (baseEl instanceof Element && baseEl.attribs['href']) {
@@ -493,14 +625,14 @@ export function extractHtml(source, options = {}) {
493
625
  if (resolved)
494
626
  base = resolved;
495
627
  }
496
- const { root, strategy, confidence } = pickRoot(doc, options.selector, notes);
628
+ const { root, strategy, confidence } = pickRoot(doc, options.selector, notes, issues);
497
629
  const lineStarts = [0];
498
630
  for (let i = 0; i < source.length; i++)
499
631
  if (source.charCodeAt(i) === 10)
500
632
  lineStarts.push(i + 1);
501
633
  if (profile === 'starlight')
502
634
  notes.push('Starlight profile: all associated tab panels are compared, including inactive panels; Expressive Code line boundaries and aside titles are retained.');
503
- const ctx = { profile, base, source, lineStarts, blocks: [], notes, root };
635
+ const ctx = { profile, base, source, lineStarts, blocks: [], notes, root, listBase: 0, ordinals: new Map(), segments: new Map() };
504
636
  // A selector that picks a heading, list item, table or pre keeps that block type, so the root goes
505
637
  // through handleBlock. Every other root is read as a container, a list included: handleBlock's list
506
638
  // branch reads only <li> children, and a root list would lose any other content it holds.
@@ -508,5 +640,5 @@ export function extractHtml(source, options = {}) {
508
640
  handleBlock(ctx, root);
509
641
  else
510
642
  walk(ctx, root);
511
- return { blocks: ctx.blocks, strategy: profile === 'generic' ? strategy : `${strategy};profile:starlight`, confidence, notes, issues: [] };
643
+ return { blocks: ctx.blocks, strategy: profile === 'generic' ? strategy : `${strategy};profile:starlight`, confidence, notes, issues };
512
644
  }
@@ -5,7 +5,7 @@ import { gfmFromMarkdown } from 'mdast-util-gfm';
5
5
  import { extractHtml, MAX_NESTING_DEPTH } from './html.js';
6
6
  import { parseDocument } from 'htmlparser2';
7
7
  import { Element, Text } from 'domhandler';
8
- import { excerpt, extractNumbers, looseNormalize, normalizeCode, resolveHref, strictNormalize } from './normalize.js';
8
+ import { excerpt, extractNumbers, insideHiddenUrlPart, looseNormalize, normalizeCode, resolveHref, strictNormalize } from './normalize.js';
9
9
  export class MarkdownExtractError extends Error {
10
10
  }
11
11
  /** Deepest node nesting of a Markdown tree, counted with an explicit stack. */
@@ -45,6 +45,7 @@ function parseInlineTag(value) {
45
45
  * text, <br> a space, everything else is dropped while the text between tags stays.
46
46
  */
47
47
  function inlineChildren(ctx, children, run, line) {
48
+ let closes = null;
48
49
  for (let i = 0; i < children.length; i++) {
49
50
  const node = children[i];
50
51
  if (node.type !== 'html') {
@@ -54,6 +55,21 @@ function inlineChildren(ctx, children, run, line) {
54
55
  const tag = parseInlineTag(node.value);
55
56
  if (!tag)
56
57
  continue; // comment or unparsable fragment: no visible content
58
+ // A hidden inline element hides its text in a reader that renders the HTML, as the same attribute does on the
59
+ // HTML side. Up to 0.2.12 only the tags were dropped and the text between them stayed (found by an outside review
60
+ // 2026-09-26). Its content up to the matching closing tag is left out; without one the tag is reported.
61
+ if (!tag.closing && (tag.attribs['hidden'] !== undefined || tag.attribs['aria-hidden'] === 'true')) {
62
+ if (VOID_TAGS.has(tag.name) || /\/>\s*$/.test(node.value))
63
+ continue;
64
+ closes ??= closingTags(children);
65
+ const end = closes.get(i) ?? -1;
66
+ if (end < 0) {
67
+ ctx.issues.push({ code: 'MARKDOWN_INLINE_HTML_UNSUPPORTED', severity: 'warning', message: `A hidden inline <${tag.name}> has no matching </${tag.name}> in the same paragraph; its text was compared as visible.`, line: node.position?.start.line ?? line, excerpt: excerpt(node.value) });
68
+ continue;
69
+ }
70
+ i = end;
71
+ continue;
72
+ }
57
73
  if (tag.closing) {
58
74
  if (tag.name === 'a')
59
75
  ctx.issues.push({ code: 'MARKDOWN_INLINE_HTML_UNSUPPORTED', severity: 'warning', message: 'A closing </a> without an opening <a> in the same paragraph; the inline HTML structure could not be resolved reliably.', line: node.position?.start.line ?? line, excerpt: excerpt(node.value) });
@@ -100,6 +116,33 @@ function inlineChildren(ctx, children, run, line) {
100
116
  i = end;
101
117
  }
102
118
  }
119
+ const VOID_TAGS = new Set(['area', 'base', 'br', 'col', 'embed', 'hr', 'img', 'input', 'link', 'meta', 'source', 'track', 'wbr']);
120
+ /**
121
+ * For every opening tag among the inline nodes of a paragraph, the index of its matching closing tag, counting
122
+ * nested tags of the same name; an opening tag without one is absent. One pass with a stack per tag name, computed
123
+ * once per paragraph. The first version scanned forward from each hidden tag, so a paragraph with 20 000 unclosed
124
+ * hidden tags took two minutes (found by an independent review before 0.2.13 was released).
125
+ */
126
+ function closingTags(children) {
127
+ const out = new Map();
128
+ const open = new Map();
129
+ children.forEach((c, j) => {
130
+ if (c.type !== 'html')
131
+ return;
132
+ const t = parseInlineTag(c.value);
133
+ if (!t)
134
+ return;
135
+ const stack = open.get(t.name) ?? [];
136
+ if (!t.closing) {
137
+ stack.push(j);
138
+ open.set(t.name, stack);
139
+ }
140
+ else if (stack.length > 0) {
141
+ out.set(stack.pop(), j);
142
+ }
143
+ });
144
+ return out;
145
+ }
103
146
  function inline(ctx, node, run) {
104
147
  switch (node.type) {
105
148
  case 'text':
@@ -142,7 +185,13 @@ function inline(ctx, node, run) {
142
185
  case 'link': {
143
186
  const inner = { text: '', links: [] };
144
187
  inlineChildren(ctx, node.children, inner, node.position?.start.line);
145
- run.links.push({ text: strictNormalize(inner.text), rawHref: node.url, resolved: resolveHref(node.url, ctx.base) });
188
+ const link = { text: strictNormalize(inner.text), rawHref: node.url, resolved: resolveHref(node.url, ctx.base) };
189
+ // A GFM literal autolink starts with neither [ nor <. One that GFM found inside a hidden part of another URL
190
+ // is compared as usual but shown masked (0.2.12).
191
+ const at = node.position?.start.offset;
192
+ if (at !== undefined && ctx.source[at] !== '[' && ctx.source[at] !== '<' && insideHiddenUrlPart(ctx.source, at))
193
+ link.masked = true;
194
+ run.links.push(link);
146
195
  run.links.push(...inner.links);
147
196
  run.text += inner.text;
148
197
  return;
@@ -186,7 +235,7 @@ function handleTable(ctx, table) {
186
235
  const text = cells.map((r) => r.join(' | ')).join(' \n ');
187
236
  push(ctx, { type: 'table', text: strictNormalize(text), cells, links }, table);
188
237
  }
189
- function handle(ctx, node, listItem = false) {
238
+ function handle(ctx, node, listItem) {
190
239
  switch (node.type) {
191
240
  case 'heading': {
192
241
  const run = { text: '', links: [] };
@@ -201,7 +250,7 @@ function handle(ctx, node, listItem = false) {
201
250
  inlineChildren(ctx, node.children, run, node.position?.start.line);
202
251
  const text = strictNormalize(run.text);
203
252
  if (text !== '')
204
- push(ctx, { type: listItem ? 'listItem' : 'paragraph', text, links: run.links }, node);
253
+ push(ctx, { type: listItem ? 'listItem' : 'paragraph', text, links: run.links, ...(listItem ? { list: listItem } : {}) }, node);
205
254
  return;
206
255
  }
207
256
  case 'code': {
@@ -213,17 +262,25 @@ function handle(ctx, node, listItem = false) {
213
262
  case 'table':
214
263
  handleTable(ctx, node);
215
264
  return;
216
- case 'list':
265
+ case 'list': {
266
+ // A Markdown list shows its start number and counts on; its items carry the list kind, the nesting level and
267
+ // the task state for the comparison (0.2.13).
268
+ let n = typeof node.start === 'number' ? node.start : 1;
217
269
  for (const item of node.children) {
270
+ const info = { ordered: node.ordered === true, depth: ctx.listDepth, checked: typeof item.checked === 'boolean' ? item.checked : null };
271
+ if (node.ordered === true)
272
+ info.ordinal = n;
273
+ n++;
274
+ ctx.listDepth++;
218
275
  let first = true;
219
276
  for (const child of item.children) {
220
- handle(ctx, child, first && child.type === 'paragraph');
277
+ handle(ctx, child, first && child.type === 'paragraph' ? info : undefined);
221
278
  first = false;
222
279
  }
223
- if (item.children.length === 0)
224
- continue;
280
+ ctx.listDepth--;
225
281
  }
226
282
  return;
283
+ }
227
284
  case 'blockquote':
228
285
  for (const c of node.children)
229
286
  handle(ctx, c);
@@ -231,9 +288,14 @@ function handle(ctx, node, listItem = false) {
231
288
  case 'html':
232
289
  handleRawHtml(ctx, node.value, node.position?.start.line);
233
290
  return;
291
+ case 'footnoteDefinition':
292
+ // Footnotes are not compared: an HTML page renders them as a numbered list with back links, which would not
293
+ // read as the Markdown definitions. Up to 0.2.12 a definition was dropped without a trace, so a footnote added
294
+ // to the Markdown passed strict mode (found by an outside review 2026-09-26). It is now a warning with its text.
295
+ ctx.issues.push({ code: 'MARKDOWN_FOOTNOTE_NOT_COMPARED', severity: 'warning', message: `Footnote [^${node.identifier}] is not compared; its text, numbers and links were left out of the comparison.`, line: node.position?.start.line, excerpt: excerpt(footnoteText(node)) });
296
+ return;
234
297
  case 'thematicBreak':
235
298
  case 'definition':
236
- case 'footnoteDefinition':
237
299
  case 'yaml':
238
300
  return;
239
301
  default:
@@ -242,6 +304,14 @@ function handle(ctx, node, listItem = false) {
242
304
  handle(ctx, c);
243
305
  }
244
306
  }
307
+ /** Plain text of a footnote definition for the report excerpt. */
308
+ function footnoteText(node) {
309
+ if ('value' in node && typeof node.value === 'string')
310
+ return node.value;
311
+ if ('children' in node)
312
+ return node.children.map((c) => footnoteText(c)).join(' ');
313
+ return '';
314
+ }
245
315
  /** A raw HTML block in Markdown is parsed with the HTML extractor so its content takes part in the
246
316
  * comparison; the report shows that this happened. Comments and bare tags without text yield nothing. */
247
317
  function handleRawHtml(ctx, value, line) {
@@ -332,7 +402,7 @@ export function extractMarkdown(source, options = {}) {
332
402
  const depth = treeDepth(tree);
333
403
  if (depth > maxDepth)
334
404
  throw new MarkdownExtractError(`Markdown nesting depth ${depth} exceeds the limit of ${maxDepth} levels; the comparison was not run.`);
335
- const ctx = { base: options.baseUrl ?? null, blocks: [], notes: [], issues: [], definitions: collectDefinitions(tree) };
405
+ const ctx = { base: options.baseUrl ?? null, source: fm.text, blocks: [], notes: [], issues: [], definitions: collectDefinitions(tree), listDepth: 0 };
336
406
  const fmExcerpt = () => excerpt(fm.body.replace(/\s+/g, ' '));
337
407
  if (mode === 'strip' && fm.skippedLines > 0) {
338
408
  ctx.notes.push(`Front matter (${fm.skippedLines} lines) was stripped as requested (--front-matter strip).`);
@@ -6,6 +6,9 @@ export interface Link {
6
6
  rawHref: string;
7
7
  /** Href resolved against the document base, or null when it could not be resolved. */
8
8
  resolved: string | null;
9
+ /** The link was made by GFM out of a hidden part of a URL (a query value, the fragment or user information), so
10
+ * a report shows neither its text nor its target. See insideHiddenUrlPart in normalize.ts. */
11
+ masked?: boolean;
9
12
  }
10
13
  export interface Location {
11
14
  /** 1-based line in the source text when the parser supplied a position. */
@@ -27,10 +30,22 @@ export interface Block {
27
30
  cells?: string[][];
28
31
  /** Code content after normalizeCode: line endings normalized to LF and trailing newlines removed. Only for type 'code'. */
29
32
  code?: string;
33
+ /** Where a list item sits in its list. Only for type 'listItem' (0.2.13). */
34
+ list?: ListInfo;
30
35
  links: Link[];
31
36
  numbers: string[];
32
37
  location: Location;
33
38
  }
39
+ export interface ListInfo {
40
+ /** A numbered list (ol, or a Markdown list with numbers). */
41
+ ordered: boolean;
42
+ /** Nesting level, 0 for a top-level list. */
43
+ depth: number;
44
+ /** The number the item shows, only in a numbered list: the list start or the item value, counted on. */
45
+ ordinal?: number;
46
+ /** Task item state: true checked, false unchecked, null when the item has no checkbox. */
47
+ checked: boolean | null;
48
+ }
34
49
  export interface Extraction {
35
50
  blocks: Block[];
36
51
  /** How the main content was selected. */
@@ -158,12 +158,39 @@ export function isPublicIPv6(ip) {
158
158
  return false; // documentation
159
159
  if (g0 === 0x2001 && g1 === 0)
160
160
  return isPublicIPv4(v4FromGroups(g6 ^ 0xffff, g7 ^ 0xffff)); // Teredo: check the embedded client address
161
+ if (g0 === 0x2001 && g1 < 0x0200)
162
+ return ietfProtocolAssignmentIsPublic(g); // 2001::/23 IETF protocol assignments
163
+ if (g0 === 0x3fff && (g1 & 0xf000) === 0)
164
+ return false; // 3fff::/20 documentation
165
+ if (g0 === 0x5f00)
166
+ return false; // 5f00::/16 SRv6 SIDs
167
+ if (g0 === 0x0100 && g1 === 0 && g2 === 0 && g3 === 1)
168
+ return false; // 100:0:0:1::/64 dummy prefix
161
169
  if (g0 === 0x2002)
162
170
  return isPublicIPv4(v4FromGroups(g1, g2)); // 6to4
163
171
  if (g0 === 0x0100 && g1 === 0 && g2 === 0 && g3 === 0)
164
172
  return false; // 100::/64 discard
165
173
  return true;
166
174
  }
175
+ /**
176
+ * 2001::/23 is not globally reachable except for the blocks the IANA IPv6 Special-Purpose Address Registry marks
177
+ * Globally Reachable True (read 2026-09-27, registry updated 2025-10-09): 2001:1::1, 2001:1::2 and 2001:1::3,
178
+ * 2001:3::/32, 2001:4:112::/48, 2001:20::/28 and 2001:30::/28. Teredo, 2001::/32, is checked before this. Up to
179
+ * 0.2.11 the whole block passed, benchmarking 2001:2::/48 among it, and so did 3fff::/20, 5f00::/16 and
180
+ * 100:0:0:1::/64 (found by an outside review 2026-09-26).
181
+ */
182
+ function ietfProtocolAssignmentIsPublic(g) {
183
+ const [, g1, g2, g3, g4, g5, g6, g7] = g;
184
+ if (g1 === 1 && g2 === 0 && g3 === 0 && g4 === 0 && g5 === 0 && g6 === 0 && g7 >= 1 && g7 <= 3)
185
+ return true;
186
+ if (g1 === 3)
187
+ return true;
188
+ if (g1 === 4 && g2 === 0x0112)
189
+ return true;
190
+ if ((g1 & 0xfff0) === 0x0020 || (g1 & 0xfff0) === 0x0030)
191
+ return true;
192
+ return false;
193
+ }
167
194
  function v4FromGroups(hi, lo) {
168
195
  return `${hi >> 8}.${hi & 0xff}.${lo >> 8}.${lo & 0xff}`;
169
196
  }
@@ -6,6 +6,16 @@ export declare function normalizeCode(code: string): string;
6
6
  export declare function extractNumbers(text: string): string[];
7
7
  /** Report excerpt: URLs inside the text are masked BEFORE truncation, so a cut query value cannot leak. */
8
8
  export declare function excerpt(text: string, max?: number): string;
9
+ /**
10
+ * Whether position at of a source text lies in a part of a URL that maskHref hides: user information, a query
11
+ * value or the fragment. GFM links a bare address it finds inside another URL on its own: the part after the
12
+ * colon of https://user_name:secret@host, which it reads as an email address, or a URL given as a query value.
13
+ * That link's text and target then reached the report unmasked, because on their own they no longer look like
14
+ * part of a URL (found by an outside review 2026-09-26). The URL is found the way rewriteUrlRuns finds it, in the
15
+ * token around the position; a token longer than HIDDEN_SCAN on either side counts as hidden, so the answer errs
16
+ * on the side of masking.
17
+ */
18
+ export declare function insideHiddenUrlPart(text: string, at: number): boolean;
9
19
  /** Masks user information, query values and fragments of every URL-like run in a piece of report text. */
10
20
  export declare function redactText(text: string): string;
11
21
  /**