@ai-matrx/content-ir 0.18.4 → 0.18.6

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,7 @@ 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;
803
825
  }
804
826
  var BRACKET_TEX_SIGNAL = /\\[A-Za-z]+|[\^{}=<>+]|_\{|(?:^|[^A-Za-z0-9])[A-Za-z]_[A-Za-z0-9](?![A-Za-z0-9])/;
805
827
  function isEscapedBracketMath(content, before) {
@@ -1170,18 +1192,14 @@ var Tokenizer = class {
1170
1192
  /**
1171
1193
  * Streaming: an open last line that begins with `{` is undecided — each new
1172
1194
  * byte can balance or unbalance its braces and flip the whole line between
1173
- * prose and a JSON island — and so is a line that is exactly one allowed
1174
- * 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}.)
1175
1197
  */
1176
1198
  markOpenJsonTail() {
1177
1199
  const last = this.lineStarts.length - 1;
1178
1200
  if (!this.isOpenTail(last)) return;
1179
- const start = this.lineStarts[last];
1180
- const t = this.lineText(last).trim();
1181
- const oneTag = t.startsWith("<") ? readXmlTag(t, 0) : null;
1182
- const loneTag = !!oneTag && oneTag.raw === t && ALLOWED_RAW_HTML_TAGS.has(oneTag.tagName.toLowerCase());
1183
- if (t.startsWith("{") || loneTag) {
1184
- this.provisionalFrom = Math.min(this.provisionalFrom, start);
1201
+ if (this.lineText(last).trim().startsWith("{")) {
1202
+ this.provisionalFrom = Math.min(this.provisionalFrom, this.lineStarts[last]);
1185
1203
  }
1186
1204
  }
1187
1205
  /**
@@ -1214,11 +1232,25 @@ var Tokenizer = class {
1214
1232
  /** Streaming only: a paragraph ending at `pe` may still be continued by a line not yet arrived. */
1215
1233
  paragraphMayGrow(pe) {
1216
1234
  if (!this.streaming) return false;
1235
+ if (this.undecidedBreaks.has(pe)) return true;
1217
1236
  const rest = this.n - pe;
1218
1237
  if (rest === 0) return true;
1219
1238
  const tail = this.text.slice(pe);
1220
- return tail === "\n" || tail === "\r\n" || tail === "\r";
1239
+ return /^(?:\r\n|\r|\n)[ \t]*$/.test(tail);
1221
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), a JSON head whose braces have not balanced (it may never parse),
1245
+ * or any opener that is itself still arriving on the open last line.
1246
+ */
1247
+ mayDissolve(block) {
1248
+ if (!this.streaming || block.complete) return false;
1249
+ if (block.type === "math_block" || block.type === "json") return true;
1250
+ return this.isOpenTail(this.lineOf(block.start));
1251
+ }
1252
+ /** Streaming: paragraph ends where an undecided, dissolvable block cut them short. */
1253
+ undecidedBreaks = /* @__PURE__ */ new Set();
1222
1254
  /** Streaming only: the final line, still waiting for its terminator. */
1223
1255
  isOpenTail(k) {
1224
1256
  return this.streaming && k === this.lineStarts.length - 1 && (this.lineEnds[k] === this.contentEnds[k] || this.pendingCR);
@@ -1236,6 +1268,12 @@ var Tokenizer = class {
1236
1268
  }
1237
1269
  return lo;
1238
1270
  }
1271
+ /** How many whitespace characters open the text at `from` (up to `to`). */
1272
+ leadingSpace(from, to) {
1273
+ let k = from;
1274
+ while (k < to && /\s/.test(this.text[k])) k++;
1275
+ return k - from;
1276
+ }
1239
1277
  isBlank(from, to) {
1240
1278
  return !NOT_WHITESPACE.test(this.text.slice(from, to));
1241
1279
  }
@@ -1260,6 +1298,10 @@ var Tokenizer = class {
1260
1298
  break;
1261
1299
  }
1262
1300
  const island = this.detectBlock(pos, li, true);
1301
+ if (island && pos + this.leadingSpace(pos, this.contentEnds[li]) >= this.provisionalFrom) {
1302
+ this.push({ kind: "prose", start: pos, end: this.n, island: null, inlines: [] });
1303
+ break;
1304
+ }
1263
1305
  if (island) {
1264
1306
  this.push({ kind: "island", start: island.start, end: island.end, island, inlines: [] });
1265
1307
  pos = island.end;
@@ -1267,8 +1309,122 @@ var Tokenizer = class {
1267
1309
  }
1268
1310
  pos = this.paragraph(pos, li);
1269
1311
  }
1312
+ if (this.streaming) this.holdUndecidedTail();
1270
1313
  return this.blocks;
1271
1314
  }
1315
+ /**
1316
+ * Streaming: settle nothing the next bytes can still change (verify-RC-B3 F3).
1317
+ *
1318
+ * 1. A block island that ends on the open last line is undecided — the next
1319
+ * bytes on that line can re-read it or dissolve it (`<!--@a:k1-->` +
1320
+ * ` more` is an inline anchor, `<br>` + `x` is prose). It runs open to
1321
+ * the end of the text.
1322
+ * 2. A box-drawing tree still forming at the end of the text claims, as its
1323
+ * root label, the lines directly above its first connector (the
1324
+ * renderer's `findTreeBlockStart`), and the open last line itself may
1325
+ * become one of its connector lines — so inlines on those lines are held
1326
+ * back as prose until a blank line, a heading or a tree line decides.
1327
+ * This also holds everything a closer still able to arrive on the open
1328
+ * last line could change (`$x$` + `5` is currency, G4; `{{v}}` + `}`).
1329
+ */
1330
+ holdUndecidedTail() {
1331
+ const last = this.lineStarts.length - 1;
1332
+ if (last < 0) return;
1333
+ if (this.isOpenTail(last)) {
1334
+ const lineStart = this.lineStarts[last];
1335
+ for (const block2 of this.blocks) {
1336
+ if (block2.kind === "prose" && block2.end > lineStart) {
1337
+ block2.inlines = block2.inlines.filter((inline) => inline.end <= lineStart);
1338
+ }
1339
+ }
1340
+ let b = this.blocks.length - 1;
1341
+ while (b >= 0 && this.blocks[b].kind === "gap") b--;
1342
+ const block = this.blocks[b];
1343
+ if (block?.island?.complete && block.island.end > this.lineStarts[last]) {
1344
+ block.island.complete = false;
1345
+ block.island.end = this.n;
1346
+ block.end = this.n;
1347
+ this.blocks.length = b + 1;
1348
+ }
1349
+ }
1350
+ const from = this.treeRootHoldFrom();
1351
+ for (let b = this.blocks.length - 1; b >= 0; b--) {
1352
+ const block = this.blocks[b];
1353
+ if (block.end <= from) break;
1354
+ if (block.kind === "prose") block.inlines = block.inlines.filter((inline) => inline.end <= from);
1355
+ if (block.island) this.blocks[b] = { kind: "prose", start: block.start, end: block.end, island: null, inlines: [] };
1356
+ }
1357
+ }
1358
+ /**
1359
+ * Streaming: where a tree that may still form at the end of the text would
1360
+ * start claiming lines (`n` when none can). Only paragraph lines can be
1361
+ * claimed — a line that starts inside an island (a fence body, an XML
1362
+ * region…) is never a tree line nor a root label, so it stops the walk.
1363
+ */
1364
+ treeRootHoldFrom() {
1365
+ const claimFrom = (k2) => {
1366
+ const lineStart = this.lineStarts[k2];
1367
+ const lineEnd = this.contentEnds[k2];
1368
+ let pos = lineStart;
1369
+ for (; ; ) {
1370
+ const block = this.blockAt(pos);
1371
+ if (!block || block.kind !== "island" || !block.island) return pos;
1372
+ if (block.island.fromParagraph === true || this.mayDissolve(block.island)) return pos;
1373
+ if (block.start < lineStart) {
1374
+ const opened = this.lineOf(block.start);
1375
+ if (this.lineStarts[opened] !== block.start && claimFrom(opened) !== -1) return pos;
1376
+ if (block.end >= lineEnd) return -1;
1377
+ pos = block.end;
1378
+ continue;
1379
+ }
1380
+ if (block.end >= lineEnd) return -1;
1381
+ pos = block.end;
1382
+ }
1383
+ };
1384
+ let k = this.lineCount - 1;
1385
+ let start = this.lineCount;
1386
+ let startPos = this.n;
1387
+ if (k >= 0 && this.isOpenTail(k)) {
1388
+ const at = claimFrom(k);
1389
+ if (at === -1) return this.n;
1390
+ start = k;
1391
+ startPos = at;
1392
+ k--;
1393
+ }
1394
+ for (; k >= 0; k--) {
1395
+ this.tick(1);
1396
+ const at = claimFrom(k);
1397
+ if (at === -1) break;
1398
+ if (this.lineIsBlank(k)) continue;
1399
+ if (!TREE_GLYPHS.test(this.lineText(k))) break;
1400
+ start = k;
1401
+ startPos = at;
1402
+ }
1403
+ let rootPos = startPos;
1404
+ for (let j = start - 1; j >= 0; j--) {
1405
+ const at = claimFrom(j);
1406
+ if (at === -1) break;
1407
+ const trimmed = this.lineText(j).trim();
1408
+ this.tick(trimmed.length + 1);
1409
+ if (!trimmed || isSplitterTreeLine(trimmed) || /^#{1,6}\s/.test(trimmed)) break;
1410
+ rootPos = at;
1411
+ if (at > this.lineStarts[j]) break;
1412
+ }
1413
+ return rootPos;
1414
+ }
1415
+ /** The block (so far) containing offset `pos`, or null. */
1416
+ blockAt(pos) {
1417
+ let lo = 0;
1418
+ let hi = this.blocks.length - 1;
1419
+ while (lo <= hi) {
1420
+ const mid = lo + hi >>> 1;
1421
+ const block = this.blocks[mid];
1422
+ if (pos < block.start) hi = mid - 1;
1423
+ else if (pos >= block.end) lo = mid + 1;
1424
+ else return block;
1425
+ }
1426
+ return null;
1427
+ }
1272
1428
  push(block) {
1273
1429
  if (block.end <= block.start) return;
1274
1430
  if (block.kind === "prose" && this.isBlank(block.start, block.end)) {
@@ -1472,7 +1628,10 @@ var Tokenizer = class {
1472
1628
  continue;
1473
1629
  }
1474
1630
  if (this.isOpenTail(k)) return this.heldFootnote(p, open[1]);
1475
- if (FOOTNOTE_DEF.test(line.trimStart()) || this.detectBlock(this.lineStarts[k], k, false)) break;
1631
+ if (FOOTNOTE_DEF.test(line.trimStart())) break;
1632
+ const block = this.detectBlock(this.lineStarts[k], k, false);
1633
+ if (block && this.mayDissolve(block)) return this.heldFootnote(p, open[1]);
1634
+ if (block) break;
1476
1635
  last = k;
1477
1636
  }
1478
1637
  const end = this.contentEnds[last];
@@ -1488,7 +1647,8 @@ var Tokenizer = class {
1488
1647
  return {
1489
1648
  count: this.lineCount,
1490
1649
  line: (k) => this.lineText(k),
1491
- openTail: (k) => this.isOpenTail(k)
1650
+ openTail: (k) => this.isOpenTail(k),
1651
+ streaming: this.streaming
1492
1652
  };
1493
1653
  }
1494
1654
  /**
@@ -1548,6 +1708,10 @@ var Tokenizer = class {
1548
1708
  if (fenceOpenerOf(this.lineText(j))?.char === "~") continue;
1549
1709
  const inner = this.detectBlockUncached(start, j, false);
1550
1710
  if (!inner) continue;
1711
+ if (this.streaming && inner.end >= tildeEnd && this.paragraphMayGrow(inner.end)) {
1712
+ this.overflowComplete = false;
1713
+ return this.n;
1714
+ }
1551
1715
  if (inner.end > end) {
1552
1716
  end = inner.end;
1553
1717
  this.overflowComplete = inner.complete;
@@ -1676,12 +1840,7 @@ var Tokenizer = class {
1676
1840
  return this.island("html_block", p, close.index + close[0].length, true, { tag: name });
1677
1841
  }
1678
1842
  const typeSix = HTML_BLOCK_LEVEL_TAGS.has(name);
1679
- const loneTag = atParaStart && t.trimEnd() === tag.raw;
1680
- if (loneTag && this.isOpenTail(li)) {
1681
- this.provisionalFrom = Math.min(this.provisionalFrom, p);
1682
- return null;
1683
- }
1684
- const typeSeven = loneTag;
1843
+ const typeSeven = atParaStart && t.trimEnd() === tag.raw;
1685
1844
  if (!typeSix && !typeSeven) return null;
1686
1845
  let end = this.contentEnds[li];
1687
1846
  for (let k = li + 1; k < this.lineCount && !this.lineIsBlank(k); k++) {
@@ -1739,6 +1898,13 @@ var Tokenizer = class {
1739
1898
  }
1740
1899
  return null;
1741
1900
  }
1901
+ if (this.isOpenTail(lastLine)) {
1902
+ if (isRecognizedJsonHead(this.text.slice(tp, tp + 512))) {
1903
+ return this.island("json", p, this.n, false, jsonMeta(this.text.slice(tp)));
1904
+ }
1905
+ this.provisionalFrom = Math.min(this.provisionalFrom, p);
1906
+ return null;
1907
+ }
1742
1908
  if (net !== 0) return null;
1743
1909
  const end = this.contentEnds[lastLine];
1744
1910
  const content = this.text.slice(tp, end).trim();
@@ -1773,8 +1939,10 @@ var Tokenizer = class {
1773
1939
  let breakLine = -1;
1774
1940
  for (let k = li + 1; k < this.lineCount; k++) {
1775
1941
  if (this.lineIsBlank(k)) break;
1776
- if (this.detectBlock(this.lineStarts[k], k, false)) {
1942
+ const block = this.detectBlock(this.lineStarts[k], k, false);
1943
+ if (block) {
1777
1944
  breakLine = k;
1945
+ if (this.mayDissolve(block)) this.undecidedBreaks.add(end);
1778
1946
  break;
1779
1947
  }
1780
1948
  end = this.contentEnds[k];
@@ -1812,7 +1980,7 @@ var Tokenizer = class {
1812
1980
  this.push({ kind: "prose", start: pos, end, island: null, inlines: scan2.inlines });
1813
1981
  return end;
1814
1982
  }
1815
- const br = scan2.breakIsland;
1983
+ const br = { ...scan2.breakIsland, fromParagraph: true };
1816
1984
  if (scan2.orphan) {
1817
1985
  let start = pos;
1818
1986
  while (this.blocks.length) {
@@ -1835,6 +2003,15 @@ var Tokenizer = class {
1835
2003
  this.push({ kind: "island", start: br.start, end: br.end, island: br, inlines: [] });
1836
2004
  return br.end;
1837
2005
  }
2006
+ /**
2007
+ * An island that opens inside a paragraph and closes past its end becomes a
2008
+ * block. Streaming: while the paragraph may still grow over its closer, the
2009
+ * island may yet be inline — undecided, so it runs open to the end (F3).
2010
+ */
2011
+ spill(type, start, end, pe, meta) {
2012
+ if (this.paragraphMayGrow(pe)) return this.island(type, start, this.n, false, meta);
2013
+ return this.island(type, start, end, true, meta);
2014
+ }
1838
2015
  scanInline(ps, pe) {
1839
2016
  const text = this.text;
1840
2017
  const inlines = [];
@@ -1869,6 +2046,9 @@ var Tokenizer = class {
1869
2046
  i = close + 2;
1870
2047
  continue;
1871
2048
  }
2049
+ if (close === -1 && this.streaming && this.paragraphMayGrow(pe)) {
2050
+ return { inlines, breakIsland: null, orphan: false };
2051
+ }
1872
2052
  }
1873
2053
  i += 2;
1874
2054
  continue;
@@ -1889,17 +2069,13 @@ var Tokenizer = class {
1889
2069
  i += 2;
1890
2070
  continue;
1891
2071
  }
1892
- if (close + 2 > pe) return stop(this.island("math_block", i, close + 2, true, { delimiter: "$$" }));
2072
+ if (close + 2 > pe) return stop(this.spill("math_block", i, close + 2, pe, { delimiter: "$$" }));
1893
2073
  inlines.push(this.island("math_inline", i, close + 2, true, { delimiter: "$$" }));
1894
2074
  i = close + 2;
1895
2075
  continue;
1896
2076
  }
1897
2077
  const end = singleDollarMathEnd(text, i, pe);
1898
2078
  this.tick(end > 0 ? end - i : Math.min(403, pe - i));
1899
- if (end > 0 && this.streaming && end === this.n) {
1900
- this.provisionalFrom = Math.min(this.provisionalFrom, i);
1901
- return { inlines, breakIsland: null, orphan: false };
1902
- }
1903
2079
  if (end > 0) {
1904
2080
  inlines.push(this.island("math_inline", i, end, true, { delimiter: "$" }));
1905
2081
  i = end;
@@ -1977,7 +2153,7 @@ var Tokenizer = class {
1977
2153
  const anchor = end - i <= 64 ? PINNED_ANCHOR_RE.exec(text.slice(i, end)) : null;
1978
2154
  const type = anchor ? "anchor" : "html_comment";
1979
2155
  const meta = anchor ? { id: anchor[1] } : {};
1980
- if (end > pe) return stop(this.island(type, i, end, true, meta));
2156
+ if (end > pe) return stop(this.spill(type, i, end, pe, meta));
1981
2157
  inlines.push(this.island(type, i, end, true, meta));
1982
2158
  i = end;
1983
2159
  continue;
@@ -2004,9 +2180,13 @@ var Tokenizer = class {
2004
2180
  const closer = `</${name}>`;
2005
2181
  const close = this.next(closer, tagEnd);
2006
2182
  const blockType = isAttr ? "xml_attr" : "xml_region";
2183
+ if (close === -1 && isThinking && !this.streaming) {
2184
+ i = tagEnd;
2185
+ continue;
2186
+ }
2007
2187
  if (close === -1) return stop(this.island(blockType, i, this.n, false, { tag: name }));
2008
2188
  const end = close + closer.length;
2009
- if (end > pe) return stop(this.island(blockType, i, end, true, { tag: name }));
2189
+ if (end > pe) return stop(this.spill(blockType, i, end, pe, { tag: name }));
2010
2190
  inlines.push(this.island("xml_inline", i, end, true, { tag: name }));
2011
2191
  i = end;
2012
2192
  continue;
@@ -2391,6 +2571,7 @@ function mapRange(changes, start, end, unit = "utf16") {
2391
2571
  return { start: newStart, end: newEnd, touched, collapsed: newEnd === newStart && end > start };
2392
2572
  }
2393
2573
 
2574
+ exports.AFTER_WINDOW = AFTER_WINDOW;
2394
2575
  exports.BacktickRuns = BacktickRuns;
2395
2576
  exports.CALLOUT_NAMES = CALLOUT_NAMES;
2396
2577
  exports.FENCE_OPENER_WHITESPACE = FENCE_OPENER_WHITESPACE;
@@ -2426,6 +2607,7 @@ exports.pairDisplayMath = pairDisplayMath;
2426
2607
  exports.parseFenceOpener = parseFenceOpener;
2427
2608
  exports.pendingDisplayMathOpener = pendingDisplayMathOpener;
2428
2609
  exports.readXmlTag = readXmlTag;
2610
+ exports.singleDollarAfter = singleDollarAfter;
2429
2611
  exports.singleDollarMathEnd = singleDollarMathEnd;
2430
2612
  exports.spliceSave = spliceSave;
2431
2613
  exports.splitsSurrogatePair = splitsSurrogatePair;