markdown-parity-check 0.2.0 → 0.2.2

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 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.0\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 --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";
3
+ export declare const HELP = "markdown-parity-check 0.2.2\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 --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 {
@@ -82,6 +82,27 @@ export function isPublicIPv4(ip) {
82
82
  }
83
83
  return true;
84
84
  }
85
+ /**
86
+ * The dotted quad at the end of s, or null: what /(\d+\.\d+\.\d+\.\d+)$/ captured up to 0.2.1, found in one
87
+ * pass. That expression took time quadratic in a run of digits (CodeQL js/polynomial-redos), and
88
+ * isPublicIPv6 is exported, so a caller can hand it any string. Of the run of digits and dots at the end
89
+ * of s, the match is the last four dot-separated parts, when there are at least four and none is empty.
90
+ */
91
+ function trailingDottedQuad(s) {
92
+ let i = s.length;
93
+ while (i > 0) {
94
+ const c = s.charCodeAt(i - 1);
95
+ if ((c >= 0x30 && c <= 0x39) || c === 0x2e)
96
+ i--;
97
+ else
98
+ break;
99
+ }
100
+ const parts = s.slice(i).split('.');
101
+ if (parts.length < 4)
102
+ return null;
103
+ const quad = parts.slice(-4);
104
+ return quad.every((p) => p !== '') ? quad.join('.') : null;
105
+ }
85
106
  /** Expands an IPv6 address into 8 16-bit groups. Returns null when it cannot be parsed. */
