@mdream/js 1.5.6 → 1.5.8

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.
@@ -97,6 +97,7 @@ const TagIdMap = {
97
97
  small: 83,
98
98
  noscript: 84,
99
99
  noframes: 85,
100
+ noembed: 112,
100
101
  xmp: 86,
101
102
  plaintext: 87,
102
103
  aside: 88,
@@ -122,9 +123,12 @@ const TagIdMap = {
122
123
  datalist: 108,
123
124
  optgroup: 109
124
125
  };
126
+ function isInsideRawHtmlBlock(depthMap) {
127
+ return Boolean(depthMap[2] || depthMap[3] || depthMap[100] || depthMap[101] || depthMap[99] || depthMap[98]);
128
+ }
125
129
  const NO_SPACING = [0, 0];
126
130
  const DEFAULT_BLOCK_SPACING = [2, 2];
127
- const BLOCKQUOTE_SPACING = [1, 1];
131
+ const BLOCKQUOTE_SPACING = [2, 2];
128
132
  const LIST_ITEM_SPACING = [1, 0];
129
133
  const TABLE_ROW_SPACING = [0, 1];
130
- export { BLOCKQUOTE_SPACING, DEFAULT_BLOCK_SPACING, ELEMENT_NODE, LIST_ITEM_SPACING, NO_SPACING, NodeEventEnter, NodeEventExit, TABLE_ROW_SPACING, TAG_H1, TAG_H2, TAG_H3, TAG_H4, TAG_H5, TAG_H6, TEXT_NODE, TagIdMap };
134
+ export { BLOCKQUOTE_SPACING, DEFAULT_BLOCK_SPACING, ELEMENT_NODE, LIST_ITEM_SPACING, NO_SPACING, NodeEventEnter, NodeEventExit, TABLE_ROW_SPACING, TAG_H1, TAG_H2, TAG_H3, TAG_H4, TAG_H5, TAG_H6, TEXT_NODE, TagIdMap, isInsideRawHtmlBlock };
@@ -1,4 +1,4 @@
1
- import { DEFAULT_BLOCK_SPACING, NO_SPACING } from "./const.mjs";
1
+ import { DEFAULT_BLOCK_SPACING, NO_SPACING, isInsideRawHtmlBlock } from "./const.mjs";
2
2
  import { breakHandler, continuationPrefix, finalizeParse, parseHtmlStream, renderBreak, resolveUrl } from "./parse.mjs";
3
3
  function processPluginsForEvent(event, plugins, state, processEvent) {
4
4
  if (plugins?.length) {
@@ -35,7 +35,7 @@ function processPluginsForEvent(event, plugins, state, processEvent) {
35
35
  processEvent(event);
36
36
  return false;
37
37
  }
38
- const INLINE_MARKER_TYPE = /* @__PURE__ */ new Uint8Array(112);
38
+ const INLINE_MARKER_TYPE = /* @__PURE__ */ new Uint8Array(113);
39
39
  INLINE_MARKER_TYPE[14] = 1;
40
40
  INLINE_MARKER_TYPE[15] = 1;
41
41
  INLINE_MARKER_TYPE[91] = 1;
@@ -129,12 +129,35 @@ function foldPreLinesToBr(value) {
129
129
  else if (c === 38) out += "&";
130
130
  else if (c === 60) out += "<";
131
131
  else if (c === 62) out += ">";
132
+ else if (c === 124) out += "|";
132
133
  else out += value[i];
133
134
  }
134
135
  return out;
135
136
  }
136
- function isInsideRawHtmlBlock(depthMap) {
137
- return Boolean(depthMap[2] || depthMap[3] || depthMap[100] || depthMap[101] || depthMap[99] || depthMap[98]);
137
+ function escapeRawHtmlText(value, depthMap) {
138
+ const inTable = Boolean(depthMap[28]);
139
+ const inLink = Boolean(depthMap[26]);
140
+ let escaped = "";
141
+ let copiedUntil = 0;
142
+ let index = 0;
143
+ while (index < value.length) {
144
+ const code = value.charCodeAt(index);
145
+ let replacement;
146
+ if (code === 38) replacement = "&amp;";
147
+ else if (code === 60) replacement = "&lt;";
148
+ else if (code === 62) replacement = "&gt;";
149
+ else if (code === 10) replacement = "&#10;";
150
+ else if (code === 13) replacement = "&#13;";
151
+ else if (inTable && code === 124) replacement = "&#124;";
152
+ else if (inLink && code === 91) replacement = "&#91;";
153
+ else if (inLink && code === 93) replacement = "&#93;";
154
+ if (replacement) {
155
+ escaped += value.slice(copiedUntil, index) + replacement;
156
+ copiedUntil = index + 1;
157
+ }
158
+ index++;
159
+ }
160
+ return copiedUntil === 0 ? value : escaped + value.slice(copiedUntil);
138
161
  }
139
162
  function markdownLineIndent(buffer) {
140
163
  let spaces = 0;
@@ -165,9 +188,6 @@ function isThematicBreak(value, start, marker) {
165
188
  }
166
189
  return count >= 3;
167
190
  }
168
- function isParserProtectedEscape(depthMap, next) {
169
- return next === 124 && Boolean(depthMap[28]) || (next === 91 || next === 93) && Boolean(depthMap[26]) || next === 62 && Boolean(depthMap[22]);
170
- }
171
191
  function isEntityReferenceAfterAmpersand(value, ampersand) {
172
192
  let index = ampersand + 1;
173
193
  if (value.charCodeAt(index) === 35) {
@@ -191,8 +211,9 @@ function isEntityReferenceAfterAmpersand(value, ampersand) {
191
211
  return index > start && value.charCodeAt(index) === 59;
192
212
  }
193
213
  const GFM_TEXT_NATIVE_TRIGGER = /[\\*_~`[<\r\n]/;
194
- function mayNeedGfmTextEscape(value, buffer) {
214
+ function mayNeedGfmTextEscape(value, buffer, depthMap) {
195
215
  if (value.search(GFM_TEXT_NATIVE_TRIGGER) !== -1) return true;
216
+ if (depthMap[28] && value.includes("|") || depthMap[26] && value.includes("]") || depthMap[22] && value.includes(">")) return true;
196
217
  let lineIndent = markdownLineIndent(buffer);
197
218
  if (lineIndent < 0) return false;
198
219
  let index = 0;
@@ -205,6 +226,9 @@ function mayNeedGfmTextEscape(value, buffer) {
205
226
  return code === 35 || code === 43 || code === 45 || code === 62 || code >= 48 && code <= 57;
206
227
  }
207
228
  function escapeGfmText(value, buffer, depthMap) {
229
+ const inTable = Boolean(depthMap[28]);
230
+ const inLink = Boolean(depthMap[26]);
231
+ const inBlockquote = Boolean(depthMap[22]);
208
232
  let lineIndent = markdownLineIndent(buffer);
209
233
  let orderedDigits = 0;
210
234
  let copiedUntil = 0;
@@ -212,13 +236,19 @@ function escapeGfmText(value, buffer, depthMap) {
212
236
  for (let index = 0; index < value.length; index++) {
213
237
  const code = value.charCodeAt(index);
214
238
  const next = index + 1 < value.length ? value.charCodeAt(index + 1) : 0;
215
- if (code === 92 && (isParserProtectedEscape(depthMap, next) || next === 38 && isEntityReferenceAfterAmpersand(value, index + 1))) {
239
+ if (code === 92 && next === 38 && isEntityReferenceAfterAmpersand(value, index + 1)) {
216
240
  lineIndent = -1;
217
241
  orderedDigits = 0;
218
242
  index++;
219
243
  continue;
220
244
  }
221
- let shouldEscape = code === 92 || code === 42 || code === 95 || code === 126 || code === 96 || code === 91 || code === 60 && canStartGfmAngleConstruct(value.charCodeAt(index + 1));
245
+ if (inTable && (code === 10 || code === 13)) {
246
+ escaped += value.slice(copiedUntil, index);
247
+ escaped += code === 10 ? "&#10;" : "&#13;";
248
+ copiedUntil = index + 1;
249
+ continue;
250
+ }
251
+ let shouldEscape = code === 92 || code === 42 || code === 95 || code === 126 || code === 96 || code === 91 || code === 93 && inLink || code === 124 && inTable || code === 62 && inBlockquote || code === 60 && canStartGfmAngleConstruct(value.charCodeAt(index + 1));
222
252
  if (!shouldEscape && lineIndent >= 0) {
223
253
  if (code === 35) {
224
254
  let end = index + 1;
@@ -393,6 +423,7 @@ function startCodeFence(state, fragment, language, indent) {
393
423
  indent,
394
424
  language
395
425
  };
426
+ state.onCodeFenceOpen?.(language);
396
427
  }
397
428
  function finalizeCodeFence(state) {
398
429
  const fence = state.codeFence;
@@ -550,7 +581,7 @@ function createMarkdownProcessor(options = {}, resolvedPlugins = [], tagOverride
550
581
  const state = {
551
582
  options,
552
583
  buffer: [],
553
- depthMap: /* @__PURE__ */ new Uint8Array(112),
584
+ depthMap: /* @__PURE__ */ new Uint16Array(113),
554
585
  listIndent: "",
555
586
  listIndentWidths: [],
556
587
  blockquotes: [],
@@ -582,7 +613,9 @@ function createMarkdownProcessor(options = {}, resolvedPlugins = [], tagOverride
582
613
  textNode.value = value;
583
614
  }
584
615
  if (state.depthMap[34] > 0 && (state.depthMap[32] > 0 || state.depthMap[31] > 0)) textNode.value = foldPreLinesToBr(textNode.value);
585
- if (!state.plainText && !state.depthMap[34] && !state.depthMap[23] && !isInsideRawHtmlBlock(state.depthMap) && mayNeedGfmTextEscape(textNode.value, state.buffer)) textNode.value = escapeGfmText(textNode.value, state.buffer, state.depthMap);
616
+ const insideRawHtmlBlock = isInsideRawHtmlBlock(state.depthMap);
617
+ if (!state.plainText && !state.depthMap[34] && insideRawHtmlBlock) textNode.value = escapeRawHtmlText(textNode.value, state.depthMap);
618
+ if (!state.plainText && !state.depthMap[34] && !state.depthMap[23] && !insideRawHtmlBlock && mayNeedGfmTextEscape(textNode.value, state.buffer, state.depthMap)) textNode.value = escapeGfmText(textNode.value, state.buffer, state.depthMap);
586
619
  if (textNode.generatedMarkdown && textNode.value) textNode.value = `${textNode.generatedMarkdown.prefix}${textNode.value}${textNode.generatedMarkdown.suffix}`;
587
620
  const wrapWidth = state.options?.wrapWidth;
588
621
  if (wrapWidth && canWrapHere(state.depthMap)) {
@@ -691,7 +724,7 @@ function createMarkdownProcessor(options = {}, resolvedPlugins = [], tagOverride
691
724
  if (secondLastChar === "\n") lastNewLines++;
692
725
  if (eventType === 1 && openMarkerCount) {
693
726
  const markerType = handlerOutput === void 0 ? 0 : INLINE_MARKER_TYPE[element.tagId];
694
- if (markerType && (element.tagId !== 23 || !state.depthMap[34]) && !handler?.literalExit) {
727
+ if (markerType && (element.tagId !== 23 || !state.depthMap[34] && !isInsideRawHtmlBlock(state.depthMap)) && !handler?.literalExit) {
695
728
  const idx = dropEmptyMarker(buff, openMarkers[--openMarkerCount], markerType);
696
729
  if (idx >= 0) {
697
730
  state.lastContentCache = idx > 0 ? buff[idx - 1] : void 0;
@@ -702,7 +735,8 @@ function createMarkdownProcessor(options = {}, resolvedPlugins = [], tagOverride
702
735
  }
703
736
  if (element.tagId === 6 && !state.depthMap[34] && output?.length === 1 && output[0] === "\n" && lastChar === "\n" && secondLastChar === "\n") output = void 0;
704
737
  const newLineConfig = calculateNewLineConfig(node, state.depthMap, state.plainText === true);
705
- const configuredNewLines = eventType === 0 && state.blockquotes.at(-1)?.fragment === state.buffer.length ? 0 : eventType === 1 && element.tagId === 13 && state.blockquotes.length > 0 ? Math.min(1, newLineConfig[eventType] || 0) : newLineConfig[eventType] || 0;
738
+ const quoteAtStart = eventType === 0 && state.blockquotes.at(-1)?.fragment === state.buffer.length;
739
+ const configuredNewLines = quoteAtStart ? 0 : eventType === 1 && element.tagId === 13 && state.blockquotes.length > 0 ? Math.min(1, newLineConfig[eventType] || 0) : newLineConfig[eventType] || 0;
706
740
  let effectiveLastNewLines = lastNewLines;
707
741
  if (eventType === 1 && output) for (let i = output.length - 1; i >= 0; i--) {
708
742
  const frag = output[i];
@@ -757,7 +791,7 @@ function createMarkdownProcessor(options = {}, resolvedPlugins = [], tagOverride
757
791
  const collapsesWhiteSpace = node.tagHandler?.collapsesInnerWhiteSpace;
758
792
  const hasSpacing = node.tagHandler?.spacing && Array.isArray(node.tagHandler.spacing);
759
793
  const isBlockElement = !isInlineElement && !collapsesWhiteSpace && configuredNewLines > 0;
760
- if (element.tagId === 6 && output?.[0]?.endsWith("\n") && !parentInPre || (!isInlineElement || eventType === 1) && !isBlockElement && !(collapsesWhiteSpace && eventType === 0) && !(hasSpacing && eventType === 0)) {
794
+ if (!quoteAtStart && (element.tagId === 6 && output?.[0]?.endsWith("\n") && !parentInPre || (!isInlineElement || eventType === 1) && !isBlockElement && !(collapsesWhiteSpace && eventType === 0) && !(hasSpacing && eventType === 0))) {
761
795
  const originalLength = lastFragment.length;
762
796
  const trimmed = trimAsciiWhitespaceEnd(lastFragment);
763
797
  if (originalLength - trimmed.length > 0) {
@@ -784,7 +818,7 @@ function createMarkdownProcessor(options = {}, resolvedPlugins = [], tagOverride
784
818
  if (eventType === 0 && element.tagId === 26 && handlerOutput === "[" && buff.at(-1) === "[") openLinkFragment = buff.length - 1;
785
819
  if (eventType === 0 && handlerOutput !== void 0 && isInlineElement && !handler?.literalEnter) {
786
820
  const markerType = INLINE_MARKER_TYPE[element.tagId];
787
- if (markerType && (element.tagId !== 23 || !state.depthMap[34]) && buff[buff.length - 1] === handlerOutput) openMarkers[openMarkerCount++] = buff.length - 1 << 3 | markerType;
821
+ if (markerType && (element.tagId !== 23 || !state.depthMap[34] && !isInsideRawHtmlBlock(state.depthMap)) && buff[buff.length - 1] === handlerOutput) openMarkers[openMarkerCount++] = buff.length - 1 << 3 | markerType;
788
822
  else if (openMarkerCount && hasNonWhitespace(handlerOutput)) openMarkerCount = 0;
789
823
  } else if (openMarkerCount && (handler?.literalExit || element.tagId !== -1 && !isInlineElement || output?.some(hasNonWhitespace))) openMarkerCount = 0;
790
824
  if (eventType === 1 && element.tagId === 26) openLinkFragment = -1;
@@ -1,4 +1,4 @@
1
- import { BLOCKQUOTE_SPACING, LIST_ITEM_SPACING, NO_SPACING, TABLE_ROW_SPACING, TagIdMap } from "./const.mjs";
1
+ import { BLOCKQUOTE_SPACING, LIST_ITEM_SPACING, NO_SPACING, TABLE_ROW_SPACING, TagIdMap, isInsideRawHtmlBlock } from "./const.mjs";
2
2
  const HTML_ENTITIES = {
3
3
  AElig: "Æ",
4
4
  AMP: "&",
@@ -289,6 +289,53 @@ const C1_REPLACEMENTS = "€‚ƒ„…†‡ˆ‰Š‹ŒŽ‘’“”
289
289
  function isAsciiAlphaNumeric(code) {
290
290
  return code >= 48 && code <= 57 || code >= 65 && code <= 90 || code >= 97 && code <= 122;
291
291
  }
292
+ function isHtmlAsciiWhitespace(code) {
293
+ return code === 32 || code === 9 || code === 10 || code === 12 || code === 13;
294
+ }
295
+ const LANGUAGE_PREFIX = [
296
+ 108,
297
+ 97,
298
+ 110,
299
+ 103,
300
+ 117,
301
+ 97,
302
+ 103,
303
+ 101,
304
+ 45
305
+ ];
306
+ function hasLanguagePrefix(value, start) {
307
+ for (let offset = 0; offset < 9; offset++) if (value.charCodeAt(start + offset) !== LANGUAGE_PREFIX[offset]) return false;
308
+ return true;
309
+ }
310
+ function isUnsafeFenceInfoCode(code) {
311
+ if (code < 32 || code === 127) return true;
312
+ return code === 34 || code === 39 || code === 38 || code === 60 || code === 62 || code === 96 || code === 126;
313
+ }
314
+ function getLanguageFromClass(className) {
315
+ if (!className) return "";
316
+ const length = className.length;
317
+ let index = 0;
318
+ while (index < length) {
319
+ if (isHtmlAsciiWhitespace(className.charCodeAt(index))) {
320
+ index++;
321
+ continue;
322
+ }
323
+ const tokenStart = index;
324
+ do
325
+ index++;
326
+ while (index < length && !isHtmlAsciiWhitespace(className.charCodeAt(index)));
327
+ const tokenEnd = index;
328
+ const languageStart = tokenStart + 9;
329
+ if (languageStart >= tokenEnd || !hasLanguagePrefix(className, tokenStart)) continue;
330
+ let valid = true;
331
+ for (let cursor = languageStart; cursor < tokenEnd; cursor++) if (isUnsafeFenceInfoCode(className.charCodeAt(cursor))) {
332
+ valid = false;
333
+ break;
334
+ }
335
+ if (valid) return className.slice(languageStart, tokenEnd);
336
+ }
337
+ return "";
338
+ }
292
339
  function numericReplacement(codePoint) {
293
340
  if (codePoint === 0 || codePoint > 1114111 || codePoint >= 55296 && codePoint <= 57343) return "�";
294
341
  if (codePoint >= 128 && codePoint <= 159) return C1_REPLACEMENTS[codePoint - 128];
@@ -446,15 +493,11 @@ function isInsideTableCell(state) {
446
493
  const depthMap = state.depthMap;
447
494
  return depthMap[32] > 0 || depthMap[31] > 0;
448
495
  }
449
- function isInsideRawHtmlBlock(state) {
450
- const depthMap = state.depthMap;
451
- return Boolean(depthMap[2] || depthMap[3] || depthMap[100] || depthMap[101] || depthMap[99] || depthMap[98]);
452
- }
453
496
  function renderBreak(node, state) {
454
497
  const depthMap = state.depthMap;
455
- if (isInsideTableCell(state) || isInsideRawHtmlBlock(state) || depthMap[7] || depthMap[8] || depthMap[9] || depthMap[10] || depthMap[11] || depthMap[12]) return "<br>";
498
+ if (isInsideTableCell(state) || isInsideRawHtmlBlock(depthMap) || depthMap[7] || depthMap[8] || depthMap[9] || depthMap[10] || depthMap[11] || depthMap[12]) return "<br>";
456
499
  if (depthMap[34] || depthMap[23]) return "\n";
457
- return `\\\n${continuationPrefix(node, state.listIndentWidths || [], !state.bufferedBlockquoteDepth)}`;
500
+ return ` \n${continuationPrefix(node, state.listIndentWidths || [], !state.bufferedBlockquoteDepth)}`;
458
501
  }
459
502
  const breakHandler = {
460
503
  enter: ({ node, state }) => renderBreak(node, state),
@@ -463,11 +506,6 @@ const breakHandler = {
463
506
  collapsesInnerWhiteSpace: true,
464
507
  isInline: true
465
508
  };
466
- function getLanguageFromClass(className) {
467
- if (!className) return "";
468
- const langParts = className.split(" ").map((c) => c.split("language-")[1]).filter(Boolean);
469
- return langParts && langParts.length > 0 ? langParts[0].trim() : "";
470
- }
471
509
  function handleHeading(depth) {
472
510
  return {
473
511
  enter: ({ state }) => {
@@ -513,6 +551,11 @@ const Strikethrough = {
513
551
  spacing: NO_SPACING,
514
552
  isInline: true
515
553
  };
554
+ const INERT_RAWTEXT = {
555
+ isNonNesting: true,
556
+ excludesTextNodes: true,
557
+ spacing: NO_SPACING
558
+ };
516
559
  const tagHandlers = {
517
560
  [1]: {
518
561
  spacing: NO_SPACING,
@@ -623,6 +666,7 @@ const tagHandlers = {
623
666
  output: `\`\`\`${language}\n`
624
667
  };
625
668
  }
669
+ if (isInsideRawHtmlBlock(state.depthMap)) return "<code>";
626
670
  if ((state.depthMap?.[25] || 0) > 0) {
627
671
  const lastEntry = state.buffer.at(-1);
628
672
  const lastChar = lastEntry?.charAt(lastEntry.length - 1) || "";
@@ -652,6 +696,7 @@ const tagHandlers = {
652
696
  output: `\n\`\`\``
653
697
  };
654
698
  }
699
+ if (isInsideRawHtmlBlock(state.depthMap)) return "</code>";
655
700
  return { _tag: "CodeSpanExit" };
656
701
  },
657
702
  collapsesInnerWhiteSpace: true,
@@ -912,10 +957,7 @@ const tagHandlers = {
912
957
  [70]: { spacing: NO_SPACING },
913
958
  [71]: { spacing: NO_SPACING },
914
959
  [72]: { spacing: NO_SPACING },
915
- [73]: {
916
- isNonNesting: true,
917
- spacing: NO_SPACING
918
- },
960
+ [73]: { ...INERT_RAWTEXT },
919
961
  [74]: { spacing: NO_SPACING },
920
962
  [75]: { spacing: NO_SPACING },
921
963
  [76]: { spacing: NO_SPACING },
@@ -959,19 +1001,14 @@ const tagHandlers = {
959
1001
  spacing: NO_SPACING,
960
1002
  isInline: true
961
1003
  },
962
- [84]: {
963
- excludesTextNodes: true,
964
- spacing: NO_SPACING
965
- },
1004
+ [84]: { ...INERT_RAWTEXT },
1005
+ [112]: { ...INERT_RAWTEXT },
966
1006
  [108]: {
967
1007
  isNonNesting: true,
968
1008
  excludesTextNodes: true,
969
1009
  spacing: NO_SPACING
970
1010
  },
971
- [85]: {
972
- isNonNesting: true,
973
- spacing: NO_SPACING
974
- },
1011
+ [85]: { ...INERT_RAWTEXT },
975
1012
  [86]: {
976
1013
  isNonNesting: true,
977
1014
  spacing: NO_SPACING
@@ -1127,20 +1164,27 @@ const EQUALS_CHAR = 61;
1127
1164
  const QUOTE_CHAR = 34;
1128
1165
  const APOS_CHAR = 39;
1129
1166
  const EXCLAMATION_CHAR = 33;
1167
+ const QUESTION_CHAR = 63;
1130
1168
  const AMPERSAND_CHAR = 38;
1131
- const BACKSLASH_CHAR = 92;
1132
1169
  const DASH_CHAR = 45;
1133
1170
  const SPACE_CHAR = 32;
1134
1171
  const TAB_CHAR = 9;
1135
1172
  const NEWLINE_CHAR = 10;
1136
1173
  const FORM_FEED_CHAR = 12;
1137
1174
  const CARRIAGE_RETURN_CHAR = 13;
1138
- const PIPE_CHAR = 124;
1139
1175
  const OPEN_BRACKET_CHAR = 91;
1140
- const CLOSE_BRACKET_CHAR = 93;
1176
+ const ATTRIBUTE_GAP = 0;
1177
+ const ATTRIBUTE_NAME = 1;
1178
+ const ATTRIBUTE_AFTER_NAME = 2;
1179
+ const ATTRIBUTE_BEFORE_VALUE = 3;
1180
+ const ATTRIBUTE_QUOTED_VALUE = 4;
1181
+ const ATTRIBUTE_UNQUOTED_VALUE = 5;
1182
+ function isAsciiAlpha(code) {
1183
+ return code >= 65 && code <= 90 || code >= 97 && code <= 122;
1184
+ }
1141
1185
  function shouldProtectDecodedEntityReferences(state) {
1142
1186
  const depthMap = state.depthMap;
1143
- return !state.plainText && !depthMap[34] && !depthMap[23] && !depthMap[2] && !depthMap[3] && !depthMap[100] && !depthMap[101] && !depthMap[99] && !depthMap[98];
1187
+ return !state.plainText && !depthMap[34] && !depthMap[23] && !isInsideRawHtmlBlock(depthMap);
1144
1188
  }
1145
1189
  const SCRIPT_DATA = 0;
1146
1190
  const SCRIPT_DATA_ESCAPED = 1;
@@ -1164,7 +1208,7 @@ const HEAD_CONTENT_TAGS = /* @__PURE__ */ new Set([
1164
1208
  78
1165
1209
  ]);
1166
1210
  const CLOSES_P = (() => {
1167
- const t = /* @__PURE__ */ new Uint8Array(112);
1211
+ const t = /* @__PURE__ */ new Uint8Array(113);
1168
1212
  for (const id of [
1169
1213
  36,
1170
1214
  35,
@@ -1327,6 +1371,14 @@ const SECTION_CELLS = /* @__PURE__ */ new Set([
1327
1371
  107
1328
1372
  ]);
1329
1373
  function closeImpliedTo(state, target, boundary, handleEvent) {
1374
+ if (state.flattenedTagName) {
1375
+ const id = state.flattenedTagId;
1376
+ if (id !== void 0 && target.has(id)) {
1377
+ clearFlattenedElements(state);
1378
+ return;
1379
+ }
1380
+ if (id !== void 0 && boundary.has(id)) return;
1381
+ }
1330
1382
  let found = false;
1331
1383
  for (let node = state.currentNode; node; node = node.parent) {
1332
1384
  const id = node.tagId;
@@ -1337,6 +1389,7 @@ function closeImpliedTo(state, target, boundary, handleEvent) {
1337
1389
  if (id !== void 0 && boundary.has(id)) break;
1338
1390
  }
1339
1391
  if (!found) return;
1392
+ clearFlattenedElements(state);
1340
1393
  while (state.currentNode) {
1341
1394
  const id = state.currentNode.tagId;
1342
1395
  const isTarget = id !== void 0 && target.has(id);
@@ -1345,6 +1398,11 @@ function closeImpliedTo(state, target, boundary, handleEvent) {
1345
1398
  }
1346
1399
  }
1347
1400
  function closeTableContext(state, closeable, handleEvent) {
1401
+ if (state.flattenedTagName) {
1402
+ const id = state.flattenedTagId;
1403
+ if (id === void 0 || !closeable.has(id)) return;
1404
+ clearFlattenedElements(state);
1405
+ }
1348
1406
  while (state.currentNode) {
1349
1407
  const id = state.currentNode.tagId;
1350
1408
  if (id === void 0 || !closeable.has(id)) break;
@@ -1352,6 +1410,14 @@ function closeTableContext(state, closeable, handleEvent) {
1352
1410
  }
1353
1411
  }
1354
1412
  function closeSelectTo(state, targetId, handleEvent) {
1413
+ if (state.flattenedTagName) {
1414
+ const id = state.flattenedTagId;
1415
+ if (id === targetId) {
1416
+ clearFlattenedElements(state);
1417
+ return true;
1418
+ }
1419
+ if (id === 65 && targetId !== 65 || id === 78) return false;
1420
+ }
1355
1421
  let target;
1356
1422
  for (let node = state.currentNode; node; node = node.parent) {
1357
1423
  if (node.tagId === targetId) {
@@ -1361,16 +1427,41 @@ function closeSelectTo(state, targetId, handleEvent) {
1361
1427
  if (node.tagId === 65 || node.tagId === 78) break;
1362
1428
  }
1363
1429
  if (!target) return false;
1430
+ clearFlattenedElements(state);
1364
1431
  while (state.currentNode && state.currentNode !== target) closeNode(state.currentNode, state, handleEvent);
1365
1432
  closeNode(target, state, handleEvent);
1366
1433
  return true;
1367
1434
  }
1435
+ function parserTagDepth(state, tagId) {
1436
+ return (state.depthMap[tagId] || 0) + (state.flattenedTagId === tagId ? state.flattenedTagDepth || 0 : 0);
1437
+ }
1438
+ function parserLastTagId(state) {
1439
+ return state.flattenedTagName ? state.flattenedTagId : state.currentNode?.tagId;
1440
+ }
1441
+ function popParserTop(state, handleEvent) {
1442
+ if (state.flattenedTagName) clearFlattenedElements(state);
1443
+ else closeNode(state.currentNode, state, handleEvent);
1444
+ }
1445
+ function findIncompleteTagResidualIndex(leftover) {
1446
+ let index = leftover.indexOf("<");
1447
+ while (index !== -1) {
1448
+ if (index + 1 === leftover.length) return index;
1449
+ const c = leftover.charCodeAt(index + 1);
1450
+ if (isAsciiAlpha(c) || c === SLASH_CHAR || c === EXCLAMATION_CHAR || c === QUESTION_CHAR) return index;
1451
+ index = leftover.indexOf("<", index + 1);
1452
+ }
1453
+ return -1;
1454
+ }
1368
1455
  function finalizeParse(leftover, state, handleEvent) {
1369
1456
  if (state.currentNode && isScriptElement(state.currentNode)) {
1370
1457
  pushScriptTextChunk(state, leftover);
1371
1458
  const scriptText = takeScriptText(state);
1372
1459
  if (scriptText) processTextBuffer(scriptText, state, handleEvent);
1373
- } else if (leftover.length > 0 && leftover.charCodeAt(0) !== LT_CHAR) processTextBuffer(leftover, state, handleEvent);
1460
+ } else if (leftover.length > 0) {
1461
+ const incompleteTagIndex = findIncompleteTagResidualIndex(leftover);
1462
+ const text = incompleteTagIndex === -1 ? leftover : leftover.slice(0, incompleteTagIndex);
1463
+ if (text) processTextBuffer(text, state, handleEvent);
1464
+ }
1374
1465
  while (state.currentNode) closeNode(state.currentNode, state, handleEvent);
1375
1466
  }
1376
1467
  const EMPTY_ATTRIBUTES = Object.freeze({});
@@ -1400,12 +1491,12 @@ var ParsedElementNode = class {
1400
1491
  }
1401
1492
  get depthMap() {
1402
1493
  if (this.cachedDepthMap) return this.cachedDepthMap;
1403
- const depthMap = /* @__PURE__ */ new Uint8Array(112);
1404
- if (this.tagId >= 0 && this.tagId < 112) depthMap[this.tagId] = 1;
1494
+ const depthMap = /* @__PURE__ */ new Uint16Array(113);
1495
+ if (this.tagId >= 0 && this.tagId < 113) depthMap[this.tagId] = 1;
1405
1496
  let node = this.parent;
1406
1497
  while (node) {
1407
1498
  const tagId = node.tagId;
1408
- if (tagId !== void 0 && tagId >= 0 && tagId < 112) depthMap[tagId] = (depthMap[tagId] || 0) + 1;
1499
+ if (tagId !== void 0 && tagId >= 0 && tagId < 113) depthMap[tagId] = (depthMap[tagId] || 0) + 1;
1409
1500
  node = node.parent;
1410
1501
  }
1411
1502
  this.cachedDepthMap = depthMap;
@@ -1523,7 +1614,7 @@ function matchesClosingTag(node, tagName, tagId, closingIsAlias) {
1523
1614
  function parseHtml(html, options = {}) {
1524
1615
  const events = [];
1525
1616
  const state = {
1526
- depthMap: /* @__PURE__ */ new Uint8Array(112),
1617
+ depthMap: /* @__PURE__ */ new Uint16Array(113),
1527
1618
  depth: 0,
1528
1619
  resolvedPlugins: options.resolvedPlugins || []
1529
1620
  };
@@ -1540,10 +1631,9 @@ function parseHtmlStream(html, state, onEvent) {
1540
1631
  return parseHtmlInternal(html, state, onEvent);
1541
1632
  }
1542
1633
  function parseHtmlInternal(htmlChunk, state, handleEvent) {
1543
- if (state.depthLimitReached) return "";
1544
1634
  let textBuffer = "";
1545
1635
  let runStart = 0;
1546
- state.depthMap ??= /* @__PURE__ */ new Uint8Array(112);
1636
+ state.depthMap ??= /* @__PURE__ */ new Uint16Array(113);
1547
1637
  state.depth ??= 0;
1548
1638
  state.lastCharWasWhitespace ??= true;
1549
1639
  state.justClosedTag ??= false;
@@ -1557,7 +1647,7 @@ function parseHtmlInternal(htmlChunk, state, handleEvent) {
1557
1647
  textBuffer = takeScriptText(state);
1558
1648
  i = scanResult;
1559
1649
  }
1560
- while (i < chunkLength && !state.depthLimitReached) {
1650
+ while (i < chunkLength) {
1561
1651
  const currentCharCode = htmlChunk.charCodeAt(i);
1562
1652
  if (currentCharCode !== LT_CHAR) {
1563
1653
  if (currentCharCode === AMPERSAND_CHAR) state.hasEncodedHtmlEntity = true;
@@ -1580,11 +1670,7 @@ function parseHtmlInternal(htmlChunk, state, handleEvent) {
1580
1670
  state.textBufferContainsNonWhitespace = true;
1581
1671
  state.lastCharWasWhitespace = false;
1582
1672
  state.justClosedTag = false;
1583
- if (!state.plainText && currentCharCode === PIPE_CHAR && state.depthMap[28]) textBuffer += "\\|";
1584
- else if (!state.plainText && currentCharCode === OPEN_BRACKET_CHAR && state.depthMap[26]) textBuffer += "\\[";
1585
- else if (!state.plainText && currentCharCode === CLOSE_BRACKET_CHAR && state.depthMap[26]) textBuffer += "\\]";
1586
- else if (!state.plainText && currentCharCode === GT_CHAR && state.depthMap[22]) textBuffer += "\\>";
1587
- else textBuffer += htmlChunk[i];
1673
+ textBuffer += htmlChunk[i];
1588
1674
  }
1589
1675
  i++;
1590
1676
  continue;
@@ -1594,6 +1680,25 @@ function parseHtmlInternal(htmlChunk, state, handleEvent) {
1594
1680
  break;
1595
1681
  }
1596
1682
  const nextCharCode = htmlChunk.charCodeAt(i + 1);
1683
+ if (state.flattenedRawTagName) if (nextCharCode === SLASH_CHAR) {
1684
+ let peekEnd = i + 2;
1685
+ while (peekEnd < chunkLength) {
1686
+ const c = htmlChunk.charCodeAt(peekEnd);
1687
+ if (c === GT_CHAR || c === SLASH_CHAR || isWhitespace(c)) break;
1688
+ peekEnd++;
1689
+ }
1690
+ if (peekEnd === chunkLength) {
1691
+ textBuffer += htmlChunk.substring(i);
1692
+ break;
1693
+ }
1694
+ if (normalizeTagName(htmlChunk.substring(i + 2, peekEnd)) !== state.flattenedRawTagName) {
1695
+ textBuffer += htmlChunk[i++];
1696
+ continue;
1697
+ }
1698
+ } else {
1699
+ textBuffer += htmlChunk[i++];
1700
+ continue;
1701
+ }
1597
1702
  if (nextCharCode === EXCLAMATION_CHAR) {
1598
1703
  if (htmlChunk.charCodeAt(i + 2) === OPEN_BRACKET_CHAR) {
1599
1704
  if (htmlChunk.startsWith("<![CDATA[", i)) {
@@ -1659,6 +1764,12 @@ function parseHtmlInternal(htmlChunk, state, handleEvent) {
1659
1764
  textBuffer += result.remainingText;
1660
1765
  break;
1661
1766
  }
1767
+ } else if (!isAsciiAlpha(nextCharCode) && nextCharCode !== QUESTION_CHAR) {
1768
+ state.textBufferContainsNonWhitespace = true;
1769
+ state.lastCharWasWhitespace = false;
1770
+ state.justClosedTag = false;
1771
+ textBuffer += htmlChunk[i++];
1772
+ continue;
1662
1773
  } else {
1663
1774
  let i2 = i + 1;
1664
1775
  const tagNameStart = i2;
@@ -1757,7 +1868,7 @@ function processTextBuffer(textBuffer, state, handleEvent) {
1757
1868
  state.lastTextNode = rootTextNode;
1758
1869
  return;
1759
1870
  }
1760
- const excludesTextNodes = state.currentNode?.tagHandler?.excludesTextNodes || state.currentNode?.excludedFromMarkdown;
1871
+ const excludesTextNodes = (state.flattenedOpaqueDepth || 0) > 0 || state.flattenedRawExcludesText || state.currentNode?.tagHandler?.excludesTextNodes || state.currentNode?.excludedFromMarkdown;
1761
1872
  const inPreTag = (state.depthMap[34] || 0) > 0;
1762
1873
  if (!inPreTag && !containsNonWhitespace && !state.currentNode.childTextNodeIndex) return;
1763
1874
  let text = textBuffer;
@@ -1829,19 +1940,88 @@ function processClosingTag(htmlChunk, position, state, handleEvent) {
1829
1940
  const mappedTagId = TagIdMap[tagName];
1830
1941
  const tagId = effectiveTagId(tagName, typeof mappedTagId === "number" ? mappedTagId : -1, state);
1831
1942
  const closingIsAlias = tagHandler?.aliasTagId !== void 0;
1943
+ if ((state.flattenedOpaqueDepth || 0) > 0) {
1944
+ if (state.flattenedRawTagName !== void 0) {
1945
+ state.flattenedRawTagName = void 0;
1946
+ state.flattenedRawExcludesText = void 0;
1947
+ }
1948
+ if (tagName === state.flattenedOpaqueTagName) {
1949
+ state.flattenedOpaqueDepth--;
1950
+ if (!state.flattenedOpaqueDepth) {
1951
+ state.flattenedOpaqueTagName = void 0;
1952
+ if (tagName === state.flattenedTagName) {
1953
+ state.flattenedTagDepth--;
1954
+ if (!state.flattenedTagDepth) clearFlattenedElements(state);
1955
+ }
1956
+ }
1957
+ }
1958
+ state.justClosedTag = true;
1959
+ return {
1960
+ complete: true,
1961
+ newPosition: i + 1,
1962
+ remainingText: ""
1963
+ };
1964
+ }
1965
+ const stopAtTemplate = tagId !== 78 && (state.depthMap[78] || 0) > 0;
1966
+ let curr;
1967
+ if (state.flattenedTagName) {
1968
+ const closedRaw = state.flattenedRawTagName !== void 0;
1969
+ if (closedRaw) {
1970
+ state.flattenedRawTagName = void 0;
1971
+ state.flattenedRawExcludesText = void 0;
1972
+ }
1973
+ if (tagName === state.flattenedTagName) {
1974
+ state.flattenedTagDepth--;
1975
+ if (!state.flattenedTagDepth) clearFlattenedElements(state);
1976
+ state.justClosedTag = true;
1977
+ return {
1978
+ complete: true,
1979
+ newPosition: i + 1,
1980
+ remainingText: ""
1981
+ };
1982
+ }
1983
+ if (!closedRaw) {
1984
+ curr = state.currentNode;
1985
+ while (curr && !matchesClosingTag(curr, tagName, tagId, closingIsAlias)) {
1986
+ if (stopAtTemplate && curr.tagId === 78) {
1987
+ curr = null;
1988
+ break;
1989
+ }
1990
+ curr = curr.parent;
1991
+ }
1992
+ if (curr) clearFlattenedElements(state);
1993
+ else {
1994
+ state.justClosedTag = true;
1995
+ return {
1996
+ complete: true,
1997
+ newPosition: i + 1,
1998
+ remainingText: ""
1999
+ };
2000
+ }
2001
+ }
2002
+ if (closedRaw) {
2003
+ state.justClosedTag = true;
2004
+ return {
2005
+ complete: true,
2006
+ newPosition: i + 1,
2007
+ remainingText: ""
2008
+ };
2009
+ }
2010
+ }
1832
2011
  if (state.currentNode?.tagHandler?.isNonNesting && !matchesClosingTag(state.currentNode, tagName, tagId, closingIsAlias)) return {
1833
2012
  complete: false,
1834
2013
  newPosition: position,
1835
2014
  remainingText: htmlChunk.substring(position)
1836
2015
  };
1837
- const stopAtTemplate = tagId !== 78 && (state.depthMap[78] || 0) > 0;
1838
- let curr = state.currentNode;
1839
- while (curr && !matchesClosingTag(curr, tagName, tagId, closingIsAlias)) {
1840
- if (stopAtTemplate && curr.tagId === 78) {
1841
- curr = null;
1842
- break;
2016
+ if (!curr) {
2017
+ curr = state.currentNode;
2018
+ while (curr && !matchesClosingTag(curr, tagName, tagId, closingIsAlias)) {
2019
+ if (stopAtTemplate && curr.tagId === 78) {
2020
+ curr = null;
2021
+ break;
2022
+ }
2023
+ curr = curr.parent;
1843
2024
  }
1844
- curr = curr.parent;
1845
2025
  }
1846
2026
  if (curr) {
1847
2027
  while (state.currentNode && state.currentNode !== curr) closeNode(state.currentNode, state, handleEvent);
@@ -1874,7 +2054,7 @@ function closeNode(node, state, handleEvent) {
1874
2054
  }
1875
2055
  }
1876
2056
  const tagId = node.tagId;
1877
- if (tagId !== void 0 && tagId >= 0 && tagId < 112) state.depthMap[tagId] = Math.max(0, (state.depthMap[tagId] || 0) - 1);
2057
+ if (tagId !== void 0 && tagId >= 0 && tagId < 113) state.depthMap[tagId] = Math.max(0, (state.depthMap[tagId] || 0) - 1);
1878
2058
  state.depth--;
1879
2059
  handleEvent({
1880
2060
  type: 1,
@@ -1884,17 +2064,62 @@ function closeNode(node, state, handleEvent) {
1884
2064
  state.hasEncodedHtmlEntity = false;
1885
2065
  state.justClosedTag = true;
1886
2066
  }
2067
+ function clearFlattenedElements(state) {
2068
+ state.flattenedTagName = void 0;
2069
+ state.flattenedTagId = void 0;
2070
+ state.flattenedTagDepth = void 0;
2071
+ state.flattenedRawTagName = void 0;
2072
+ state.flattenedRawExcludesText = void 0;
2073
+ }
2074
+ function startsOpaqueFlattening(state, tagName, tagId, attributes, tagHandler) {
2075
+ if (tagId === 78) return true;
2076
+ const plugins = state.resolvedPlugins;
2077
+ if (!plugins) return false;
2078
+ let node;
2079
+ for (const plugin of plugins) {
2080
+ if (!plugin.excludesOverflowSubtree) continue;
2081
+ node ||= new ParsedElementNode(tagName, attributes, state.currentNode, state.depth + 1, 0, tagId, tagHandler);
2082
+ if (plugin.excludesOverflowSubtree(node)) return true;
2083
+ }
2084
+ return false;
2085
+ }
2086
+ function enterOpaqueFlattening(state, tagName, tagHandler) {
2087
+ state.flattenedOpaqueTagName = tagName;
2088
+ state.flattenedOpaqueDepth = 1;
2089
+ if (tagHandler?.isNonNesting) {
2090
+ state.flattenedRawTagName = tagName;
2091
+ state.flattenedRawExcludesText = tagHandler.excludesTextNodes;
2092
+ }
2093
+ }
1887
2094
  function processCommentOrDoctype(htmlChunk, position) {
1888
2095
  let i = position;
1889
2096
  const chunkLength = htmlChunk.length;
1890
2097
  if (i + 3 < chunkLength && htmlChunk.charCodeAt(i + 2) === DASH_CHAR && htmlChunk.charCodeAt(i + 3) === DASH_CHAR) {
1891
2098
  i += 4;
1892
- while (i < chunkLength - 2) {
1893
- if (htmlChunk.charCodeAt(i) === DASH_CHAR && htmlChunk.charCodeAt(i + 1) === DASH_CHAR && htmlChunk.charCodeAt(i + 2) === GT_CHAR) {
1894
- i += 3;
1895
- return {
2099
+ if (i < chunkLength && htmlChunk.charCodeAt(i) === GT_CHAR) return {
2100
+ complete: true,
2101
+ newPosition: i + 1,
2102
+ remainingText: ""
2103
+ };
2104
+ if (i + 1 < chunkLength && htmlChunk.charCodeAt(i) === DASH_CHAR && htmlChunk.charCodeAt(i + 1) === GT_CHAR) return {
2105
+ complete: true,
2106
+ newPosition: i + 2,
2107
+ remainingText: ""
2108
+ };
2109
+ while (i + 2 < chunkLength) {
2110
+ const dash = htmlChunk.indexOf("-", i);
2111
+ if (dash < 0 || dash + 2 >= chunkLength) break;
2112
+ i = dash;
2113
+ if (htmlChunk.charCodeAt(i + 1) === DASH_CHAR) {
2114
+ const afterDashes = htmlChunk.charCodeAt(i + 2);
2115
+ if (afterDashes === GT_CHAR) return {
1896
2116
  complete: true,
1897
- newPosition: i,
2117
+ newPosition: i + 3,
2118
+ remainingText: ""
2119
+ };
2120
+ if (afterDashes === EXCLAMATION_CHAR && i + 3 < chunkLength && htmlChunk.charCodeAt(i + 3) === GT_CHAR) return {
2121
+ complete: true,
2122
+ newPosition: i + 4,
1898
2123
  remainingText: ""
1899
2124
  };
1900
2125
  }
@@ -1952,19 +2177,33 @@ function processOpeningTag(tagName, tagId, htmlChunk, i, state, handleEvent) {
1952
2177
  tagId = effectiveTagId(tagName, tagId, state);
1953
2178
  if (state.currentNode?.tagHandler?.isNonNesting) closeNode(state.currentNode, state, handleEvent);
1954
2179
  const tagHandler = state.tagOverrideHandlers?.get(tagName) ?? tagHandlers[tagId];
1955
- const result = processTagAttributes(htmlChunk, i, tagHandler);
1956
- if (!result.complete) return {
2180
+ const result = scanTagAttributes(htmlChunk, i, tagHandler);
2181
+ if (result._tag === "incomplete") return {
1957
2182
  complete: false,
1958
2183
  newPosition: i,
1959
2184
  remainingText: `<${tagName}${result.attrBuffer}`,
1960
2185
  selfClosing: false
1961
2186
  };
1962
- if ((state.depthMap[1] || 0) > 0 && (state.depthMap[78] || 0) === 0 && !HEAD_CONTENT_TAGS.has(tagId)) {
2187
+ if ((state.flattenedOpaqueDepth || 0) > 0) {
2188
+ if (!result.selfClosing && tagName === state.flattenedOpaqueTagName) state.flattenedOpaqueDepth++;
2189
+ if (!result.selfClosing && tagHandler?.isNonNesting) {
2190
+ state.flattenedRawTagName = tagName;
2191
+ state.flattenedRawExcludesText = tagHandler.excludesTextNodes;
2192
+ }
2193
+ return {
2194
+ complete: true,
2195
+ newPosition: result.newPosition,
2196
+ remainingText: "",
2197
+ selfClosing: result.selfClosing,
2198
+ skip: true
2199
+ };
2200
+ }
2201
+ if (!state.flattenedTagName && (state.depthMap[1] || 0) > 0 && (state.depthMap[78] || 0) === 0 && !HEAD_CONTENT_TAGS.has(tagId)) {
1963
2202
  while (state.currentNode && state.currentNode.tagId !== 1) closeNode(state.currentNode, state, handleEvent);
1964
2203
  const headNode = state.currentNode;
1965
2204
  if (headNode && headNode.tagId === 1) closeNode(headNode, state, handleEvent);
1966
2205
  }
1967
- if (tagId >= 0 && tagId < 112 && NEEDS_IMPLIED_END_RECOVERY[tagId] === 1) if (tagId === 65 && (state.depthMap[65] || 0) > 0) {
2206
+ if (tagId >= 0 && tagId < 113 && NEEDS_IMPLIED_END_RECOVERY[tagId] === 1) if (tagId === 65 && parserTagDepth(state, 65) > 0) {
1968
2207
  if (closeSelectTo(state, 65, handleEvent)) return {
1969
2208
  complete: true,
1970
2209
  newPosition: result.newPosition,
@@ -1973,32 +2212,54 @@ function processOpeningTag(tagName, tagId, htmlChunk, i, state, handleEvent) {
1973
2212
  skip: true
1974
2213
  };
1975
2214
  } else if (tagId === 67) {
1976
- if ((state.depthMap[65] || 0) > 0) closeSelectTo(state, 67, handleEvent);
1977
- else if (state.currentNode?.tagId === 67) closeNode(state.currentNode, state, handleEvent);
1978
- } else if (tagId === 109 && (state.depthMap[65] || 0) > 0) {
2215
+ if (parserTagDepth(state, 65) > 0) closeSelectTo(state, 67, handleEvent);
2216
+ else if (parserLastTagId(state) === 67) popParserTop(state, handleEvent);
2217
+ } else if (tagId === 109 && parserTagDepth(state, 65) > 0) {
1979
2218
  closeSelectTo(state, 67, handleEvent);
1980
2219
  closeSelectTo(state, 109, handleEvent);
1981
2220
  } else if (tagId === 26) {
1982
- if ((state.depthMap[26] || 0) > 0) closeImpliedTo(state, SINGLE_A, A_SCOPE_BOUNDARY, handleEvent);
2221
+ if (parserTagDepth(state, 26) > 0) closeImpliedTo(state, SINGLE_A, A_SCOPE_BOUNDARY, handleEvent);
1983
2222
  } else if (tagId === 32 || tagId === 31 || tagId === 30 || tagId === 29 || tagId === 38 || tagId === 39) {
1984
- if ((state.depthMap[28] || 0) > 0) if (tagId === 32 || tagId === 31) {
1985
- if ((state.depthMap[32] || 0) > 0 || (state.depthMap[31] || 0) > 0) closeImpliedTo(state, TD_TH, CELL_SCOPE_BOUNDARY, handleEvent);
2223
+ if (parserTagDepth(state, 28) > 0) if (tagId === 32 || tagId === 31) {
2224
+ if (parserTagDepth(state, 32) > 0 || parserTagDepth(state, 31) > 0) closeImpliedTo(state, TD_TH, CELL_SCOPE_BOUNDARY, handleEvent);
1986
2225
  } else if (tagId === 30) {
1987
- if ((state.depthMap[30] || 0) > 0) closeTableContext(state, TR_CELLS, handleEvent);
2226
+ if (parserTagDepth(state, 30) > 0) closeTableContext(state, TR_CELLS, handleEvent);
1988
2227
  } else closeTableContext(state, SECTION_CELLS, handleEvent);
1989
2228
  } else {
1990
- if ((state.depthMap[35] || 0) > 0) closeImpliedTo(state, SINGLE_P, P_SCOPE_BOUNDARY, handleEvent);
2229
+ if (parserTagDepth(state, 35) > 0) closeImpliedTo(state, SINGLE_P, P_SCOPE_BOUNDARY, handleEvent);
1991
2230
  if (HEADINGS.has(tagId)) {
1992
- const top = state.currentNode;
1993
- if (top && top.tagId !== void 0 && HEADINGS.has(top.tagId)) closeNode(top, state, handleEvent);
2231
+ const topId = parserLastTagId(state);
2232
+ if (topId !== void 0 && HEADINGS.has(topId)) popParserTop(state, handleEvent);
1994
2233
  } else if (tagId === 25) {
1995
- if ((state.depthMap[25] || 0) > 0) closeImpliedTo(state, SINGLE_LI, LI_SCOPE_BOUNDARY, handleEvent);
2234
+ if (parserTagDepth(state, 25) > 0) closeImpliedTo(state, SINGLE_LI, LI_SCOPE_BOUNDARY, handleEvent);
1996
2235
  } else if (tagId === 99 || tagId === 98) {
1997
- if ((state.depthMap[99] || 0) > 0 || (state.depthMap[98] || 0) > 0) closeImpliedTo(state, DT_DD, DL_SCOPE_BOUNDARY, handleEvent);
2236
+ if (parserTagDepth(state, 99) > 0 || parserTagDepth(state, 98) > 0) closeImpliedTo(state, DT_DD, DL_SCOPE_BOUNDARY, handleEvent);
2237
+ }
2238
+ }
2239
+ if (state.flattenedTagName && !result.selfClosing) {
2240
+ if (tagName === state.flattenedTagName) state.flattenedTagDepth++;
2241
+ if (startsOpaqueFlattening(state, tagName, tagId, result.attributes, tagHandler)) enterOpaqueFlattening(state, tagName, tagHandler);
2242
+ else if (tagHandler?.isNonNesting) {
2243
+ state.flattenedRawTagName = tagName;
2244
+ state.flattenedRawExcludesText = tagHandler.excludesTextNodes;
1998
2245
  }
2246
+ return {
2247
+ complete: true,
2248
+ newPosition: result.newPosition,
2249
+ remainingText: "",
2250
+ selfClosing: false,
2251
+ skip: true
2252
+ };
1999
2253
  }
2000
2254
  if (!result.selfClosing && state.depth >= MAX_ELEMENT_DEPTH) {
2001
- state.depthLimitReached = true;
2255
+ state.flattenedTagName = tagName;
2256
+ state.flattenedTagId = tagId;
2257
+ state.flattenedTagDepth = 1;
2258
+ if (startsOpaqueFlattening(state, tagName, tagId, result.attributes, tagHandler)) enterOpaqueFlattening(state, tagName, tagHandler);
2259
+ else if (tagHandler?.isNonNesting) {
2260
+ state.flattenedRawTagName = tagName;
2261
+ state.flattenedRawExcludesText = tagHandler.excludesTextNodes;
2262
+ }
2002
2263
  return {
2003
2264
  complete: true,
2004
2265
  newPosition: result.newPosition,
@@ -2007,7 +2268,7 @@ function processOpeningTag(tagName, tagId, htmlChunk, i, state, handleEvent) {
2007
2268
  skip: true
2008
2269
  };
2009
2270
  }
2010
- if (tagId >= 0 && tagId < 112) state.depthMap[tagId] = (state.depthMap[tagId] || 0) + 1;
2271
+ if (tagId >= 0 && tagId < 113) state.depthMap[tagId] = (state.depthMap[tagId] || 0) + 1;
2011
2272
  state.depth++;
2012
2273
  i = result.newPosition;
2013
2274
  if (state.currentNode) state.currentNode.currentWalkIndex = state.currentNode.currentWalkIndex || 0;
@@ -2034,137 +2295,118 @@ function processOpeningTag(tagName, tagId, htmlChunk, i, state, handleEvent) {
2034
2295
  selfClosing: result.selfClosing
2035
2296
  };
2036
2297
  }
2037
- function processTagAttributes(htmlChunk, position, tagHandler) {
2038
- let i = position;
2039
- const chunkLength = htmlChunk.length;
2040
- const selfClosing = tagHandler?.isSelfClosing || false;
2041
- const attrStartPos = i;
2042
- let insideQuote = false;
2043
- let quoteChar = 0;
2044
- let hasAttributeContent = false;
2045
- let prevChar = 0;
2046
- while (i < chunkLength) {
2047
- const c = htmlChunk.charCodeAt(i);
2048
- if (insideQuote) {
2049
- if (c === quoteChar && prevChar !== BACKSLASH_CHAR) insideQuote = false;
2050
- i++;
2051
- continue;
2052
- } else if (c === QUOTE_CHAR || c === APOS_CHAR) {
2053
- insideQuote = true;
2054
- quoteChar = c;
2055
- hasAttributeContent = true;
2056
- } else if (c === SLASH_CHAR && i + 1 < chunkLength && htmlChunk.charCodeAt(i + 1) === GT_CHAR) {
2057
- const attrStr = hasAttributeContent ? htmlChunk.substring(attrStartPos, i).trim() : "";
2058
- return {
2059
- complete: true,
2060
- newPosition: i + 2,
2061
- attributes: hasAttributeContent ? parseAttributes(attrStr) : EMPTY_ATTRIBUTES,
2062
- selfClosing: true,
2063
- attrBuffer: attrStr
2064
- };
2065
- } else if (c === GT_CHAR) {
2066
- const attrStr = hasAttributeContent ? htmlChunk.substring(attrStartPos, i).trim() : "";
2067
- return {
2068
- complete: true,
2069
- newPosition: i + 1,
2070
- attributes: hasAttributeContent ? parseAttributes(attrStr) : EMPTY_ATTRIBUTES,
2071
- selfClosing,
2072
- attrBuffer: attrStr
2073
- };
2074
- } else if (!isWhitespace(c)) hasAttributeContent = true;
2075
- i++;
2076
- prevChar = c;
2077
- }
2078
- return {
2079
- complete: false,
2080
- newPosition: i,
2081
- attributes: EMPTY_ATTRIBUTES,
2082
- selfClosing: false,
2083
- attrBuffer: htmlChunk.substring(attrStartPos, i)
2084
- };
2298
+ function setAttribute(attributes, rawName, rawValue) {
2299
+ if (!rawName) return attributes;
2300
+ const name = rawName.toLowerCase();
2301
+ if (Object.hasOwn(attributes, name)) return attributes;
2302
+ const result = attributes === EMPTY_ATTRIBUTES ? {} : attributes;
2303
+ result[name] = rawValue.includes("&") ? decodeHTMLEntities(rawValue, true) : rawValue;
2304
+ return result;
2085
2305
  }
2086
- function parseAttributes(attrStr) {
2087
- if (!attrStr) return EMPTY_ATTRIBUTES;
2088
- const result = {};
2089
- const len = attrStr.length;
2090
- let i = 0;
2091
- const WHITESPACE = 0;
2092
- const NAME = 1;
2093
- const AFTER_NAME = 2;
2094
- const BEFORE_VALUE = 3;
2095
- const QUOTED_VALUE = 4;
2096
- const UNQUOTED_VALUE = 5;
2097
- let state = WHITESPACE;
2306
+ function scanAttributes(source, position, selfClosing, stopAtTagEnd) {
2307
+ const length = source.length;
2308
+ let attributes = EMPTY_ATTRIBUTES;
2309
+ let state = ATTRIBUTE_GAP;
2098
2310
  let nameStart = 0;
2099
2311
  let nameEnd = 0;
2100
2312
  let valueStart = 0;
2101
2313
  let quoteChar = 0;
2102
- let name = "";
2103
- while (i < len) {
2104
- const charCode = attrStr.charCodeAt(i);
2314
+ let newPosition = -1;
2315
+ let valueEnd = length;
2316
+ let i = position;
2317
+ while (i < length) {
2318
+ const charCode = source.charCodeAt(i);
2319
+ if (stopAtTagEnd && state !== ATTRIBUTE_QUOTED_VALUE) {
2320
+ if (state !== ATTRIBUTE_UNQUOTED_VALUE && charCode === SLASH_CHAR && i + 1 < length && source.charCodeAt(i + 1) === GT_CHAR) {
2321
+ selfClosing = true;
2322
+ valueEnd = i;
2323
+ newPosition = i + 2;
2324
+ break;
2325
+ }
2326
+ if (charCode === GT_CHAR) {
2327
+ valueEnd = i;
2328
+ newPosition = i + 1;
2329
+ break;
2330
+ }
2331
+ }
2105
2332
  const isSpace = isWhitespace(charCode);
2106
2333
  switch (state) {
2107
- case WHITESPACE:
2334
+ case ATTRIBUTE_GAP:
2108
2335
  if (!isSpace) {
2109
- state = NAME;
2336
+ state = ATTRIBUTE_NAME;
2110
2337
  nameStart = i;
2111
- nameEnd = 0;
2112
2338
  }
2113
2339
  break;
2114
- case NAME:
2340
+ case ATTRIBUTE_NAME:
2115
2341
  if (charCode === EQUALS_CHAR || isSpace) {
2116
2342
  nameEnd = i;
2117
- name = attrStr.substring(nameStart, nameEnd).toLowerCase();
2118
- state = charCode === EQUALS_CHAR ? BEFORE_VALUE : AFTER_NAME;
2343
+ state = charCode === EQUALS_CHAR ? ATTRIBUTE_BEFORE_VALUE : ATTRIBUTE_AFTER_NAME;
2119
2344
  }
2120
2345
  break;
2121
- case AFTER_NAME:
2122
- if (charCode === EQUALS_CHAR) state = BEFORE_VALUE;
2346
+ case ATTRIBUTE_AFTER_NAME:
2347
+ if (charCode === EQUALS_CHAR) state = ATTRIBUTE_BEFORE_VALUE;
2123
2348
  else if (!isSpace) {
2124
- result[name] = "";
2125
- state = NAME;
2349
+ attributes = setAttribute(attributes, source.substring(nameStart, nameEnd), "");
2350
+ state = ATTRIBUTE_NAME;
2126
2351
  nameStart = i;
2127
- nameEnd = 0;
2128
2352
  }
2129
2353
  break;
2130
- case BEFORE_VALUE:
2354
+ case ATTRIBUTE_BEFORE_VALUE:
2131
2355
  if (charCode === QUOTE_CHAR || charCode === APOS_CHAR) {
2132
2356
  quoteChar = charCode;
2133
- state = QUOTED_VALUE;
2357
+ state = ATTRIBUTE_QUOTED_VALUE;
2134
2358
  valueStart = i + 1;
2135
2359
  } else if (!isSpace) {
2136
- state = UNQUOTED_VALUE;
2360
+ state = ATTRIBUTE_UNQUOTED_VALUE;
2137
2361
  valueStart = i;
2138
2362
  }
2139
2363
  break;
2140
- case QUOTED_VALUE:
2141
- if (charCode === BACKSLASH_CHAR && i + 1 < len) i++;
2142
- else if (charCode === quoteChar) {
2143
- const raw = attrStr.substring(valueStart, i);
2144
- result[name] = raw.includes("&") ? decodeHTMLEntities(raw, true) : raw;
2145
- state = WHITESPACE;
2364
+ case ATTRIBUTE_QUOTED_VALUE:
2365
+ if (charCode === quoteChar) {
2366
+ attributes = setAttribute(attributes, source.substring(nameStart, nameEnd), source.substring(valueStart, i));
2367
+ state = ATTRIBUTE_GAP;
2146
2368
  }
2147
2369
  break;
2148
- case UNQUOTED_VALUE:
2149
- if (isSpace || charCode === GT_CHAR) {
2150
- const raw = attrStr.substring(valueStart, i);
2151
- result[name] = raw.includes("&") ? decodeHTMLEntities(raw, true) : raw;
2152
- state = WHITESPACE;
2370
+ case ATTRIBUTE_UNQUOTED_VALUE:
2371
+ if (isSpace || !stopAtTagEnd && charCode === GT_CHAR) {
2372
+ attributes = setAttribute(attributes, source.substring(nameStart, nameEnd), source.substring(valueStart, i));
2373
+ state = ATTRIBUTE_GAP;
2153
2374
  }
2154
2375
  break;
2155
2376
  }
2156
2377
  i++;
2157
2378
  }
2158
- if (state === QUOTED_VALUE || state === UNQUOTED_VALUE) {
2159
- if (name) {
2160
- const raw = attrStr.substring(valueStart, i);
2161
- result[name] = raw.includes("&") ? decodeHTMLEntities(raw, true) : raw;
2162
- }
2163
- } else if (state === NAME || state === AFTER_NAME || state === BEFORE_VALUE) {
2164
- nameEnd = nameEnd || i;
2165
- const currentName = attrStr.substring(nameStart, nameEnd).toLowerCase();
2166
- if (currentName) result[currentName] = "";
2379
+ if (stopAtTagEnd && newPosition === -1) return {
2380
+ _tag: "incomplete",
2381
+ attrBuffer: source.substring(position)
2382
+ };
2383
+ switch (state) {
2384
+ case ATTRIBUTE_NAME:
2385
+ attributes = setAttribute(attributes, source.substring(nameStart, valueEnd), "");
2386
+ break;
2387
+ case ATTRIBUTE_AFTER_NAME:
2388
+ case ATTRIBUTE_BEFORE_VALUE:
2389
+ attributes = setAttribute(attributes, source.substring(nameStart, nameEnd), "");
2390
+ break;
2391
+ case ATTRIBUTE_QUOTED_VALUE:
2392
+ case ATTRIBUTE_UNQUOTED_VALUE:
2393
+ attributes = setAttribute(attributes, source.substring(nameStart, nameEnd), source.substring(valueStart, valueEnd));
2394
+ break;
2167
2395
  }
2168
- return result;
2396
+ return {
2397
+ _tag: "complete",
2398
+ newPosition: stopAtTagEnd ? newPosition : length,
2399
+ attributes,
2400
+ selfClosing
2401
+ };
2402
+ }
2403
+ function scanTagAttributes(htmlChunk, position, tagHandler) {
2404
+ return scanAttributes(htmlChunk, position, tagHandler?.isSelfClosing || false, true);
2405
+ }
2406
+ function parseAttributes(attrStr) {
2407
+ if (!attrStr) return EMPTY_ATTRIBUTES;
2408
+ const result = scanAttributes(attrStr, 0, false, false);
2409
+ if (result._tag === "incomplete") throw new Error("Isolated attribute scans always complete at EOF");
2410
+ return result.attributes === EMPTY_ATTRIBUTES ? {} : result.attributes;
2169
2411
  }
2170
2412
  export { breakHandler, buildTagOverrideHandlers, continuationPrefix, finalizeParse, isEmptyLinkHref, parseAttributes, parseHtml, parseHtmlStream, renderBreak, resolveUrl };
@@ -227,34 +227,37 @@ function filterPlugin(options = {}) {
227
227
  const excludeSelectors = options.exclude?.map((selector) => compileSelector(selector)) || [];
228
228
  const processChildren = options.processChildren !== false;
229
229
  const skippedSubtrees = /* @__PURE__ */ new WeakSet();
230
- return createPlugin({ beforeNodeProcess(event) {
231
- const { node } = event;
232
- if (node.type === 2) {
233
- const parent = node.parent;
234
- if (parent && skippedSubtrees.has(parent)) return { skip: true };
235
- return;
236
- }
237
- if (node.type !== 1) return;
238
- const element = node;
239
- if (skippedSubtrees.has(element)) return { skip: true };
240
- if ((element.parent ? skippedSubtrees.has(element.parent) : false) || isHidden(element)) {
230
+ function excludesSubtree(element) {
231
+ if (skippedSubtrees.has(element)) return true;
232
+ if ((element.parent ? skippedSubtrees.has(element.parent) : false) || isHidden(element) || excludeSelectors.some((selector) => selector.matches(element))) {
241
233
  skippedSubtrees.add(element);
242
- return { skip: true };
234
+ return true;
243
235
  }
244
- if (excludeSelectors.length && excludeSelectors.some((selector) => selector.matches(element))) {
245
- skippedSubtrees.add(element);
246
- return { skip: true };
247
- }
248
- if (includeSelectors.length) {
249
- let currentElement = element;
250
- while (currentElement) {
251
- if (includeSelectors.some((selector) => selector.matches(currentElement))) return;
252
- if (!processChildren) break;
253
- currentElement = currentElement.parent;
236
+ return false;
237
+ }
238
+ return createPlugin({
239
+ excludesOverflowSubtree: excludesSubtree,
240
+ beforeNodeProcess(event) {
241
+ const { node } = event;
242
+ if (node.type === 2) {
243
+ const parent = node.parent;
244
+ if (parent && skippedSubtrees.has(parent)) return { skip: true };
245
+ return;
246
+ }
247
+ if (node.type !== 1) return;
248
+ const element = node;
249
+ if (excludesSubtree(element)) return { skip: true };
250
+ if (includeSelectors.length) {
251
+ let currentElement = element;
252
+ while (currentElement) {
253
+ if (includeSelectors.some((selector) => selector.matches(currentElement))) return;
254
+ if (!processChildren) break;
255
+ currentElement = currentElement.parent;
256
+ }
257
+ return { skip: true };
254
258
  }
255
- return { skip: true };
256
259
  }
257
- } });
260
+ });
258
261
  }
259
262
  const BACKSLASH_RE = /\\/g;
260
263
  const DOUBLE_QUOTE_RE = /"/g;
@@ -611,6 +614,11 @@ function processTailwindClasses(classes) {
611
614
  }
612
615
  function tailwindPlugin() {
613
616
  return createPlugin({
617
+ excludesOverflowSubtree(node) {
618
+ if (node.parent?.context?.tailwind?.hidden) return true;
619
+ const classes = node.attributes?.class?.trim().split(" ").filter(Boolean);
620
+ return classes ? processTailwindClasses(classes).hidden : false;
621
+ },
614
622
  processAttributes(node) {
615
623
  const parentHidden = node.parent?.context?.tailwind?.hidden;
616
624
  const classAttr = node.attributes?.class;
@@ -14,6 +14,11 @@ declare const NodeEventExit$1 = 1;
14
14
  * For declarative config that works with both engines, use `BuiltinPlugins`.
15
15
  */
16
16
  interface TransformPlugin {
17
+ /**
18
+ * Whether a flattened element starts an output-excluded subtree.
19
+ * @internal
20
+ */
21
+ excludesOverflowSubtree?: (node: ElementNode) => boolean;
17
22
  /**
18
23
  * Process a node before it's handled by the parser
19
24
  */
@@ -159,8 +164,8 @@ interface ElementNode extends Node {
159
164
  context?: PluginContext;
160
165
  /** ID of the tag for fast handler lookup */
161
166
  tagId?: number;
162
- /** Map of tag names to their nesting count (using Uint8Array for performance) */
163
- depthMap: Uint8Array;
167
+ /** Map of tag names to their nesting count */
168
+ depthMap: Uint16Array;
164
169
  /** Plugin outputs collected during processing */
165
170
  pluginOutput?: string[];
166
171
  }
@@ -208,7 +213,7 @@ interface Node {
208
213
  */
209
214
  interface MdreamProcessingState {
210
215
  /** Map of tag names to their current nesting depth - uses TypedArray for performance */
211
- depthMap: Uint8Array;
216
+ depthMap: Uint16Array;
212
217
  /** Current overall nesting depth */
213
218
  depth: number;
214
219
  /** Currently processing element node */
package/dist/parse.d.mts CHANGED
@@ -5,12 +5,12 @@ import { ElementNode, Node, NodeEvent, TagHandler, TransformPlugin } from "./_ch
5
5
  * pending (a later chunk might continue them); at true EOF they must be
6
6
  * committed so trailing content is not dropped (e.g. `<p>a<p>b`).
7
7
  *
8
- * `leftover` is the residual returned by the final `parseHtmlStream`. Pure
9
- * trailing text (no leading `<`) is emitted; a residual that is an incomplete
10
- * start tag (leading `<`) is dropped, matching the browser tokenizer's
11
- * EOF-in-tag behaviour. The text-buffer flags set while the trailing text was
12
- * scanned persist on `state`, so `processTextBuffer` commits it as if the next
13
- * tag had triggered the flush.
8
+ * `leftover` is the residual returned by the final `parseHtmlStream`. Trailing
9
+ * text is emitted; a residual that is an incomplete start tag is dropped,
10
+ * matching the browser tokenizer's EOF-in-tag behaviour. A leading `<` alone
11
+ * does not make it a tag: `<3` is text. The text-buffer flags set while the
12
+ * trailing text was scanned persist on `state`, so `processTextBuffer` commits
13
+ * it as if the next tag had triggered the flush.
14
14
  */
15
15
  declare function finalizeParse(leftover: string, state: ParseState, handleEvent: (event: NodeEvent) => void): void;
16
16
  interface ParseOptions {
@@ -18,11 +18,22 @@ interface ParseOptions {
18
18
  }
19
19
  interface ParseState {
20
20
  /** Map of tag names to their current nesting depth - uses TypedArray for performance */
21
- depthMap: Uint8Array;
21
+ depthMap: Uint16Array;
22
22
  /** Current overall nesting depth */
23
23
  depth: number;
24
- /** Whether parsing stopped after reaching the practical browser depth limit. */
25
- depthLimitReached?: boolean;
24
+ /** Root tag currently flattened beyond the rich element depth. */
25
+ flattenedTagName?: string;
26
+ /** Resolved built-in identity of the flattened root. */
27
+ flattenedTagId?: number;
28
+ /** Same-name nesting below the flattened root. */
29
+ flattenedTagDepth?: number;
30
+ /** Exact output-excluded scope nested inside the flattened subtree. */
31
+ flattenedOpaqueTagName?: string;
32
+ flattenedOpaqueDepth?: number;
33
+ /** Raw-text tag currently open inside the flattened subtree. */
34
+ flattenedRawTagName?: string;
35
+ /** Whether flattened raw text must be excluded from output. */
36
+ flattenedRawExcludesText?: boolean;
26
37
  /** Currently processing element node */
27
38
  currentNode?: ElementNode | null;
28
39
  /** Whether current content contains HTML entities that need decoding */
package/dist/splitter.mjs CHANGED
@@ -23,12 +23,6 @@ function createOptions(options) {
23
23
  resolvedPlugins: resolvePlugins(options).plugins
24
24
  };
25
25
  }
26
- function getCodeLanguage(node) {
27
- const className = node.attributes?.class;
28
- if (!className) return "";
29
- const langParts = className.split(" ").map((c) => c.split("language-")[1]).filter(Boolean);
30
- return langParts && langParts.length > 0 ? langParts[0].trim() : "";
31
- }
32
26
  function shouldSplitOnHeader(tagId, options) {
33
27
  return options.headersToSplitOn.includes(tagId);
34
28
  }
@@ -38,10 +32,13 @@ function getCurrentMarkdown(state) {
38
32
  function* htmlToMarkdownSplitChunksStream(html, options = {}) {
39
33
  const opts = createOptions(options);
40
34
  if (opts.chunkOverlap >= opts.chunkSize) throw new Error("chunkOverlap must be less than chunkSize");
35
+ let currentChunkCodeLanguage = "";
41
36
  const processor = createMarkdownProcessor(options, opts.resolvedPlugins);
37
+ processor.state.onCodeFenceOpen = (language) => {
38
+ if (language && !currentChunkCodeLanguage) currentChunkCodeLanguage = language;
39
+ };
42
40
  const headerHierarchy = /* @__PURE__ */ new Map();
43
41
  const seenSplitHeaders = /* @__PURE__ */ new Set();
44
- let currentChunkCodeLanguage = "";
45
42
  let collectingHeaderText = false;
46
43
  let currentHeaderTagId = null;
47
44
  let currentHeaderText = "";
@@ -108,8 +105,7 @@ function* htmlToMarkdownSplitChunksStream(html, options = {}) {
108
105
  if (tagId !== void 0 && tagId >= 0 && tagId < processor.state.depthMap.length) processor.state.depthMap[tagId] = Math.max(0, processor.state.depthMap[tagId] - 1);
109
106
  }
110
107
  if (node.type === 1) {
111
- const element = node;
112
- const tagId = element.tagId;
108
+ const tagId = node.tagId;
113
109
  if (tagId && tagId >= 7 && tagId <= 12) {
114
110
  if (eventType === 0) {
115
111
  collectingHeaderText = true;
@@ -128,12 +124,6 @@ function* htmlToMarkdownSplitChunksStream(html, options = {}) {
128
124
  currentHeaderTagId = null;
129
125
  }
130
126
  }
131
- if (tagId === 23 && (element.depthMap[34] || 0) > 0) {
132
- if (eventType === 0) {
133
- const lang = getCodeLanguage(element);
134
- if (lang && !currentChunkCodeLanguage) currentChunkCodeLanguage = lang;
135
- }
136
- }
137
127
  if (tagId === 13 && eventType === 0) yield* flushChunk();
138
128
  }
139
129
  if (collectingHeaderText && node.type === 2) currentHeaderText += node.value;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@mdream/js",
3
3
  "type": "module",
4
- "version": "1.5.6",
4
+ "version": "1.5.8",
5
5
  "description": "JavaScript HTML-to-Markdown engine for mdream. Escape hatch for hooks and edge runtimes.",
6
6
  "author": {
7
7
  "name": "Harlan Wilton",