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.
@@ -3,7 +3,12 @@
3
3
  // classifies minor text differences (TEXT_MINOR_CHANGED).
4
4
  const NBSP = /[\u00a0\u2007\u202f]/g;
5
5
  const WS = /[ \t\r\n\f\v]+/g;
6
- const ZERO_WIDTH = /[\u200b-\u200d\ufeff\u00ad]/g;
6
+ // Zero width space, byte order mark and soft hyphen: invisible artefacts of editing and export. Up to 0.2.12 the
7
+ // zero width joiner and non-joiner went with them, and two texts that render differently, such as an emoji
8
+ // sequence with and without its joiner, compared equal (found by an outside review 2026-09-26). They stay in the
9
+ // strict text now, and JOINERS takes them out of the loose text, so a difference in them alone is a minor change.
10
+ const ZERO_WIDTH = /[\u200b\ufeff\u00ad]/g;
11
+ const JOINERS = /[\u200c\u200d]/g;
7
12
  export function strictNormalize(text) {
8
13
  return text
9
14
  .normalize('NFC')
@@ -14,6 +19,7 @@ export function strictNormalize(text) {
14
19
  }
15
20
  export function looseNormalize(text) {
16
21
  return strictNormalize(text)
22
+ .replace(JOINERS, '')
17
23
  .toLowerCase()
18
24
  // Map typographic quotes and dashes to plain forms, for alignment and for classifying minor text differences.
19
25
  .replace(/[\u2018\u2019\u201a\u2032]/g, "'")
@@ -148,27 +154,154 @@ function urlStart(t, s, e) {
148
154
  if (k > p + 2)
149
155
  p = k - 1;
150
156
  }
157
+ // (?:https?|ftp|wss?|file):[\/\\]*[^\s<>"'()\/\\?#@]*@ A special scheme whose authority carries user information,
158
+ // behind any number of slashes or backslashes, none included (0.2.12). The URL parser reads an authority after a
159
+ // special scheme whatever the separators, so https:user:secret@host and https:\user:secret@host carry user
160
+ // information just as https://user:secret@host does, and both passed a report unmasked, the first also as a GFM
161
+ // email link (found by an independent review before 0.2.12 was released). A failed candidate found no '@' before
162
+ // its delimiter, so a later candidate can succeed only when its colon sits right before that delimiter and the
163
+ // delimiter is its separator: the search goes on from at most the length of "https:" before the delimiter, and
164
+ // each position is scanned a bounded number of times.
165
+ for (let p = s; p < e && (best < 0 || p < best); p++) {
166
+ const n = specialSchemeLength(t, p, e);
167
+ if (n < 0)
168
+ continue;
169
+ let k = p + n + 1;
170
+ while (k < e && isSep(t.charCodeAt(k)))
171
+ k++;
172
+ while (k < e) {
173
+ const c = t.charCodeAt(k);
174
+ if (c === 0x2f || c === 0x5c || c === 0x3f || c === 0x23 || c === 0x40)
175
+ break;
176
+ k++;
177
+ }
178
+ if (k < e && t.charCodeAt(k) === 0x40) {
179
+ best = p;
180
+ break;
181
+ }
182
+ p = Math.max(p, k - 7);
183
+ }
151
184
  return best;
152
185
  }
186
+ const SPECIAL_SCHEME_NAMES = ['https', 'http', 'ftp', 'wss', 'ws', 'file'];
187
+ /** Length of the special scheme name at t[p] when a colon follows it, else -1. Letters compare with the old
188
+ * expression's i and u flags, under which U+017F matches s. */
189
+ function specialSchemeLength(t, p, e) {
190
+ const fold = (c) => (c === 0x17f ? 0x73 : c >= 0x41 && c <= 0x5a ? c + 0x20 : c);
191
+ for (const name of SPECIAL_SCHEME_NAMES) {
192
+ if (p + name.length >= e || t.charCodeAt(p + name.length) !== 0x3a)
193
+ continue;
194
+ let ok = true;
195
+ for (let i = 0; i < name.length && ok; i++)
196
+ ok = fold(t.charCodeAt(p + i)) === name.charCodeAt(i);
197
+ if (ok)
198
+ return name.length;
199
+ }
200
+ return -1;
201
+ }
202
+ // 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
203
+ // 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
204
+ // value such as ?token=(4711) left the parenthesis and the digits outside the mask, where the report showed them
205
+ // as text and as a changed number (found by an outside review 2026-09-26). Text glued to a query value without a
206
+ // space, such as the 's of ?x=1's, is part of the value to the URL parser too, and is masked with it.
207
+ const TOKEN_TAIL = /[^\s<>]*/uy;
208
+ /** End of the URL that starts at p inside the token t[p, t): trailing sentence punctuation, quotes and closing
209
+ * parentheses that have no opening one inside the URL are left outside it. One pass, so the scan stays linear. */
210
+ function urlEnd(text, p, t) {
211
+ let open = 0;
212
+ let close = 0;
213
+ for (let k = p; k < t; k++) {
214
+ const c = text.charCodeAt(k);
215
+ if (c === 0x28)
216
+ open++;
217
+ else if (c === 0x29)
218
+ close++;
219
+ }
220
+ let end = t;
221
+ while (end > p) {
222
+ const c = text[end - 1];
223
+ if (TRAIL.includes(c) || c === '"' || c === "'")
224
+ end--;
225
+ else if (c === ')' && close > open) {
226
+ close--;
227
+ end--;
228
+ }
229
+ else
230
+ break;
231
+ }
232
+ return end;
233
+ }
234
+ /** End of the token whose run ends at e: the run end moved forward over TOKEN_TAIL. */
235
+ function tokenEnd(text, e) {
236
+ TOKEN_TAIL.lastIndex = e;
237
+ const m = TOKEN_TAIL.exec(text);
238
+ return e + (m ? m[0].length : 0);
239
+ }
153
240
  /** Rewrites the URL-like part of every run in a piece of text and leaves the rest untouched. */
154
241
  function rewriteUrlRuns(text, rewrite) {
155
242
  let out = '';
156
243
  let done = 0;
157
- for (const run of text.matchAll(RUN)) {
244
+ const runs = new RegExp(RUN.source, RUN.flags);
245
+ for (let run = runs.exec(text); run !== null; run = runs.exec(text)) {
158
246
  const s = run.index;
159
247
  const e = s + run[0].length;
160
248
  const p = urlStart(text, s, e);
161
249
  if (p < 0)
162
250
  continue;
163
- // Keep trailing sentence punctuation outside the URL.
164
- let end = e;
165
- while (end > p && TRAIL.includes(text[end - 1]))
166
- end--;
167
- out += text.slice(done, p) + rewrite(text.slice(p, end)) + text.slice(end, e);
168
- done = e;
251
+ const t = tokenEnd(text, e);
252
+ const end = urlEnd(text, p, t);
253
+ out += text.slice(done, p) + rewrite(text.slice(p, end)) + text.slice(end, t);
254
+ done = t;
255
+ runs.lastIndex = t;
169
256
  }
170
257
  return out + text.slice(done);
171
258
  }
259
+ // How far insideHiddenUrlPart reads the token around a position. A longer token counts as hidden.
260
+ const HIDDEN_SCAN = 4096;
261
+ /**
262
+ * Whether position at of a source text lies in a part of a URL that maskHref hides: user information, a query
263
+ * value or the fragment. GFM links a bare address it finds inside another URL on its own: the part after the
264
+ * colon of https://user_name:secret@host, which it reads as an email address, or a URL given as a query value.
265
+ * That link's text and target then reached the report unmasked, because on their own they no longer look like
266
+ * part of a URL (found by an outside review 2026-09-26). The URL is found the way rewriteUrlRuns finds it, in the
267
+ * token around the position; a token longer than HIDDEN_SCAN on either side counts as hidden, so the answer errs
268
+ * on the side of masking.
269
+ */
270
+ export function insideHiddenUrlPart(text, at) {
271
+ const isEdge = (i) => {
272
+ const ch = text[i];
273
+ return ch === '<' || ch === '>' || /\s/u.test(ch);
274
+ };
275
+ let start = at;
276
+ while (start > 0 && !isEdge(start - 1)) {
277
+ if (at - start >= HIDDEN_SCAN)
278
+ return true;
279
+ start--;
280
+ }
281
+ let stop = at;
282
+ while (stop < text.length && !isEdge(stop)) {
283
+ if (stop - at >= HIDDEN_SCAN)
284
+ return true;
285
+ stop++;
286
+ }
287
+ const token = text.slice(start, stop);
288
+ const offset = at - start;
289
+ const runs = new RegExp(RUN.source, RUN.flags);
290
+ for (let run = runs.exec(token); run !== null && run.index < offset; run = runs.exec(token)) {
291
+ const p = urlStart(token, run.index, run.index + run[0].length);
292
+ if (p < 0)
293
+ continue;
294
+ if (p >= offset)
295
+ return false;
296
+ const before = token.slice(p, offset);
297
+ if (/[?#]/.test(before))
298
+ return true;
299
+ // Still inside the authority: what came before the position is user information. A special scheme has an
300
+ // authority behind any number of separators, none included.
301
+ return /^(?:(?:https?|ftp|wss?|file):[\/\\]*|(?:[a-z][a-z0-9+.-]*:)?[\/\\]{2})[^\/\\?#]*$/i.test(before);
302
+ }
303
+ return false;
304
+ }
172
305
  /** Masks user information, query values and fragments of every URL-like run in a piece of report text. */
173
306
  export function redactText(text) {
174
307
  return rewriteUrlRuns(text, maskHref);
@@ -1,2 +1,2 @@
1
1
  export declare const TOOL_NAME = "markdown-parity-check";
2
- export declare const TOOL_VERSION = "0.2.11";
2
+ export declare const TOOL_VERSION = "0.2.13";
@@ -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.11';
5
+ export const TOOL_VERSION = '0.2.13';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "markdown-parity-check",
3
- "version": "0.2.11",
3
+ "version": "0.2.13",
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",