@ai-matrx/content-ir 0.14.3 → 0.15.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.cjs CHANGED
@@ -222,6 +222,24 @@ function trimFenceLine(line) {
222
222
  while (end > start && isFenceWhitespace(line[end - 1])) end--;
223
223
  return start === 0 && end === line.length ? line : line.slice(start, end);
224
224
  }
225
+ var FENCE_OPENER_WHITESPACE = " \n\v\f\r \xA0\u1680\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200A\u2028\u2029\u202F\u205F\u3000\uFEFF";
226
+ function isOpenerWhitespace(ch) {
227
+ return ch !== void 0 && ch !== "" && FENCE_OPENER_WHITESPACE.includes(ch);
228
+ }
229
+ function parseFenceOpener(line) {
230
+ let start = 0;
231
+ let end = line.length;
232
+ while (start < end && isOpenerWhitespace(line[start])) start++;
233
+ while (end > start && isOpenerWhitespace(line[end - 1])) end--;
234
+ let ticks = 0;
235
+ while (start + ticks < end && line[start + ticks] === "`") ticks++;
236
+ if (ticks < 3) return null;
237
+ let infoStart = start + ticks;
238
+ while (infoStart < end && isOpenerWhitespace(line[infoStart])) infoStart++;
239
+ let langEnd = infoStart;
240
+ while (langEnd < end && !isOpenerWhitespace(line[langEnd])) langEnd++;
241
+ return { ticks, lang: line.slice(infoStart, langEnd) };
242
+ }
225
243
  function classifyInnerFenceLine(trimmed, openTicks, nests, nestedDepth) {
226
244
  let ticks = 0;
227
245
  while (ticks < trimmed.length && trimmed[ticks] === "`") ticks++;
@@ -303,15 +321,20 @@ var STRUCTURAL_MARKDOWN = /\]\(|https?:\/\/|\*\*|(?:^|\n)[ \t]{0,3}#{1,6}[ \t]|(
303
321
  var LATEX_COMMAND = /\\[a-zA-Z]/;
304
322
  var PROSE_WORD = /[A-Za-z]{3,}/g;
305
323
  var PROSE_WORD_LIMIT = 6;
306
- function looksLikeDisplayMath(inner) {
324
+ function opensFlowBlock(text, open) {
325
+ for (let k = open - 1; k >= 0 && text[k] !== "\n"; k--) if (text[k] !== " " && text[k] !== " ") return false;
326
+ for (let k = open + 2; k < text.length && text[k] !== "\n"; k++) if (text[k] !== " " && text[k] !== " " && text[k] !== "\r") return false;
327
+ return true;
328
+ }
329
+ function looksLikeDisplayMath(inner, flow = false) {
307
330
  const s = inner.trim();
308
331
  if (!s) return false;
309
332
  if (STRUCTURAL_MARKDOWN.test(s)) return false;
310
333
  if (s.length > MAX_MATH_SPAN) return false;
334
+ if (!flow && /\n[ \t]*\n/.test(inner)) return false;
311
335
  if (LATEX_COMMAND.test(s)) {
312
336
  return (s.match(PROSE_WORD) ?? []).length < PROSE_WORD_LIMIT * 3;
313
337
  }
314
- if (/\n[ \t]*\n/.test(s)) return false;
315
338
  return (s.match(PROSE_WORD) ?? []).length < PROSE_WORD_LIMIT;
316
339
  }
317
340
  function codeRanges(text) {
@@ -345,7 +368,7 @@ function pairDisplayMath(text) {
345
368
  const open = tokens[j];
346
369
  const close = tokens[j + 1];
347
370
  if (close === void 0) break;
348
- if (looksLikeDisplayMath(text.slice(open + 2, close))) {
371
+ if (looksLikeDisplayMath(text.slice(open + 2, close), opensFlowBlock(text, open))) {
349
372
  pairs.set(open, close);
350
373
  j += 2;
351
374
  continue;
@@ -982,8 +1005,7 @@ var Tokenizer = class {
982
1005
  */
983
1006
  backtickFence(p, li, t) {
984
1007
  const openTicks = backtickRunLength(t, 0);
985
- const info = t.slice(openTicks).trim();
986
- const lang = info.split(/\s/)[0] ?? "";
1008
+ const lang = parseFenceOpener(t)?.lang ?? "";
987
1009
  const isJson = lang === "json";
988
1010
  const normalized = normalizeFenceLanguage(lang);
989
1011
  const meta = { fence: "`", ticks: openTicks, lang };
@@ -1113,6 +1135,7 @@ var Tokenizer = class {
1113
1135
  const endLine = this.lineOf(close);
1114
1136
  const endCe = this.contentEnds[endLine];
1115
1137
  if (endLine !== li && !this.isBlank(end, endCe)) return null;
1138
+ if (this.isOpenTail(endLine)) return this.island("math_block", p, this.n, false, { delimiter: "$$" });
1116
1139
  return this.island("math_block", p, end, true, { delimiter: "$$" });
1117
1140
  }
1118
1141
  /** `<artifact …>`, `<decision …>`, … at the start of a line. */
@@ -1442,7 +1465,7 @@ var Tokenizer = class {
1442
1465
  continue;
1443
1466
  }
1444
1467
  const end = singleDollarMathEnd(text, i, pe);
1445
- if (end > 0) {
1468
+ if (end > 0 && !(this.streaming && end === this.n)) {
1446
1469
  inlines.push(this.island("math_inline", i, end, true, { delimiter: "$" }));
1447
1470
  i = end;
1448
1471
  continue;
@@ -1852,11 +1875,17 @@ function spliceSave(original, edits, options = {}) {
1852
1875
  else newIslandsByStart.set(island.start, [island.raw]);
1853
1876
  }
1854
1877
  const disturbed = [];
1878
+ const carried = (island) => sorted.some((edit) => !edit.island && island.start >= edit.start && island.end <= edit.end);
1879
+ const changedInside = [];
1855
1880
  for (const island of islands) {
1856
1881
  if (replacedIslands.has(`${island.start}:${island.end}`)) continue;
1857
1882
  const mappedStart = mapPosition(changes, island.start, { assoc: 1 }).pos;
1858
1883
  const candidates = newIslandsByStart.get(mappedStart);
1859
1884
  if (!candidates || !candidates.includes(island.raw)) {
1885
+ if (carried(island)) {
1886
+ changedInside.push(island);
1887
+ continue;
1888
+ }
1860
1889
  disturbed.push({
1861
1890
  islandType: island.islandType,
1862
1891
  inline: island.inline,
@@ -1866,6 +1895,14 @@ function spliceSave(original, edits, options = {}) {
1866
1895
  });
1867
1896
  }
1868
1897
  }
1898
+ if (changedInside.length > 0) {
1899
+ const first = changedInside[0];
1900
+ const name = first.meta.tag ?? first.meta.name ?? first.meta.lang ?? "";
1901
+ throw new SourceSpliceError(
1902
+ "island_edit",
1903
+ `the edit changes the protected ${first.islandType}${name ? ` (${String(name)})` : ""} island at [${first.start}, ${first.end}) \u2014 its bytes survive only as ordinary text${changedInside.length > 1 ? ` (and ${changedInside.length - 1} more)` : ""}; islands change only through islandEdit()`
1904
+ );
1905
+ }
1869
1906
  const integrity = {
1870
1907
  ok: bytesOutsideEditsIdentical && disturbed.length === 0,
1871
1908
  bytesOutsideEditsIdentical,
@@ -1918,6 +1955,7 @@ function mapRange(changes, start, end, unit = "utf16") {
1918
1955
  }
1919
1956
 
1920
1957
  exports.CALLOUT_NAMES = CALLOUT_NAMES;
1958
+ exports.FENCE_OPENER_WHITESPACE = FENCE_OPENER_WHITESPACE;
1921
1959
  exports.FENCE_WHITESPACE = FENCE_WHITESPACE;
1922
1960
  exports.NESTING_FENCE_LANGUAGES = NESTING_FENCE_LANGUAGES;
1923
1961
  exports.SourceSpliceError = SourceSpliceError;
@@ -1936,6 +1974,7 @@ exports.looksLikeDisplayMath = looksLikeDisplayMath;
1936
1974
  exports.mapPosition = mapPosition;
1937
1975
  exports.mapRange = mapRange;
1938
1976
  exports.pairDisplayMath = pairDisplayMath;
1977
+ exports.parseFenceOpener = parseFenceOpener;
1939
1978
  exports.readXmlTag = readXmlTag;
1940
1979
  exports.singleDollarMathEnd = singleDollarMathEnd;
1941
1980
  exports.spliceSave = spliceSave;