@stream-mdx/worker 0.1.1 → 0.2.0

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.
@@ -17946,7 +17946,7 @@ function getGroupContents(expression, contentsStartPos) {
17946
17946
  return expression.slice(contentsStartPos, contentsEndPos);
17947
17947
  }
17948
17948
 
17949
- // ../../node_modules/regex/src/subclass.js
17949
+ // node_modules/regex/src/subclass.js
17950
17950
  var emulationGroupMarker = "$E$";
17951
17951
  var RegExpSubclass = class _RegExpSubclass extends RegExp {
17952
17952
  /**
@@ -18059,13 +18059,13 @@ function unmarkEmulationGroups(expression) {
18059
18059
  };
18060
18060
  }
18061
18061
 
18062
- // ../../node_modules/regex/src/utils-internals.js
18062
+ // node_modules/regex/src/utils-internals.js
18063
18063
  var noncapturingDelim = String.raw`\(\?(?:[:=!>A-Za-z\-]|<[=!]|\(DEFINE\))`;
18064
18064
  function spliceStr(str, pos, oldValue, newValue) {
18065
18065
  return str.slice(0, pos) + newValue + str.slice(pos + oldValue.length);
18066
18066
  }
18067
18067
 
18068
- // ../../node_modules/regex/src/atomic.js
18068
+ // node_modules/regex/src/atomic.js
18069
18069
  var atomicPluginToken = new RegExp(String.raw`(?<noncapturingStart>${noncapturingDelim})|(?<capturingStart>\((?:\?<[^>]+>)?)|\\?.`, "gsu");
18070
18070
  function atomic(expression, data2) {
18071
18071
  if (!/\(\?>/.test(expression)) {
@@ -18206,7 +18206,7 @@ function possessive(expression) {
18206
18206
  return expression;
18207
18207
  }
18208
18208
 
18209
- // ../../node_modules/regex-recursion/src/index.js
18209
+ // node_modules/regex-recursion/src/index.js
18210
18210
  var r2 = String.raw;
18211
18211
  var gRToken = r2`\\g<(?<gRNameOrNum>[^>&]+)&R=(?<gRDepth>[^>]+)>`;
18212
18212
  var recursiveToken = r2`\(\?R=(?<rDepth>[^\)]+)\)|${gRToken}`;
@@ -18356,7 +18356,7 @@ function emulationGroupMarkerLength(expression, index2) {
18356
18356
  return match ? match[0].length : 0;
18357
18357
  }
18358
18358
 
18359
- // ../../node_modules/oniguruma-to-es/dist/esm/index.js
18359
+ // node_modules/oniguruma-to-es/dist/esm/index.js
18360
18360
  var cp = String.fromCodePoint;
18361
18361
  var r3 = String.raw;
18362
18362
  var envSupportsFlagGroups = (() => {
@@ -21379,7 +21379,7 @@ function toRegExp(pattern, options) {
21379
21379
  return new RegExp(result.pattern, result.flags);
21380
21380
  }
21381
21381
 
21382
- // ../../node_modules/@shikijs/engine-javascript/dist/shared/engine-javascript.hzpS1_41.mjs
21382
+ // node_modules/@shikijs/engine-javascript/dist/shared/engine-javascript.hzpS1_41.mjs
21383
21383
  var MAX = 4294967295;
21384
21384
  var JavaScriptScanner = class {
21385
21385
  constructor(patterns, options = {}) {
@@ -21472,7 +21472,7 @@ var JavaScriptScanner = class {
21472
21472
  }
21473
21473
  };
21474
21474
 
21475
- // ../../node_modules/@shikijs/engine-javascript/dist/engine-compile.mjs
21475
+ // node_modules/@shikijs/engine-javascript/dist/engine-compile.mjs
21476
21476
  function defaultJavaScriptRegexConstructor(pattern, options) {
21477
21477
  return toRegExp(
21478
21478
  pattern,
@@ -43324,6 +43324,22 @@ function manualExtractHighlightedLines(html9, fallbackLength) {
43324
43324
  }
43325
43325
  return normalizeHighlightedLines(lines, fallbackLength);
43326
43326
  }
43327
+ function getDefaultCodeWrapperAttributes(lang236, themes = { dark: "github-dark", light: "github-light" }) {
43328
+ const language = lang236 && typeof lang236 === "string" && lang236.length > 0 ? lang236 : "text";
43329
+ const themeLabel = `${themes.dark} ${themes.light}`;
43330
+ return {
43331
+ preAttrs: {
43332
+ class: `shiki shiki-themes ${themeLabel}`,
43333
+ "data-language": language,
43334
+ style: "--shiki-dark-bg: transparent; --shiki-light-bg: transparent"
43335
+ },
43336
+ codeAttrs: {
43337
+ "data-language": language,
43338
+ "data-theme": themeLabel,
43339
+ style: "display: grid;"
43340
+ }
43341
+ };
43342
+ }
43327
43343
  function dedentIndentedCode(raw2) {
43328
43344
  if (!raw2) return "";
43329
43345
  const normalized = normalizeNewlines(raw2);
@@ -43697,6 +43713,128 @@ function splitTextByRegexWithPrecedence(text12, regex3, toNode3) {
43697
43713
  }
43698
43714
  return result;
43699
43715
  }
43716
+ var DEFAULT_FORMAT_ANTICIPATION = {
43717
+ inline: false,
43718
+ mathInline: false,
43719
+ mathBlock: false,
43720
+ html: false,
43721
+ mdx: false,
43722
+ regex: false
43723
+ };
43724
+ function normalizeFormatAnticipation(input) {
43725
+ if (input === true) {
43726
+ return { ...DEFAULT_FORMAT_ANTICIPATION, inline: true };
43727
+ }
43728
+ if (!input) {
43729
+ return { ...DEFAULT_FORMAT_ANTICIPATION };
43730
+ }
43731
+ return {
43732
+ inline: input.inline ?? false,
43733
+ mathInline: input.mathInline ?? false,
43734
+ mathBlock: input.mathBlock ?? false,
43735
+ html: input.html ?? false,
43736
+ mdx: input.mdx ?? false,
43737
+ regex: input.regex ?? false
43738
+ };
43739
+ }
43740
+ function prepareInlineStreamingContent(content4, options) {
43741
+ const enableMath2 = options?.math !== false;
43742
+ const anticipation = normalizeFormatAnticipation(options?.formatAnticipation);
43743
+ const enableInlineAnticipation = anticipation.inline;
43744
+ const enableMathInlineAnticipation = anticipation.mathInline;
43745
+ const enableMathBlockAnticipation = anticipation.mathBlock;
43746
+ const stack = [];
43747
+ const toggleToken = (token2) => {
43748
+ const last = stack[stack.length - 1];
43749
+ if (last === token2) {
43750
+ stack.pop();
43751
+ } else {
43752
+ stack.push(token2);
43753
+ }
43754
+ };
43755
+ let mathDisplayOpen = false;
43756
+ let mathDisplayCrossedNewline = false;
43757
+ for (let i = 0; i < content4.length; i++) {
43758
+ const code4 = content4.charCodeAt(i);
43759
+ if (code4 === 10 || code4 === 13) {
43760
+ if (mathDisplayOpen) {
43761
+ mathDisplayCrossedNewline = true;
43762
+ }
43763
+ continue;
43764
+ }
43765
+ if (code4 === 96) {
43766
+ toggleToken("code");
43767
+ continue;
43768
+ }
43769
+ if (code4 === 126 && i + 1 < content4.length && content4.charCodeAt(i + 1) === 126) {
43770
+ toggleToken("strike");
43771
+ i += 1;
43772
+ continue;
43773
+ }
43774
+ if (code4 === 42) {
43775
+ if (i + 1 < content4.length && content4.charCodeAt(i + 1) === 42) {
43776
+ toggleToken("strong");
43777
+ i += 1;
43778
+ } else {
43779
+ toggleToken("em");
43780
+ }
43781
+ continue;
43782
+ }
43783
+ if (enableMath2 && code4 === 36) {
43784
+ if (i + 1 < content4.length && content4.charCodeAt(i + 1) === 36) {
43785
+ toggleToken("math-display");
43786
+ if (mathDisplayOpen) {
43787
+ mathDisplayOpen = false;
43788
+ mathDisplayCrossedNewline = false;
43789
+ } else {
43790
+ mathDisplayOpen = true;
43791
+ mathDisplayCrossedNewline = false;
43792
+ }
43793
+ i += 1;
43794
+ } else {
43795
+ toggleToken("math-inline");
43796
+ }
43797
+ }
43798
+ }
43799
+ const hasIncompleteFormatting = stack.some((token2) => token2 === "code" || token2 === "strike" || token2 === "strong" || token2 === "em");
43800
+ const hasIncompleteMathInline = stack.includes("math-inline");
43801
+ const hasIncompleteMathDisplay = stack.includes("math-display");
43802
+ const hasIncompleteMath = hasIncompleteMathInline || hasIncompleteMathDisplay;
43803
+ if (enableMath2 && hasIncompleteMath) {
43804
+ if (hasIncompleteMathInline && !enableMathInlineAnticipation) {
43805
+ return { kind: "raw", status: "raw", reason: "incomplete-math" };
43806
+ }
43807
+ if (hasIncompleteMathDisplay && (!enableMathBlockAnticipation || mathDisplayCrossedNewline)) {
43808
+ return { kind: "raw", status: "raw", reason: "incomplete-math" };
43809
+ }
43810
+ }
43811
+ if (hasIncompleteFormatting && !enableInlineAnticipation) {
43812
+ return { kind: "raw", status: "raw", reason: "incomplete-formatting" };
43813
+ }
43814
+ if (!hasIncompleteFormatting && !hasIncompleteMath) {
43815
+ return { kind: "parse", status: "complete", content: content4, appended: "" };
43816
+ }
43817
+ const appendForToken = (token2) => {
43818
+ switch (token2) {
43819
+ case "code":
43820
+ return "`";
43821
+ case "strike":
43822
+ return "~~";
43823
+ case "strong":
43824
+ return "**";
43825
+ case "em":
43826
+ return "*";
43827
+ case "math-inline":
43828
+ return "$";
43829
+ case "math-display":
43830
+ return "$$";
43831
+ default:
43832
+ return "";
43833
+ }
43834
+ };
43835
+ const appended = stack.slice().reverse().map((token2) => appendForToken(token2)).join("");
43836
+ return { kind: "parse", status: "anticipated", content: content4 + appended, appended };
43837
+ }
43700
43838
  var rehypeSanitizeModule = rehype_sanitize_exports;
43701
43839
  var defaultSchema2 = rehypeSanitizeModule.defaultSchema;
43702
43840
  var resolvePlugin = (mod) => {
@@ -43871,6 +44009,8 @@ function splitByTagSegments(source, baseOffset, parseInline, options) {
43871
44009
  const mdxAutoClose = options?.mdx?.autoClose === true;
43872
44010
  const mdxMaxNewlines = normalizeNewlineLimit(options?.mdx?.maxNewlines);
43873
44011
  const mdxAllowlist = normalizeComponentAllowlist(options?.mdx?.componentAllowlist);
44012
+ const protectedRanges = options?.protectedRanges ?? [];
44013
+ const protectedKinds = protectedRanges.length ? new Set(options?.protectedRangeKinds ?? ["math-inline", "math-display", "code-inline", "code-block", "autolink"]) : null;
43874
44014
  while (match !== null) {
43875
44015
  const start2 = match.index;
43876
44016
  const tagName = match[1];
@@ -43885,6 +44025,18 @@ function splitByTagSegments(source, baseOffset, parseInline, options) {
43885
44025
  continue;
43886
44026
  }
43887
44027
  let end = tagPattern.lastIndex;
44028
+ if (protectedKinds && protectedRanges.length > 0) {
44029
+ const absoluteStart = baseIsFinite ? baseOffset + start2 : start2;
44030
+ const absoluteEnd = baseIsFinite ? baseOffset + end : end;
44031
+ const covered = protectedRanges.some(
44032
+ (range2) => protectedKinds.has(range2.kind) && typeof range2.from === "number" && typeof range2.to === "number" && range2.from <= absoluteStart && range2.to >= absoluteEnd
44033
+ );
44034
+ if (covered) {
44035
+ tagPattern.lastIndex = start2 + 1;
44036
+ match = tagPattern.exec(source);
44037
+ continue;
44038
+ }
44039
+ }
43888
44040
  if (!isSelfClosing && !mdxAllowed) {
43889
44041
  const closingIndex = findClosingHtmlTag(lowerSource, tagName.toLowerCase(), end);
43890
44042
  if (closingIndex === -1) {
@@ -44164,23 +44316,33 @@ function normalizeLang(raw2) {
44164
44316
  }
44165
44317
  function detectMDX(content4, options) {
44166
44318
  const inlineCodeRanges = collectInlineCodeRanges(content4);
44319
+ const protectedRanges = options?.protectedRanges ?? [];
44320
+ const baseOffset = typeof options?.baseOffset === "number" ? options.baseOffset : 0;
44321
+ const protectedKinds = protectedRanges.length ? /* @__PURE__ */ new Set(["math-inline", "math-display", "code-inline", "code-block", "html-inline", "html-block", "autolink"]) : null;
44167
44322
  const componentPattern = /<([A-Z][\w-]*)(\s|\/?>)/g;
44168
44323
  let componentMatch = componentPattern.exec(content4);
44169
44324
  while (componentMatch !== null) {
44170
44325
  const start2 = componentMatch.index;
44171
44326
  const end = start2 + componentMatch[0].length;
44172
- if (!isWithinRanges(start2, end, inlineCodeRanges)) {
44173
- return true;
44327
+ if (isWithinRanges(start2, end, inlineCodeRanges)) {
44328
+ componentMatch = componentPattern.exec(content4);
44329
+ continue;
44174
44330
  }
44175
- componentMatch = componentPattern.exec(content4);
44331
+ if (protectedKinds) {
44332
+ const absoluteStart = baseOffset + start2;
44333
+ const absoluteEnd = baseOffset + end;
44334
+ const covered = protectedRanges.some((range2) => protectedKinds.has(range2.kind) && range2.from <= absoluteStart && range2.to >= absoluteEnd);
44335
+ if (covered) {
44336
+ componentMatch = componentPattern.exec(content4);
44337
+ continue;
44338
+ }
44339
+ }
44340
+ return true;
44176
44341
  }
44177
44342
  if (/(^|\n)\s*(import|export)\s/.test(content4)) {
44178
44343
  return true;
44179
44344
  }
44180
44345
  const expressionPattern = /\{[^{}]+\}/g;
44181
- const protectedRanges = options?.protectedRanges ?? [];
44182
- const baseOffset = typeof options?.baseOffset === "number" ? options.baseOffset : 0;
44183
- const protectedKinds = protectedRanges.length ? /* @__PURE__ */ new Set(["math-inline", "math-display", "code-inline", "code-block"]) : null;
44184
44346
  for (let match = expressionPattern.exec(content4); match !== null; match = expressionPattern.exec(content4)) {
44185
44347
  const index2 = match.index;
44186
44348
  const prev = index2 > 0 ? content4[index2 - 1] : "";
@@ -44397,7 +44559,12 @@ function buildListItemSnapshot(block, listItemNode, ordered, index2, id, baseOff
44397
44559
  }
44398
44560
  if (name3 === "Paragraph") {
44399
44561
  const paragraphRaw = raw2.slice(cursor.from, cursor.to);
44400
- const paragraphData = processListItemParagraph(paragraphRaw);
44562
+ const meta2 = block.payload.meta ?? {};
44563
+ const paragraphData = processListItemParagraph(paragraphRaw, {
44564
+ formatAnticipation: meta2.formatAnticipation,
44565
+ math: meta2.mathEnabled,
44566
+ streaming: !block.isFinalized
44567
+ });
44401
44568
  const parsedInline = paragraphData.inline;
44402
44569
  if (!paragraphHandled) {
44403
44570
  inlineNodes = parsedInline;
@@ -44429,7 +44596,10 @@ function buildListItemSnapshot(block, listItemNode, ordered, index2, id, baseOff
44429
44596
  } else if (name3 === "BulletList" || name3 === "OrderedList") {
44430
44597
  const nestedId = `${id}::list:${subListIndex++}`;
44431
44598
  const nestedOrdered = name3 === "OrderedList";
44432
- childSnapshots.push(buildListNodeSnapshot(block, cursor.node, nestedOrdered, nestedId, baseOffset, raw2));
44599
+ const nestedSnapshot = buildListNodeSnapshot(block, cursor.node, nestedOrdered, nestedId, baseOffset, raw2);
44600
+ if (Array.isArray(nestedSnapshot.children) && nestedSnapshot.children.length > 0) {
44601
+ childSnapshots.push(nestedSnapshot);
44602
+ }
44433
44603
  } else if (name3 === "Blockquote") {
44434
44604
  const quoteId = `${id}::blockquote:${blockquoteIndex++}`;
44435
44605
  childSnapshots.push(buildBlockquoteSnapshot(block, cursor.node, quoteId, baseOffset, raw2));
@@ -44465,11 +44635,31 @@ function buildListItemSnapshot(block, listItemNode, ordered, index2, id, baseOff
44465
44635
  };
44466
44636
  return itemSnapshot;
44467
44637
  }
44468
- function processListItemParagraph(raw2) {
44638
+ function parseListInline(raw2, options) {
44639
+ if (!options?.streaming || !options.formatAnticipation) {
44640
+ return listInlineParser.parse(raw2);
44641
+ }
44642
+ const prepared = prepareInlineStreamingContent(raw2, { formatAnticipation: options.formatAnticipation, math: options.math });
44643
+ if (prepared.kind === "raw") {
44644
+ return [{ kind: "text", text: raw2 }];
44645
+ }
44646
+ let preparedContent = prepared.content;
44647
+ let appended = prepared.appended;
44648
+ const normalized = normalizeFormatAnticipation(options.formatAnticipation);
44649
+ if (normalized.regex) {
44650
+ const regexAppend = listInlineParser.getRegexAnticipationAppend(raw2);
44651
+ if (regexAppend) {
44652
+ preparedContent += regexAppend;
44653
+ appended += regexAppend;
44654
+ }
44655
+ }
44656
+ return listInlineParser.parse(preparedContent, { cache: false });
44657
+ }
44658
+ function processListItemParagraph(raw2, options) {
44469
44659
  const normalized = normalizeParagraphText(raw2);
44470
44660
  const { content: content4, task } = stripTaskMarker(normalized);
44471
- const inline = listInlineParser.parse(content4);
44472
- const segments = extractMixedContentSegments(content4, void 0, (value) => listInlineParser.parse(value));
44661
+ const inline = parseListInline(content4, options);
44662
+ const segments = extractMixedContentSegments(content4, void 0, (value) => parseListInline(value, options));
44473
44663
  return {
44474
44664
  inline,
44475
44665
  segments,
@@ -44754,28 +44944,82 @@ function enrichTableSnapshot(block, snapshot) {
44754
44944
  function enrichCodeSnapshot(block, snapshot) {
44755
44945
  const source = typeof block.payload.meta?.code === "string" ? block.payload.meta?.code : block.payload.raw ?? "";
44756
44946
  const lines = extractCodeLines(source);
44947
+ const meta2 = block.payload.meta;
44757
44948
  const highlightedHtml = block.payload.highlightedHtml ?? "";
44758
- const highlightedLines = extractHighlightedLines(highlightedHtml, lines.length);
44759
- const { preAttrs, codeAttrs } = extractCodeWrapperAttributes(highlightedHtml);
44760
- const lang236 = typeof block.payload.meta?.lang === "string" ? String(block.payload.meta?.lang) : void 0;
44949
+ const hasBlockHighlight = typeof block.payload.highlightedHtml === "string" && block.payload.highlightedHtml.length > 0;
44950
+ const metaLines = Array.isArray(meta2?.highlightedLines) ? meta2?.highlightedLines : null;
44951
+ const includeLineHtml = metaLines ? true : !hasBlockHighlight || lines.length >= 200;
44952
+ const highlightedLines = metaLines ? normalizeHighlightedLines(metaLines, lines.length) : extractHighlightedLines(highlightedHtml, lines.length);
44953
+ const metaTokenLines = Array.isArray(meta2?.tokenLines) ? meta2?.tokenLines : null;
44954
+ const tokenLines = metaTokenLines ? normalizeTokenLines(metaTokenLines, lines.length) : null;
44955
+ const metaDiffKind = Array.isArray(meta2?.diffKind) ? meta2?.diffKind : null;
44956
+ const diffKindLines = metaDiffKind ? normalizeOptionalArray(metaDiffKind, lines.length) : null;
44957
+ const metaOldNo = Array.isArray(meta2?.oldNo) ? meta2?.oldNo : null;
44958
+ const oldNoLines = metaOldNo ? normalizeOptionalArray(metaOldNo, lines.length) : null;
44959
+ const metaNewNo = Array.isArray(meta2?.newNo) ? meta2?.newNo : null;
44960
+ const newNoLines = metaNewNo ? normalizeOptionalArray(metaNewNo, lines.length) : null;
44961
+ const lang236 = typeof meta2?.lang === "string" ? String(meta2.lang) : void 0;
44962
+ let { preAttrs, codeAttrs } = extractCodeWrapperAttributes(highlightedHtml);
44963
+ if (!preAttrs || !codeAttrs) {
44964
+ const defaults = getDefaultCodeWrapperAttributes(lang236);
44965
+ preAttrs = preAttrs ?? defaults.preAttrs;
44966
+ codeAttrs = codeAttrs ?? defaults.codeAttrs;
44967
+ }
44761
44968
  snapshot.props = {
44762
44969
  ...snapshot.props ?? {},
44763
44970
  lang: lang236,
44764
44971
  preAttrs,
44765
44972
  codeAttrs
44766
44973
  };
44767
- snapshot.children = lines.map((line, index2) => ({
44768
- id: `${block.id}::line:${index2}`,
44769
- type: "code-line",
44770
- props: {
44974
+ snapshot.children = lines.map((line, index2) => {
44975
+ const props = {
44771
44976
  index: index2,
44772
44977
  text: line,
44773
- html: highlightedLines[index2] ?? null
44774
- },
44775
- children: []
44776
- }));
44978
+ html: includeLineHtml ? highlightedLines[index2] ?? null : null
44979
+ };
44980
+ if (tokenLines) {
44981
+ props.tokens = tokenLines[index2] ?? null;
44982
+ }
44983
+ if (diffKindLines) {
44984
+ props.diffKind = diffKindLines[index2] ?? null;
44985
+ }
44986
+ if (oldNoLines) {
44987
+ props.oldNo = oldNoLines[index2] ?? null;
44988
+ }
44989
+ if (newNoLines) {
44990
+ props.newNo = newNoLines[index2] ?? null;
44991
+ }
44992
+ return {
44993
+ id: `${block.id}::line:${index2}`,
44994
+ type: "code-line",
44995
+ props,
44996
+ children: []
44997
+ };
44998
+ });
44777
44999
  return snapshot;
44778
45000
  }
45001
+ function normalizeTokenLines(lines, fallbackLength) {
45002
+ if (!lines || lines.length === 0) {
45003
+ return new Array(Math.max(0, fallbackLength)).fill(null);
45004
+ }
45005
+ const length = Math.max(fallbackLength, lines.length);
45006
+ const result = new Array(length).fill(null);
45007
+ for (let i = 0; i < lines.length; i++) {
45008
+ result[i] = lines[i] ?? null;
45009
+ }
45010
+ return result;
45011
+ }
45012
+ function normalizeOptionalArray(lines, fallbackLength) {
45013
+ if (!lines || lines.length === 0) {
45014
+ return new Array(Math.max(0, fallbackLength)).fill(null);
45015
+ }
45016
+ const length = Math.max(fallbackLength, lines.length);
45017
+ const result = new Array(length).fill(null);
45018
+ for (let i = 0; i < lines.length; i++) {
45019
+ result[i] = lines[i] ?? null;
45020
+ }
45021
+ return result;
45022
+ }
44779
45023
  function cloneInlineNodes(nodes) {
44780
45024
  return nodes.map((node2) => {
44781
45025
  if ("children" in node2 && Array.isArray(node2.children)) {
@@ -44892,128 +45136,6 @@ function computeHeavyPatchBudget(credit, config = DEFAULT_BACKPRESSURE_CONFIG) {
44892
45136
  return Math.min(config.maxHeavyPatchBudget, budget);
44893
45137
  }
44894
45138
  var USE_LINEAR_COALESCING = typeof process === "undefined" ? true : process.env.V2_USE_LINEAR_COALESCING !== "false" && true;
44895
- var DEFAULT_FORMAT_ANTICIPATION = {
44896
- inline: false,
44897
- mathInline: false,
44898
- mathBlock: false,
44899
- html: false,
44900
- mdx: false,
44901
- regex: false
44902
- };
44903
- function normalizeFormatAnticipation(input) {
44904
- if (input === true) {
44905
- return { ...DEFAULT_FORMAT_ANTICIPATION, inline: true };
44906
- }
44907
- if (!input) {
44908
- return { ...DEFAULT_FORMAT_ANTICIPATION };
44909
- }
44910
- return {
44911
- inline: input.inline ?? false,
44912
- mathInline: input.mathInline ?? false,
44913
- mathBlock: input.mathBlock ?? false,
44914
- html: input.html ?? false,
44915
- mdx: input.mdx ?? false,
44916
- regex: input.regex ?? false
44917
- };
44918
- }
44919
- function prepareInlineStreamingContent(content4, options) {
44920
- const enableMath2 = options?.math !== false;
44921
- const anticipation = normalizeFormatAnticipation(options?.formatAnticipation);
44922
- const enableInlineAnticipation = anticipation.inline;
44923
- const enableMathInlineAnticipation = anticipation.mathInline;
44924
- const enableMathBlockAnticipation = anticipation.mathBlock;
44925
- const stack = [];
44926
- const toggleToken = (token2) => {
44927
- const last = stack[stack.length - 1];
44928
- if (last === token2) {
44929
- stack.pop();
44930
- } else {
44931
- stack.push(token2);
44932
- }
44933
- };
44934
- let mathDisplayOpen = false;
44935
- let mathDisplayCrossedNewline = false;
44936
- for (let i = 0; i < content4.length; i++) {
44937
- const code4 = content4.charCodeAt(i);
44938
- if (code4 === 10 || code4 === 13) {
44939
- if (mathDisplayOpen) {
44940
- mathDisplayCrossedNewline = true;
44941
- }
44942
- continue;
44943
- }
44944
- if (code4 === 96) {
44945
- toggleToken("code");
44946
- continue;
44947
- }
44948
- if (code4 === 126 && i + 1 < content4.length && content4.charCodeAt(i + 1) === 126) {
44949
- toggleToken("strike");
44950
- i += 1;
44951
- continue;
44952
- }
44953
- if (code4 === 42) {
44954
- if (i + 1 < content4.length && content4.charCodeAt(i + 1) === 42) {
44955
- toggleToken("strong");
44956
- i += 1;
44957
- } else {
44958
- toggleToken("em");
44959
- }
44960
- continue;
44961
- }
44962
- if (enableMath2 && code4 === 36) {
44963
- if (i + 1 < content4.length && content4.charCodeAt(i + 1) === 36) {
44964
- toggleToken("math-display");
44965
- if (mathDisplayOpen) {
44966
- mathDisplayOpen = false;
44967
- mathDisplayCrossedNewline = false;
44968
- } else {
44969
- mathDisplayOpen = true;
44970
- mathDisplayCrossedNewline = false;
44971
- }
44972
- i += 1;
44973
- } else {
44974
- toggleToken("math-inline");
44975
- }
44976
- }
44977
- }
44978
- const hasIncompleteFormatting = stack.some((token2) => token2 === "code" || token2 === "strike" || token2 === "strong" || token2 === "em");
44979
- const hasIncompleteMathInline = stack.includes("math-inline");
44980
- const hasIncompleteMathDisplay = stack.includes("math-display");
44981
- const hasIncompleteMath = hasIncompleteMathInline || hasIncompleteMathDisplay;
44982
- if (enableMath2 && hasIncompleteMath) {
44983
- if (hasIncompleteMathInline && !enableMathInlineAnticipation) {
44984
- return { kind: "raw", status: "raw", reason: "incomplete-math" };
44985
- }
44986
- if (hasIncompleteMathDisplay && (!enableMathBlockAnticipation || mathDisplayCrossedNewline)) {
44987
- return { kind: "raw", status: "raw", reason: "incomplete-math" };
44988
- }
44989
- }
44990
- if (hasIncompleteFormatting && !enableInlineAnticipation) {
44991
- return { kind: "raw", status: "raw", reason: "incomplete-formatting" };
44992
- }
44993
- if (!hasIncompleteFormatting && !hasIncompleteMath) {
44994
- return { kind: "parse", status: "complete", content: content4, appended: "" };
44995
- }
44996
- const appendForToken = (token2) => {
44997
- switch (token2) {
44998
- case "code":
44999
- return "`";
45000
- case "strike":
45001
- return "~~";
45002
- case "strong":
45003
- return "**";
45004
- case "em":
45005
- return "*";
45006
- case "math-inline":
45007
- return "$";
45008
- case "math-display":
45009
- return "$$";
45010
- default:
45011
- return "";
45012
- }
45013
- };
45014
- const appended = stack.slice().reverse().map((token2) => appendForToken(token2)).join("");
45015
- return { kind: "parse", status: "anticipated", content: content4 + appended, appended };
45016
- }
45017
45139
 
45018
45140
  // ../markdown-v2-core/dist/perf/patch-batching.mjs
45019
45141
  var LIGHT_APPEND_LINE_THRESHOLD = 4;
@@ -45679,9 +45801,26 @@ var MDXDetectionPlugin = {
45679
45801
  if (block.type !== "paragraph" && block.type !== "html") continue;
45680
45802
  const raw2 = block.payload.raw;
45681
45803
  const blockRange = block.payload.range;
45682
- const baseOffset = typeof blockRange?.from === "number" ? blockRange.from : 0;
45683
- const relevantProtected = filterProtectedRanges(ctx.protectedRanges, baseOffset, baseOffset + raw2.length);
45684
- if (detectMDX(raw2, { protectedRanges: relevantProtected, baseOffset })) {
45804
+ const baseOffset = typeof blockRange?.from === "number" ? blockRange.from : null;
45805
+ let protectedBase = baseOffset ?? 0;
45806
+ let relevantProtected = filterProtectedRanges(ctx.protectedRanges, protectedBase, protectedBase + raw2.length);
45807
+ if (relevantProtected.length === 0) {
45808
+ const metaProtected = Array.isArray(block.payload.meta?.protectedRanges) ? block.payload.meta.protectedRanges ?? [] : [];
45809
+ if (metaProtected.length > 0) {
45810
+ if (baseOffset === null) {
45811
+ protectedBase = 0;
45812
+ relevantProtected = metaProtected;
45813
+ } else {
45814
+ protectedBase = baseOffset;
45815
+ relevantProtected = metaProtected.map((range2) => ({
45816
+ ...range2,
45817
+ from: baseOffset + range2.from,
45818
+ to: baseOffset + range2.to
45819
+ }));
45820
+ }
45821
+ }
45822
+ }
45823
+ if (detectMDX(raw2, { protectedRanges: relevantProtected, baseOffset: protectedBase })) {
45685
45824
  block.payload.meta = { ...block.payload.meta || {}, originalType: block.type };
45686
45825
  block.type = "mdx";
45687
45826
  if ("sanitizedHtml" in block.payload) {
@@ -45774,6 +45913,7 @@ var BLOCK_NODE_TYPES = /* @__PURE__ */ new Set([
45774
45913
  "OrderedList",
45775
45914
  "HTMLBlock",
45776
45915
  "ThematicBreak",
45916
+ "HorizontalRule",
45777
45917
  "ATXHeading",
45778
45918
  "SetextHeading"
45779
45919
  ]);
@@ -45813,6 +45953,7 @@ function mapLezerNodeToBlockType(nodeType) {
45813
45953
  case "HTMLBlock":
45814
45954
  return "html";
45815
45955
  case "ThematicBreak":
45956
+ case "HorizontalRule":
45816
45957
  return "hr";
45817
45958
  default:
45818
45959
  return "paragraph";
@@ -45861,6 +46002,36 @@ function computeParagraphPatchLimit(patches, {
45861
46002
  return Math.min(scaledLimit, patches.length);
45862
46003
  }
45863
46004
 
46005
+ // src/lazy-tokenization.ts
46006
+ var PRIORITY_ORDER = {
46007
+ visible: 2,
46008
+ prefetch: 1
46009
+ };
46010
+ function clampLazyRange(startLine, endLine, totalLines) {
46011
+ const clampedStart = Math.max(0, Math.min(Math.floor(startLine), totalLines));
46012
+ const clampedEnd = Math.max(clampedStart, Math.min(Math.floor(endLine), totalLines));
46013
+ return { startLine: clampedStart, endLine: clampedEnd };
46014
+ }
46015
+ function compareLazyPriority(a, b) {
46016
+ return (PRIORITY_ORDER[a] ?? 0) - (PRIORITY_ORDER[b] ?? 0);
46017
+ }
46018
+ function mergeLazyRequests(existing, next2) {
46019
+ const priority = compareLazyPriority(existing.priority, next2.priority) >= 0 ? existing.priority : next2.priority;
46020
+ const startLine = Math.min(existing.startLine, next2.startLine);
46021
+ const endLine = Math.max(existing.endLine, next2.endLine);
46022
+ const useNextTimestamp = compareLazyPriority(next2.priority, existing.priority) >= 0;
46023
+ return {
46024
+ blockId: existing.blockId,
46025
+ startLine,
46026
+ endLine,
46027
+ priority,
46028
+ requestedAt: useNextTimestamp ? next2.requestedAt : existing.requestedAt
46029
+ };
46030
+ }
46031
+ function lazyRequestRangeSize(request) {
46032
+ return Math.max(0, request.endLine - request.startLine);
46033
+ }
46034
+
45864
46035
  // ../../node_modules/markdown-extensions/index.js
45865
46036
  var markdownExtension = [
45866
46037
  "md",
@@ -81979,9 +82150,21 @@ var deferredPatchQueue = [];
81979
82150
  var MAX_DEFERRED_FLUSH_PATCHES = 120;
81980
82151
  var CODE_HIGHLIGHT_CACHE = /* @__PURE__ */ new Map();
81981
82152
  var MAX_CODE_HIGHLIGHT_CACHE_ENTRIES = 200;
82153
+ var incrementalHighlightStates = /* @__PURE__ */ new Map();
82154
+ var lazyTokenizationStates = /* @__PURE__ */ new Map();
82155
+ var lazyTokenizationQueue = /* @__PURE__ */ new Map();
82156
+ var lazyTokenizationScheduled = false;
82157
+ var lazyTokenizationProcessing = false;
82158
+ var DEFAULT_LAZY_TOKENIZATION_THRESHOLD = 200;
82159
+ var MIN_LAZY_TOKENIZATION_THRESHOLD = 50;
82160
+ var MAX_LAZY_TOKENIZATION_THRESHOLD = 1e4;
82161
+ var lazyTokenizationEnabled = true;
82162
+ var lazyTokenizationThresholdLines = DEFAULT_LAZY_TOKENIZATION_THRESHOLD;
82163
+ var CODE_HIGHLIGHT_THEMES = { dark: "github-dark", light: "github-light" };
81982
82164
  var mdxCompileMode = "server";
81983
82165
  var formatAnticipationConfig = normalizeFormatAnticipation(false);
81984
- var enableLiveCodeHighlighting = false;
82166
+ var codeHighlightingMode = "final";
82167
+ var highlightOutputMode = "html";
81985
82168
  var enableMath = true;
81986
82169
  var mdxComponentAllowlist = null;
81987
82170
  var WORKER_MDX_CACHE = /* @__PURE__ */ new Map();
@@ -82083,6 +82266,12 @@ var WorkerMetricsCollector = class {
82083
82266
  this.appendLineBatchCount = 0;
82084
82267
  this.appendLineTotalLines = 0;
82085
82268
  this.appendLineMaxLines = 0;
82269
+ this.lazyTokenizationRequests = 0;
82270
+ this.lazyTokenizationRangeTotal = 0;
82271
+ this.lazyTokenizationRangeMax = 0;
82272
+ this.lazyTokenizationLatencyTotal = 0;
82273
+ this.lazyTokenizationLatencyMax = 0;
82274
+ this.lazyTokenizationMaxQueue = 0;
82086
82275
  this.grammarEngine = grammarEngine;
82087
82276
  this.startedAt = now();
82088
82277
  }
@@ -82137,6 +82326,29 @@ var WorkerMetricsCollector = class {
82137
82326
  this.appendLineMaxLines = normalized;
82138
82327
  }
82139
82328
  }
82329
+ recordLazyTokenization(rangeLines, latencyMs, queueDepth) {
82330
+ if (Number.isFinite(rangeLines ?? Number.NaN)) {
82331
+ const normalized = Math.max(0, Math.floor(Number(rangeLines)));
82332
+ this.lazyTokenizationRequests += 1;
82333
+ this.lazyTokenizationRangeTotal += normalized;
82334
+ if (normalized > this.lazyTokenizationRangeMax) {
82335
+ this.lazyTokenizationRangeMax = normalized;
82336
+ }
82337
+ }
82338
+ if (Number.isFinite(latencyMs ?? Number.NaN)) {
82339
+ const normalizedLatency = Math.max(0, Number(latencyMs));
82340
+ this.lazyTokenizationLatencyTotal += normalizedLatency;
82341
+ if (normalizedLatency > this.lazyTokenizationLatencyMax) {
82342
+ this.lazyTokenizationLatencyMax = normalizedLatency;
82343
+ }
82344
+ }
82345
+ if (Number.isFinite(queueDepth ?? Number.NaN)) {
82346
+ const normalizedQueue = Math.max(0, Math.floor(Number(queueDepth)));
82347
+ if (normalizedQueue > this.lazyTokenizationMaxQueue) {
82348
+ this.lazyTokenizationMaxQueue = normalizedQueue;
82349
+ }
82350
+ }
82351
+ }
82140
82352
  markDiffStart() {
82141
82353
  this.diffStart = now();
82142
82354
  }
@@ -82199,7 +82411,15 @@ var WorkerMetricsCollector = class {
82199
82411
  highlightByLanguage: mapToHighlightRecord(this.highlightByLanguage),
82200
82412
  appendLineBatches: this.appendLineBatchCount || void 0,
82201
82413
  appendLineTotalLines: this.appendLineTotalLines || void 0,
82202
- appendLineMaxLines: this.appendLineMaxLines || void 0
82414
+ appendLineMaxLines: this.appendLineMaxLines || void 0,
82415
+ lazyTokenization: this.lazyTokenizationRequests > 0 ? {
82416
+ requests: this.lazyTokenizationRequests,
82417
+ avgRangeLines: roundMetric(this.lazyTokenizationRangeTotal / this.lazyTokenizationRequests),
82418
+ maxRangeLines: this.lazyTokenizationRangeMax,
82419
+ avgLatencyMs: roundMetric(this.lazyTokenizationLatencyTotal / this.lazyTokenizationRequests),
82420
+ maxLatencyMs: roundMetric(this.lazyTokenizationLatencyMax),
82421
+ maxQueue: this.lazyTokenizationMaxQueue
82422
+ } : void 0
82203
82423
  };
82204
82424
  }
82205
82425
  };
@@ -82362,6 +82582,11 @@ async function initialize(initialContent = "", prewarmLangs = [], docPlugins, md
82362
82582
  lastTree = null;
82363
82583
  currentContent = "";
82364
82584
  deferredPatchQueue = [];
82585
+ resetIncrementalHighlightState();
82586
+ lazyTokenizationStates.clear();
82587
+ lazyTokenizationQueue.clear();
82588
+ lazyTokenizationScheduled = false;
82589
+ lazyTokenizationProcessing = false;
82365
82590
  mdxCompileMode = mdxOptions?.compileMode ?? "server";
82366
82591
  try {
82367
82592
  if (DEBUG_MDX) {
@@ -82378,11 +82603,29 @@ async function initialize(initialContent = "", prewarmLangs = [], docPlugins, md
82378
82603
  callouts: docPlugins?.callouts ?? false,
82379
82604
  math: docPlugins?.math ?? true,
82380
82605
  formatAnticipation: docPlugins?.formatAnticipation ?? false,
82606
+ codeHighlighting: docPlugins?.codeHighlighting,
82381
82607
  liveCodeHighlighting: docPlugins?.liveCodeHighlighting ?? false
82382
82608
  };
82383
82609
  enableMath = enable.math;
82384
82610
  formatAnticipationConfig = normalizeFormatAnticipation(enable.formatAnticipation);
82385
- enableLiveCodeHighlighting = enable.liveCodeHighlighting;
82611
+ if (enable.codeHighlighting === "incremental" || enable.codeHighlighting === "live" || enable.codeHighlighting === "final") {
82612
+ codeHighlightingMode = enable.codeHighlighting;
82613
+ } else {
82614
+ codeHighlightingMode = enable.liveCodeHighlighting ? "live" : "final";
82615
+ }
82616
+ if (docPlugins?.outputMode === "html" || docPlugins?.outputMode === "tokens" || docPlugins?.outputMode === "both") {
82617
+ highlightOutputMode = docPlugins.outputMode;
82618
+ } else {
82619
+ highlightOutputMode = "html";
82620
+ }
82621
+ if (docPlugins?.lazyTokenization) {
82622
+ lazyTokenizationEnabled = docPlugins.lazyTokenization.enabled ?? true;
82623
+ const desiredThreshold = docPlugins.lazyTokenization.thresholdLines ?? DEFAULT_LAZY_TOKENIZATION_THRESHOLD;
82624
+ lazyTokenizationThresholdLines = clampInt(desiredThreshold, MIN_LAZY_TOKENIZATION_THRESHOLD, MAX_LAZY_TOKENIZATION_THRESHOLD);
82625
+ } else {
82626
+ lazyTokenizationEnabled = true;
82627
+ lazyTokenizationThresholdLines = DEFAULT_LAZY_TOKENIZATION_THRESHOLD;
82628
+ }
82386
82629
  if (Array.isArray(docPlugins?.mdxComponentNames) && docPlugins?.mdxComponentNames.length > 0) {
82387
82630
  mdxComponentAllowlist = new Set(docPlugins.mdxComponentNames);
82388
82631
  } else {
@@ -82396,7 +82639,7 @@ async function initialize(initialContent = "", prewarmLangs = [], docPlugins, md
82396
82639
  if (enable.html) globalDocumentPluginRegistry.register(HTMLBlockPlugin);
82397
82640
  if (enable.mdx) globalDocumentPluginRegistry.register(MDXDetectionPlugin);
82398
82641
  performanceTimer.mark("highlighter-init");
82399
- const coreLangs = ["javascript", "typescript", "json", "text", "markdown"];
82642
+ const coreLangs = ["javascript", "typescript", "json", "text", "markdown", "diff"];
82400
82643
  const initialLangs = [...coreLangs, ...prewarmLangs];
82401
82644
  highlighter = await createHighlighter({
82402
82645
  engine: createJavaScriptRegexEngine(),
@@ -82493,6 +82736,7 @@ async function appendAndReparse(appendedText, metrics) {
82493
82736
  blocks = changedBlocks;
82494
82737
  lastTree = newTree;
82495
82738
  currentContent = newContent;
82739
+ pruneLazyTokenizationStates(blocks);
82496
82740
  performanceTimer.measure("incremental-parse");
82497
82741
  metrics?.markParseEnd();
82498
82742
  metrics?.setBlocksProduced(blocks.length);
@@ -82671,22 +82915,35 @@ async function enrichBlock(block) {
82671
82915
  nextMeta.inlineStatus = void 0;
82672
82916
  metaChanged = true;
82673
82917
  }
82674
- const mathRanges = enableMath ? collectMathProtectedRanges(rawParagraph) : [];
82675
- if (mathRanges.length > 0) {
82676
- nextMeta.protectedRanges = mathRanges;
82677
- metaChanged = true;
82678
- } else if (Object.prototype.hasOwnProperty.call(nextMeta, "protectedRanges")) {
82679
- nextMeta.protectedRanges = void 0;
82680
- metaChanged = true;
82918
+ const baseOffset = typeof block.payload.range?.from === "number" ? block.payload.range.from : void 0;
82919
+ const protectedRanges = [];
82920
+ if (enableMath) {
82921
+ protectedRanges.push(...collectMathProtectedRanges(rawParagraph));
82922
+ }
82923
+ const autolinkRanges = collectAutolinkProtectedRanges(rawParagraph);
82924
+ if (autolinkRanges.length > 0) {
82925
+ protectedRanges.push(...autolinkRanges);
82681
82926
  }
82682
82927
  const shouldExtractSegments = typeof rawParagraph === "string" && (rawParagraph.includes("<") || rawParagraph.includes("{"));
82683
82928
  if (shouldExtractSegments) {
82684
- const baseOffset = typeof block.payload.range?.from === "number" ? block.payload.range.from : void 0;
82685
82929
  const mixedOptions = !block.isFinalized && shouldAllowMixedStreaming() ? {
82686
82930
  html: formatAnticipationConfig.html ? { autoClose: true, maxNewlines: MIXED_CONTENT_AUTOCLOSE_NEWLINES } : void 0,
82687
82931
  mdx: formatAnticipationConfig.mdx ? { autoClose: true, maxNewlines: MIXED_CONTENT_AUTOCLOSE_NEWLINES, componentAllowlist: mdxComponentAllowlist ?? void 0 } : void 0
82688
82932
  } : void 0;
82689
- const segments = extractMixedContentSegments(rawParagraph, baseOffset, (value) => inlineParse(value), mixedOptions);
82933
+ const protectedRangesAbsolute = typeof baseOffset === "number" && protectedRanges.length > 0 ? protectedRanges.map((range2) => ({
82934
+ ...range2,
82935
+ from: baseOffset + range2.from,
82936
+ to: baseOffset + range2.to
82937
+ })) : void 0;
82938
+ const mixedSegmentOptions = mixedOptions || protectedRangesAbsolute ? {
82939
+ ...mixedOptions,
82940
+ protectedRanges: protectedRangesAbsolute
82941
+ } : void 0;
82942
+ const segments = extractMixedContentSegments(rawParagraph, baseOffset, (value) => inlineParse(value), mixedSegmentOptions);
82943
+ const htmlRanges = collectHtmlProtectedRangesFromSegments(segments, baseOffset);
82944
+ if (htmlRanges.length > 0) {
82945
+ protectedRanges.push(...htmlRanges);
82946
+ }
82690
82947
  if (segments.length > 0) {
82691
82948
  nextMeta.mixedSegments = segments;
82692
82949
  metaChanged = true;
@@ -82713,6 +82970,13 @@ async function enrichBlock(block) {
82713
82970
  metaChanged = true;
82714
82971
  }
82715
82972
  }
82973
+ if (protectedRanges.length > 0) {
82974
+ nextMeta.protectedRanges = protectedRanges;
82975
+ metaChanged = true;
82976
+ } else if (Object.prototype.hasOwnProperty.call(nextMeta, "protectedRanges")) {
82977
+ nextMeta.protectedRanges = void 0;
82978
+ metaChanged = true;
82979
+ }
82716
82980
  if (metaChanged) {
82717
82981
  if (Object.keys(nextMeta).length > 0) {
82718
82982
  block.payload.meta = nextMeta;
@@ -82728,9 +82992,14 @@ async function enrichBlock(block) {
82728
82992
  await enrichCodeBlock(block);
82729
82993
  break;
82730
82994
  case "html": {
82731
- const sanitized = sanitizeHtmlInWorker(block.payload.raw);
82995
+ const rawHtml = typeof block.payload.raw === "string" ? block.payload.raw : "";
82996
+ const sanitized = sanitizeHtmlInWorker(rawHtml);
82732
82997
  block.payload.sanitizedHtml = sanitized;
82733
- block.payload.meta = { ...block.payload.meta || {}, sanitized: true };
82998
+ const nextMeta = { ...block.payload.meta || {}, sanitized: true };
82999
+ if (rawHtml) {
83000
+ nextMeta.protectedRanges = [{ from: 0, to: rawHtml.length, kind: "html-block" }];
83001
+ }
83002
+ block.payload.meta = nextMeta;
82734
83003
  break;
82735
83004
  }
82736
83005
  case "list":
@@ -82750,8 +83019,12 @@ async function enrichBlock(block) {
82750
83019
  const mdxOptions = normalizedRanges && normalizedRanges.length > 0 ? { protectedRanges: normalizedRanges, baseOffset } : baseOffset ? { baseOffset } : void 0;
82751
83020
  const mdxDetectStart = now();
82752
83021
  let shouldConvertToMDX = detectMDX(block.payload.raw, mdxOptions);
83022
+ if (!shouldConvertToMDX && block.type === "html") {
83023
+ const fallbackOptions = baseOffset ? { baseOffset } : void 0;
83024
+ shouldConvertToMDX = detectMDX(block.payload.raw, fallbackOptions);
83025
+ }
82753
83026
  metrics?.recordMdxDetect(now() - mdxDetectStart);
82754
- if (shouldConvertToMDX && protectedRanges && protectedRanges.length > 0) {
83027
+ if (shouldConvertToMDX && protectedRanges && protectedRanges.length > 0 && block.type !== "html") {
82755
83028
  const exprPattern = /\{[^{}]+\}/g;
82756
83029
  let match;
82757
83030
  while (true) {
@@ -82838,65 +83111,898 @@ function collectMathProtectedRanges(content4) {
82838
83111
  }
82839
83112
  return ranges;
82840
83113
  }
83114
+ function collectAutolinkProtectedRanges(content4) {
83115
+ if (!content4) return [];
83116
+ const ranges = [];
83117
+ const autolinkPattern = /<((?:https?:\/\/|mailto:)[^>\s]+)>/gi;
83118
+ let match = autolinkPattern.exec(content4);
83119
+ while (match !== null) {
83120
+ ranges.push({ from: match.index, to: match.index + match[0].length, kind: "autolink" });
83121
+ match = autolinkPattern.exec(content4);
83122
+ }
83123
+ return ranges;
83124
+ }
83125
+ function collectHtmlProtectedRangesFromSegments(segments, baseOffset) {
83126
+ if (!segments || segments.length === 0) return [];
83127
+ if (typeof baseOffset !== "number" || !Number.isFinite(baseOffset)) {
83128
+ return [];
83129
+ }
83130
+ const ranges = [];
83131
+ for (const segment of segments) {
83132
+ if (segment.kind !== "html") continue;
83133
+ const range2 = segment.range;
83134
+ if (!range2) continue;
83135
+ const from = range2.from - baseOffset;
83136
+ const to = range2.to - baseOffset;
83137
+ if (!Number.isFinite(from) || !Number.isFinite(to) || to <= from) continue;
83138
+ ranges.push({ from, to, kind: "html-inline" });
83139
+ }
83140
+ return ranges;
83141
+ }
83142
+ var FONT_STYLE_ITALIC = 1;
83143
+ var FONT_STYLE_BOLD = 2;
83144
+ var FONT_STYLE_UNDERLINE = 4;
83145
+ var FONT_STYLE_STRIKETHROUGH = 8;
83146
+ function escapeHtmlText(text12) {
83147
+ return text12.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;").replace(/'/g, "&#39;");
83148
+ }
83149
+ function renderShikiToken(token2) {
83150
+ const dark = token2.variants.dark;
83151
+ const light = token2.variants.light;
83152
+ const styles2 = [];
83153
+ if (dark?.color) {
83154
+ styles2.push(`--shiki-dark:${dark.color}`);
83155
+ }
83156
+ if (light?.color) {
83157
+ styles2.push(`--shiki-light:${light.color}`);
83158
+ }
83159
+ const fontStyle = light?.fontStyle ?? dark?.fontStyle;
83160
+ if (typeof fontStyle === "number" && fontStyle > 0) {
83161
+ if (fontStyle & FONT_STYLE_ITALIC) {
83162
+ styles2.push("font-style: italic");
83163
+ }
83164
+ if (fontStyle & FONT_STYLE_BOLD) {
83165
+ styles2.push("font-weight: bold");
83166
+ }
83167
+ const decorations2 = [];
83168
+ if (fontStyle & FONT_STYLE_UNDERLINE) {
83169
+ decorations2.push("underline");
83170
+ }
83171
+ if (fontStyle & FONT_STYLE_STRIKETHROUGH) {
83172
+ decorations2.push("line-through");
83173
+ }
83174
+ if (decorations2.length > 0) {
83175
+ styles2.push(`text-decoration: ${decorations2.join(" ")}`);
83176
+ }
83177
+ }
83178
+ const styleAttr = styles2.length > 0 ? ` style="${styles2.join(";")}"` : "";
83179
+ return `<span${styleAttr}>${escapeHtmlText(token2.content)}</span>`;
83180
+ }
83181
+ function getTokenFontStyle(token2) {
83182
+ const dark = token2.variants?.dark?.fontStyle ?? 0;
83183
+ const light = token2.variants?.light?.fontStyle ?? 0;
83184
+ return (typeof dark === "number" ? dark : 0) | (typeof light === "number" ? light : 0);
83185
+ }
83186
+ function mergeWhitespaceTokens2(tokens) {
83187
+ return tokens.map((line) => {
83188
+ const merged = [];
83189
+ let carryContent = "";
83190
+ let carryOffset = 0;
83191
+ let carryVariants = null;
83192
+ line.forEach((token2, idx) => {
83193
+ const fontStyle = getTokenFontStyle(token2);
83194
+ const couldMerge = (fontStyle & FONT_STYLE_UNDERLINE) === 0;
83195
+ if (couldMerge && /^\s+$/.test(token2.content) && line[idx + 1]) {
83196
+ if (!carryContent) {
83197
+ carryOffset = token2.offset ?? 0;
83198
+ carryVariants = token2.variants;
83199
+ }
83200
+ carryContent += token2.content;
83201
+ return;
83202
+ }
83203
+ if (carryContent) {
83204
+ if (couldMerge) {
83205
+ merged.push({ ...token2, offset: carryOffset, content: carryContent + token2.content });
83206
+ } else {
83207
+ merged.push({ content: carryContent, offset: carryOffset, variants: carryVariants ?? token2.variants });
83208
+ merged.push(token2);
83209
+ }
83210
+ carryContent = "";
83211
+ carryOffset = 0;
83212
+ carryVariants = null;
83213
+ return;
83214
+ }
83215
+ merged.push(token2);
83216
+ });
83217
+ return merged;
83218
+ });
83219
+ }
83220
+ function renderShikiLines(tokens) {
83221
+ const merged = mergeWhitespaceTokens2(tokens);
83222
+ return merged.map((lineTokens) => lineTokens.map(renderShikiToken).join(""));
83223
+ }
83224
+ function detectDiffLanguage(lang236, meta2) {
83225
+ const normalized = (lang236 || "").trim().toLowerCase();
83226
+ if (!normalized) {
83227
+ return { isDiff: false, diffLang: "diff", baseLang: null };
83228
+ }
83229
+ let baseLang = null;
83230
+ if (normalized === "diff" || normalized === "udiff" || normalized === "diff-unified") {
83231
+ baseLang = null;
83232
+ } else if (normalized.startsWith("diff-")) {
83233
+ baseLang = normalized.slice("diff-".length) || null;
83234
+ } else if (normalized.endsWith("-diff")) {
83235
+ baseLang = normalized.slice(0, Math.max(0, normalized.length - "-diff".length)) || null;
83236
+ } else {
83237
+ return { isDiff: false, diffLang: "diff", baseLang: null };
83238
+ }
83239
+ if (!baseLang) {
83240
+ const metaLang = typeof meta2.lang === "string" ? meta2.lang : typeof meta2.language === "string" ? meta2.language : typeof meta2.base === "string" ? meta2.base : null;
83241
+ if (metaLang) {
83242
+ baseLang = metaLang;
83243
+ } else {
83244
+ const metaKeys = Object.keys(meta2);
83245
+ if (metaKeys.length > 0) {
83246
+ baseLang = metaKeys[0] ?? null;
83247
+ }
83248
+ }
83249
+ }
83250
+ return {
83251
+ isDiff: true,
83252
+ diffLang: "diff",
83253
+ baseLang: baseLang ? normalizeLang(String(baseLang)) : null
83254
+ };
83255
+ }
83256
+ function parseDiffHunkHeader(line) {
83257
+ const match = line.match(/^@@\s+-(\d+)(?:,(\d+))?\s+\+(\d+)(?:,(\d+))?\s+@@/);
83258
+ if (!match) return null;
83259
+ return {
83260
+ oldStart: Number.parseInt(match[1] ?? "0", 10),
83261
+ newStart: Number.parseInt(match[3] ?? "0", 10)
83262
+ };
83263
+ }
83264
+ function isDiffMetaLine(line) {
83265
+ if (!line) return false;
83266
+ return line.startsWith("diff ") || line.startsWith("index ") || line.startsWith("new file") || line.startsWith("deleted file") || line.startsWith("similarity index") || line.startsWith("rename from") || line.startsWith("rename to") || line.startsWith("mode ") || line.startsWith("\\\\ No newline");
83267
+ }
83268
+ function parseUnifiedDiffLine(line, cursor) {
83269
+ if (line.startsWith("@@")) {
83270
+ const header = parseDiffHunkHeader(line);
83271
+ if (header) {
83272
+ cursor.oldLine = header.oldStart;
83273
+ cursor.newLine = header.newStart;
83274
+ }
83275
+ return { text: line, kind: "hunk", prefix: "", content: line, oldNo: null, newNo: null };
83276
+ }
83277
+ if (line.startsWith("--- ") || line.startsWith("+++ ") || isDiffMetaLine(line)) {
83278
+ return { text: line, kind: "meta", prefix: "", content: line, oldNo: null, newNo: null };
83279
+ }
83280
+ if (line.startsWith("+")) {
83281
+ if (line.startsWith("+++")) {
83282
+ return { text: line, kind: "meta", prefix: "", content: line, oldNo: null, newNo: null };
83283
+ }
83284
+ const newNo = cursor.newLine;
83285
+ if (cursor.newLine !== null) cursor.newLine += 1;
83286
+ return { text: line, kind: "add", prefix: "+", content: line.slice(1), oldNo: null, newNo };
83287
+ }
83288
+ if (line.startsWith("-")) {
83289
+ if (line.startsWith("---")) {
83290
+ return { text: line, kind: "meta", prefix: "", content: line, oldNo: null, newNo: null };
83291
+ }
83292
+ const oldNo = cursor.oldLine;
83293
+ if (cursor.oldLine !== null) cursor.oldLine += 1;
83294
+ return { text: line, kind: "remove", prefix: "-", content: line.slice(1), oldNo, newNo: null };
83295
+ }
83296
+ if (line.startsWith(" ")) {
83297
+ const oldNo = cursor.oldLine;
83298
+ const newNo = cursor.newLine;
83299
+ if (cursor.oldLine !== null) cursor.oldLine += 1;
83300
+ if (cursor.newLine !== null) cursor.newLine += 1;
83301
+ return { text: line, kind: "context", prefix: " ", content: line.slice(1), oldNo, newNo };
83302
+ }
83303
+ return { text: line, kind: "meta", prefix: "", content: line, oldNo: null, newNo: null };
83304
+ }
83305
+ function tokenizeDiffRun(lines, language, grammarState) {
83306
+ if (!highlighter) {
83307
+ return { tokens: [], nextGrammarState: grammarState };
83308
+ }
83309
+ const content4 = lines.map((line) => line.content).join("\\n");
83310
+ if (!content4 && lines.length > 0) {
83311
+ return { tokens: lines.map(() => []), nextGrammarState: grammarState };
83312
+ }
83313
+ const tokens = highlighter.codeToTokensWithThemes(content4, {
83314
+ lang: language,
83315
+ themes: CODE_HIGHLIGHT_THEMES,
83316
+ grammarState
83317
+ });
83318
+ const nextGrammarState = highlighter.getLastGrammarState(tokens);
83319
+ return { tokens, nextGrammarState };
83320
+ }
83321
+ function buildDiffTokenLines(lines, language, grammarState) {
83322
+ const output = [];
83323
+ let idx = 0;
83324
+ let nextOldState = grammarState.old;
83325
+ let nextNewState = grammarState.new;
83326
+ while (idx < lines.length) {
83327
+ const line = lines[idx];
83328
+ if (line.kind === "meta" || line.kind === "hunk") {
83329
+ output.push({ spans: [{ t: line.text }] });
83330
+ idx += 1;
83331
+ continue;
83332
+ }
83333
+ const runKind = line.kind;
83334
+ const run = [];
83335
+ while (idx < lines.length && lines[idx].kind === runKind) {
83336
+ run.push(lines[idx]);
83337
+ idx += 1;
83338
+ }
83339
+ try {
83340
+ if (runKind === "add") {
83341
+ const { tokens, nextGrammarState } = tokenizeDiffRun(run, language, nextNewState);
83342
+ nextNewState = nextGrammarState;
83343
+ const tokenLines = renderTokenLines(tokens);
83344
+ for (let i = 0; i < run.length; i++) {
83345
+ const prefixSpan = { t: run[i].prefix };
83346
+ const lineTokens = tokenLines[i] ?? null;
83347
+ if (!lineTokens || lineTokens.spans.length === 0) {
83348
+ const fullText = `${run[i].prefix}${run[i].content}`;
83349
+ output.push(fullText.length > 0 ? { spans: [{ t: fullText }] } : { spans: [] });
83350
+ } else {
83351
+ output.push({ spans: [prefixSpan, ...lineTokens.spans] });
83352
+ }
83353
+ }
83354
+ continue;
83355
+ }
83356
+ if (runKind === "remove") {
83357
+ const { tokens, nextGrammarState } = tokenizeDiffRun(run, language, nextOldState);
83358
+ nextOldState = nextGrammarState;
83359
+ const tokenLines = renderTokenLines(tokens);
83360
+ for (let i = 0; i < run.length; i++) {
83361
+ const prefixSpan = { t: run[i].prefix };
83362
+ const lineTokens = tokenLines[i] ?? null;
83363
+ if (!lineTokens || lineTokens.spans.length === 0) {
83364
+ const fullText = `${run[i].prefix}${run[i].content}`;
83365
+ output.push(fullText.length > 0 ? { spans: [{ t: fullText }] } : { spans: [] });
83366
+ } else {
83367
+ output.push({ spans: [prefixSpan, ...lineTokens.spans] });
83368
+ }
83369
+ }
83370
+ continue;
83371
+ }
83372
+ if (runKind === "context") {
83373
+ const { tokens, nextGrammarState } = tokenizeDiffRun(run, language, nextNewState);
83374
+ nextNewState = nextGrammarState;
83375
+ if (run.length > 0) {
83376
+ const oldResult = tokenizeDiffRun(run, language, nextOldState);
83377
+ nextOldState = oldResult.nextGrammarState;
83378
+ }
83379
+ const tokenLines = renderTokenLines(tokens);
83380
+ for (let i = 0; i < run.length; i++) {
83381
+ const prefixSpan = { t: run[i].prefix };
83382
+ const lineTokens = tokenLines[i] ?? null;
83383
+ if (!lineTokens || lineTokens.spans.length === 0) {
83384
+ const fullText = `${run[i].prefix}${run[i].content}`;
83385
+ output.push(fullText.length > 0 ? { spans: [{ t: fullText }] } : { spans: [] });
83386
+ } else {
83387
+ output.push({ spans: [prefixSpan, ...lineTokens.spans] });
83388
+ }
83389
+ }
83390
+ continue;
83391
+ }
83392
+ } catch (error) {
83393
+ console.warn("Diff tokenization failed for", language, error);
83394
+ }
83395
+ for (let i = 0; i < run.length; i++) {
83396
+ const fullText = run[i].text;
83397
+ output.push(fullText.length > 0 ? { spans: [{ t: fullText }] } : { spans: [] });
83398
+ }
83399
+ }
83400
+ return { tokenLines: output, grammarState: { old: nextOldState, new: nextNewState } };
83401
+ }
83402
+ function toTokenStyle(style2) {
83403
+ if (!style2) return void 0;
83404
+ const fg = typeof style2.color === "string" ? style2.color : void 0;
83405
+ const fs = typeof style2.fontStyle === "number" && style2.fontStyle > 0 ? style2.fontStyle : void 0;
83406
+ if (!fg && fs === void 0) return void 0;
83407
+ return { fg, fs };
83408
+ }
83409
+ function toTokenSpan(token2) {
83410
+ const dark = toTokenStyle(token2.variants.dark);
83411
+ const light = toTokenStyle(token2.variants.light);
83412
+ const span = { t: token2.content };
83413
+ if (dark || light) {
83414
+ span.v = {};
83415
+ if (dark) span.v.dark = dark;
83416
+ if (light) span.v.light = light;
83417
+ }
83418
+ return span;
83419
+ }
83420
+ function renderTokenLines(tokens) {
83421
+ return tokens.map((lineTokens) => ({
83422
+ spans: lineTokens.map((token2) => toTokenSpan(token2))
83423
+ }));
83424
+ }
83425
+ function resetIncrementalHighlightState(blockId) {
83426
+ if (blockId) {
83427
+ incrementalHighlightStates.delete(blockId);
83428
+ return;
83429
+ }
83430
+ incrementalHighlightStates.clear();
83431
+ }
83432
+ function getIncrementalHighlightState(blockId, lang236) {
83433
+ const existing = incrementalHighlightStates.get(blockId);
83434
+ if (!existing || existing.lang !== lang236) {
83435
+ const fresh = {
83436
+ lang: lang236,
83437
+ tokenLang: void 0,
83438
+ processedLength: 0,
83439
+ pendingLine: "",
83440
+ highlightedLines: [],
83441
+ tokenLines: [],
83442
+ diffKind: [],
83443
+ oldNo: [],
83444
+ newNo: [],
83445
+ diffCursor: void 0,
83446
+ diffGrammar: void 0,
83447
+ grammarState: void 0
83448
+ };
83449
+ incrementalHighlightStates.set(blockId, fresh);
83450
+ return fresh;
83451
+ }
83452
+ return existing;
83453
+ }
83454
+ async function resolveHighlightLanguage(requestedLanguage) {
83455
+ if (!highlighter) return "text";
83456
+ const loadedLangs = highlighter.getLoadedLanguages();
83457
+ if (!loadedLangs.includes(requestedLanguage)) {
83458
+ try {
83459
+ await highlighter.loadLanguage(requestedLanguage);
83460
+ } catch (loadError) {
83461
+ console.warn(`Failed to load language ${requestedLanguage}, falling back to text:`, loadError);
83462
+ }
83463
+ }
83464
+ const nextLangs = highlighter.getLoadedLanguages();
83465
+ return nextLangs.includes(requestedLanguage) ? requestedLanguage : "text";
83466
+ }
83467
+ function makeLazySignature(codeBody, lang236, tokenLang, diffEnabled) {
83468
+ const head2 = codeBody.slice(0, 32);
83469
+ const tail = codeBody.slice(Math.max(0, codeBody.length - 32));
83470
+ return `${lang236}|${tokenLang}|${diffEnabled ? "diff" : "plain"}|${codeBody.length}|${head2}|${tail}`;
83471
+ }
83472
+ function getOrInitLazyState(blockId, signature, lineCount, lang236, tokenLang, diffEnabled) {
83473
+ const existing = lazyTokenizationStates.get(blockId);
83474
+ if (existing && existing.signature === signature && existing.highlightedLines.length === lineCount) {
83475
+ return existing;
83476
+ }
83477
+ const fresh = {
83478
+ signature,
83479
+ lang: lang236,
83480
+ tokenLang,
83481
+ processedLines: 0,
83482
+ highlightedLines: new Array(lineCount).fill(null),
83483
+ tokenLines: new Array(lineCount).fill(null),
83484
+ diffKind: new Array(lineCount).fill(null),
83485
+ oldNo: new Array(lineCount).fill(null),
83486
+ newNo: new Array(lineCount).fill(null),
83487
+ diffCursor: diffEnabled ? { oldLine: null, newLine: null } : void 0,
83488
+ diffGrammar: diffEnabled ? {} : void 0,
83489
+ grammarState: void 0
83490
+ };
83491
+ lazyTokenizationStates.set(blockId, fresh);
83492
+ return fresh;
83493
+ }
83494
+ function shouldLazyTokenizeBlock(lineCount, hasHighlighter, hasHighlightableContent) {
83495
+ if (!lazyTokenizationEnabled) return false;
83496
+ if (!hasHighlighter || !hasHighlightableContent) return false;
83497
+ return lineCount >= lazyTokenizationThresholdLines;
83498
+ }
83499
+ function pruneLazyTokenizationStates(nextBlocks) {
83500
+ if (lazyTokenizationStates.size === 0) return;
83501
+ const ids = new Set(nextBlocks.map((block) => block.id));
83502
+ for (const key2 of lazyTokenizationStates.keys()) {
83503
+ if (!ids.has(key2)) {
83504
+ lazyTokenizationStates.delete(key2);
83505
+ }
83506
+ }
83507
+ }
83508
+ function enqueueLazyTokenization(request) {
83509
+ if (!lazyTokenizationEnabled) return;
83510
+ const existing = lazyTokenizationQueue.get(request.blockId);
83511
+ const next2 = existing ? mergeLazyRequests(existing, request) : request;
83512
+ lazyTokenizationQueue.set(request.blockId, next2);
83513
+ if (!lazyTokenizationScheduled) {
83514
+ lazyTokenizationScheduled = true;
83515
+ setTimeout(() => {
83516
+ lazyTokenizationScheduled = false;
83517
+ void processLazyTokenizationQueue();
83518
+ }, 0);
83519
+ }
83520
+ }
83521
+ function selectNextLazyRequest() {
83522
+ let selected = null;
83523
+ for (const request of lazyTokenizationQueue.values()) {
83524
+ if (!selected) {
83525
+ selected = request;
83526
+ continue;
83527
+ }
83528
+ const priorityDiff = compareLazyPriority(request.priority, selected.priority);
83529
+ if (priorityDiff > 0) {
83530
+ selected = request;
83531
+ continue;
83532
+ }
83533
+ if (priorityDiff === 0 && request.requestedAt < selected.requestedAt) {
83534
+ selected = request;
83535
+ }
83536
+ }
83537
+ return selected;
83538
+ }
83539
+ async function processLazyTokenizationQueue() {
83540
+ if (lazyTokenizationProcessing) return;
83541
+ lazyTokenizationProcessing = true;
83542
+ try {
83543
+ while (lazyTokenizationQueue.size > 0) {
83544
+ const next2 = selectNextLazyRequest();
83545
+ if (!next2) break;
83546
+ lazyTokenizationQueue.delete(next2.blockId);
83547
+ await handleLazyTokenizationRequest(next2);
83548
+ }
83549
+ } finally {
83550
+ lazyTokenizationProcessing = false;
83551
+ }
83552
+ }
83553
+ async function handleLazyTokenizationRequest(request) {
83554
+ if (!lazyTokenizationEnabled || !highlighter) return;
83555
+ const index2 = blocks.findIndex((block2) => block2.id === request.blockId);
83556
+ if (index2 === -1) return;
83557
+ const block = blocks[index2];
83558
+ if (block.type !== "code" || !block.isFinalized) return;
83559
+ const raw2 = block.payload.raw ?? "";
83560
+ const { code: code4, info, hadFence } = stripCodeFence(raw2);
83561
+ const { lang: lang236, meta: meta2 } = parseCodeFenceInfo(info);
83562
+ const diffInfo = detectDiffLanguage(lang236, meta2);
83563
+ const requestedLanguage = diffInfo.isDiff ? diffInfo.diffLang : lang236 || "text";
83564
+ const tokenLanguage = diffInfo.isDiff ? diffInfo.baseLang ?? "text" : requestedLanguage;
83565
+ const codeBody = hadFence ? code4 : dedentIndentedCode(raw2);
83566
+ const codeLines = extractCodeLines(raw2);
83567
+ const hasHighlightableContent = codeBody.trim().length > 0;
83568
+ if (!shouldLazyTokenizeBlock(codeLines.length, Boolean(highlighter), hasHighlightableContent)) {
83569
+ return;
83570
+ }
83571
+ const wantsHtml = highlightOutputMode === "html" || highlightOutputMode === "both";
83572
+ const wantsTokens = highlightOutputMode === "tokens" || highlightOutputMode === "both";
83573
+ const diffEnabled = diffInfo.isDiff && wantsTokens;
83574
+ if (!wantsHtml && !wantsTokens) return;
83575
+ const { startLine, endLine } = clampLazyRange(request.startLine, request.endLine, codeLines.length);
83576
+ if (endLine <= startLine) return;
83577
+ let resolvedLanguage = requestedLanguage;
83578
+ let resolvedTokenLanguage = tokenLanguage;
83579
+ if (wantsHtml) {
83580
+ resolvedLanguage = await resolveHighlightLanguage(requestedLanguage);
83581
+ }
83582
+ if (wantsTokens) {
83583
+ resolvedTokenLanguage = await resolveHighlightLanguage(tokenLanguage);
83584
+ }
83585
+ const signature = makeLazySignature(codeBody, resolvedLanguage, resolvedTokenLanguage, diffEnabled);
83586
+ const state = getOrInitLazyState(block.id, signature, codeLines.length, resolvedLanguage, resolvedTokenLanguage, diffEnabled);
83587
+ if (state.processedLines < 0 || state.processedLines > codeLines.length) {
83588
+ state.processedLines = 0;
83589
+ }
83590
+ const targetEnd = Math.min(endLine, codeLines.length);
83591
+ if (targetEnd <= state.processedLines) {
83592
+ return;
83593
+ }
83594
+ const metricsCollector = new WorkerMetricsCollector(workerGrammarEngine);
83595
+ setActiveMetricsCollector(metricsCollector);
83596
+ metricsCollector.setBlocksProduced(blocks.length);
83597
+ const prevSnapshot = await blockToNodeSnapshot(block);
83598
+ const tokenizationStart = now();
83599
+ const segmentLines = codeLines.slice(state.processedLines, targetEnd);
83600
+ if (segmentLines.length > 0) {
83601
+ if (wantsHtml || wantsTokens && !diffEnabled) {
83602
+ try {
83603
+ const sharedLanguage = wantsHtml ? resolvedLanguage : resolvedTokenLanguage;
83604
+ const tokens = highlighter.codeToTokensWithThemes(segmentLines.join("\n"), {
83605
+ lang: sharedLanguage,
83606
+ themes: CODE_HIGHLIGHT_THEMES,
83607
+ grammarState: state.grammarState
83608
+ });
83609
+ if (wantsHtml) {
83610
+ const htmlLines = renderShikiLines(tokens);
83611
+ for (let i = 0; i < htmlLines.length; i += 1) {
83612
+ state.highlightedLines[state.processedLines + i] = htmlLines[i] ?? null;
83613
+ }
83614
+ }
83615
+ if (wantsTokens && !diffEnabled) {
83616
+ const tokenLines = renderTokenLines(tokens);
83617
+ for (let i = 0; i < tokenLines.length; i += 1) {
83618
+ state.tokenLines[state.processedLines + i] = tokenLines[i] ?? null;
83619
+ }
83620
+ }
83621
+ state.grammarState = highlighter.getLastGrammarState(tokens);
83622
+ } catch (error) {
83623
+ console.warn("Lazy tokenization failed for", requestedLanguage, error);
83624
+ if (wantsHtml) {
83625
+ for (let i = 0; i < segmentLines.length; i += 1) {
83626
+ state.highlightedLines[state.processedLines + i] = null;
83627
+ }
83628
+ }
83629
+ if (wantsTokens && !diffEnabled) {
83630
+ for (let i = 0; i < segmentLines.length; i += 1) {
83631
+ state.tokenLines[state.processedLines + i] = null;
83632
+ }
83633
+ }
83634
+ }
83635
+ }
83636
+ if (wantsTokens && diffEnabled) {
83637
+ const cursor = state.diffCursor ?? { oldLine: null, newLine: null };
83638
+ const diffLines = segmentLines.map((line) => parseUnifiedDiffLine(line, cursor));
83639
+ for (let i = 0; i < diffLines.length; i += 1) {
83640
+ const line = diffLines[i];
83641
+ const idx = state.processedLines + i;
83642
+ state.diffKind[idx] = line.kind;
83643
+ state.oldNo[idx] = line.oldNo ?? null;
83644
+ state.newNo[idx] = line.newNo ?? null;
83645
+ }
83646
+ try {
83647
+ const { tokenLines, grammarState } = buildDiffTokenLines(diffLines, resolvedTokenLanguage, state.diffGrammar ?? {});
83648
+ for (let i = 0; i < tokenLines.length; i += 1) {
83649
+ state.tokenLines[state.processedLines + i] = tokenLines[i] ?? null;
83650
+ }
83651
+ state.diffGrammar = grammarState;
83652
+ } catch (error) {
83653
+ console.warn("Lazy diff tokenization failed for", tokenLanguage, error);
83654
+ for (let i = 0; i < diffLines.length; i += 1) {
83655
+ const idx = state.processedLines + i;
83656
+ const line = diffLines[i];
83657
+ state.tokenLines[idx] = line.text.length > 0 ? { spans: [{ t: line.text }] } : { spans: [] };
83658
+ }
83659
+ }
83660
+ state.diffCursor = cursor;
83661
+ }
83662
+ }
83663
+ state.processedLines = targetEnd;
83664
+ const tokenizationDuration = now() - tokenizationStart;
83665
+ metricsCollector.recordShiki(tokenizationDuration);
83666
+ metricsCollector.recordHighlightForLanguage(resolvedLanguage, tokenizationDuration);
83667
+ metricsCollector.recordLazyTokenization(
83668
+ lazyRequestRangeSize({ ...request, startLine, endLine }),
83669
+ now() - request.requestedAt,
83670
+ lazyTokenizationQueue.size
83671
+ );
83672
+ const updated = cloneBlock(block);
83673
+ updated.payload.highlightedHtml = void 0;
83674
+ const effectiveLang = wantsHtml ? resolvedLanguage : resolvedTokenLanguage;
83675
+ const nextMeta = { ...updated.payload.meta ?? {}, ...meta2, lang: effectiveLang };
83676
+ nextMeta.lazyTokenization = true;
83677
+ nextMeta.lazyTokenizedUntil = state.processedLines;
83678
+ if (wantsHtml) {
83679
+ nextMeta.highlightedLines = state.highlightedLines;
83680
+ } else if ("highlightedLines" in nextMeta) {
83681
+ delete nextMeta.highlightedLines;
83682
+ }
83683
+ if (wantsTokens) {
83684
+ nextMeta.tokenLines = state.tokenLines;
83685
+ if (diffEnabled) {
83686
+ nextMeta.diffKind = state.diffKind;
83687
+ nextMeta.oldNo = state.oldNo;
83688
+ nextMeta.newNo = state.newNo;
83689
+ } else {
83690
+ if ("diffKind" in nextMeta) delete nextMeta.diffKind;
83691
+ if ("oldNo" in nextMeta) delete nextMeta.oldNo;
83692
+ if ("newNo" in nextMeta) delete nextMeta.newNo;
83693
+ }
83694
+ } else if ("tokenLines" in nextMeta) {
83695
+ delete nextMeta.tokenLines;
83696
+ if ("diffKind" in nextMeta) delete nextMeta.diffKind;
83697
+ if ("oldNo" in nextMeta) delete nextMeta.oldNo;
83698
+ if ("newNo" in nextMeta) delete nextMeta.newNo;
83699
+ }
83700
+ updated.payload.meta = nextMeta;
83701
+ blocks[index2] = updated;
83702
+ const nextSnapshot = await blockToNodeSnapshot(updated);
83703
+ const patches = [];
83704
+ diffNodeSnapshot(updated.id, prevSnapshot, nextSnapshot, patches, metricsCollector);
83705
+ if (patches.length > 0) {
83706
+ dispatchPatchBatch(patches, metricsCollector);
83707
+ } else {
83708
+ emitMetricsSample(metricsCollector);
83709
+ if (getActiveMetricsCollector() === metricsCollector) {
83710
+ setActiveMetricsCollector(null);
83711
+ }
83712
+ }
83713
+ }
82841
83714
  async function enrichCodeBlock(block) {
82842
83715
  performanceTimer.mark("highlight-code");
82843
83716
  const metrics = getActiveMetricsCollector();
82844
83717
  const raw2 = block.payload.raw ?? "";
82845
83718
  const { code: code4, info, hadFence } = stripCodeFence(raw2);
82846
83719
  const { lang: lang236, meta: meta2 } = parseCodeFenceInfo(info);
82847
- const requestedLanguage = lang236 || "text";
83720
+ const diffInfo = detectDiffLanguage(lang236, meta2);
83721
+ const requestedLanguage = diffInfo.isDiff ? diffInfo.diffLang : lang236 || "text";
83722
+ const tokenLanguage = diffInfo.isDiff ? diffInfo.baseLang ?? "text" : requestedLanguage;
82848
83723
  const codeBody = hadFence ? code4 : dedentIndentedCode(raw2);
83724
+ const codeLines = extractCodeLines(raw2);
83725
+ const baseMeta = block.payload.meta ?? {};
82849
83726
  let resolvedLanguage = requestedLanguage;
82850
- let cachedHighlight = getHighlightCacheEntry(requestedLanguage, codeBody);
82851
- if (!block.isFinalized && !enableLiveCodeHighlighting) {
83727
+ let resolvedTokenLanguage = tokenLanguage;
83728
+ const hasHighlighter = Boolean(highlighter);
83729
+ const hasHighlightableContent = codeBody.trim().length > 0;
83730
+ const wantsHtml = highlightOutputMode === "html" || highlightOutputMode === "both";
83731
+ const wantsTokens = highlightOutputMode === "tokens" || highlightOutputMode === "both";
83732
+ if (!block.isFinalized) {
83733
+ if (codeHighlightingMode === "final" || !hasHighlighter || !hasHighlightableContent) {
83734
+ resetIncrementalHighlightState(block.id);
83735
+ block.payload.highlightedHtml = void 0;
83736
+ const nextMeta2 = { ...baseMeta, ...meta2, lang: resolvedLanguage };
83737
+ if ("highlightedLines" in nextMeta2) {
83738
+ delete nextMeta2.highlightedLines;
83739
+ }
83740
+ if ("tokenLines" in nextMeta2) {
83741
+ delete nextMeta2.tokenLines;
83742
+ }
83743
+ if ("diffKind" in nextMeta2) {
83744
+ delete nextMeta2.diffKind;
83745
+ }
83746
+ if ("oldNo" in nextMeta2) {
83747
+ delete nextMeta2.oldNo;
83748
+ }
83749
+ if ("newNo" in nextMeta2) {
83750
+ delete nextMeta2.newNo;
83751
+ }
83752
+ block.payload.meta = nextMeta2;
83753
+ return;
83754
+ }
83755
+ if (codeHighlightingMode === "incremental") {
83756
+ if (hasHighlighter) {
83757
+ if (wantsHtml) {
83758
+ resolvedLanguage = await resolveHighlightLanguage(requestedLanguage);
83759
+ }
83760
+ if (wantsTokens) {
83761
+ resolvedTokenLanguage = await resolveHighlightLanguage(tokenLanguage);
83762
+ }
83763
+ }
83764
+ let state = getIncrementalHighlightState(block.id, resolvedLanguage);
83765
+ if (wantsTokens && state.tokenLang && state.tokenLang !== resolvedTokenLanguage) {
83766
+ resetIncrementalHighlightState(block.id);
83767
+ state = getIncrementalHighlightState(block.id, resolvedLanguage);
83768
+ }
83769
+ if (codeBody.length < state.processedLength) {
83770
+ resetIncrementalHighlightState(block.id);
83771
+ state = getIncrementalHighlightState(block.id, resolvedLanguage);
83772
+ }
83773
+ state.tokenLang = wantsTokens ? resolvedTokenLanguage : void 0;
83774
+ const appended = codeBody.slice(state.processedLength);
83775
+ const combined = state.pendingLine + appended;
83776
+ const diffEnabled = diffInfo.isDiff && wantsTokens;
83777
+ if (combined.length > 0) {
83778
+ const parts = combined.split("\n");
83779
+ const completeLines = parts.slice(0, -1);
83780
+ const tail = parts.length > 0 ? parts[parts.length - 1] ?? "" : "";
83781
+ if (completeLines.length > 0) {
83782
+ if (wantsHtml || wantsTokens && !diffEnabled) {
83783
+ try {
83784
+ const sharedLanguage = wantsHtml ? resolvedLanguage : resolvedTokenLanguage;
83785
+ const tokens = highlighter?.codeToTokensWithThemes(completeLines.join("\n"), {
83786
+ lang: sharedLanguage,
83787
+ themes: CODE_HIGHLIGHT_THEMES,
83788
+ grammarState: state.grammarState
83789
+ });
83790
+ if (wantsHtml) {
83791
+ const htmlLines = renderShikiLines(tokens);
83792
+ state.highlightedLines.push(...htmlLines);
83793
+ }
83794
+ if (wantsTokens && !diffEnabled) {
83795
+ const tokenLines2 = renderTokenLines(tokens);
83796
+ state.tokenLines.push(...tokenLines2);
83797
+ }
83798
+ state.grammarState = highlighter?.getLastGrammarState(tokens);
83799
+ } catch (error) {
83800
+ console.warn("Incremental highlighting failed for", requestedLanguage, error);
83801
+ if (wantsHtml) {
83802
+ state.highlightedLines.push(...completeLines.map(() => null));
83803
+ }
83804
+ if (wantsTokens && !diffEnabled) {
83805
+ state.tokenLines.push(...completeLines.map(() => null));
83806
+ }
83807
+ }
83808
+ }
83809
+ if (wantsTokens && diffEnabled) {
83810
+ const cursor = state.diffCursor ?? { oldLine: null, newLine: null };
83811
+ const diffLines = completeLines.map((line) => parseUnifiedDiffLine(line, cursor));
83812
+ state.diffCursor = cursor;
83813
+ state.diffKind.push(...diffLines.map((line) => line.kind));
83814
+ state.oldNo.push(...diffLines.map((line) => line.oldNo ?? null));
83815
+ state.newNo.push(...diffLines.map((line) => line.newNo ?? null));
83816
+ try {
83817
+ const { tokenLines: tokenLines2, grammarState } = buildDiffTokenLines(diffLines, resolvedTokenLanguage, state.diffGrammar ?? {});
83818
+ state.tokenLines.push(...tokenLines2);
83819
+ state.diffGrammar = grammarState;
83820
+ } catch (error) {
83821
+ console.warn("Incremental diff tokenization failed for", tokenLanguage, error);
83822
+ state.tokenLines.push(
83823
+ ...diffLines.map((line) => line.text.length > 0 ? { spans: [{ t: line.text }] } : { spans: [] })
83824
+ );
83825
+ }
83826
+ }
83827
+ }
83828
+ state.pendingLine = tail ?? "";
83829
+ }
83830
+ state.processedLength = codeBody.length;
83831
+ state.lang = resolvedLanguage;
83832
+ block.payload.highlightedHtml = void 0;
83833
+ const effectiveLang2 = wantsHtml ? resolvedLanguage : resolvedTokenLanguage;
83834
+ const nextMeta2 = { ...baseMeta, ...meta2, lang: effectiveLang2 };
83835
+ if (wantsHtml) {
83836
+ nextMeta2.highlightedLines = state.highlightedLines;
83837
+ } else if ("highlightedLines" in nextMeta2) {
83838
+ delete nextMeta2.highlightedLines;
83839
+ }
83840
+ if (wantsTokens) {
83841
+ nextMeta2.tokenLines = state.tokenLines;
83842
+ if (diffEnabled) {
83843
+ nextMeta2.diffKind = state.diffKind;
83844
+ nextMeta2.oldNo = state.oldNo;
83845
+ nextMeta2.newNo = state.newNo;
83846
+ } else {
83847
+ if ("diffKind" in nextMeta2) delete nextMeta2.diffKind;
83848
+ if ("oldNo" in nextMeta2) delete nextMeta2.oldNo;
83849
+ if ("newNo" in nextMeta2) delete nextMeta2.newNo;
83850
+ }
83851
+ } else if ("tokenLines" in nextMeta2) {
83852
+ delete nextMeta2.tokenLines;
83853
+ if ("diffKind" in nextMeta2) delete nextMeta2.diffKind;
83854
+ if ("oldNo" in nextMeta2) delete nextMeta2.oldNo;
83855
+ if ("newNo" in nextMeta2) delete nextMeta2.newNo;
83856
+ }
83857
+ block.payload.meta = nextMeta2;
83858
+ const highlightDuration2 = performanceTimer.measure("highlight-code");
83859
+ metrics?.recordShiki(highlightDuration2);
83860
+ metrics?.recordHighlightForLanguage(resolvedLanguage, highlightDuration2);
83861
+ return;
83862
+ }
83863
+ }
83864
+ resetIncrementalHighlightState(block.id);
83865
+ if (block.isFinalized && shouldLazyTokenizeBlock(codeLines.length, hasHighlighter, hasHighlightableContent) && (wantsHtml || wantsTokens)) {
83866
+ if (hasHighlighter) {
83867
+ if (wantsHtml) {
83868
+ resolvedLanguage = await resolveHighlightLanguage(requestedLanguage);
83869
+ }
83870
+ if (wantsTokens) {
83871
+ resolvedTokenLanguage = await resolveHighlightLanguage(tokenLanguage);
83872
+ }
83873
+ }
83874
+ const diffEnabled = diffInfo.isDiff && wantsTokens;
83875
+ const signature = makeLazySignature(codeBody, resolvedLanguage, resolvedTokenLanguage, diffEnabled);
83876
+ const state = getOrInitLazyState(block.id, signature, codeLines.length, resolvedLanguage, resolvedTokenLanguage, diffEnabled);
83877
+ const effectiveLang2 = wantsHtml ? resolvedLanguage : resolvedTokenLanguage;
83878
+ const nextMeta2 = { ...baseMeta, ...meta2, lang: effectiveLang2 };
83879
+ nextMeta2.lazyTokenization = true;
83880
+ nextMeta2.lazyTokenizedUntil = state.processedLines;
83881
+ if (wantsHtml) {
83882
+ nextMeta2.highlightedLines = state.highlightedLines;
83883
+ } else if ("highlightedLines" in nextMeta2) {
83884
+ delete nextMeta2.highlightedLines;
83885
+ }
83886
+ if (wantsTokens) {
83887
+ nextMeta2.tokenLines = state.tokenLines;
83888
+ if (diffEnabled) {
83889
+ nextMeta2.diffKind = state.diffKind;
83890
+ nextMeta2.oldNo = state.oldNo;
83891
+ nextMeta2.newNo = state.newNo;
83892
+ } else {
83893
+ if ("diffKind" in nextMeta2) delete nextMeta2.diffKind;
83894
+ if ("oldNo" in nextMeta2) delete nextMeta2.oldNo;
83895
+ if ("newNo" in nextMeta2) delete nextMeta2.newNo;
83896
+ }
83897
+ } else if ("tokenLines" in nextMeta2) {
83898
+ delete nextMeta2.tokenLines;
83899
+ if ("diffKind" in nextMeta2) delete nextMeta2.diffKind;
83900
+ if ("oldNo" in nextMeta2) delete nextMeta2.oldNo;
83901
+ if ("newNo" in nextMeta2) delete nextMeta2.newNo;
83902
+ }
82852
83903
  block.payload.highlightedHtml = void 0;
82853
- block.payload.meta = {
82854
- ...meta2,
82855
- lang: resolvedLanguage
82856
- };
83904
+ block.payload.meta = nextMeta2;
82857
83905
  return;
82858
83906
  }
82859
- if (!cachedHighlight && highlighter && codeBody.trim()) {
82860
- try {
82861
- const loadedLangs = highlighter.getLoadedLanguages();
82862
- if (!loadedLangs.includes(requestedLanguage)) {
83907
+ let tokenLines;
83908
+ let diffKind;
83909
+ let diffOldNo;
83910
+ let diffNewNo;
83911
+ if (wantsHtml) {
83912
+ let cachedHighlight = block.isFinalized ? getHighlightCacheEntry(requestedLanguage, codeBody) : null;
83913
+ if (!cachedHighlight && highlighter && hasHighlightableContent) {
83914
+ try {
83915
+ resolvedLanguage = await resolveHighlightLanguage(requestedLanguage);
83916
+ const highlighted = highlighter.codeToHtml(codeBody, {
83917
+ lang: resolvedLanguage,
83918
+ themes: CODE_HIGHLIGHT_THEMES,
83919
+ defaultColor: false
83920
+ });
83921
+ const enhanced = enhanceHighlightedHtml(highlighted, resolvedLanguage);
83922
+ block.payload.highlightedHtml = enhanced;
83923
+ if (block.isFinalized) {
83924
+ setHighlightCacheEntry(resolvedLanguage, codeBody, enhanced);
83925
+ if (resolvedLanguage !== requestedLanguage) {
83926
+ setHighlightCacheEntry(requestedLanguage, codeBody, enhanced);
83927
+ }
83928
+ }
83929
+ cachedHighlight = getHighlightCacheEntry(resolvedLanguage, codeBody);
83930
+ } catch (error) {
83931
+ console.warn("Highlighting failed for", requestedLanguage, error);
83932
+ resolvedLanguage = "text";
83933
+ }
83934
+ }
83935
+ if (cachedHighlight) {
83936
+ resolvedLanguage = cachedHighlight.lang;
83937
+ block.payload.highlightedHtml = cachedHighlight.html;
83938
+ }
83939
+ } else {
83940
+ block.payload.highlightedHtml = void 0;
83941
+ }
83942
+ if (wantsTokens) {
83943
+ if (diffInfo.isDiff) {
83944
+ if (hasHighlighter) {
83945
+ resolvedTokenLanguage = await resolveHighlightLanguage(tokenLanguage);
83946
+ }
83947
+ const diffLines = codeBody.length > 0 ? codeBody.split("\n") : [];
83948
+ const cursor = { oldLine: null, newLine: null };
83949
+ const diffInfoLines = diffLines.map((line) => parseUnifiedDiffLine(line, cursor));
83950
+ diffKind = diffInfoLines.map((line) => line.kind);
83951
+ diffOldNo = diffInfoLines.map((line) => line.oldNo ?? null);
83952
+ diffNewNo = diffInfoLines.map((line) => line.newNo ?? null);
83953
+ if (!hasHighlighter || !hasHighlightableContent) {
83954
+ tokenLines = diffInfoLines.map((line) => line.text.length > 0 ? { spans: [{ t: line.text }] } : { spans: [] });
83955
+ } else {
82863
83956
  try {
82864
- await highlighter.loadLanguage(requestedLanguage);
82865
- } catch (loadError) {
82866
- console.warn(`Failed to load language ${requestedLanguage}, falling back to text:`, loadError);
83957
+ const result = buildDiffTokenLines(diffInfoLines, resolvedTokenLanguage, {});
83958
+ tokenLines = result.tokenLines;
83959
+ } catch (error) {
83960
+ console.warn("Diff tokenization failed for", tokenLanguage, error);
83961
+ tokenLines = diffInfoLines.map((line) => line.text.length > 0 ? { spans: [{ t: line.text }] } : { spans: [] });
82867
83962
  }
82868
83963
  }
82869
- resolvedLanguage = loadedLangs.includes(requestedLanguage) ? requestedLanguage : "text";
82870
- const highlighted = highlighter.codeToHtml(codeBody, {
82871
- lang: resolvedLanguage,
82872
- themes: {
82873
- dark: "github-dark",
82874
- light: "github-light"
82875
- },
82876
- defaultColor: false
82877
- });
82878
- const enhanced = enhanceHighlightedHtml(highlighted, resolvedLanguage);
82879
- block.payload.highlightedHtml = enhanced;
82880
- if (block.isFinalized) {
82881
- setHighlightCacheEntry(resolvedLanguage, codeBody, enhanced);
82882
- if (resolvedLanguage !== requestedLanguage) {
82883
- setHighlightCacheEntry(requestedLanguage, codeBody, enhanced);
83964
+ } else {
83965
+ const lineCount = codeBody.length > 0 ? codeBody.split("\n").length : 0;
83966
+ if (!hasHighlighter || !hasHighlightableContent) {
83967
+ tokenLines = new Array(lineCount).fill(null);
83968
+ } else {
83969
+ try {
83970
+ resolvedTokenLanguage = await resolveHighlightLanguage(tokenLanguage);
83971
+ const tokens = highlighter?.codeToTokensWithThemes(codeBody, {
83972
+ lang: resolvedTokenLanguage,
83973
+ themes: CODE_HIGHLIGHT_THEMES
83974
+ });
83975
+ tokenLines = renderTokenLines(tokens);
83976
+ } catch (error) {
83977
+ console.warn("Tokenization failed for", requestedLanguage, error);
83978
+ tokenLines = new Array(lineCount).fill(null);
82884
83979
  }
82885
83980
  }
82886
- cachedHighlight = getHighlightCacheEntry(resolvedLanguage, codeBody);
82887
- } catch (error) {
82888
- console.warn("Highlighting failed for", requestedLanguage, error);
82889
- resolvedLanguage = "text";
82890
83981
  }
82891
83982
  }
82892
- if (cachedHighlight) {
82893
- resolvedLanguage = cachedHighlight.lang;
82894
- block.payload.highlightedHtml = cachedHighlight.html;
83983
+ const effectiveLang = wantsHtml ? resolvedLanguage : resolvedTokenLanguage;
83984
+ const nextMeta = { ...baseMeta, ...meta2, lang: effectiveLang };
83985
+ if ("highlightedLines" in nextMeta) {
83986
+ delete nextMeta.highlightedLines;
82895
83987
  }
82896
- block.payload.meta = {
82897
- ...meta2,
82898
- lang: resolvedLanguage
82899
- };
83988
+ if (wantsTokens) {
83989
+ nextMeta.tokenLines = tokenLines ?? [];
83990
+ if (diffKind) {
83991
+ nextMeta.diffKind = diffKind;
83992
+ nextMeta.oldNo = diffOldNo ?? [];
83993
+ nextMeta.newNo = diffNewNo ?? [];
83994
+ } else {
83995
+ if ("diffKind" in nextMeta) delete nextMeta.diffKind;
83996
+ if ("oldNo" in nextMeta) delete nextMeta.oldNo;
83997
+ if ("newNo" in nextMeta) delete nextMeta.newNo;
83998
+ }
83999
+ } else if ("tokenLines" in nextMeta) {
84000
+ delete nextMeta.tokenLines;
84001
+ if ("diffKind" in nextMeta) delete nextMeta.diffKind;
84002
+ if ("oldNo" in nextMeta) delete nextMeta.oldNo;
84003
+ if ("newNo" in nextMeta) delete nextMeta.newNo;
84004
+ }
84005
+ block.payload.meta = nextMeta;
82900
84006
  const highlightDuration = performanceTimer.measure("highlight-code");
82901
84007
  metrics?.recordShiki(highlightDuration);
82902
84008
  metrics?.recordHighlightForLanguage(resolvedLanguage, highlightDuration);
@@ -83149,7 +84255,12 @@ function enrichListBlock(block) {
83149
84255
  }
83150
84256
  const items = itemsRaw.map((raw2) => inlineParser.parse(raw2));
83151
84257
  const isOrdered = /^\d+\./.test(lines[0]?.trimStart() || "");
83152
- block.payload.meta = { ordered: isOrdered, items };
84258
+ block.payload.meta = {
84259
+ ordered: isOrdered,
84260
+ items,
84261
+ formatAnticipation: block.isFinalized ? void 0 : formatAnticipationConfig,
84262
+ mathEnabled: enableMath
84263
+ };
83153
84264
  }
83154
84265
  function runDocumentPlugins(inputBlocks, content4) {
83155
84266
  const blocks2 = inputBlocks.slice();
@@ -83492,6 +84603,10 @@ function diffNodeSnapshot(blockId, prevNode, nextNode, patches, metrics) {
83492
84603
  if (onlyAppend && nextChildren.length > prevChildren.length) {
83493
84604
  const startIndex = prevChildren.length;
83494
84605
  const appended = nextChildren.slice(startIndex);
84606
+ const hasTokenLines = appended.some((child) => Object.prototype.hasOwnProperty.call(child.props ?? {}, "tokens"));
84607
+ const hasDiffKind = appended.some((child) => Object.prototype.hasOwnProperty.call(child.props ?? {}, "diffKind"));
84608
+ const hasOldNo = appended.some((child) => Object.prototype.hasOwnProperty.call(child.props ?? {}, "oldNo"));
84609
+ const hasNewNo = appended.some((child) => Object.prototype.hasOwnProperty.call(child.props ?? {}, "newNo"));
83495
84610
  patches.push({
83496
84611
  op: "appendLines",
83497
84612
  at: { blockId, nodeId: prevNode.id },
@@ -83500,7 +84615,27 @@ function diffNodeSnapshot(blockId, prevNode, nextNode, patches, metrics) {
83500
84615
  const text12 = typeof child.props?.text === "string" ? child.props?.text : "";
83501
84616
  return text12;
83502
84617
  }),
83503
- highlight: appended.map((child) => typeof child.props?.html === "string" ? child.props?.html : null)
84618
+ highlight: appended.map((child) => typeof child.props?.html === "string" ? child.props?.html : null),
84619
+ ...hasTokenLines ? {
84620
+ tokens: appended.map(
84621
+ (child) => Object.prototype.hasOwnProperty.call(child.props ?? {}, "tokens") ? child.props?.tokens : null
84622
+ )
84623
+ } : {},
84624
+ ...hasDiffKind ? {
84625
+ diffKind: appended.map(
84626
+ (child) => Object.prototype.hasOwnProperty.call(child.props ?? {}, "diffKind") ? child.props?.diffKind : null
84627
+ )
84628
+ } : {},
84629
+ ...hasOldNo ? {
84630
+ oldNo: appended.map(
84631
+ (child) => Object.prototype.hasOwnProperty.call(child.props ?? {}, "oldNo") ? child.props?.oldNo : null
84632
+ )
84633
+ } : {},
84634
+ ...hasNewNo ? {
84635
+ newNo: appended.map(
84636
+ (child) => Object.prototype.hasOwnProperty.call(child.props ?? {}, "newNo") ? child.props?.newNo : null
84637
+ )
84638
+ } : {}
83504
84639
  });
83505
84640
  metrics?.recordAppendLines(appended.length);
83506
84641
  return;
@@ -84019,6 +85154,17 @@ async function processWorkerMessage(msg) {
84019
85154
  });
84020
85155
  return;
84021
85156
  }
85157
+ case "TOKENIZE_RANGE": {
85158
+ const priority = msg.priority === "prefetch" ? "prefetch" : "visible";
85159
+ enqueueLazyTokenization({
85160
+ blockId: msg.blockId,
85161
+ startLine: msg.startLine,
85162
+ endLine: msg.endLine,
85163
+ priority,
85164
+ requestedAt: now()
85165
+ });
85166
+ return;
85167
+ }
84022
85168
  case "MDX_COMPILED":
84023
85169
  handleMdxStatus(msg.blockId, {
84024
85170
  compiledRef: { id: msg.compiledId },