@ai-matrx/content-ir 0.18.0 → 0.18.2

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
@@ -394,7 +394,7 @@ function scan(text) {
394
394
  }
395
395
  const count = starts.length;
396
396
  const raw = (k) => text.slice(starts[k], ends[k]);
397
- const out = { ranges: [], paragraphs: [], endsInFence: false, pendingPara: -1 };
397
+ const out = { ranges: [], fences: [], paragraphs: [], endsInFence: false, pendingPara: -1 };
398
398
  const rawLines = { count, line: raw };
399
399
  let segment = 0;
400
400
  for (let k = 0; k < count; k++) {
@@ -403,6 +403,7 @@ function scan(text) {
403
403
  prose(text, starts, ends, raw, segment, k, out);
404
404
  const end = closeFence(rawLines, k, opener);
405
405
  out.ranges.push({ start: starts[k], end: ends[end.line], kind: "fence" });
406
+ out.fences.push([k, end.line, end.closed]);
406
407
  out.pendingPara = -1;
407
408
  if (!end.closed && end.line === count - 1) out.endsInFence = true;
408
409
  k = end.line;
@@ -468,6 +469,7 @@ function prose(text, starts, ends, raw, from, to, out) {
468
469
  };
469
470
  const end = closeFence(src, k, opener);
470
471
  out.ranges.push({ start: starts[k], end: ends[end.line], kind: "fence" });
472
+ out.fences.push([k, end.line, end.closed]);
471
473
  if (!end.closed && end.line === starts.length - 1) out.endsInFence = true;
472
474
  k = end.line;
473
475
  }
@@ -531,6 +533,45 @@ function findCodeRanges(text) {
531
533
  for (const [from, to] of s.paragraphs) codeSpans(text, from, to, ranges);
532
534
  return ranges.sort((a, b) => a.start - b.start);
533
535
  }
536
+ function mapCodeRanges(text, fn) {
537
+ let out = "";
538
+ let at = 0;
539
+ for (const r of findCodeRanges(text)) {
540
+ if (r.start < at) continue;
541
+ out += text.slice(at, r.start) + fn(r, text.slice(r.start, r.end));
542
+ at = r.end;
543
+ }
544
+ return out + text.slice(at);
545
+ }
546
+ function codeSpanText(raw) {
547
+ const ticks = backtickRunLength(raw, 0);
548
+ const inner = raw.slice(ticks, raw.length - ticks);
549
+ return inner.length >= 2 && inner.startsWith(" ") && inner.endsWith(" ") && inner.trim() !== "" ? inner.slice(1, -1) : inner;
550
+ }
551
+ function fenceParts(raw) {
552
+ const lines = raw.split("\n").map((l) => l.endsWith("\r") ? l.slice(0, -1) : l);
553
+ const { offset } = quotePrefix(lines[0] ?? "");
554
+ const opener = fenceOpenerOf((lines[0] ?? "").slice(offset).replace(/^ *(?:[-*+]|\d{1,9}[.)])(?= |\t|$) ?/, ""));
555
+ if (!opener) return null;
556
+ const rest = lines.slice(1);
557
+ const closed = rest.length > 0 && new FenceReader(opener, { nesting: false }).feed(rest[rest.length - 1]);
558
+ if (!closed) return { opener, body: rest.join("\n"), closed };
559
+ const last = rest[rest.length - 1];
560
+ const before = trimFenceLine(last).startsWith("```") ? "" : last.slice(0, last.indexOf("```"));
561
+ return { opener, body: [...rest.slice(0, -1), ...before.trim() ? [before] : []].join("\n"), closed };
562
+ }
563
+ function fenceLineKinds(text) {
564
+ const s = scan(text);
565
+ let count = 1;
566
+ for (let i = text.indexOf("\n"); i !== -1; i = text.indexOf("\n", i + 1)) count++;
567
+ const kinds = new Array(count).fill("prose");
568
+ for (const [open, last, closed] of s.fences) {
569
+ kinds[open] = "open";
570
+ for (let k = open + 1; k <= last; k++) kinds[k] = "body";
571
+ if (closed && last > open) kinds[last] = "close";
572
+ }
573
+ return kinds;
574
+ }
534
575
  function pendingCodeSpanStart(text) {
535
576
  const n = text.length;
536
577
  if (n === 0 || /\n[ \t]*\n[ \t]*$/.test(text)) return -1;
@@ -829,25 +870,20 @@ var XmlContainerTracker = class {
829
870
  inComment = false;
830
871
  inCdata = false;
831
872
  inlineTicks = 0;
832
- fenceMarker = null;
873
+ /** The open fence, read by THE one closer (source/code-ranges.ts). */
874
+ fence = null;
833
875
  consumeLine(line, startOffset = 0) {
834
- const fence = /^[ \t]*(`{3,}|~{3,})(.*)$/.exec(line);
835
- if (!this.inComment && !this.inCdata && this.inlineTicks === 0 && fence) {
836
- const marker = fence[1];
837
- const char = marker[0];
838
- const suffix = fence[2];
839
- if (marker.length >= 3) {
840
- if (!this.fenceMarker) {
841
- this.fenceMarker = { char, ticks: marker.length };
842
- return null;
843
- }
844
- if (char === this.fenceMarker.char && marker.length >= this.fenceMarker.ticks && suffix.trim() === "") {
845
- this.fenceMarker = null;
846
- }
876
+ if (this.fence) {
877
+ if (this.fence.feed(line)) this.fence = null;
878
+ return null;
879
+ }
880
+ if (!this.inComment && !this.inCdata && this.inlineTicks === 0) {
881
+ const opener = fenceOpenerOf(line);
882
+ if (opener) {
883
+ this.fence = new FenceReader(opener);
847
884
  return null;
848
885
  }
849
886
  }
850
- if (this.fenceMarker) return null;
851
887
  for (let i = startOffset; i < line.length; i++) {
852
888
  if (this.inlineTicks > 0) {
853
889
  if (line[i] === "`") {
@@ -2337,9 +2373,11 @@ function mapRange(changes, start, end, unit = "utf16") {
2337
2373
  return { start: newStart, end: newEnd, touched, collapsed: newEnd === newStart && end > start };
2338
2374
  }
2339
2375
 
2376
+ exports.BacktickRuns = BacktickRuns;
2340
2377
  exports.CALLOUT_NAMES = CALLOUT_NAMES;
2341
2378
  exports.FENCE_OPENER_WHITESPACE = FENCE_OPENER_WHITESPACE;
2342
2379
  exports.FENCE_WHITESPACE = FENCE_WHITESPACE;
2380
+ exports.FenceReader = FenceReader;
2343
2381
  exports.NESTING_FENCE_LANGUAGES = NESTING_FENCE_LANGUAGES;
2344
2382
  exports.SourceSpliceError = SourceSpliceError;
2345
2383
  exports.XmlContainerTracker = XmlContainerTracker;
@@ -2348,7 +2386,12 @@ exports.blockEdit = blockEdit;
2348
2386
  exports.blockRangeEdit = blockRangeEdit;
2349
2387
  exports.buildCodePointIndex = buildCodePointIndex;
2350
2388
  exports.classifyInnerFenceLine = classifyInnerFenceLine;
2389
+ exports.closeFence = closeFence;
2390
+ exports.codeSpanText = codeSpanText;
2391
+ exports.fenceLineKinds = fenceLineKinds;
2351
2392
  exports.fenceNestsInnerFences = fenceNestsInnerFences;
2393
+ exports.fenceOpenerOf = fenceOpenerOf;
2394
+ exports.fenceParts = fenceParts;
2352
2395
  exports.findCodeRanges = findCodeRanges;
2353
2396
  exports.isMathPlaceholder = isMathPlaceholder;
2354
2397
  exports.isSingleDollarMath = isSingleDollarMath;
@@ -2357,6 +2400,7 @@ exports.joinSource = joinSource;
2357
2400
  exports.listIslands = listIslands;
2358
2401
  exports.looksLikeDisplayMath = looksLikeDisplayMath;
2359
2402
  exports.looksLikeOpenInlineMath = looksLikeOpenInlineMath;
2403
+ exports.mapCodeRanges = mapCodeRanges;
2360
2404
  exports.mapPosition = mapPosition;
2361
2405
  exports.mapRange = mapRange;
2362
2406
  exports.pairDisplayMath = pairDisplayMath;