86
107
  export function expandIPv6(ip) {
87
108
  let s = ip.toLowerCase();
@@ -89,10 +110,10 @@ export function expandIPv6(ip) {
89
110
  if (zone >= 0)
90
111
  s = s.slice(0, zone);
91
112
  // Embedded IPv4 tail, for example ::ffff:127.0.0.1.
92
- const v4 = /(\d+\.\d+\.\d+\.\d+)$/.exec(s);
113
+ const v4 = trailingDottedQuad(s);
93
114
  if (v4) {
94
- const n = ipv4ToInt(v4[1]);
95
- s = s.slice(0, -v4[1].length) + ((n >>> 16) & 0xffff).toString(16) + ':' + (n & 0xffff).toString(16);
115
+ const n = ipv4ToInt(v4);
116
+ s = s.slice(0, -v4.length) + ((n >>> 16) & 0xffff).toString(16) + ':' + (n & 0xffff).toString(16);
96
117
  }
97
118
  const halves = s.split('::');
98
119
  if (halves.length > 2)
@@ -47,15 +47,103 @@ export function excerpt(text, max = 80) {
47
47
  }
48
48
  // URL-like runs inside free text: absolute URLs, www. hosts, and root-relative paths that carry a query
49
49
  // or fragment. Only the query values and the fragment are rewritten; other text is left untouched.
50
- const URL_IN_TEXT = /(?:[a-z][a-z0-9+.-]*:\/\/[^\s<>"'()]+|www\.[^\s<>"'()]+|(?<![\w/])\/[^\s<>"'()]*[?#][^\s<>"'()]*|[^\s<>"'()]*\?[\w%.-]+=[^\s<>"'()]*)/giu;
50
+ //
51
+ // Up to 0.2.1 this was one regular expression, kept in test/linear.test.ts as the reference. Its time was
52
+ // quadratic in the length of a run without whitespace (CodeQL js/polynomial-redos; 80 000 characters took
53
+ // 4.2 s), and the text comes from the compared page. Every alternative of that expression ends where the
54
+ // run of characters other than whitespace and < > " ' ( ) ends, so a run holds at most one match, and the
55
+ // match starts at the leftmost position where any alternative can start. urlStart finds that position
56
+ // with a few linear scans of the run, and the test compares the two on random input.
57
+ const RUN = /[^\s<>"'()]+/gu;
58
+ const TRAIL = '.,;:!?';
59
+ // Character tests with the old expression's i and u flags, under which \w and [a-z] also match U+017F and
60
+ // U+212A, because they case-fold to s and k.
61
+ const isLetter = (c) => (c >= 0x41 && c <= 0x5a) || (c >= 0x61 && c <= 0x7a) || c === 0x17f || c === 0x212a;
62
+ const isDigit = (c) => c >= 0x30 && c <= 0x39;
63
+ const isWord = (c) => isLetter(c) || isDigit(c) || c === 0x5f;
64
+ const isSchemeChar = (c) => isLetter(c) || isDigit(c) || c === 0x2b || c === 0x2e || c === 0x2d; // [a-z0-9+.-]
65
+ const isParamChar = (c) => isWord(c) || c === 0x25 || c === 0x2e || c === 0x2d; // [\w%.-]
66
+ const isW = (c) => c === 0x57 || c === 0x77;
67
+ /** Start of the URL-like match in the run t[s, e), or -1 when the run holds none. */
68
+ function urlStart(t, s, e) {
69
+ // [^\s<>"'()]*\?[\w%.-]+= A query parameter anywhere in the run makes the whole run the match.
70
+ for (let q = s; q < e; q++) {
71
+ if (t.charCodeAt(q) !== 0x3f)
72
+ continue;
73
+ let k = q + 1;
74
+ while (k < e && isParamChar(t.charCodeAt(k)))
75
+ k++;
76
+ if (k > q + 1 && k < e && t.charCodeAt(k) === 0x3d)
77
+ return s;
78
+ q = k - 1;
79
+ }
80
+ let best = -1;
81
+ // [a-z][a-z0-9+.-]*:\/\/[^\s<>"'()]+ The first letter of a scheme run that is followed by :// and at
82
+ // least one more character.
83
+ for (let j = s; j < e;) {
84
+ if (!isSchemeChar(t.charCodeAt(j))) {
85
+ j++;
86
+ continue;
87
+ }
88
+ let letter = -1;
89
+ let b = j;
90
+ while (b < e && isSchemeChar(t.charCodeAt(b))) {
91
+ if (letter < 0 && isLetter(t.charCodeAt(b)))
92
+ letter = b;
93
+ b++;
94
+ }
95
+ if (letter >= 0 && b + 3 < e && t.startsWith('://', b)) {
96
+ best = letter;
97
+ break;
98
+ }
99
+ j = b;
100
+ }
101
+ // www\.[^\s<>"'()]+
102
+ for (let p = s; p + 4 < e && (best < 0 || p < best); p++) {
103
+ if (isW(t.charCodeAt(p)) && isW(t.charCodeAt(p + 1)) && isW(t.charCodeAt(p + 2)) && t.charCodeAt(p + 3) === 0x2e) {
104
+ best = p;
105
+ break;
106
+ }
107
+ }
108
+ // (?<![\w/])\/[^\s<>"'()]*[?#][^\s<>"'()]* The first '/' that does not follow a word character or '/',
109
+ // when a '?' or '#' comes after it in the run.
110
+ let lastQueryOrHash = -1;
111
+ for (let r = e - 1; r > s; r--) {
112
+ const c = t.charCodeAt(r);
113
+ if (c === 0x3f || c === 0x23) {
114
+ lastQueryOrHash = r;
115
+ break;
116
+ }
117
+ }
118
+ for (let p = s; p < lastQueryOrHash && (best < 0 || p < best); p++) {
119
+ if (t.charCodeAt(p) !== 0x2f)
120
+ continue;
121
+ const before = p > 0 ? t.charCodeAt(p - 1) : -1;
122
+ if (before === 0x2f || isWord(before))
123
+ continue;
124
+ best = p;
125
+ break;
126
+ }
127
+ return best;
128
+ }
51
129
  /** Masks query values and fragments of every URL-like run in a piece of report text. */
52
130
  export function redactText(text) {
53
- return text.replace(URL_IN_TEXT, (m) => {
131
+ let out = '';
132
+ let done = 0;
133
+ for (const run of text.matchAll(RUN)) {
134
+ const s = run.index;
135
+ const e = s + run[0].length;
136
+ const p = urlStart(text, s, e);
137
+ if (p < 0)
138
+ continue;
54
139
  // Keep trailing sentence punctuation outside the URL.
55
- const trail = /[.,;:!?]+$/.exec(m)?.[0] ?? '';
56
- const core = m.slice(0, m.length - trail.length);
57
- return maskHref(core) + trail;
58
- });
140
+ let end = e;
141
+ while (end > p && TRAIL.includes(text[end - 1]))
142
+ end--;
143
+ out += text.slice(done, p) + maskHref(text.slice(p, end)) + text.slice(end, e);
144
+ done = e;
145
+ }
146
+ return out + text.slice(done);
59
147
  }
60
148
  /** Resolves a possibly relative href against a base. Returns null when it cannot be resolved. */
61
149
  export function resolveHref(rawHref, base) {
@@ -1,2 +1,2 @@
1
1
  export declare const TOOL_NAME = "markdown-parity-check";
2
- export declare const TOOL_VERSION = "0.2.0";
2
+ export declare const TOOL_VERSION = "0.2.2";
@@ -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.0';
5
+ export const TOOL_VERSION = '0.2.2';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "markdown-parity-check",
3
- "version": "0.2.0",
3
+ "version": "0.2.2",
4
4
  "description": "Compares the main content of a web page's HTML and Markdown representations and reports concrete differences.",
5
5
  "keywords": [
6
6
  "markdown",