markdown-parity-check 0.2.10 → 0.2.12
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/cli.d.ts +1 -1
- package/dist/src/cli.js +35 -3
- package/dist/src/compare.d.ts +26 -1
- package/dist/src/compare.js +124 -33
- package/dist/src/fetch.js +8 -3
- package/dist/src/html.js +7 -1
- package/dist/src/markdown.js +9 -3
- package/dist/src/model.d.ts +3 -0
- package/dist/src/netguard.js +27 -0
- package/dist/src/normalize.d.ts +10 -0
- package/dist/src/normalize.js +134 -7
- package/dist/src/version.d.ts +1 -1
- package/dist/src/version.js +1 -1
- package/package.json +1 -1
package/dist/src/cli.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import { type FetchOptions, type FetchResult } from './fetch.js';
|
|
3
|
-
export declare const HELP = "markdown-parity-check 0.2.
|
|
3
|
+
export declare const HELP = "markdown-parity-check 0.2.12\nCompares the main content of a web page's HTML and Markdown representations and reports concrete\nmissing, added and changed blocks.\n\nUsage:\n markdown-parity-check --url <https://example.com/page> [--markdown-url <url>] [options]\n markdown-parity-check --html-file <page.html> --markdown-file <page.md> [--base-url <url>] [options]\n\nModes:\n --url <url> Fetch <url> twice: with Accept: text/html and with Accept: text/markdown.\n --markdown-url <url> Fetch the Markdown from this explicit address instead (requires --url).\n --html-file <path> Offline: local HTML file (requires --markdown-file).\n --markdown-file <path> Offline: local Markdown file (requires --html-file).\n --base-url <url> Offline: base for resolving relative links on both sides.\n\nOptions:\n --html-profile generic|starlight\n HTML extraction rules (default: generic). Starlight includes inactive tab panels.\n --selector <css> CSS selector for the HTML main content (default: main, article, [role=main], then body).\n --front-matter keep|strip\n Markdown front matter. keep (default) removes nothing and warns when the document\n starts with a YAML-looking fenced block; strip removes that leading block.\n --format text|json Report format (default: text).\n --output <path> Write the report to a file instead of stdout. Refuses to overwrite an input file.\n --timeout-ms <n> Per-fetch timeout in milliseconds (default: 15000).\n --max-bytes <n> Cap on decoded response bytes per fetch (default: 5242880).\n --strict Treat warnings as rejecting findings too.\n --help, -h Show this help.\n --version, -V Show the version.\n\nExit codes:\n 0 comparison completed, no rejecting finding\n 1 comparison completed, rejecting content or delivery differences found\n 2 input, fetch, parse or report error prevented a reliable comparison\n\nOnly http(s) URLs to public addresses are fetched; loopback, private, link-local and other non-public\naddresses are refused, also behind DNS and redirects. No JavaScript is executed. Exit 0 does not prove\nsemantic equivalence.\n";
|
|
4
4
|
export declare class CliError extends Error {
|
|
5
5
|
}
|
|
6
6
|
export interface CliArgs {
|
package/dist/src/cli.js
CHANGED
|
@@ -8,7 +8,7 @@ import { fileURLToPath } from 'node:url';
|
|
|
8
8
|
import { fetchUrl, FetchError, validateUrl } from './fetch.js';
|
|
9
9
|
import { renderJson, renderText } from './report.js';
|
|
10
10
|
import { errorReport, run, RunError } from './run.js';
|
|
11
|
-
import { maskUrl } from './normalize.js';
|
|
11
|
+
import { maskUrl, redactText } from './normalize.js';
|
|
12
12
|
import { TOOL_NAME, TOOL_VERSION } from './version.js';
|
|
13
13
|
export const HELP = `${TOOL_NAME} ${TOOL_VERSION}
|
|
14
14
|
Compares the main content of a web page's HTML and Markdown representations and reports concrete
|
|
@@ -150,7 +150,7 @@ export function parseCliArgs(argv) {
|
|
|
150
150
|
if (args.output !== undefined) {
|
|
151
151
|
const out = path.resolve(args.output);
|
|
152
152
|
for (const input of [args.htmlFile, args.markdownFile]) {
|
|
153
|
-
if (input !== undefined && samePath(out, path.resolve(input)))
|
|
153
|
+
if (input !== undefined && (samePath(out, path.resolve(input)) || sameFile(out, path.resolve(input))))
|
|
154
154
|
throw new CliError(`--output must not point at an input file (${args.output}).`);
|
|
155
155
|
}
|
|
156
156
|
}
|
|
@@ -169,6 +169,22 @@ function samePath(a, b) {
|
|
|
169
169
|
const y = norm(b);
|
|
170
170
|
return process.platform === 'win32' ? x.toLowerCase() === y.toLowerCase() : x === y;
|
|
171
171
|
}
|
|
172
|
+
/**
|
|
173
|
+
* Whether two existing paths name the same file, by device and file number. A hard link is a second name for the
|
|
174
|
+
* same file and passes samePath, so up to 0.2.11 --output through a hard link overwrote the input with the report
|
|
175
|
+
* (found by an outside review 2026-09-26). On Windows the file number is the NTFS file index; a file system that
|
|
176
|
+
* reports 0 gives no answer, and samePath and the write through a temporary file below still apply.
|
|
177
|
+
*/
|
|
178
|
+
function sameFile(a, b) {
|
|
179
|
+
try {
|
|
180
|
+
const x = fs.statSync(a, { bigint: true });
|
|
181
|
+
const y = fs.statSync(b, { bigint: true });
|
|
182
|
+
return x.ino !== BigInt(0) && x.dev === y.dev && x.ino === y.ino;
|
|
183
|
+
}
|
|
184
|
+
catch {
|
|
185
|
+
return false;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
172
188
|
function readFileInput(file, base) {
|
|
173
189
|
let body;
|
|
174
190
|
try {
|
|
@@ -195,10 +211,21 @@ function emit(report, args, io) {
|
|
|
195
211
|
io.stdout(text);
|
|
196
212
|
return;
|
|
197
213
|
}
|
|
214
|
+
// The report goes to a new file next to the target, which then replaces the target by name. Writing into the
|
|
215
|
+
// target would truncate whatever file it names, also an input reached through a link (0.2.12).
|
|
216
|
+
const target = path.resolve(args.output);
|
|
217
|
+
const temp = path.join(path.dirname(target), `.${path.basename(target)}.${process.pid}.${Date.now()}.tmp`);
|
|
198
218
|
try {
|
|
199
|
-
fs.writeFileSync(
|
|
219
|
+
fs.writeFileSync(temp, text, { encoding: 'utf8', flag: 'wx' });
|
|
220
|
+
fs.renameSync(temp, target);
|
|
200
221
|
}
|
|
201
222
|
catch (err) {
|
|
223
|
+
try {
|
|
224
|
+
fs.rmSync(temp, { force: true });
|
|
225
|
+
}
|
|
226
|
+
catch {
|
|
227
|
+
// Nothing more to do: the error below names the target.
|
|
228
|
+
}
|
|
202
229
|
throw new CliError(`Cannot write report to ${args.output}: ${err.message}`);
|
|
203
230
|
}
|
|
204
231
|
io.stderr(`Report written to ${args.output} (${report.summary.result.toUpperCase()}, exit ${report.summary.exitCode}).\n`);
|
|
@@ -206,6 +233,11 @@ function emit(report, args, io) {
|
|
|
206
233
|
/** Runs the CLI and returns the exit code. Throws nothing; every failure becomes exit 2. */
|
|
207
234
|
export async function main(argv, io = { stdout: (s) => process.stdout.write(s), stderr: (s) => process.stderr.write(s) }, deps = {}) {
|
|
208
235
|
const fetchFn = deps.fetch ?? fetchUrl;
|
|
236
|
+
// Everything written to stderr goes through the same masking as the report. An error can repeat a URL the user
|
|
237
|
+
// gave or a header a server sent, and up to 0.2.11 stderr printed both as they were (found by an outside review
|
|
238
|
+
// 2026-09-26).
|
|
239
|
+
const raw = io;
|
|
240
|
+
io = { stdout: raw.stdout, stderr: (s) => raw.stderr(redactText(s)) };
|
|
209
241
|
let args;
|
|
210
242
|
try {
|
|
211
243
|
args = parseCliArgs(argv);
|
package/dist/src/compare.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { type AlignmentLimits } from './align.js';
|
|
2
|
-
import type { Extraction, Finding } from './model.js';
|
|
2
|
+
import type { Extraction, Finding, Link } from './model.js';
|
|
3
3
|
export interface Coverage {
|
|
4
4
|
htmlBlocks: number;
|
|
5
5
|
markdownBlocks: number;
|
|
@@ -13,6 +13,31 @@ export interface CompareResult {
|
|
|
13
13
|
findings: Finding[];
|
|
14
14
|
coverage: Coverage;
|
|
15
15
|
}
|
|
16
|
+
/**
|
|
17
|
+
* The numbers each side has that the other lacks, or both lists when the multisets are equal and only the order
|
|
18
|
+
* differs; null when the lists are equal. Counted with a map: up to 0.2.11 every value was looked up with indexOf
|
|
19
|
+
* and removed with splice, so one block with 20 000 numbers took two seconds (found by an outside review
|
|
20
|
+
* 2026-09-26). The result is the same.
|
|
21
|
+
* @internal Exported for test/compare-links.test.ts.
|
|
22
|
+
*/
|
|
23
|
+
export declare function diffNumbers(a: string[], b: string[]): {
|
|
24
|
+
before: string[];
|
|
25
|
+
after: string[];
|
|
26
|
+
} | null;
|
|
27
|
+
/**
|
|
28
|
+
* Pairs the HTML block's links with the Markdown block's. Each HTML link takes the first unused Markdown link, in
|
|
29
|
+
* document order, that has the same text and the same target, or else the first unused one with the same text.
|
|
30
|
+
* Up to 0.2.11 both searches scanned the whole remaining list, so one block with 20 000 links took more than a
|
|
31
|
+
* second (found by an outside review 2026-09-26). Now the Markdown links are indexed by text and, for a resolved
|
|
32
|
+
* HTML link, by text and target, which gives the same pairs; an unresolved HTML link still scans the links that
|
|
33
|
+
* share its text, because relativeHrefRelation compares two references and has no key of its own.
|
|
34
|
+
* @internal Exported for test/compare-links.test.ts.
|
|
35
|
+
*/
|
|
36
|
+
export declare function pairLinks(hLinks: Link[], mLinks: Link[]): {
|
|
37
|
+
pairs: Array<[number, number]>;
|
|
38
|
+
missing: number[];
|
|
39
|
+
added: number[];
|
|
40
|
+
};
|
|
16
41
|
export declare function compare(html: Extraction, markdown: Extraction, options?: {
|
|
17
42
|
bothBases?: boolean;
|
|
18
43
|
limits?: Partial<AlignmentLimits>;
|
package/dist/src/compare.js
CHANGED
|
@@ -1,8 +1,16 @@
|
|
|
1
1
|
// The comparison core: aligns blocks and turns the alignment into findings. Pure and network-free.
|
|
2
2
|
import { align, UNCERTAIN_THRESHOLD } from './align.js';
|
|
3
3
|
import { excerpt, hrefDifference, maskHref, redactText, relativeHrefRelation, visibleNumbers } from './normalize.js';
|
|
4
|
+
// The excerpt masks the whole block text before it cuts it, so it is computed once per block. Up to 0.2.11 every
|
|
5
|
+
// finding computed it again, and a block with thousands of link findings did that thousands of times (0.2.12).
|
|
6
|
+
const sides = new WeakMap();
|
|
4
7
|
function side(b) {
|
|
5
|
-
|
|
8
|
+
let s = sides.get(b);
|
|
9
|
+
if (!s) {
|
|
10
|
+
s = { line: b.location.line, path: b.location.path, blockIndex: b.location.blockIndex, excerpt: excerpt(b.type === 'code' ? (b.code ?? b.text) : b.text) };
|
|
11
|
+
sides.set(b, s);
|
|
12
|
+
}
|
|
13
|
+
return { ...s };
|
|
6
14
|
}
|
|
7
15
|
function label(b) {
|
|
8
16
|
switch (b.type) {
|
|
@@ -16,65 +24,148 @@ function label(b) {
|
|
|
16
24
|
return b.type;
|
|
17
25
|
}
|
|
18
26
|
}
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
const
|
|
23
|
-
|
|
27
|
+
/** The values of a that b does not account for, one for one, in the order of a. */
|
|
28
|
+
function unmatched(a, b) {
|
|
29
|
+
const left = new Map();
|
|
30
|
+
for (const v of b)
|
|
31
|
+
left.set(v, (left.get(v) ?? 0) + 1);
|
|
32
|
+
const out = [];
|
|
24
33
|
for (const v of a) {
|
|
25
|
-
const
|
|
26
|
-
if (
|
|
27
|
-
|
|
34
|
+
const n = left.get(v) ?? 0;
|
|
35
|
+
if (n > 0)
|
|
36
|
+
left.set(v, n - 1);
|
|
28
37
|
else
|
|
29
|
-
|
|
30
|
-
}
|
|
31
|
-
const remA = [...a];
|
|
32
|
-
const after = [];
|
|
33
|
-
for (const v of b) {
|
|
34
|
-
const k = remA.indexOf(v);
|
|
35
|
-
if (k >= 0)
|
|
36
|
-
remA.splice(k, 1);
|
|
37
|
-
else
|
|
38
|
-
after.push(v);
|
|
38
|
+
out.push(v);
|
|
39
39
|
}
|
|
40
|
+
return out;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* The numbers each side has that the other lacks, or both lists when the multisets are equal and only the order
|
|
44
|
+
* differs; null when the lists are equal. Counted with a map: up to 0.2.11 every value was looked up with indexOf
|
|
45
|
+
* and removed with splice, so one block with 20 000 numbers took two seconds (found by an outside review
|
|
46
|
+
* 2026-09-26). The result is the same.
|
|
47
|
+
* @internal Exported for test/compare-links.test.ts.
|
|
48
|
+
*/
|
|
49
|
+
export function diffNumbers(a, b) {
|
|
50
|
+
if (a.length === b.length && a.every((v, i) => v === b[i]))
|
|
51
|
+
return null;
|
|
52
|
+
const before = unmatched(a, b);
|
|
53
|
+
const after = unmatched(b, a);
|
|
40
54
|
if (before.length === 0 && after.length === 0)
|
|
41
55
|
return { before: a, after: b }; // same multiset, different order
|
|
42
56
|
return { before, after };
|
|
43
57
|
}
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
58
|
+
// What a finding shows of a link. A link GFM made out of a hidden part of a URL shows neither text nor target.
|
|
59
|
+
const linkText = (l, other) => (l.masked || other?.masked ? '***' : redactText(l.text));
|
|
60
|
+
const linkHref = (l, href) => (l.masked ? '***' : maskHref(href));
|
|
61
|
+
/**
|
|
62
|
+
* Pairs the HTML block's links with the Markdown block's. Each HTML link takes the first unused Markdown link, in
|
|
63
|
+
* document order, that has the same text and the same target, or else the first unused one with the same text.
|
|
64
|
+
* Up to 0.2.11 both searches scanned the whole remaining list, so one block with 20 000 links took more than a
|
|
65
|
+
* second (found by an outside review 2026-09-26). Now the Markdown links are indexed by text and, for a resolved
|
|
66
|
+
* HTML link, by text and target, which gives the same pairs; an unresolved HTML link still scans the links that
|
|
67
|
+
* share its text, because relativeHrefRelation compares two references and has no key of its own.
|
|
68
|
+
* @internal Exported for test/compare-links.test.ts.
|
|
69
|
+
*/
|
|
70
|
+
export function pairLinks(hLinks, mLinks) {
|
|
71
|
+
const used = new Array(mLinks.length).fill(false);
|
|
72
|
+
const lists = new Map();
|
|
73
|
+
const heads = new Map();
|
|
74
|
+
const key = (...parts) => JSON.stringify(parts);
|
|
75
|
+
const add = (k, i) => {
|
|
76
|
+
const list = lists.get(k);
|
|
77
|
+
if (list)
|
|
78
|
+
list.push(i);
|
|
79
|
+
else
|
|
80
|
+
lists.set(k, [i]);
|
|
81
|
+
};
|
|
82
|
+
const first = (k) => {
|
|
83
|
+
const list = lists.get(k);
|
|
84
|
+
if (!list)
|
|
85
|
+
return -1;
|
|
86
|
+
let n = heads.get(k) ?? 0;
|
|
87
|
+
while (n < list.length && used[list[n]])
|
|
88
|
+
n++;
|
|
89
|
+
heads.set(k, n);
|
|
90
|
+
return n < list.length ? list[n] : -1;
|
|
91
|
+
};
|
|
92
|
+
mLinks.forEach((l, i) => {
|
|
93
|
+
add(key('text', l.text), i);
|
|
94
|
+
if (l.resolved !== null)
|
|
95
|
+
add(key('resolved', l.text, l.resolved), i);
|
|
96
|
+
else
|
|
97
|
+
add(key('raw-unresolved', l.text, l.rawHref.trim()), i);
|
|
98
|
+
});
|
|
99
|
+
const pairs = [];
|
|
100
|
+
const missing = [];
|
|
101
|
+
hLinks.forEach((hl, hi) => {
|
|
102
|
+
let idx = -1;
|
|
103
|
+
if (hl.resolved !== null) {
|
|
104
|
+
// sameTarget: a resolved Markdown link with the same resolved href, or an unresolved one with the same raw href.
|
|
105
|
+
const a = first(key('resolved', hl.text, hl.resolved));
|
|
106
|
+
const b = first(key('raw-unresolved', hl.text, hl.rawHref.trim()));
|
|
107
|
+
idx = a < 0 ? b : b < 0 ? a : Math.min(a, b);
|
|
108
|
+
}
|
|
109
|
+
else {
|
|
110
|
+
const list = lists.get(key('text', hl.text)) ?? [];
|
|
111
|
+
for (const i of list) {
|
|
112
|
+
if (!used[i] && sameTarget(hl, mLinks[i])) {
|
|
113
|
+
idx = i;
|
|
114
|
+
break;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
}
|
|
49
118
|
if (idx < 0)
|
|
50
|
-
idx =
|
|
119
|
+
idx = first(key('text', hl.text));
|
|
51
120
|
if (idx < 0) {
|
|
52
|
-
|
|
121
|
+
missing.push(hi);
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
used[idx] = true;
|
|
125
|
+
pairs.push([hi, idx]);
|
|
126
|
+
});
|
|
127
|
+
const added = [];
|
|
128
|
+
used.forEach((u, i) => {
|
|
129
|
+
if (!u)
|
|
130
|
+
added.push(i);
|
|
131
|
+
});
|
|
132
|
+
return { pairs, missing, added };
|
|
133
|
+
}
|
|
134
|
+
function compareLinks(out, h, m, bothBases) {
|
|
135
|
+
// Link targets are compared unmasked; every value that reaches a finding goes through maskHref.
|
|
136
|
+
const { pairs, missing, added } = pairLinks(h.links, m.links);
|
|
137
|
+
const events = [...pairs.map(([hi, mi]) => ({ hi, mi })), ...missing.map((hi) => ({ hi, mi: -1 }))].sort((x, y) => x.hi - y.hi);
|
|
138
|
+
for (const { hi, mi } of events) {
|
|
139
|
+
const hl = h.links[hi];
|
|
140
|
+
if (mi < 0) {
|
|
141
|
+
out.push({ code: 'LINK_MISSING', severity: 'error', direction: 'html_only', message: `Link "${linkText(hl)}" (${linkHref(hl, hl.rawHref)}) is in the HTML block but not in the Markdown block.`, html: side(h), markdown: side(m), before: linkHref(hl, hl.rawHref) });
|
|
53
142
|
continue;
|
|
54
143
|
}
|
|
55
|
-
const ml =
|
|
144
|
+
const ml = m.links[mi];
|
|
56
145
|
if (sameTarget(hl, ml))
|
|
57
146
|
continue;
|
|
147
|
+
const text = linkText(hl, ml);
|
|
58
148
|
if (hl.resolved !== null && ml.resolved !== null) {
|
|
59
149
|
const diff = hrefDifference(hl.resolved, ml.resolved);
|
|
60
|
-
out.push({ code: 'LINK_TARGET_CHANGED', severity: 'error', direction: 'both', message: `Link "${
|
|
150
|
+
out.push({ code: 'LINK_TARGET_CHANGED', severity: 'error', direction: 'both', message: `Link "${text}" points to a different target (${diff} differs).`, html: side(h), markdown: side(m), before: linkHref(hl, hl.resolved), after: linkHref(ml, ml.resolved) });
|
|
61
151
|
}
|
|
62
152
|
else if (hl.resolved === null && ml.resolved === null && relativeHrefRelation(hl.rawHref, ml.rawHref) === 'uncertain') {
|
|
63
153
|
// Neither side resolved and no base could be ruled out as making them meet. A textual difference
|
|
64
154
|
// alone does not establish different destinations, so this is a warning and not an error.
|
|
65
|
-
out.push({ code: 'LINK_UNVERIFIED', severity: 'warning', direction: 'both', message: `Link "${
|
|
155
|
+
out.push({ code: 'LINK_UNVERIFIED', severity: 'warning', direction: 'both', message: `Link "${text}" differs textually and no base URL is known, so the two relative forms cannot be confirmed equal or different.`, html: side(h), markdown: side(m), before: linkHref(hl, hl.rawHref), after: linkHref(ml, ml.rawHref) });
|
|
66
156
|
}
|
|
67
157
|
else if (hl.resolved === null && ml.resolved === null) {
|
|
68
158
|
// Neither side resolved, and relativeHrefRelation found a difference no base can remove.
|
|
69
159
|
const diff = hrefDifference(hl.rawHref, ml.rawHref);
|
|
70
|
-
out.push({ code: 'LINK_TARGET_CHANGED', severity: 'error', direction: 'both', message: `Link "${
|
|
160
|
+
out.push({ code: 'LINK_TARGET_CHANGED', severity: 'error', direction: 'both', message: `Link "${text}" points to a different relative target (${diff} differs).`, html: side(h), markdown: side(m), before: linkHref(hl, hl.rawHref), after: linkHref(ml, ml.rawHref) });
|
|
71
161
|
}
|
|
72
162
|
else {
|
|
73
|
-
out.push({ code: 'LINK_UNVERIFIED', severity: 'warning', direction: 'both', message: bothBases ? `Link "${
|
|
163
|
+
out.push({ code: 'LINK_UNVERIFIED', severity: 'warning', direction: 'both', message: bothBases ? `Link "${text}" could not be resolved on one side.` : `Link "${text}" differs textually and no base URL is known, so relative and absolute forms cannot be confirmed equal.`, html: side(h), markdown: side(m), before: linkHref(hl, hl.rawHref), after: linkHref(ml, ml.rawHref) });
|
|
74
164
|
}
|
|
75
165
|
}
|
|
76
|
-
for (const
|
|
77
|
-
|
|
166
|
+
for (const mi of added) {
|
|
167
|
+
const ml = m.links[mi];
|
|
168
|
+
out.push({ code: 'LINK_ADDED', severity: 'error', direction: 'markdown_only', message: `Link "${linkText(ml)}" (${linkHref(ml, ml.rawHref)}) is in the Markdown block but not in the HTML block.`, html: side(h), markdown: side(m), after: linkHref(ml, ml.rawHref) });
|
|
78
169
|
}
|
|
79
170
|
}
|
|
80
171
|
function sameTarget(a, b) {
|
package/dist/src/fetch.js
CHANGED
|
@@ -7,7 +7,7 @@ import https from 'node:https';
|
|
|
7
7
|
import dns from 'node:dns';
|
|
8
8
|
import zlib from 'node:zlib';
|
|
9
9
|
import { assertPublicHost, assertPublicResolved, BlockedAddressError } from './netguard.js';
|
|
10
|
-
import { maskUrl } from './normalize.js';
|
|
10
|
+
import { maskUrl, redactText } from './normalize.js';
|
|
11
11
|
export class FetchError extends Error {
|
|
12
12
|
kind;
|
|
13
13
|
url;
|
|
@@ -82,7 +82,8 @@ function decompressor(encoding) {
|
|
|
82
82
|
case 'identity':
|
|
83
83
|
return null;
|
|
84
84
|
default:
|
|
85
|
-
|
|
85
|
+
// The header is the server's text: masked and cut before it goes into a message (0.2.12).
|
|
86
|
+
throw new FetchError('protocol', '', `Unsupported Content-Encoding: ${redactText(String(encoding)).slice(0, 100)}`);
|
|
86
87
|
}
|
|
87
88
|
}
|
|
88
89
|
/**
|
|
@@ -168,7 +169,11 @@ function requestOnce(url, address, options, deadline) {
|
|
|
168
169
|
dec = decompressor(res.headers['content-encoding']);
|
|
169
170
|
}
|
|
170
171
|
catch (err) {
|
|
171
|
-
res.resume()
|
|
172
|
+
// Close the connection at once. Up to 0.2.11 this path drained the body with res.resume(), and since neither
|
|
173
|
+
// the byte count nor the deadline is attached here, a server that kept sending held the socket open and
|
|
174
|
+
// reading after the fetch had already failed (found by an outside review 2026-09-26).
|
|
175
|
+
res.destroy();
|
|
176
|
+
req.destroy();
|
|
172
177
|
reject(new FetchError('protocol', url.href, err.message));
|
|
173
178
|
return;
|
|
174
179
|
}
|
package/dist/src/html.js
CHANGED
|
@@ -501,6 +501,12 @@ export function extractHtml(source, options = {}) {
|
|
|
501
501
|
if (profile === 'starlight')
|
|
502
502
|
notes.push('Starlight profile: all associated tab panels are compared, including inactive panels; Expressive Code line boundaries and aside titles are retained.');
|
|
503
503
|
const ctx = { profile, base, source, lineStarts, blocks: [], notes, root };
|
|
504
|
-
|
|
504
|
+
// A selector that picks a heading, list item, table or pre keeps that block type, so the root goes
|
|
505
|
+
// through handleBlock. Every other root is read as a container, a list included: handleBlock's list
|
|
506
|
+
// branch reads only <li> children, and a root list would lose any other content it holds.
|
|
507
|
+
if (HEADING_RE.test(root.name) || root.name === 'li' || root.name === 'table' || root.name === 'pre')
|
|
508
|
+
handleBlock(ctx, root);
|
|
509
|
+
else
|
|
510
|
+
walk(ctx, root);
|
|
505
511
|
return { blocks: ctx.blocks, strategy: profile === 'generic' ? strategy : `${strategy};profile:starlight`, confidence, notes, issues: [] };
|
|
506
512
|
}
|
package/dist/src/markdown.js
CHANGED
|
@@ -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. */
|
|
@@ -142,7 +142,13 @@ function inline(ctx, node, run) {
|
|
|
142
142
|
case 'link': {
|
|
143
143
|
const inner = { text: '', links: [] };
|
|
144
144
|
inlineChildren(ctx, node.children, inner, node.position?.start.line);
|
|
145
|
-
|
|
145
|
+
const link = { text: strictNormalize(inner.text), rawHref: node.url, resolved: resolveHref(node.url, ctx.base) };
|
|
146
|
+
// A GFM literal autolink starts with neither [ nor <. One that GFM found inside a hidden part of another URL
|
|
147
|
+
// is compared as usual but shown masked (0.2.12).
|
|
148
|
+
const at = node.position?.start.offset;
|
|
149
|
+
if (at !== undefined && ctx.source[at] !== '[' && ctx.source[at] !== '<' && insideHiddenUrlPart(ctx.source, at))
|
|
150
|
+
link.masked = true;
|
|
151
|
+
run.links.push(link);
|
|
146
152
|
run.links.push(...inner.links);
|
|
147
153
|
run.text += inner.text;
|
|
148
154
|
return;
|
|
@@ -332,7 +338,7 @@ export function extractMarkdown(source, options = {}) {
|
|
|
332
338
|
const depth = treeDepth(tree);
|
|
333
339
|
if (depth > maxDepth)
|
|
334
340
|
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) };
|
|
341
|
+
const ctx = { base: options.baseUrl ?? null, source: fm.text, blocks: [], notes: [], issues: [], definitions: collectDefinitions(tree) };
|
|
336
342
|
const fmExcerpt = () => excerpt(fm.body.replace(/\s+/g, ' '));
|
|
337
343
|
if (mode === 'strip' && fm.skippedLines > 0) {
|
|
338
344
|
ctx.notes.push(`Front matter (${fm.skippedLines} lines) was stripped as requested (--front-matter strip).`);
|
package/dist/src/model.d.ts
CHANGED
|
@@ -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. */
|
package/dist/src/netguard.js
CHANGED
|
@@ -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
|
}
|
package/dist/src/normalize.d.ts
CHANGED
|
@@ -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
|
/**
|
package/dist/src/normalize.js
CHANGED
|
@@ -148,27 +148,154 @@ function urlStart(t, s, e) {
|
|
|
148
148
|
if (k > p + 2)
|
|
149
149
|
p = k - 1;
|
|
150
150
|
}
|
|
151
|
+
// (?:https?|ftp|wss?|file):[\/\\]*[^\s<>"'()\/\\?#@]*@ A special scheme whose authority carries user information,
|
|
152
|
+
// behind any number of slashes or backslashes, none included (0.2.12). The URL parser reads an authority after a
|
|
153
|
+
// special scheme whatever the separators, so https:user:secret@host and https:\user:secret@host carry user
|
|
154
|
+
// information just as https://user:secret@host does, and both passed a report unmasked, the first also as a GFM
|
|
155
|
+
// email link (found by an independent review before 0.2.12 was released). A failed candidate found no '@' before
|
|
156
|
+
// its delimiter, so a later candidate can succeed only when its colon sits right before that delimiter and the
|
|
157
|
+
// delimiter is its separator: the search goes on from at most the length of "https:" before the delimiter, and
|
|
158
|
+
// each position is scanned a bounded number of times.
|
|
159
|
+
for (let p = s; p < e && (best < 0 || p < best); p++) {
|
|
160
|
+
const n = specialSchemeLength(t, p, e);
|
|
161
|
+
if (n < 0)
|
|
162
|
+
continue;
|
|
163
|
+
let k = p + n + 1;
|
|
164
|
+
while (k < e && isSep(t.charCodeAt(k)))
|
|
165
|
+
k++;
|
|
166
|
+
while (k < e) {
|
|
167
|
+
const c = t.charCodeAt(k);
|
|
168
|
+
if (c === 0x2f || c === 0x5c || c === 0x3f || c === 0x23 || c === 0x40)
|
|
169
|
+
break;
|
|
170
|
+
k++;
|
|
171
|
+
}
|
|
172
|
+
if (k < e && t.charCodeAt(k) === 0x40) {
|
|
173
|
+
best = p;
|
|
174
|
+
break;
|
|
175
|
+
}
|
|
176
|
+
p = Math.max(p, k - 7);
|
|
177
|
+
}
|
|
151
178
|
return best;
|
|
152
179
|
}
|
|
180
|
+
const SPECIAL_SCHEME_NAMES = ['https', 'http', 'ftp', 'wss', 'ws', 'file'];
|
|
181
|
+
/** Length of the special scheme name at t[p] when a colon follows it, else -1. Letters compare with the old
|
|
182
|
+
* expression's i and u flags, under which U+017F matches s. */
|
|
183
|
+
function specialSchemeLength(t, p, e) {
|
|
184
|
+
const fold = (c) => (c === 0x17f ? 0x73 : c >= 0x41 && c <= 0x5a ? c + 0x20 : c);
|
|
185
|
+
for (const name of SPECIAL_SCHEME_NAMES) {
|
|
186
|
+
if (p + name.length >= e || t.charCodeAt(p + name.length) !== 0x3a)
|
|
187
|
+
continue;
|
|
188
|
+
let ok = true;
|
|
189
|
+
for (let i = 0; i < name.length && ok; i++)
|
|
190
|
+
ok = fold(t.charCodeAt(p + i)) === name.charCodeAt(i);
|
|
191
|
+
if (ok)
|
|
192
|
+
return name.length;
|
|
193
|
+
}
|
|
194
|
+
return -1;
|
|
195
|
+
}
|
|
196
|
+
// The rest of the token after a URL-like run. A run ends at ( ) " and ', but a URL can carry all four, so once a
|
|
197
|
+
// URL has started it goes on to the next whitespace, < or >. Up to 0.2.11 the URL ended at the run, and a query
|
|
198
|
+
// value such as ?token=(4711) left the parenthesis and the digits outside the mask, where the report showed them
|
|
199
|
+
// as text and as a changed number (found by an outside review 2026-09-26). Text glued to a query value without a
|
|
200
|
+
// space, such as the 's of ?x=1's, is part of the value to the URL parser too, and is masked with it.
|
|
201
|
+
const TOKEN_TAIL = /[^\s<>]*/uy;
|
|
202
|
+
/** End of the URL that starts at p inside the token t[p, t): trailing sentence punctuation, quotes and closing
|
|
203
|
+
* parentheses that have no opening one inside the URL are left outside it. One pass, so the scan stays linear. */
|
|
204
|
+
function urlEnd(text, p, t) {
|
|
205
|
+
let open = 0;
|
|
206
|
+
let close = 0;
|
|
207
|
+
for (let k = p; k < t; k++) {
|
|
208
|
+
const c = text.charCodeAt(k);
|
|
209
|
+
if (c === 0x28)
|
|
210
|
+
open++;
|
|
211
|
+
else if (c === 0x29)
|
|
212
|
+
close++;
|
|
213
|
+
}
|
|
214
|
+
let end = t;
|
|
215
|
+
while (end > p) {
|
|
216
|
+
const c = text[end - 1];
|
|
217
|
+
if (TRAIL.includes(c) || c === '"' || c === "'")
|
|
218
|
+
end--;
|
|
219
|
+
else if (c === ')' && close > open) {
|
|
220
|
+
close--;
|
|
221
|
+
end--;
|
|
222
|
+
}
|
|
223
|
+
else
|
|
224
|
+
break;
|
|
225
|
+
}
|
|
226
|
+
return end;
|
|
227
|
+
}
|
|
228
|
+
/** End of the token whose run ends at e: the run end moved forward over TOKEN_TAIL. */
|
|
229
|
+
function tokenEnd(text, e) {
|
|
230
|
+
TOKEN_TAIL.lastIndex = e;
|
|
231
|
+
const m = TOKEN_TAIL.exec(text);
|
|
232
|
+
return e + (m ? m[0].length : 0);
|
|
233
|
+
}
|
|
153
234
|
/** Rewrites the URL-like part of every run in a piece of text and leaves the rest untouched. */
|
|
154
235
|
function rewriteUrlRuns(text, rewrite) {
|
|
155
236
|
let out = '';
|
|
156
237
|
let done = 0;
|
|
157
|
-
|
|
238
|
+
const runs = new RegExp(RUN.source, RUN.flags);
|
|
239
|
+
for (let run = runs.exec(text); run !== null; run = runs.exec(text)) {
|
|
158
240
|
const s = run.index;
|
|
159
241
|
const e = s + run[0].length;
|
|
160
242
|
const p = urlStart(text, s, e);
|
|
161
243
|
if (p < 0)
|
|
162
244
|
continue;
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
done = e;
|
|
245
|
+
const t = tokenEnd(text, e);
|
|
246
|
+
const end = urlEnd(text, p, t);
|
|
247
|
+
out += text.slice(done, p) + rewrite(text.slice(p, end)) + text.slice(end, t);
|
|
248
|
+
done = t;
|
|
249
|
+
runs.lastIndex = t;
|
|
169
250
|
}
|
|
170
251
|
return out + text.slice(done);
|
|
171
252
|
}
|
|
253
|
+
// How far insideHiddenUrlPart reads the token around a position. A longer token counts as hidden.
|
|
254
|
+
const HIDDEN_SCAN = 4096;
|
|
255
|
+
/**
|
|
256
|
+
* Whether position at of a source text lies in a part of a URL that maskHref hides: user information, a query
|
|
257
|
+
* value or the fragment. GFM links a bare address it finds inside another URL on its own: the part after the
|
|
258
|
+
* colon of https://user_name:secret@host, which it reads as an email address, or a URL given as a query value.
|
|
259
|
+
* That link's text and target then reached the report unmasked, because on their own they no longer look like
|
|
260
|
+
* part of a URL (found by an outside review 2026-09-26). The URL is found the way rewriteUrlRuns finds it, in the
|
|
261
|
+
* token around the position; a token longer than HIDDEN_SCAN on either side counts as hidden, so the answer errs
|
|
262
|
+
* on the side of masking.
|
|
263
|
+
*/
|
|
264
|
+
export function insideHiddenUrlPart(text, at) {
|
|
265
|
+
const isEdge = (i) => {
|
|
266
|
+
const ch = text[i];
|
|
267
|
+
return ch === '<' || ch === '>' || /\s/u.test(ch);
|
|
268
|
+
};
|
|
269
|
+
let start = at;
|
|
270
|
+
while (start > 0 && !isEdge(start - 1)) {
|
|
271
|
+
if (at - start >= HIDDEN_SCAN)
|
|
272
|
+
return true;
|
|
273
|
+
start--;
|
|
274
|
+
}
|
|
275
|
+
let stop = at;
|
|
276
|
+
while (stop < text.length && !isEdge(stop)) {
|
|
277
|
+
if (stop - at >= HIDDEN_SCAN)
|
|
278
|
+
return true;
|
|
279
|
+
stop++;
|
|
280
|
+
}
|
|
281
|
+
const token = text.slice(start, stop);
|
|
282
|
+
const offset = at - start;
|
|
283
|
+
const runs = new RegExp(RUN.source, RUN.flags);
|
|
284
|
+
for (let run = runs.exec(token); run !== null && run.index < offset; run = runs.exec(token)) {
|
|
285
|
+
const p = urlStart(token, run.index, run.index + run[0].length);
|
|
286
|
+
if (p < 0)
|
|
287
|
+
continue;
|
|
288
|
+
if (p >= offset)
|
|
289
|
+
return false;
|
|
290
|
+
const before = token.slice(p, offset);
|
|
291
|
+
if (/[?#]/.test(before))
|
|
292
|
+
return true;
|
|
293
|
+
// Still inside the authority: what came before the position is user information. A special scheme has an
|
|
294
|
+
// authority behind any number of separators, none included.
|
|
295
|
+
return /^(?:(?:https?|ftp|wss?|file):[\/\\]*|(?:[a-z][a-z0-9+.-]*:)?[\/\\]{2})[^\/\\?#]*$/i.test(before);
|
|
296
|
+
}
|
|
297
|
+
return false;
|
|
298
|
+
}
|
|
172
299
|
/** Masks user information, query values and fragments of every URL-like run in a piece of report text. */
|
|
173
300
|
export function redactText(text) {
|
|
174
301
|
return rewriteUrlRuns(text, maskHref);
|
package/dist/src/version.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
export declare const TOOL_NAME = "markdown-parity-check";
|
|
2
|
-
export declare const TOOL_VERSION = "0.2.
|
|
2
|
+
export declare const TOOL_VERSION = "0.2.12";
|
package/dist/src/version.js
CHANGED
|
@@ -2,4 +2,4 @@
|
|
|
2
2
|
// Cloudflare Worker as well as under Node.js. test/version.test.ts fails the build when these differ
|
|
3
3
|
// from package.json, which stays the version the release workflow publishes.
|
|
4
4
|
export const TOOL_NAME = 'markdown-parity-check';
|
|
5
|
-
export const TOOL_VERSION = '0.2.
|
|
5
|
+
export const TOOL_VERSION = '0.2.12';
|
package/package.json
CHANGED