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/README.md +2 -2
- package/dist/src/cli.d.ts +1 -1
- package/dist/src/cli.js +35 -3
- package/dist/src/compare.d.ts +32 -1
- package/dist/src/compare.js +207 -34
- package/dist/src/fetch.js +8 -3
- package/dist/src/html.js +173 -41
- package/dist/src/markdown.js +80 -10
- package/dist/src/model.d.ts +15 -0
- package/dist/src/netguard.js +27 -0
- package/dist/src/normalize.d.ts +10 -0
- package/dist/src/normalize.js +141 -8
- package/dist/src/version.d.ts +1 -1
- package/dist/src/version.js +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -62,9 +62,9 @@ Exit code 0 means the implemented checks found nothing to reject. It does not pr
|
|
|
62
62
|
|
|
63
63
|
## What is compared
|
|
64
64
|
|
|
65
|
-
Without `--selector`, the HTML content comes from `main`, `article` or `[role=main]`. If none of them exists, the page body is used and the report warns that page chrome may leak in. Front matter is kept by default and compared as content. If the HTML does not carry the same text, that alone fails the comparison. The tool also warns when the Markdown starts with a block that looks like YAML front matter, and `--front-matter strip` removes it.
|
|
65
|
+
Without `--selector`, the HTML content comes from the first visible `main`, `article` or `[role=main]`. If none of them exists, the page body is used and the report warns that page chrome may leak in. Two visible elements of the same kind that do not contain one another also give a warning, because only the first is compared. Front matter is kept by default and compared as content. If the HTML does not carry the same text, that alone fails the comparison. The tool also warns when the Markdown starts with a block that looks like YAML front matter, and `--front-matter strip` removes it.
|
|
66
66
|
|
|
67
|
-
Headings, paragraphs, list items, tables, code blocks and links are compared block by block. Missing and added blocks are errors.
|
|
67
|
+
Headings, paragraphs, list items, tables, code blocks and links are compared block by block. Content marked `hidden` or `aria-hidden` is left out on both sides, and a table caption counts as a paragraph before the table. Missing and added blocks are errors. A change in text or numbers is an error too, as is a changed table or link, and a list item that shows another number or task state. A difference in order, heading level, case, punctuation, list kind or nesting level is a warning. Footnotes in the Markdown are not compared. Each one is reported as a warning, and repeated blocks are reported too.
|
|
68
68
|
|
|
69
69
|
In URL mode the Markdown response is checked first. An HTTP error or an HTML page in place of Markdown fails the check, and nothing is compared.
|
|
70
70
|
|
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.13\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,37 @@ 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, in three steps.
|
|
29
|
+
*
|
|
30
|
+
* 1. Links whose text occurs equally often on both sides pair in document order: the first "download" with the
|
|
31
|
+
* first "download". Up to 0.2.12 every link took any unused link with the same text and target, so two links with
|
|
32
|
+
* the same text and swapped targets passed with no finding (0.2.13, found by an outside review 2026-09-26).
|
|
33
|
+
* 2. Every other link takes the first unused Markdown link, in document order, that has the same text and the same
|
|
34
|
+
* target, or else the first unused one with the same text. The Markdown links are indexed by text and, for a
|
|
35
|
+
* resolved HTML link, by text and target; an unresolved HTML link scans the links that share its text, because
|
|
36
|
+
* relativeHrefRelation compares two references and has no key of its own (0.2.12, where the old scans of the
|
|
37
|
+
* whole list made one block with 20 000 links take more than a second).
|
|
38
|
+
* 3. What is still unpaired pairs by loosely normalized text, the same way as step 1 and then in order, so a link
|
|
39
|
+
* whose text differs only in case or punctuation is one changed text and not a missing and an added link (0.2.13).
|
|
40
|
+
* @internal Exported for test/compare-links.test.ts.
|
|
41
|
+
*/
|
|
42
|
+
export declare function pairLinks(hLinks: Link[], mLinks: Link[]): {
|
|
43
|
+
pairs: Array<[number, number]>;
|
|
44
|
+
missing: number[];
|
|
45
|
+
added: number[];
|
|
46
|
+
};
|
|
16
47
|
export declare function compare(html: Extraction, markdown: Extraction, options?: {
|
|
17
48
|
bothBases?: boolean;
|
|
18
49
|
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
|
-
import { excerpt, hrefDifference, maskHref, redactText, relativeHrefRelation, visibleNumbers } from './normalize.js';
|
|
3
|
+
import { excerpt, hrefDifference, looseNormalize, 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,203 @@ 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, in three steps.
|
|
63
|
+
*
|
|
64
|
+
* 1. Links whose text occurs equally often on both sides pair in document order: the first "download" with the
|
|
65
|
+
* first "download". Up to 0.2.12 every link took any unused link with the same text and target, so two links with
|
|
66
|
+
* the same text and swapped targets passed with no finding (0.2.13, found by an outside review 2026-09-26).
|
|
67
|
+
* 2. Every other link takes the first unused Markdown link, in document order, that has the same text and the same
|
|
68
|
+
* target, or else the first unused one with the same text. The Markdown links are indexed by text and, for a
|
|
69
|
+
* resolved HTML link, by text and target; an unresolved HTML link scans the links that share its text, because
|
|
70
|
+
* relativeHrefRelation compares two references and has no key of its own (0.2.12, where the old scans of the
|
|
71
|
+
* whole list made one block with 20 000 links take more than a second).
|
|
72
|
+
* 3. What is still unpaired pairs by loosely normalized text, the same way as step 1 and then in order, so a link
|
|
73
|
+
* whose text differs only in case or punctuation is one changed text and not a missing and an added link (0.2.13).
|
|
74
|
+
* @internal Exported for test/compare-links.test.ts.
|
|
75
|
+
*/
|
|
76
|
+
export function pairLinks(hLinks, mLinks) {
|
|
77
|
+
const used = new Array(mLinks.length).fill(false);
|
|
78
|
+
const lists = new Map();
|
|
79
|
+
const heads = new Map();
|
|
80
|
+
const key = (...parts) => JSON.stringify(parts);
|
|
81
|
+
const add = (k, i) => {
|
|
82
|
+
const list = lists.get(k);
|
|
83
|
+
if (list)
|
|
84
|
+
list.push(i);
|
|
85
|
+
else
|
|
86
|
+
lists.set(k, [i]);
|
|
87
|
+
};
|
|
88
|
+
const first = (k) => {
|
|
89
|
+
const list = lists.get(k);
|
|
90
|
+
if (!list)
|
|
91
|
+
return -1;
|
|
92
|
+
let n = heads.get(k) ?? 0;
|
|
93
|
+
while (n < list.length && used[list[n]])
|
|
94
|
+
n++;
|
|
95
|
+
heads.set(k, n);
|
|
96
|
+
return n < list.length ? list[n] : -1;
|
|
97
|
+
};
|
|
98
|
+
mLinks.forEach((l, i) => {
|
|
99
|
+
add(key('text', l.text), i);
|
|
100
|
+
if (l.resolved !== null)
|
|
101
|
+
add(key('resolved', l.text, l.resolved), i);
|
|
102
|
+
else
|
|
103
|
+
add(key('raw-unresolved', l.text, l.rawHref.trim()), i);
|
|
104
|
+
});
|
|
105
|
+
const count = (links) => {
|
|
106
|
+
const m = new Map();
|
|
107
|
+
for (const l of links)
|
|
108
|
+
m.set(l.text, (m.get(l.text) ?? 0) + 1);
|
|
109
|
+
return m;
|
|
110
|
+
};
|
|
111
|
+
const hCount = count(hLinks);
|
|
112
|
+
const mCount = count(mLinks);
|
|
113
|
+
const seenInH = new Map();
|
|
114
|
+
const pairs = [];
|
|
115
|
+
let missing = [];
|
|
116
|
+
hLinks.forEach((hl, hi) => {
|
|
117
|
+
let idx = -1;
|
|
118
|
+
const k = seenInH.get(hl.text) ?? 0;
|
|
119
|
+
seenInH.set(hl.text, k + 1);
|
|
120
|
+
if (hCount.get(hl.text) === mCount.get(hl.text)) {
|
|
121
|
+
idx = lists.get(key('text', hl.text))[k];
|
|
122
|
+
}
|
|
123
|
+
else if (hl.resolved !== null) {
|
|
124
|
+
// sameTarget: a resolved Markdown link with the same resolved href, or an unresolved one with the same raw href.
|
|
125
|
+
const a = first(key('resolved', hl.text, hl.resolved));
|
|
126
|
+
const b = first(key('raw-unresolved', hl.text, hl.rawHref.trim()));
|
|
127
|
+
idx = a < 0 ? b : b < 0 ? a : Math.min(a, b);
|
|
128
|
+
}
|
|
129
|
+
else {
|
|
130
|
+
const list = lists.get(key('text', hl.text)) ?? [];
|
|
131
|
+
for (const i of list) {
|
|
132
|
+
if (!used[i] && sameTarget(hl, mLinks[i])) {
|
|
133
|
+
idx = i;
|
|
134
|
+
break;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
49
138
|
if (idx < 0)
|
|
50
|
-
idx =
|
|
139
|
+
idx = first(key('text', hl.text));
|
|
51
140
|
if (idx < 0) {
|
|
52
|
-
|
|
141
|
+
missing.push(hi);
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
used[idx] = true;
|
|
145
|
+
pairs.push([hi, idx]);
|
|
146
|
+
});
|
|
147
|
+
// Step 3: the links still unpaired, by loosely normalized text.
|
|
148
|
+
const looseLists = new Map();
|
|
149
|
+
used.forEach((u, i) => {
|
|
150
|
+
const k = u ? '' : looseNormalize(mLinks[i].text);
|
|
151
|
+
if (k === '')
|
|
152
|
+
return;
|
|
153
|
+
const list = looseLists.get(k);
|
|
154
|
+
if (list)
|
|
155
|
+
list.push(i);
|
|
156
|
+
else
|
|
157
|
+
looseLists.set(k, [i]);
|
|
158
|
+
});
|
|
159
|
+
const looseMissing = new Map();
|
|
160
|
+
for (const hi of missing) {
|
|
161
|
+
const k = looseNormalize(hLinks[hi].text);
|
|
162
|
+
if (k !== '')
|
|
163
|
+
looseMissing.set(k, (looseMissing.get(k) ?? 0) + 1);
|
|
164
|
+
}
|
|
165
|
+
const looseHeads = new Map();
|
|
166
|
+
const stillMissing = [];
|
|
167
|
+
for (const hi of missing) {
|
|
168
|
+
const k = looseNormalize(hLinks[hi].text);
|
|
169
|
+
const list = k === '' ? undefined : looseLists.get(k);
|
|
170
|
+
const n = looseHeads.get(k) ?? 0;
|
|
171
|
+
if (!list || n >= list.length) {
|
|
172
|
+
stillMissing.push(hi);
|
|
53
173
|
continue;
|
|
54
174
|
}
|
|
55
|
-
|
|
175
|
+
// Equal counts pair in order and unequal counts take the next in order, so both are the next unused link.
|
|
176
|
+
looseHeads.set(k, n + 1);
|
|
177
|
+
used[list[n]] = true;
|
|
178
|
+
pairs.push([hi, list[n]]);
|
|
179
|
+
}
|
|
180
|
+
missing = stillMissing;
|
|
181
|
+
pairs.sort((x, y) => x[0] - y[0]);
|
|
182
|
+
const added = [];
|
|
183
|
+
used.forEach((u, i) => {
|
|
184
|
+
if (!u)
|
|
185
|
+
added.push(i);
|
|
186
|
+
});
|
|
187
|
+
return { pairs, missing, added };
|
|
188
|
+
}
|
|
189
|
+
function compareLinks(out, h, m, bothBases) {
|
|
190
|
+
// Link targets are compared unmasked; every value that reaches a finding goes through maskHref.
|
|
191
|
+
const { pairs, missing, added } = pairLinks(h.links, m.links);
|
|
192
|
+
const events = [...pairs.map(([hi, mi]) => ({ hi, mi })), ...missing.map((hi) => ({ hi, mi: -1 }))].sort((x, y) => x.hi - y.hi);
|
|
193
|
+
for (const { hi, mi } of events) {
|
|
194
|
+
const hl = h.links[hi];
|
|
195
|
+
if (mi < 0) {
|
|
196
|
+
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) });
|
|
197
|
+
continue;
|
|
198
|
+
}
|
|
199
|
+
const ml = m.links[mi];
|
|
56
200
|
if (sameTarget(hl, ml))
|
|
57
201
|
continue;
|
|
202
|
+
const text = linkText(hl, ml);
|
|
58
203
|
if (hl.resolved !== null && ml.resolved !== null) {
|
|
59
204
|
const diff = hrefDifference(hl.resolved, ml.resolved);
|
|
60
|
-
out.push({ code: 'LINK_TARGET_CHANGED', severity: 'error', direction: 'both', message: `Link "${
|
|
205
|
+
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
206
|
}
|
|
62
207
|
else if (hl.resolved === null && ml.resolved === null && relativeHrefRelation(hl.rawHref, ml.rawHref) === 'uncertain') {
|
|
63
208
|
// Neither side resolved and no base could be ruled out as making them meet. A textual difference
|
|
64
209
|
// 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 "${
|
|
210
|
+
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
211
|
}
|
|
67
212
|
else if (hl.resolved === null && ml.resolved === null) {
|
|
68
213
|
// Neither side resolved, and relativeHrefRelation found a difference no base can remove.
|
|
69
214
|
const diff = hrefDifference(hl.rawHref, ml.rawHref);
|
|
70
|
-
out.push({ code: 'LINK_TARGET_CHANGED', severity: 'error', direction: 'both', message: `Link "${
|
|
215
|
+
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
216
|
}
|
|
72
217
|
else {
|
|
73
|
-
out.push({ code: 'LINK_UNVERIFIED', severity: 'warning', direction: 'both', message: bothBases ? `Link "${
|
|
218
|
+
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
219
|
}
|
|
75
220
|
}
|
|
76
|
-
for (const
|
|
77
|
-
|
|
221
|
+
for (const mi of added) {
|
|
222
|
+
const ml = m.links[mi];
|
|
223
|
+
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
224
|
}
|
|
79
225
|
}
|
|
80
226
|
function sameTarget(a, b) {
|
|
@@ -84,6 +230,31 @@ function sameTarget(a, b) {
|
|
|
84
230
|
return relativeHrefRelation(a.rawHref, b.rawHref) === 'same';
|
|
85
231
|
return a.rawHref.trim() === b.rawHref.trim();
|
|
86
232
|
}
|
|
233
|
+
/**
|
|
234
|
+
* Where a list item sits in its list (0.2.13): the list kind and the nesting level are warnings, a changed item
|
|
235
|
+
* number and a changed task state are errors, because the reader sees a different number or a different state.
|
|
236
|
+
*/
|
|
237
|
+
function compareListItems(out, h, m) {
|
|
238
|
+
const a = h.list;
|
|
239
|
+
const b = m.list;
|
|
240
|
+
if (!a || !b)
|
|
241
|
+
return;
|
|
242
|
+
const kind = (o) => (o ? 'numbered' : 'bulleted');
|
|
243
|
+
if (a.ordered !== b.ordered) {
|
|
244
|
+
out.push({ code: 'LIST_KIND_CHANGED', severity: 'warning', direction: 'both', message: `The list item is in a ${kind(a.ordered)} list in the HTML and a ${kind(b.ordered)} list in the Markdown.`, html: side(h), markdown: side(m), before: kind(a.ordered), after: kind(b.ordered) });
|
|
245
|
+
}
|
|
246
|
+
else if (a.ordinal !== undefined && b.ordinal !== undefined && a.ordinal !== b.ordinal) {
|
|
247
|
+
out.push({ code: 'LIST_NUMBER_CHANGED', severity: 'error', direction: 'both', message: `List item number differs: ${a.ordinal} in the HTML, ${b.ordinal} in the Markdown.`, html: side(h), markdown: side(m), before: String(a.ordinal), after: String(b.ordinal) });
|
|
248
|
+
}
|
|
249
|
+
if (a.depth !== b.depth) {
|
|
250
|
+
out.push({ code: 'LIST_NESTING_CHANGED', severity: 'warning', direction: 'both', message: `List nesting level differs: level ${a.depth + 1} in the HTML, level ${b.depth + 1} in the Markdown.`, html: side(h), markdown: side(m), before: String(a.depth + 1), after: String(b.depth + 1) });
|
|
251
|
+
}
|
|
252
|
+
if (a.checked !== b.checked) {
|
|
253
|
+
const state = (c) => (c === null ? 'no checkbox' : c ? 'checked' : 'unchecked');
|
|
254
|
+
const one = a.checked === null || b.checked === null;
|
|
255
|
+
out.push({ code: 'LIST_TASK_CHANGED', severity: one ? 'warning' : 'error', direction: 'both', message: `Task state differs: ${state(a.checked)} in the HTML, ${state(b.checked)} in the Markdown.`, html: side(h), markdown: side(m), before: state(a.checked), after: state(b.checked) });
|
|
256
|
+
}
|
|
257
|
+
}
|
|
87
258
|
function compareTables(out, h, m) {
|
|
88
259
|
const hc = h.cells ?? [];
|
|
89
260
|
const mc = m.cells ?? [];
|
|
@@ -152,6 +323,8 @@ function comparePair(out, p, h, m, bothBases) {
|
|
|
152
323
|
out.push({ code: 'TEXT_CHANGED', severity: 'error', direction: 'both', message: `The ${label(h)} text differs.`, html: side(h), markdown: side(m), before: excerpt(h.text), after: excerpt(m.text) });
|
|
153
324
|
}
|
|
154
325
|
}
|
|
326
|
+
if (h.type === 'listItem' && m.type === 'listItem')
|
|
327
|
+
compareListItems(out, h, m);
|
|
155
328
|
compareLinks(out, h, m, bothBases);
|
|
156
329
|
}
|
|
157
330
|
const SEVERITY_ORDER = { error: 0, warning: 1, info: 2 };
|
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
|
}
|