@ai-matrx/content-ir 0.18.3 → 0.18.5

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.cjs CHANGED
@@ -323,6 +323,7 @@ var FenceReader = class {
323
323
  function closeFence(src, open, opener) {
324
324
  const first = readFence(src, open, new FenceReader(opener));
325
325
  if (first.closed || !first.reader.sawNested || first.reader.nestedDepth <= 0) return { line: first.line, closed: first.closed };
326
+ if (src.streaming) return { line: first.line, closed: false };
326
327
  const strict = readFence(src, open, new FenceReader(opener, { nesting: false }));
327
328
  return { line: strict.line, closed: strict.closed };
328
329
  }
@@ -586,6 +587,8 @@ function fenceLineKinds(text) {
586
587
  function pendingCodeSpanStart(text) {
587
588
  const n = text.length;
588
589
  if (n === 0 || /\n[ \t]*\n[ \t]*$/.test(text)) return -1;
590
+ const lastLine = text.slice(text.lastIndexOf("\n") + 1);
591
+ if (lastLine && /^(?: {0,3}>[ \t]?)*[ \t]*$/.test(lastLine)) return pendingCodeSpanStart(`${text}x`);
589
592
  const s = scan(text);
590
593
  const para = s.pendingPara;
591
594
  if (s.endsInFence || para === -1) return -1;
@@ -732,13 +735,26 @@ var MATH_SHAPE_CHARS = /^[A-Za-z0-9'\s(),.*\/-]+$/;
732
735
  function isSingleDollarMath(content, after) {
733
736
  if (!content || content.length > 400) return false;
734
737
  if (/^\s/.test(content) || /\s$/.test(content)) return false;
735
- if (after !== void 0 && /[0-9]/.test(after)) return false;
738
+ if (after !== void 0 && /^[0-9]/.test(after)) return false;
739
+ if (isVariableChain(content, after)) return false;
736
740
  if (/(?:^|[^\\])%/.test(content)) return false;
737
741
  if (/^[)\]}"/]/.test(content) || /[(\[{":;,/]$/.test(content)) return false;
738
- if (/^[0-9]/.test(content) && after !== void 0 && /[A-Za-z]/.test(after)) return false;
742
+ if (/^[0-9]/.test(content) && after !== void 0 && /^[A-Za-z]/.test(after)) return false;
739
743
  if (isMathPlaceholder(content)) return false;
740
744
  return TEX_SIGNAL.test(content) || !readsAsProse(content);
741
745
  }
746
+ var IDENTIFIER = /^[A-Za-z_][A-Za-z0-9_]*$/;
747
+ var MATH_SUFFIX = /^(?:s|es|th|st|nd|rd)$/i;
748
+ function isVariableChain(content, after) {
749
+ if (/^\{[A-Za-z_][\w.]*\}$/.test(content)) return true;
750
+ if (!after) return false;
751
+ if (after[0] === "{") return IDENTIFIER.test(content);
752
+ const run = /^[A-Za-z0-9_]+/.exec(after)?.[0];
753
+ if (!run) return false;
754
+ if (after.length === 1) return false;
755
+ if (/^[A-Za-z_][A-Za-z0-9_]*[.@-]$/.test(content)) return true;
756
+ return IDENTIFIER.test(content) && !MATH_SUFFIX.test(run);
757
+ }
742
758
  var FUNCTION_NAMES = /* @__PURE__ */ new Set([
743
759
  "sin",
744
760
  "cos",
@@ -782,6 +798,12 @@ function isMathShape(content) {
782
798
  if (/[A-Za-z]'+[A-Za-z]/.test(content) || /(?:^|[\s(,])[A-Za-z]\.(?:[A-Za-z]\.|\s|$)/.test(content)) return false;
783
799
  return /[A-Za-z]'*\(|[A-Za-z]'|\b[A-Za-z]\s+[A-Za-z]\b|\([^)]*[A-Za-z][^)]*\)/.test(content);
784
800
  }
801
+ var AFTER_WINDOW = 32;
802
+ function singleDollarAfter(text, close) {
803
+ const rest = text.slice(close + 1, close + 1 + AFTER_WINDOW);
804
+ return rest.length < AFTER_WINDOW ? `${rest}
805
+ ` : rest;
806
+ }
785
807
  function singleDollarMathEnd(text, open, limit = text.length) {
786
808
  const bound = Math.min(limit, open + 403);
787
809
  let close = -1;
@@ -799,7 +821,13 @@ function singleDollarMathEnd(text, open, limit = text.length) {
799
821
  }
800
822
  if (close === -1 || text[close + 1] === "$") return -1;
801
823
  const content = text.slice(open + 1, close);
802
- return isSingleDollarMath(content, text[close + 1]) ? close + 1 : -1;
824
+ return isSingleDollarMath(content, singleDollarAfter(text, close)) ? close + 1 : -1;
825
+ }
826
+ var BRACKET_TEX_SIGNAL = /\\[A-Za-z]+|[\^{}=<>+]|_\{|(?:^|[^A-Za-z0-9])[A-Za-z]_[A-Za-z0-9](?![A-Za-z0-9])/;
827
+ function isEscapedBracketMath(content, before) {
828
+ if (!content.trim()) return false;
829
+ if (before !== void 0 && /[A-Za-z0-9_)\]]/.test(before)) return false;
830
+ return BRACKET_TEX_SIGNAL.test(content);
803
831
  }
804
832
 
805
833
  // source/xml-tag.ts
@@ -1164,18 +1192,14 @@ var Tokenizer = class {
1164
1192
  /**
1165
1193
  * Streaming: an open last line that begins with `{` is undecided — each new
1166
1194
  * byte can balance or unbalance its braces and flip the whole line between
1167
- * prose and a JSON island — and so is a line that is exactly one allowed
1168
- * HTML tag so far; both are provisional until they end (G5).
1195
+ * prose and a JSON island — so it is provisional until it ends (G5).
1196
+ * (A lone HTML tag on that line is held by {@link holdUndecidedTail}.)
1169
1197
  */
1170
1198
  markOpenJsonTail() {
1171
1199
  const last = this.lineStarts.length - 1;
1172
1200
  if (!this.isOpenTail(last)) return;
1173
- const start = this.lineStarts[last];
1174
- const t = this.lineText(last).trim();
1175
- const oneTag = t.startsWith("<") ? readXmlTag(t, 0) : null;
1176
- const loneTag = !!oneTag && oneTag.raw === t && ALLOWED_RAW_HTML_TAGS.has(oneTag.tagName.toLowerCase());
1177
- if (t.startsWith("{") || loneTag) {
1178
- this.provisionalFrom = Math.min(this.provisionalFrom, start);
1201
+ if (this.lineText(last).trim().startsWith("{")) {
1202
+ this.provisionalFrom = Math.min(this.provisionalFrom, this.lineStarts[last]);
1179
1203
  }
1180
1204
  }
1181
1205
  /**
@@ -1208,11 +1232,22 @@ var Tokenizer = class {
1208
1232
  /** Streaming only: a paragraph ending at `pe` may still be continued by a line not yet arrived. */
1209
1233
  paragraphMayGrow(pe) {
1210
1234
  if (!this.streaming) return false;
1235
+ if (this.undecidedBreaks.has(pe)) return true;
1211
1236
  const rest = this.n - pe;
1212
1237
  if (rest === 0) return true;
1213
1238
  const tail = this.text.slice(pe);
1214
- return tail === "\n" || tail === "\r\n" || tail === "\r";
1239
+ return /^(?:\r\n|\r|\n)[ \t]*$/.test(tail);
1240
+ }
1241
+ /**
1242
+ * Streaming: an open block that may still turn back into prose — a `$$`
1243
+ * pair whose closer is on the open last line (prose after it makes it
1244
+ * inline), or a JSON head whose braces have not balanced (it may never parse).
1245
+ */
1246
+ mayDissolve(block) {
1247
+ return this.streaming && !block.complete && (block.type === "math_block" || block.type === "json");
1215
1248
  }
1249
+ /** Streaming: paragraph ends where an undecided, dissolvable block cut them short. */
1250
+ undecidedBreaks = /* @__PURE__ */ new Set();
1216
1251
  /** Streaming only: the final line, still waiting for its terminator. */
1217
1252
  isOpenTail(k) {
1218
1253
  return this.streaming && k === this.lineStarts.length - 1 && (this.lineEnds[k] === this.contentEnds[k] || this.pendingCR);
@@ -1261,8 +1296,103 @@ var Tokenizer = class {
1261
1296
  }
1262
1297
  pos = this.paragraph(pos, li);
1263
1298
  }
1299
+ if (this.streaming) this.holdUndecidedTail();
1264
1300
  return this.blocks;
1265
1301
  }
1302
+ /**
1303
+ * Streaming: settle nothing the next bytes can still change (verify-RC-B3 F3).
1304
+ *
1305
+ * 1. A block island that ends on the open last line is undecided — the next
1306
+ * bytes on that line can re-read it or dissolve it (`<!--@a:k1-->` +
1307
+ * ` more` is an inline anchor, `<br>` + `x` is prose). It runs open to
1308
+ * the end of the text.
1309
+ * 2. A box-drawing tree still forming at the end of the text claims, as its
1310
+ * root label, the lines directly above its first connector (the
1311
+ * renderer's `findTreeBlockStart`), and the open last line itself may
1312
+ * become one of its connector lines — so inlines on those lines are held
1313
+ * back as prose until a blank line, a heading or a tree line decides.
1314
+ * This also holds everything a closer still able to arrive on the open
1315
+ * last line could change (`$x$` + `5` is currency, G4; `{{v}}` + `}`).
1316
+ */
1317
+ holdUndecidedTail() {
1318
+ const last = this.lineStarts.length - 1;
1319
+ if (last < 0) return;
1320
+ if (this.isOpenTail(last)) {
1321
+ const lineStart = this.lineStarts[last];
1322
+ for (const block2 of this.blocks) {
1323
+ if (block2.kind === "prose" && block2.end > lineStart) {
1324
+ block2.inlines = block2.inlines.filter((inline) => inline.end <= lineStart);
1325
+ }
1326
+ }
1327
+ let b = this.blocks.length - 1;
1328
+ while (b >= 0 && this.blocks[b].kind === "gap") b--;
1329
+ const block = this.blocks[b];
1330
+ if (block?.island?.complete && block.island.end > this.lineStarts[last]) {
1331
+ block.island.complete = false;
1332
+ block.island.end = this.n;
1333
+ block.end = this.n;
1334
+ this.blocks.length = b + 1;
1335
+ }
1336
+ }
1337
+ const from = this.treeRootHoldFrom();
1338
+ for (let b = this.blocks.length - 1; b >= 0; b--) {
1339
+ const block = this.blocks[b];
1340
+ if (block.end <= from) break;
1341
+ if (block.kind === "prose") block.inlines = block.inlines.filter((inline) => inline.end <= from);
1342
+ if (block.island) this.blocks[b] = { kind: "prose", start: block.start, end: block.end, island: null, inlines: [] };
1343
+ }
1344
+ }
1345
+ /**
1346
+ * Streaming: where a tree that may still form at the end of the text would
1347
+ * start claiming lines (`n` when none can). Only paragraph lines can be
1348
+ * claimed — a line that starts inside an island (a fence body, an XML
1349
+ * region…) is never a tree line nor a root label, so it stops the walk.
1350
+ */
1351
+ treeRootHoldFrom() {
1352
+ const claimable = (k2) => {
1353
+ for (; ; ) {
1354
+ const block = this.blockAt(this.lineStarts[k2]);
1355
+ if (!block || block.kind !== "island" || block.island?.fromParagraph === true) return true;
1356
+ const opened = this.lineOf(block.start);
1357
+ if (this.lineStarts[opened] === block.start || opened === k2) return false;
1358
+ k2 = opened;
1359
+ }
1360
+ };
1361
+ let k = this.lineCount - 1;
1362
+ let start = this.lineCount;
1363
+ if (k >= 0 && this.isOpenTail(k)) {
1364
+ if (!claimable(k)) return this.n;
1365
+ start = k;
1366
+ k--;
1367
+ }
1368
+ for (; k >= 0 && claimable(k); k--) {
1369
+ this.tick(1);
1370
+ if (this.lineIsBlank(k)) continue;
1371
+ if (!TREE_GLYPHS.test(this.lineText(k))) break;
1372
+ start = k;
1373
+ }
1374
+ let root = start;
1375
+ for (let j = start - 1; j >= 0 && claimable(j); j--) {
1376
+ const trimmed = this.lineText(j).trim();
1377
+ this.tick(trimmed.length + 1);
1378
+ if (!trimmed || isSplitterTreeLine(trimmed) || /^#{1,6}\s/.test(trimmed)) break;
1379
+ root = j;
1380
+ }
1381
+ return root < this.lineCount ? this.lineStarts[root] : this.n;
1382
+ }
1383
+ /** The block (so far) containing offset `pos`, or null. */
1384
+ blockAt(pos) {
1385
+ let lo = 0;
1386
+ let hi = this.blocks.length - 1;
1387
+ while (lo <= hi) {
1388
+ const mid = lo + hi >>> 1;
1389
+ const block = this.blocks[mid];
1390
+ if (pos < block.start) hi = mid - 1;
1391
+ else if (pos >= block.end) lo = mid + 1;
1392
+ else return block;
1393
+ }
1394
+ return null;
1395
+ }
1266
1396
  push(block) {
1267
1397
  if (block.end <= block.start) return;
1268
1398
  if (block.kind === "prose" && this.isBlank(block.start, block.end)) {
@@ -1466,7 +1596,10 @@ var Tokenizer = class {
1466
1596
  continue;
1467
1597
  }
1468
1598
  if (this.isOpenTail(k)) return this.heldFootnote(p, open[1]);
1469
- if (FOOTNOTE_DEF.test(line.trimStart()) || this.detectBlock(this.lineStarts[k], k, false)) break;
1599
+ if (FOOTNOTE_DEF.test(line.trimStart())) break;
1600
+ const block = this.detectBlock(this.lineStarts[k], k, false);
1601
+ if (block && this.mayDissolve(block)) return this.heldFootnote(p, open[1]);
1602
+ if (block) break;
1470
1603
  last = k;
1471
1604
  }
1472
1605
  const end = this.contentEnds[last];
@@ -1482,7 +1615,8 @@ var Tokenizer = class {
1482
1615
  return {
1483
1616
  count: this.lineCount,
1484
1617
  line: (k) => this.lineText(k),
1485
- openTail: (k) => this.isOpenTail(k)
1618
+ openTail: (k) => this.isOpenTail(k),
1619
+ streaming: this.streaming
1486
1620
  };
1487
1621
  }
1488
1622
  /**
@@ -1542,6 +1676,10 @@ var Tokenizer = class {
1542
1676
  if (fenceOpenerOf(this.lineText(j))?.char === "~") continue;
1543
1677
  const inner = this.detectBlockUncached(start, j, false);
1544
1678
  if (!inner) continue;
1679
+ if (this.streaming && inner.end >= tildeEnd && this.paragraphMayGrow(inner.end)) {
1680
+ this.overflowComplete = false;
1681
+ return this.n;
1682
+ }
1545
1683
  if (inner.end > end) {
1546
1684
  end = inner.end;
1547
1685
  this.overflowComplete = inner.complete;
@@ -1670,12 +1808,7 @@ var Tokenizer = class {
1670
1808
  return this.island("html_block", p, close.index + close[0].length, true, { tag: name });
1671
1809
  }
1672
1810
  const typeSix = HTML_BLOCK_LEVEL_TAGS.has(name);
1673
- const loneTag = atParaStart && t.trimEnd() === tag.raw;
1674
- if (loneTag && this.isOpenTail(li)) {
1675
- this.provisionalFrom = Math.min(this.provisionalFrom, p);
1676
- return null;
1677
- }
1678
- const typeSeven = loneTag;
1811
+ const typeSeven = atParaStart && t.trimEnd() === tag.raw;
1679
1812
  if (!typeSix && !typeSeven) return null;
1680
1813
  let end = this.contentEnds[li];
1681
1814
  for (let k = li + 1; k < this.lineCount && !this.lineIsBlank(k); k++) {
@@ -1733,6 +1866,13 @@ var Tokenizer = class {
1733
1866
  }
1734
1867
  return null;
1735
1868
  }
1869
+ if (this.isOpenTail(lastLine)) {
1870
+ if (isRecognizedJsonHead(this.text.slice(tp, tp + 512))) {
1871
+ return this.island("json", p, this.n, false, jsonMeta(this.text.slice(tp)));
1872
+ }
1873
+ this.provisionalFrom = Math.min(this.provisionalFrom, p);
1874
+ return null;
1875
+ }
1736
1876
  if (net !== 0) return null;
1737
1877
  const end = this.contentEnds[lastLine];
1738
1878
  const content = this.text.slice(tp, end).trim();
@@ -1767,8 +1907,10 @@ var Tokenizer = class {
1767
1907
  let breakLine = -1;
1768
1908
  for (let k = li + 1; k < this.lineCount; k++) {
1769
1909
  if (this.lineIsBlank(k)) break;
1770
- if (this.detectBlock(this.lineStarts[k], k, false)) {
1910
+ const block = this.detectBlock(this.lineStarts[k], k, false);
1911
+ if (block) {
1771
1912
  breakLine = k;
1913
+ if (this.mayDissolve(block)) this.undecidedBreaks.add(end);
1772
1914
  break;
1773
1915
  }
1774
1916
  end = this.contentEnds[k];
@@ -1806,7 +1948,7 @@ var Tokenizer = class {
1806
1948
  this.push({ kind: "prose", start: pos, end, island: null, inlines: scan2.inlines });
1807
1949
  return end;
1808
1950
  }
1809
- const br = scan2.breakIsland;
1951
+ const br = { ...scan2.breakIsland, fromParagraph: true };
1810
1952
  if (scan2.orphan) {
1811
1953
  let start = pos;
1812
1954
  while (this.blocks.length) {
@@ -1829,6 +1971,15 @@ var Tokenizer = class {
1829
1971
  this.push({ kind: "island", start: br.start, end: br.end, island: br, inlines: [] });
1830
1972
  return br.end;
1831
1973
  }
1974
+ /**
1975
+ * An island that opens inside a paragraph and closes past its end becomes a
1976
+ * block. Streaming: while the paragraph may still grow over its closer, the
1977
+ * island may yet be inline — undecided, so it runs open to the end (F3).
1978
+ */
1979
+ spill(type, start, end, pe, meta) {
1980
+ if (this.paragraphMayGrow(pe)) return this.island(type, start, this.n, false, meta);
1981
+ return this.island(type, start, end, true, meta);
1982
+ }
1832
1983
  scanInline(ps, pe) {
1833
1984
  const text = this.text;
1834
1985
  const inlines = [];
@@ -1857,11 +2008,15 @@ var Tokenizer = class {
1857
2008
  const nextCh = text[i + 1];
1858
2009
  if (nextCh === "(" || nextCh === "[") {
1859
2010
  const close = within(nextCh === "(" ? "\\)" : "\\]", i + 2);
1860
- if (close !== -1) {
2011
+ const isMath = close !== -1 && (nextCh === "(" || isEscapedBracketMath(text.slice(i + 2, close), i > 0 ? text[i - 1] : void 0));
2012
+ if (close !== -1 && isMath) {
1861
2013
  inlines.push(this.island("math_inline", i, close + 2, true, { delimiter: nextCh === "(" ? "\\(" : "\\[" }));
1862
2014
  i = close + 2;
1863
2015
  continue;
1864
2016
  }
2017
+ if (close === -1 && this.streaming && this.paragraphMayGrow(pe)) {
2018
+ return { inlines, breakIsland: null, orphan: false };
2019
+ }
1865
2020
  }
1866
2021
  i += 2;
1867
2022
  continue;
@@ -1882,17 +2037,13 @@ var Tokenizer = class {
1882
2037
  i += 2;
1883
2038
  continue;
1884
2039
  }
1885
- if (close + 2 > pe) return stop(this.island("math_block", i, close + 2, true, { delimiter: "$$" }));
2040
+ if (close + 2 > pe) return stop(this.spill("math_block", i, close + 2, pe, { delimiter: "$$" }));
1886
2041
  inlines.push(this.island("math_inline", i, close + 2, true, { delimiter: "$$" }));
1887
2042
  i = close + 2;
1888
2043
  continue;
1889
2044
  }
1890
2045
  const end = singleDollarMathEnd(text, i, pe);
1891
2046
  this.tick(end > 0 ? end - i : Math.min(403, pe - i));
1892
- if (end > 0 && this.streaming && end === this.n) {
1893
- this.provisionalFrom = Math.min(this.provisionalFrom, i);
1894
- return { inlines, breakIsland: null, orphan: false };
1895
- }
1896
2047
  if (end > 0) {
1897
2048
  inlines.push(this.island("math_inline", i, end, true, { delimiter: "$" }));
1898
2049
  i = end;
@@ -1970,7 +2121,7 @@ var Tokenizer = class {
1970
2121
  const anchor = end - i <= 64 ? PINNED_ANCHOR_RE.exec(text.slice(i, end)) : null;
1971
2122
  const type = anchor ? "anchor" : "html_comment";
1972
2123
  const meta = anchor ? { id: anchor[1] } : {};
1973
- if (end > pe) return stop(this.island(type, i, end, true, meta));
2124
+ if (end > pe) return stop(this.spill(type, i, end, pe, meta));
1974
2125
  inlines.push(this.island(type, i, end, true, meta));
1975
2126
  i = end;
1976
2127
  continue;
@@ -1999,7 +2150,7 @@ var Tokenizer = class {
1999
2150
  const blockType = isAttr ? "xml_attr" : "xml_region";
2000
2151
  if (close === -1) return stop(this.island(blockType, i, this.n, false, { tag: name }));
2001
2152
  const end = close + closer.length;
2002
- if (end > pe) return stop(this.island(blockType, i, end, true, { tag: name }));
2153
+ if (end > pe) return stop(this.spill(blockType, i, end, pe, { tag: name }));
2003
2154
  inlines.push(this.island("xml_inline", i, end, true, { tag: name }));
2004
2155
  i = end;
2005
2156
  continue;
@@ -2384,6 +2535,7 @@ function mapRange(changes, start, end, unit = "utf16") {
2384
2535
  return { start: newStart, end: newEnd, touched, collapsed: newEnd === newStart && end > start };
2385
2536
  }
2386
2537
 
2538
+ exports.AFTER_WINDOW = AFTER_WINDOW;
2387
2539
  exports.BacktickRuns = BacktickRuns;
2388
2540
  exports.CALLOUT_NAMES = CALLOUT_NAMES;
2389
2541
  exports.FENCE_OPENER_WHITESPACE = FENCE_OPENER_WHITESPACE;
@@ -2404,6 +2556,7 @@ exports.fenceNestsInnerFences = fenceNestsInnerFences;
2404
2556
  exports.fenceOpenerOf = fenceOpenerOf;
2405
2557
  exports.fenceParts = fenceParts;
2406
2558
  exports.findCodeRanges = findCodeRanges;
2559
+ exports.isEscapedBracketMath = isEscapedBracketMath;
2407
2560
  exports.isMathPlaceholder = isMathPlaceholder;
2408
2561
  exports.isSingleDollarMath = isSingleDollarMath;
2409
2562
  exports.islandEdit = islandEdit;
@@ -2418,6 +2571,7 @@ exports.pairDisplayMath = pairDisplayMath;
2418
2571
  exports.parseFenceOpener = parseFenceOpener;
2419
2572
  exports.pendingDisplayMathOpener = pendingDisplayMathOpener;
2420
2573
  exports.readXmlTag = readXmlTag;
2574
+ exports.singleDollarAfter = singleDollarAfter;
2421
2575
  exports.singleDollarMathEnd = singleDollarMathEnd;
2422
2576
  exports.spliceSave = spliceSave;
2423
2577
  exports.splitsSurrogatePair = splitsSurrogatePair;