@ai-matrx/content-ir 0.17.0 → 0.18.1

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/source.js CHANGED
@@ -252,75 +252,233 @@ function classifyInnerFenceLine(trimmed, openTicks, nests, nestedDepth) {
252
252
  return "content";
253
253
  }
254
254
 
255
- // source/math.ts
256
- var TEX_SIGNAL = /\\[A-Za-z]+|[\\^_{}=<>+]/;
257
- var LONE_VARIABLE = /^[A-Za-z](?:'|[0-9])?$/;
258
- var MATH_SHAPE_CHARS = /^[A-Za-z0-9'\s(),.*\/-]+$/;
259
- var FUNCTION_NAMES = /* @__PURE__ */ new Set([
260
- "sin",
261
- "cos",
262
- "tan",
263
- "sec",
264
- "csc",
265
- "cot",
266
- "log",
267
- "ln",
268
- "exp",
269
- "lim",
270
- "max",
271
- "min",
272
- "det",
273
- "gcd",
274
- "mod",
275
- "arg",
276
- "sinh",
277
- "cosh",
278
- "tanh"
279
- ]);
280
- function isMathShape(content) {
281
- if (!MATH_SHAPE_CHARS.test(content)) return false;
282
- const words = content.match(/[A-Za-z]+/g);
283
- if (!words) return false;
284
- if (!words.every((w) => w.length === 1 || FUNCTION_NAMES.has(w))) return false;
285
- if (/[A-Za-z]'+[A-Za-z]/.test(content) || /(?:^|[\s(,])[A-Za-z]\.(?:[A-Za-z]\.|\s|$)/.test(content)) return false;
286
- return /[A-Za-z]'*\(|[A-Za-z]'|\b[A-Za-z]\s+[A-Za-z]\b|\([^)]*[A-Za-z][^)]*\)/.test(content);
255
+ // source/code-ranges.ts
256
+ function fenceOpenerOf(content) {
257
+ const t = content.trimStart();
258
+ if (t.startsWith("```")) {
259
+ let ticks = 0;
260
+ while (ticks < t.length && t[ticks] === "`") ticks++;
261
+ return { char: "`", ticks, lang: parseFenceOpener(t)?.lang ?? "" };
262
+ }
263
+ if (content.length - t.length <= 3 && t.startsWith("~~~")) {
264
+ const open = /^(~{3,})(.*)$/.exec(t);
265
+ if (!open) return null;
266
+ return { char: "~", ticks: open[1].length, lang: open[2].trim().split(/\s/)[0] ?? "" };
267
+ }
268
+ return null;
287
269
  }
288
- function isSingleDollarMath(content, after) {
289
- if (!content || content.length > 400) return false;
290
- if (/^\s/.test(content) || /\s$/.test(content)) return false;
291
- if (after !== void 0 && /[0-9]/.test(after)) return false;
292
- if (LONE_VARIABLE.test(content)) return true;
293
- if (/^[0-9]/.test(content)) return /\\[A-Za-z]+|[\\^_{}]/.test(content);
294
- return TEX_SIGNAL.test(content) || isMathShape(content);
270
+ var FenceReader = class {
271
+ constructor(opener, options = {}) {
272
+ this.opener = opener;
273
+ this.isJson = opener.char === "`" && opener.lang === "json";
274
+ this.nesting = opener.char === "`" && (options.nesting ?? fenceNestsInnerFences(opener.lang));
275
+ this.tildeCloser = opener.char === "~" ? new RegExp(`^ {0,3}~{${opener.ticks},}[ \\t]*$`) : null;
276
+ }
277
+ opener;
278
+ state = { inString: false, escaped: false };
279
+ depth = 0;
280
+ /** A nested fence was opened at some point. */
281
+ sawNested = false;
282
+ isJson;
283
+ nesting;
284
+ tildeCloser;
285
+ /** How many nested fences are open right now. */
286
+ get nestedDepth() {
287
+ return this.depth;
288
+ }
289
+ feed(line) {
290
+ if (this.tildeCloser) return this.tildeCloser.test(line);
291
+ const trimmedLine = trimFenceLine(line);
292
+ const at = line.indexOf("```");
293
+ if (at === -1) {
294
+ if (this.isJson) this.state = jsonStringState(line, 0, line.length, this.state);
295
+ return false;
296
+ }
297
+ if (this.isJson) {
298
+ const probe = jsonStringState(line, 0, at, this.state);
299
+ if (probe.inString) {
300
+ this.state = jsonStringState(line, at, line.length, probe);
301
+ return false;
302
+ }
303
+ }
304
+ if (trimmedLine.startsWith("```")) {
305
+ const kind = classifyInnerFenceLine(trimmedLine, this.opener.ticks, this.nesting, this.depth);
306
+ if (kind === "close-outer") return true;
307
+ if (kind === "open-nested") {
308
+ this.depth++;
309
+ this.sawNested = true;
310
+ }
311
+ if (kind === "close-nested") this.depth--;
312
+ } else {
313
+ const closeTicks = backtickRunLength(line, at);
314
+ const after = trimFenceLine(line.slice(at + closeTicks));
315
+ if (closeTicks >= this.opener.ticks && after === "" && this.depth === 0) return true;
316
+ }
317
+ if (this.isJson) this.state = jsonStringState(line, 0, line.length, this.state);
318
+ return false;
319
+ }
320
+ };
321
+ function closeFence(src, open, opener) {
322
+ const first = readFence(src, open, new FenceReader(opener));
323
+ if (first.closed || !first.reader.sawNested || first.reader.nestedDepth <= 0) return { line: first.line, closed: first.closed };
324
+ const strict = readFence(src, open, new FenceReader(opener, { nesting: false }));
325
+ return { line: strict.line, closed: strict.closed };
295
326
  }
296
- function singleDollarMathEnd(text, open, limit = text.length) {
297
- const bound = Math.min(limit, open + 403);
298
- let close = -1;
299
- for (let k = open + 1; k < bound; k += 1) {
300
- const c = text[k];
301
- if (c === "\n") break;
302
- if (c === "\\") {
303
- k += 1;
327
+ function readFence(src, open, reader) {
328
+ for (let k = open + 1; k < src.count; k++) {
329
+ if (src.openTail?.(k)) continue;
330
+ const line = src.line(k);
331
+ if (line === null) return { line: k - 1, closed: false, reader };
332
+ if (reader.feed(line)) return { line: k, closed: true, reader };
333
+ }
334
+ return { line: src.count - 1, closed: false, reader };
335
+ }
336
+ function jsonStringState(line, from, upTo, state) {
337
+ let { inString, escaped } = state;
338
+ for (let idx = from; idx < upTo; idx++) {
339
+ const ch = line[idx];
340
+ if (escaped) {
341
+ escaped = false;
304
342
  continue;
305
343
  }
306
- if (c === "$") {
307
- close = k;
308
- break;
344
+ if (ch === "\\" && inString) {
345
+ escaped = true;
346
+ continue;
309
347
  }
348
+ if (ch === '"') inString = !inString;
310
349
  }
311
- if (close === -1 || text[close + 1] === "$") return -1;
312
- const content = text.slice(open + 1, close);
313
- return isSingleDollarMath(content, text[close + 1]) ? close + 1 : -1;
350
+ return { inString, escaped };
314
351
  }
315
-
316
- // source/code-ranges.ts
317
- function findCodeRanges(text) {
318
- const ranges = [];
319
- const n = text.length;
320
- let fence = null;
352
+ function backtickRunLength(str, pos) {
353
+ let n = 0;
354
+ while (pos + n < str.length && str[pos + n] === "`") n++;
355
+ return n;
356
+ }
357
+ function quotePrefix(line) {
358
+ let depth = 0;
359
+ let offset = 0;
360
+ for (; ; ) {
361
+ const m = /^ {0,3}> ?/.exec(line.slice(offset));
362
+ if (!m) return { depth, offset };
363
+ depth++;
364
+ offset += m[0].length;
365
+ }
366
+ }
367
+ function stripQuotes(line, depth) {
368
+ let offset = 0;
369
+ for (let d = 0; d < depth; d++) {
370
+ const m = /^ {0,3}> ?/.exec(line.slice(offset));
371
+ if (!m) return null;
372
+ offset += m[0].length;
373
+ }
374
+ return line.slice(offset);
375
+ }
376
+ var LIST_ITEM = /^( *)([-*+]|\d{1,9}[.)])( +|\t|$)/;
377
+ function leadingSpaces(s) {
378
+ let i = 0;
379
+ while (i < s.length && s[i] === " ") i++;
380
+ return i;
381
+ }
382
+ function scan(text) {
383
+ const starts = [];
384
+ const ends = [];
385
+ for (let pos = 0; ; ) {
386
+ const nl = text.indexOf("\n", pos);
387
+ const lineEnd = nl === -1 ? text.length : nl;
388
+ starts.push(pos);
389
+ ends.push(lineEnd > pos && text.charCodeAt(lineEnd - 1) === 13 ? lineEnd - 1 : lineEnd);
390
+ if (nl === -1) break;
391
+ pos = nl + 1;
392
+ }
393
+ const count = starts.length;
394
+ const raw = (k) => text.slice(starts[k], ends[k]);
395
+ const out = { ranges: [], fences: [], paragraphs: [], endsInFence: false, pendingPara: -1 };
396
+ const rawLines = { count, line: raw };
397
+ let segment = 0;
398
+ for (let k = 0; k < count; k++) {
399
+ const opener = fenceOpenerOf(raw(k));
400
+ if (!opener || opener.char !== "`") continue;
401
+ prose(text, starts, ends, raw, segment, k, out);
402
+ const end = closeFence(rawLines, k, opener);
403
+ out.ranges.push({ start: starts[k], end: ends[end.line], kind: "fence" });
404
+ out.fences.push([k, end.line, end.closed]);
405
+ out.pendingPara = -1;
406
+ if (!end.closed && end.line === count - 1) out.endsInFence = true;
407
+ k = end.line;
408
+ segment = k + 1;
409
+ }
410
+ prose(text, starts, ends, raw, segment, count, out);
411
+ return out;
412
+ }
413
+ function prose(text, starts, ends, raw, from, to, out) {
321
414
  let para = -1;
322
- const spans = (from, to) => {
323
- const runs = [];
415
+ let quoteDepth = 0;
416
+ let lists = [];
417
+ const closePara = (at) => {
418
+ if (para !== -1) out.paragraphs.push([para, at]);
419
+ para = -1;
420
+ };
421
+ for (let k = from; k < to; k++) {
422
+ const line = raw(k);
423
+ const { depth, offset } = quotePrefix(line);
424
+ if (depth !== quoteDepth) {
425
+ quoteDepth = depth;
426
+ lists = [];
427
+ }
428
+ let content = line.slice(offset);
429
+ if (content.trim() === "") {
430
+ closePara(starts[k]);
431
+ if (!(k === starts.length - 1 && line === "")) out.pendingPara = -1;
432
+ continue;
433
+ }
434
+ const indent = leadingSpaces(content);
435
+ while (lists.length && indent < lists[lists.length - 1]) lists.pop();
436
+ const item = LIST_ITEM.exec(content);
437
+ let column = lists.length ? lists[lists.length - 1] : 0;
438
+ if (item) {
439
+ const gap = item[3].length;
440
+ column = item[1].length + item[2].length + (gap >= 1 && gap <= 4 ? gap : 1);
441
+ lists.push(column);
442
+ content = content.slice(Math.min(column, content.length));
443
+ } else {
444
+ content = content.slice(Math.min(indent, column));
445
+ }
446
+ const opener = fenceOpenerOf(content);
447
+ if (!opener) {
448
+ if (para === -1) para = starts[k];
449
+ if (out.pendingPara === -1) out.pendingPara = starts[k];
450
+ continue;
451
+ }
452
+ closePara(starts[k]);
453
+ out.pendingPara = -1;
454
+ const col = column;
455
+ const src = {
456
+ count: to,
457
+ line: (j) => {
458
+ const inner = stripQuotes(raw(j), depth);
459
+ if (inner === null) return null;
460
+ if (opener.char === "~" && col > 0) {
461
+ const ind = leadingSpaces(inner);
462
+ if (inner.trim() !== "" && ind < col) return null;
463
+ return inner.slice(Math.min(ind, col));
464
+ }
465
+ return inner;
466
+ }
467
+ };
468
+ const end = closeFence(src, k, opener);
469
+ out.ranges.push({ start: starts[k], end: ends[end.line], kind: "fence" });
470
+ out.fences.push([k, end.line, end.closed]);
471
+ if (!end.closed && end.line === starts.length - 1) out.endsInFence = true;
472
+ k = end.line;
473
+ }
474
+ closePara(to < starts.length ? starts[to] : text.length);
475
+ }
476
+ var BacktickRuns = class {
477
+ /** Every run, in order: [start, length]. */
478
+ all = [];
479
+ byLength = /* @__PURE__ */ new Map();
480
+ indexOf = /* @__PURE__ */ new Map();
481
+ constructor(text, from, to) {
324
482
  for (let k = from; k < to; ) {
325
483
  if (text.charCodeAt(k) !== 96) {
326
484
  k++;
@@ -328,117 +486,69 @@ function findCodeRanges(text) {
328
486
  }
329
487
  let len = 1;
330
488
  while (k + len < to && text.charCodeAt(k + len) === 96) len++;
331
- runs.push([k, len]);
489
+ this.indexOf.set(k, this.all.length);
490
+ this.all.push([k, len]);
491
+ const list = this.byLength.get(len);
492
+ if (list) list.push(k);
493
+ else this.byLength.set(len, [k]);
332
494
  k += len;
333
495
  }
334
- const nextSame = new Array(runs.length).fill(-1);
335
- const last = /* @__PURE__ */ new Map();
336
- for (let r = runs.length - 1; r >= 0; r--) {
337
- const len = runs[r][1];
338
- nextSame[r] = last.get(len) ?? -1;
339
- last.set(len, r);
340
- }
341
- for (let r = 0; r < runs.length; ) {
342
- const j = nextSame[r];
343
- if (j === -1) {
344
- r++;
345
- continue;
346
- }
347
- const [s0] = runs[r];
348
- const [s1, l1] = runs[j];
349
- ranges.push({ start: s0, end: s1 + l1, kind: "span" });
350
- r = j + 1;
496
+ }
497
+ /** Start of the first run of exactly `n` backticks at or after `after`, or -1. */
498
+ next(n, after) {
499
+ const list = this.byLength.get(n);
500
+ if (!list) return -1;
501
+ let lo = 0;
502
+ let hi = list.length;
503
+ while (lo < hi) {
504
+ const mid = lo + hi >>> 1;
505
+ if (list[mid] < after) lo = mid + 1;
506
+ else hi = mid;
351
507
  }
352
- };
353
- let pos = 0;
354
- while (pos <= n) {
355
- const nl = text.indexOf("\n", pos);
356
- const lineEnd = nl === -1 ? n : nl;
357
- const line = text.slice(pos, lineEnd);
358
- let indent = 0;
359
- while (indent < line.length && line[indent] === " ") indent++;
360
- const body = line.slice(indent);
361
- if (fence) {
362
- const trimmed = trimFenceLine(line);
363
- let closes = false;
364
- if (fence.char === "`") {
365
- const kind = classifyInnerFenceLine(trimmed, fence.len, fence.nests, fence.depth);
366
- if (kind === "open-nested") fence.depth++;
367
- else if (kind === "close-nested") fence.depth--;
368
- else if (kind === "close-outer") closes = indent <= 3;
369
- } else {
370
- let t = 0;
371
- while (t < trimmed.length && trimmed[t] === "~") t++;
372
- closes = indent <= 3 && t >= fence.len && trimFenceLine(trimmed.slice(t)) === "";
373
- }
374
- if (closes) {
375
- ranges.push({ start: fence.start, end: lineEnd, kind: "fence" });
376
- fence = null;
377
- }
378
- } else {
379
- const opener = indent <= 3 ? /^(`{3,}|~{3,})(.*)$/.exec(body) : null;
380
- const info = opener ? opener[2] : "";
381
- if (opener && !(opener[1][0] === "`" && info.includes("`"))) {
382
- if (para !== -1) spans(para, pos);
383
- para = -1;
384
- const char = opener[1][0];
385
- const parsed = char === "`" ? parseFenceOpener(line) : null;
386
- fence = { char, len: opener[1].length, start: pos, nests: fenceNestsInnerFences(parsed?.lang), depth: 0 };
387
- } else if (body.trim() === "") {
388
- if (para !== -1) spans(para, pos);
389
- para = -1;
390
- } else if (para === -1) para = pos;
508
+ return list[lo] ?? -1;
509
+ }
510
+ /** Index of the run starting at `start` in `all`. */
511
+ index(start) {
512
+ return this.indexOf.get(start) ?? -1;
513
+ }
514
+ };
515
+ function codeSpans(text, from, to, out) {
516
+ const runs = new BacktickRuns(text, from, to);
517
+ for (let r = 0; r < runs.all.length; ) {
518
+ const [start, n] = runs.all[r];
519
+ const close = runs.next(n, start + n);
520
+ if (close === -1) {
521
+ r++;
522
+ continue;
391
523
  }
392
- if (nl === -1) break;
393
- pos = nl + 1;
524
+ out.push({ start, end: close + n, kind: "span" });
525
+ r = runs.index(close) + 1;
394
526
  }
395
- if (fence) ranges.push({ start: fence.start, end: n, kind: "fence" });
396
- else if (para !== -1) spans(para, n);
527
+ }
528
+ function findCodeRanges(text) {
529
+ const s = scan(text);
530
+ const ranges = [...s.ranges];
531
+ for (const [from, to] of s.paragraphs) codeSpans(text, from, to, ranges);
397
532
  return ranges.sort((a, b) => a.start - b.start);
398
533
  }
534
+ function fenceLineKinds(text) {
535
+ const s = scan(text);
536
+ let count = 1;
537
+ for (let i = text.indexOf("\n"); i !== -1; i = text.indexOf("\n", i + 1)) count++;
538
+ const kinds = new Array(count).fill("prose");
539
+ for (const [open, last, closed] of s.fences) {
540
+ kinds[open] = "open";
541
+ for (let k = open + 1; k <= last; k++) kinds[k] = "body";
542
+ if (closed && last > open) kinds[last] = "close";
543
+ }
544
+ return kinds;
545
+ }
399
546
  function pendingCodeSpanStart(text) {
400
547
  const n = text.length;
401
548
  if (n === 0 || /\n[ \t]*\n[ \t]*$/.test(text)) return -1;
402
- let fence = null;
403
- let para = -1;
404
- let pos = 0;
405
- while (pos <= n) {
406
- const nl = text.indexOf("\n", pos);
407
- const lineEnd = nl === -1 ? n : nl;
408
- const line = text.slice(pos, lineEnd);
409
- let indent = 0;
410
- while (indent < line.length && line[indent] === " ") indent++;
411
- const body = line.slice(indent);
412
- if (fence) {
413
- const trimmed = trimFenceLine(line);
414
- let closes = false;
415
- if (fence.char === "`") {
416
- const kind = classifyInnerFenceLine(trimmed, fence.len, fence.nests, fence.depth);
417
- if (kind === "open-nested") fence.depth++;
418
- else if (kind === "close-nested") fence.depth--;
419
- else if (kind === "close-outer") closes = indent <= 3;
420
- } else {
421
- let t = 0;
422
- while (t < trimmed.length && trimmed[t] === "~") t++;
423
- closes = indent <= 3 && t >= fence.len && trimFenceLine(trimmed.slice(t)) === "";
424
- }
425
- if (closes) fence = null;
426
- } else {
427
- const opener = indent <= 3 ? /^(`{3,}|~{3,})(.*)$/.exec(body) : null;
428
- const info = opener ? opener[2] : "";
429
- if (opener && !(opener[1][0] === "`" && info.includes("`"))) {
430
- para = -1;
431
- const char = opener[1][0];
432
- const parsed = char === "`" ? parseFenceOpener(line) : null;
433
- fence = { char, len: opener[1].length, nests: fenceNestsInnerFences(parsed?.lang), depth: 0 };
434
- } else if (body.trim() === "") {
435
- if (!(nl === -1 && line === "")) para = -1;
436
- } else if (para === -1) para = pos;
437
- }
438
- if (nl === -1) break;
439
- pos = nl + 1;
440
- }
441
- if (fence || para === -1) return -1;
549
+ const s = scan(text);
550
+ const para = s.pendingPara;
551
+ if (s.endsInFence || para === -1) return -1;
442
552
  const runs = [];
443
553
  for (let k = para; k < n; ) {
444
554
  if (text.charCodeAt(k) !== 96) {
@@ -575,6 +685,83 @@ function pairDisplayMath(text) {
575
685
  return pairs;
576
686
  }
577
687
 
688
+ // source/math.ts
689
+ var TEX_SIGNAL = /\\[A-Za-z]+|[\\^_{}=<>+]/;
690
+ var LONE_VARIABLE = /^[A-Za-z](?:'|[0-9])?$/;
691
+ var MATH_SHAPE_CHARS = /^[A-Za-z0-9'\s(),.*\/-]+$/;
692
+ function isSingleDollarMath(content, after) {
693
+ if (!content || content.length > 400) return false;
694
+ if (/^\s/.test(content) || /\s$/.test(content)) return false;
695
+ if (after !== void 0 && /[0-9]/.test(after)) return false;
696
+ if (/(?:^|[^\\])%/.test(content)) return false;
697
+ if (/^[)\]}"/]/.test(content) || /[(\[{":;,/]$/.test(content)) return false;
698
+ if (/^[0-9]/.test(content) && after !== void 0 && /[A-Za-z]/.test(after)) return false;
699
+ if (isMathPlaceholder(content)) return false;
700
+ return TEX_SIGNAL.test(content) || !readsAsProse(content);
701
+ }
702
+ var FUNCTION_NAMES = /* @__PURE__ */ new Set([
703
+ "sin",
704
+ "cos",
705
+ "tan",
706
+ "sec",
707
+ "csc",
708
+ "cot",
709
+ "log",
710
+ "ln",
711
+ "exp",
712
+ "lim",
713
+ "max",
714
+ "min",
715
+ "det",
716
+ "gcd",
717
+ "mod",
718
+ "arg",
719
+ "sinh",
720
+ "cosh",
721
+ "tanh"
722
+ ]);
723
+ function readsAsProse(content) {
724
+ if (/[A-Za-z]'[a-z]/.test(content)) return true;
725
+ if (/(?:^|[\s(,])[A-Za-z]\.(?:[A-Za-z]\.|\s|$)/.test(content)) return true;
726
+ if (!/\s/.test(content)) return false;
727
+ const words = (content.match(/[A-Za-z]{3,}/g) ?? []).filter((w) => !FUNCTION_NAMES.has(w.toLowerCase()));
728
+ return words.length >= 2;
729
+ }
730
+ function looksLikeOpenInlineMath(partial) {
731
+ if (!partial || partial.length > 400) return false;
732
+ if (/^\s/.test(partial) || /\s$/.test(partial)) return false;
733
+ if (LONE_VARIABLE.test(partial)) return true;
734
+ if (/^[0-9]/.test(partial)) return /\\[A-Za-z]+|[\\^_{}]/.test(partial);
735
+ return TEX_SIGNAL.test(partial) || isMathShape(partial);
736
+ }
737
+ function isMathShape(content) {
738
+ if (!MATH_SHAPE_CHARS.test(content)) return false;
739
+ const words = content.match(/[A-Za-z]+/g);
740
+ if (!words) return false;
741
+ if (!words.every((w) => w.length === 1 || FUNCTION_NAMES.has(w))) return false;
742
+ if (/[A-Za-z]'+[A-Za-z]/.test(content) || /(?:^|[\s(,])[A-Za-z]\.(?:[A-Za-z]\.|\s|$)/.test(content)) return false;
743
+ return /[A-Za-z]'*\(|[A-Za-z]'|\b[A-Za-z]\s+[A-Za-z]\b|\([^)]*[A-Za-z][^)]*\)/.test(content);
744
+ }
745
+ function singleDollarMathEnd(text, open, limit = text.length) {
746
+ const bound = Math.min(limit, open + 403);
747
+ let close = -1;
748
+ for (let k = open + 1; k < bound; k += 1) {
749
+ const c = text[k];
750
+ if (c === "\n") break;
751
+ if (c === "\\") {
752
+ k += 1;
753
+ continue;
754
+ }
755
+ if (c === "$") {
756
+ close = k;
757
+ break;
758
+ }
759
+ }
760
+ if (close === -1 || text[close + 1] === "$") return -1;
761
+ const content = text.slice(open + 1, close);
762
+ return isSingleDollarMath(content, text[close + 1]) ? close + 1 : -1;
763
+ }
764
+
578
765
  // source/xml-tag.ts
579
766
  var XML_NAME_START = /[A-Za-z_]/;
580
767
  var XML_NAME_CHARACTER = /[\w.:-]/;
@@ -654,25 +841,20 @@ var XmlContainerTracker = class {
654
841
  inComment = false;
655
842
  inCdata = false;
656
843
  inlineTicks = 0;
657
- fenceMarker = null;
844
+ /** The open fence, read by THE one closer (source/code-ranges.ts). */
845
+ fence = null;
658
846
  consumeLine(line, startOffset = 0) {
659
- const fence = /^[ \t]*(`{3,}|~{3,})(.*)$/.exec(line);
660
- if (!this.inComment && !this.inCdata && this.inlineTicks === 0 && fence) {
661
- const marker = fence[1];
662
- const char = marker[0];
663
- const suffix = fence[2];
664
- if (marker.length >= 3) {
665
- if (!this.fenceMarker) {
666
- this.fenceMarker = { char, ticks: marker.length };
667
- return null;
668
- }
669
- if (char === this.fenceMarker.char && marker.length >= this.fenceMarker.ticks && suffix.trim() === "") {
670
- this.fenceMarker = null;
671
- }
847
+ if (this.fence) {
848
+ if (this.fence.feed(line)) this.fence = null;
849
+ return null;
850
+ }
851
+ if (!this.inComment && !this.inCdata && this.inlineTicks === 0) {
852
+ const opener = fenceOpenerOf(line);
853
+ if (opener) {
854
+ this.fence = new FenceReader(opener);
672
855
  return null;
673
856
  }
674
857
  }
675
- if (this.fenceMarker) return null;
676
858
  for (let i = startOffset; i < line.length; i++) {
677
859
  if (this.inlineTicks > 0) {
678
860
  if (line[i] === "`") {
@@ -867,27 +1049,6 @@ function isUnclosedGenericXmlOpening(source) {
867
1049
  }
868
1050
  return true;
869
1051
  }
870
- function jsonStringState(line, from, upTo, state) {
871
- let { inString, escaped } = state;
872
- for (let idx = from; idx < upTo; idx++) {
873
- const ch = line[idx];
874
- if (escaped) {
875
- escaped = false;
876
- continue;
877
- }
878
- if (ch === "\\" && inString) {
879
- escaped = true;
880
- continue;
881
- }
882
- if (ch === '"') inString = !inString;
883
- }
884
- return { inString, escaped };
885
- }
886
- function backtickRunLength(str, pos) {
887
- let n = 0;
888
- while (pos + n < str.length && str[pos + n] === "`") n++;
889
- return n;
890
- }
891
1052
  function isSplitterTreeLine(line) {
892
1053
  return TREE_GLYPHS.test(line) || /^[\s│|]*[├└+|][\s─\-]+/.test(line);
893
1054
  }
@@ -1119,11 +1280,9 @@ var Tokenizer = class {
1119
1280
  const footnote = this.footnoteDefinition(p, li, t);
1120
1281
  if (footnote) return footnote;
1121
1282
  }
1122
- if (t.startsWith("```")) return this.backtickFence(p, li, t);
1123
- if (indent <= 3 && t.startsWith("~~~")) {
1124
- const tilde = this.tildeFence(p, li, t);
1125
- if (tilde) return tilde;
1126
- }
1283
+ const fence = fenceOpenerOf(seg);
1284
+ if (fence?.char === "`") return this.backtickFence(p, li, fence);
1285
+ if (fence?.char === "~") return this.tildeFence(p, li, fence);
1127
1286
  if (t.startsWith("$$")) {
1128
1287
  const math = this.mathBlock(p, tp);
1129
1288
  if (math) return math;
@@ -1278,74 +1437,31 @@ var Tokenizer = class {
1278
1437
  heldFootnote(p, id) {
1279
1438
  return this.island("footnote_def", p, this.n, false, { id });
1280
1439
  }
1440
+ /** The text's lines as the one fence closer reads them (streaming: the open tail closes nothing). */
1441
+ get fenceLines() {
1442
+ return {
1443
+ count: this.lineCount,
1444
+ line: (k) => this.lineText(k),
1445
+ openTail: (k) => this.isOpenTail(k)
1446
+ };
1447
+ }
1281
1448
  /**
1282
- * Backtick fence — the renderer splitter's close rules: JSON-string aware for
1283
- * ```json, and THE nested-fence rule (source/fence-nesting.ts) for markdown
1284
- * fences, with the splitter's strict-CommonMark retry when a nested fence is
1285
- * still open at the end of the text.
1449
+ * Backtick fence — THE one closer (`closeFence`, source/code-ranges.ts): the
1450
+ * nested-fence rule for markdown fences with the strict-CommonMark retry, the
1451
+ * renderer's line-end closer, JSON-string awareness for ```json.
1286
1452
  */
1287
- backtickFence(p, li, t) {
1288
- const openTicks = backtickRunLength(t, 0);
1289
- const lang = parseFenceOpener(t)?.lang ?? "";
1453
+ backtickFence(p, li, opener) {
1454
+ const lang = opener.lang;
1290
1455
  const isJson = lang === "json";
1291
1456
  const normalized = normalizeFenceLanguage(lang);
1292
- const meta = { fence: "`", ticks: openTicks, lang };
1457
+ const meta = { fence: "`", ticks: opener.ticks, lang };
1293
1458
  if (normalized && SPECIAL_CODE_LANGUAGES.has(normalized)) meta.special = normalized;
1294
- const nesting = fenceNestsInnerFences(lang);
1295
- let close = this.backtickFenceClose(li, openTicks, isJson, nesting);
1296
- if (close === "retry-strict") close = this.backtickFenceClose(li, openTicks, isJson, false);
1297
- if (typeof close === "number") {
1298
- return this.fenceIsland(p, this.contentEnds[close], true, meta, li, close, isJson);
1459
+ const end = closeFence(this.fenceLines, li, opener);
1460
+ if (end.closed) {
1461
+ return this.fenceIsland(p, this.contentEnds[end.line], true, meta, li, end.line, isJson);
1299
1462
  }
1300
1463
  return this.fenceIsland(p, this.n, false, meta, li, this.lineCount, isJson);
1301
1464
  }
1302
- /** The closing line of a backtick fence opened on line `li`, or null when it never closes. */
1303
- backtickFenceClose(li, openTicks, isJson, nesting) {
1304
- let state = { inString: false, escaped: false };
1305
- let nestedDepth = 0;
1306
- let sawNested = false;
1307
- for (let k = li + 1; k < this.lineCount; k++) {
1308
- if (this.isOpenTail(k)) continue;
1309
- const line = this.lineText(k);
1310
- const trimmedLine = trimFenceLine(line);
1311
- if (trimmedLine.startsWith("```")) {
1312
- const at2 = line.indexOf("```");
1313
- if (isJson) {
1314
- const probe = jsonStringState(line, 0, at2, state);
1315
- if (probe.inString) {
1316
- state = jsonStringState(line, at2, line.length, probe);
1317
- continue;
1318
- }
1319
- }
1320
- const kind = classifyInnerFenceLine(trimmedLine, openTicks, nesting, nestedDepth);
1321
- if (kind === "close-outer") return k;
1322
- if (kind === "open-nested") {
1323
- nestedDepth++;
1324
- sawNested = true;
1325
- }
1326
- if (kind === "close-nested") nestedDepth--;
1327
- if (isJson) state = jsonStringState(line, 0, line.length, state);
1328
- continue;
1329
- }
1330
- const at = line.indexOf("```");
1331
- if (at !== -1) {
1332
- if (isJson) {
1333
- const probe = jsonStringState(line, 0, at, state);
1334
- if (probe.inString) {
1335
- state = jsonStringState(line, at, line.length, probe);
1336
- continue;
1337
- }
1338
- }
1339
- const closeTicks = backtickRunLength(line, at);
1340
- const after = trimFenceLine(line.slice(at + closeTicks));
1341
- if (closeTicks >= openTicks && after === "" && nestedDepth === 0) return k;
1342
- if (isJson) state = jsonStringState(line, 0, line.length, state);
1343
- continue;
1344
- }
1345
- if (isJson) state = jsonStringState(line, 0, line.length, state);
1346
- }
1347
- return sawNested && nestedDepth > 0 ? "retry-strict" : null;
1348
- }
1349
1465
  fenceIsland(p, end, complete, meta, openLine, closeLine, isJson) {
1350
1466
  if (isJson && openLine + 1 < this.lineCount) {
1351
1467
  const bodyEnd = closeLine < this.lineCount ? this.lineStarts[closeLine] : this.n;
@@ -1354,22 +1470,16 @@ var Tokenizer = class {
1354
1470
  }
1355
1471
  return this.island("fence", p, end, complete, meta);
1356
1472
  }
1357
- tildeFence(p, li, t) {
1358
- const open = /^(~{3,})(.*)$/.exec(t);
1359
- if (!open) return null;
1360
- const ticks = open[1].length;
1361
- const lang = open[2].trim().split(/\s/)[0] ?? "";
1362
- const meta = { fence: "~", ticks, lang };
1473
+ tildeFence(p, li, opener) {
1474
+ const lang = opener.lang;
1475
+ const meta = { fence: "~", ticks: opener.ticks, lang };
1363
1476
  const normalized = normalizeFenceLanguage(lang);
1364
1477
  if (normalized && SPECIAL_CODE_LANGUAGES.has(normalized)) meta.special = normalized;
1365
- const close = new RegExp(`^ {0,3}~{${ticks},}[ \\t]*$`);
1366
- for (let k = li + 1; k < this.lineCount; k++) {
1367
- if (this.isOpenTail(k)) continue;
1368
- if (close.test(this.lineText(k))) {
1369
- const tildeEnd = this.contentEnds[k];
1370
- const end = this.rendererOverflow(li + 1, k, tildeEnd);
1371
- return this.island("fence", p, end, end === this.n ? this.overflowComplete : true, meta);
1372
- }
1478
+ const close = closeFence(this.fenceLines, li, opener);
1479
+ if (close.closed) {
1480
+ const tildeEnd = this.contentEnds[close.line];
1481
+ const end = this.rendererOverflow(li + 1, close.line, tildeEnd);
1482
+ return this.island("fence", p, end, end === this.n ? this.overflowComplete : true, meta);
1373
1483
  }
1374
1484
  return this.island("fence", p, this.n, false, meta);
1375
1485
  }
@@ -1389,7 +1499,7 @@ var Tokenizer = class {
1389
1499
  for (let j = firstLine; j <= closeLine && j < this.lineCount; j++) {
1390
1500
  const start = this.lineStarts[j];
1391
1501
  if (this.lineIsBlank(j)) continue;
1392
- if (this.lineText(j).trimStart().startsWith("~~~")) continue;
1502
+ if (fenceOpenerOf(this.lineText(j))?.char === "~") continue;
1393
1503
  const inner = this.detectBlockUncached(start, j, false);
1394
1504
  if (!inner) continue;
1395
1505
  if (inner.end > end) {
@@ -1637,9 +1747,9 @@ var Tokenizer = class {
1637
1747
  const tree = { ...breakIsland, start: treeStart };
1638
1748
  if (treeStart > pos) {
1639
1749
  const proseEnd = this.contentEnds[root - 1];
1640
- const scan2 = this.scanInline(pos, proseEnd);
1641
- if (!scan2.breakIsland) {
1642
- this.push({ kind: "prose", start: pos, end: proseEnd, island: null, inlines: scan2.inlines });
1750
+ const scan3 = this.scanInline(pos, proseEnd);
1751
+ if (!scan3.breakIsland) {
1752
+ this.push({ kind: "prose", start: pos, end: proseEnd, island: null, inlines: scan3.inlines });
1643
1753
  this.push({ kind: "gap", start: proseEnd, end: treeStart, island: null, inlines: [] });
1644
1754
  this.push({ kind: "island", start: treeStart, end: tree.end, island: tree, inlines: [] });
1645
1755
  return tree.end;
@@ -1651,13 +1761,13 @@ var Tokenizer = class {
1651
1761
  }
1652
1762
  }
1653
1763
  }
1654
- const scan = this.scanInline(pos, end);
1655
- if (!scan.breakIsland) {
1656
- this.push({ kind: "prose", start: pos, end, island: null, inlines: scan.inlines });
1764
+ const scan2 = this.scanInline(pos, end);
1765
+ if (!scan2.breakIsland) {
1766
+ this.push({ kind: "prose", start: pos, end, island: null, inlines: scan2.inlines });
1657
1767
  return end;
1658
1768
  }
1659
- const br = scan.breakIsland;
1660
- if (scan.orphan) {
1769
+ const br = scan2.breakIsland;
1770
+ if (scan2.orphan) {
1661
1771
  let start = pos;
1662
1772
  while (this.blocks.length) {
1663
1773
  const last = this.blocks[this.blocks.length - 1];
@@ -1674,7 +1784,7 @@ var Tokenizer = class {
1674
1784
  start: pos,
1675
1785
  end: br.start,
1676
1786
  island: null,
1677
- inlines: scan.inlines.filter((inline) => inline.end <= br.start)
1787
+ inlines: scan2.inlines.filter((inline) => inline.end <= br.start)
1678
1788
  });
1679
1789
  this.push({ kind: "island", start: br.start, end: br.end, island: br, inlines: [] });
1680
1790
  return br.end;
@@ -1694,30 +1804,10 @@ var Tokenizer = class {
1694
1804
  const codeSpanClose = (n, from) => {
1695
1805
  if (!runs) {
1696
1806
  this.tick(pe - ps);
1697
- runs = /* @__PURE__ */ new Map();
1698
- for (let k = ps; k < pe; ) {
1699
- if (text.charCodeAt(k) !== 96) {
1700
- k++;
1701
- continue;
1702
- }
1703
- const len = backtickRunLength(text, k);
1704
- const list2 = runs.get(len);
1705
- if (list2) list2.push(k);
1706
- else runs.set(len, [k]);
1707
- k += len;
1708
- }
1709
- }
1710
- const list = runs.get(n);
1711
- if (!list) return -1;
1712
- let lo = 0;
1713
- let hi = list.length;
1714
- while (lo < hi) {
1715
- const mid = lo + hi >>> 1;
1716
- if (list[mid] < from) lo = mid + 1;
1717
- else hi = mid;
1807
+ runs = new BacktickRuns(text, ps, pe);
1718
1808
  }
1719
- const at = list[lo];
1720
- return at !== void 0 && at + n <= pe ? at : -1;
1809
+ const at = runs.next(n, from);
1810
+ return at !== -1 && at + n <= pe ? at : -1;
1721
1811
  };
1722
1812
  let i = ps;
1723
1813
  const scanEnd = Math.min(pe, this.provisionalFrom);
@@ -2254,6 +2344,6 @@ function mapRange(changes, start, end, unit = "utf16") {
2254
2344
  return { start: newStart, end: newEnd, touched, collapsed: newEnd === newStart && end > start };
2255
2345
  }
2256
2346
 
2257
- export { CALLOUT_NAMES, FENCE_OPENER_WHITESPACE, FENCE_WHITESPACE, NESTING_FENCE_LANGUAGES, SourceSpliceError, XmlContainerTracker, blockAt, blockEdit, blockRangeEdit, buildCodePointIndex, classifyInnerFenceLine, fenceNestsInnerFences, findCodeRanges, isMathPlaceholder, isSingleDollarMath, islandEdit, joinSource, listIslands, looksLikeDisplayMath, mapPosition, mapRange, pairDisplayMath, parseFenceOpener, pendingDisplayMathOpener, readXmlTag, singleDollarMathEnd, spliceSave, splitsSurrogatePair, toCodePointOffset, toUtf16Offset, tokenizeSource, trimFenceLine };
2347
+ export { BacktickRuns, CALLOUT_NAMES, FENCE_OPENER_WHITESPACE, FENCE_WHITESPACE, FenceReader, NESTING_FENCE_LANGUAGES, SourceSpliceError, XmlContainerTracker, blockAt, blockEdit, blockRangeEdit, buildCodePointIndex, classifyInnerFenceLine, closeFence, fenceLineKinds, fenceNestsInnerFences, fenceOpenerOf, findCodeRanges, isMathPlaceholder, isSingleDollarMath, islandEdit, joinSource, listIslands, looksLikeDisplayMath, looksLikeOpenInlineMath, mapPosition, mapRange, pairDisplayMath, parseFenceOpener, pendingDisplayMathOpener, readXmlTag, singleDollarMathEnd, spliceSave, splitsSurrogatePair, toCodePointOffset, toUtf16Offset, tokenizeSource, trimFenceLine };
2258
2348
  //# sourceMappingURL=source.js.map
2259
2349
  //# sourceMappingURL=source.js.map