@mdream/js 1.5.6 → 1.5.7

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.
@@ -122,9 +122,12 @@ const TagIdMap = {
122
122
  datalist: 108,
123
123
  optgroup: 109
124
124
  };
125
+ function isInsideRawHtmlBlock(depthMap) {
126
+ return Boolean(depthMap[2] || depthMap[3] || depthMap[100] || depthMap[101] || depthMap[99] || depthMap[98]);
127
+ }
125
128
  const NO_SPACING = [0, 0];
126
129
  const DEFAULT_BLOCK_SPACING = [2, 2];
127
- const BLOCKQUOTE_SPACING = [1, 1];
130
+ const BLOCKQUOTE_SPACING = [2, 2];
128
131
  const LIST_ITEM_SPACING = [1, 0];
129
132
  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 };
133
+ 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) {
@@ -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(112),
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 }) => {
@@ -623,6 +661,7 @@ const tagHandlers = {
623
661
  output: `\`\`\`${language}\n`
624
662
  };
625
663
  }
664
+ if (isInsideRawHtmlBlock(state.depthMap)) return "<code>";
626
665
  if ((state.depthMap?.[25] || 0) > 0) {
627
666
  const lastEntry = state.buffer.at(-1);
628
667
  const lastChar = lastEntry?.charAt(lastEntry.length - 1) || "";
@@ -652,6 +691,7 @@ const tagHandlers = {
652
691
  output: `\n\`\`\``
653
692
  };
654
693
  }
694
+ if (isInsideRawHtmlBlock(state.depthMap)) return "</code>";
655
695
  return { _tag: "CodeSpanExit" };
656
696
  },
657
697
  collapsesInnerWhiteSpace: true,
@@ -1135,12 +1175,10 @@ const TAB_CHAR = 9;
1135
1175
  const NEWLINE_CHAR = 10;
1136
1176
  const FORM_FEED_CHAR = 12;
1137
1177
  const CARRIAGE_RETURN_CHAR = 13;
1138
- const PIPE_CHAR = 124;
1139
1178
  const OPEN_BRACKET_CHAR = 91;
1140
- const CLOSE_BRACKET_CHAR = 93;
1141
1179
  function shouldProtectDecodedEntityReferences(state) {
1142
1180
  const depthMap = state.depthMap;
1143
- return !state.plainText && !depthMap[34] && !depthMap[23] && !depthMap[2] && !depthMap[3] && !depthMap[100] && !depthMap[101] && !depthMap[99] && !depthMap[98];
1181
+ return !state.plainText && !depthMap[34] && !depthMap[23] && !isInsideRawHtmlBlock(depthMap);
1144
1182
  }
1145
1183
  const SCRIPT_DATA = 0;
1146
1184
  const SCRIPT_DATA_ESCAPED = 1;
@@ -1327,6 +1365,14 @@ const SECTION_CELLS = /* @__PURE__ */ new Set([
1327
1365
  107
1328
1366
  ]);
1329
1367
  function closeImpliedTo(state, target, boundary, handleEvent) {
1368
+ if (state.flattenedTagName) {
1369
+ const id = state.flattenedTagId;
1370
+ if (id !== void 0 && target.has(id)) {
1371
+ clearFlattenedElements(state);
1372
+ return;
1373
+ }
1374
+ if (id !== void 0 && boundary.has(id)) return;
1375
+ }
1330
1376
  let found = false;
1331
1377
  for (let node = state.currentNode; node; node = node.parent) {
1332
1378
  const id = node.tagId;
@@ -1337,6 +1383,7 @@ function closeImpliedTo(state, target, boundary, handleEvent) {
1337
1383
  if (id !== void 0 && boundary.has(id)) break;
1338
1384
  }
1339
1385
  if (!found) return;
1386
+ clearFlattenedElements(state);
1340
1387
  while (state.currentNode) {
1341
1388
  const id = state.currentNode.tagId;
1342
1389
  const isTarget = id !== void 0 && target.has(id);
@@ -1345,6 +1392,11 @@ function closeImpliedTo(state, target, boundary, handleEvent) {
1345
1392
  }
1346
1393
  }
1347
1394
  function closeTableContext(state, closeable, handleEvent) {
1395
+ if (state.flattenedTagName) {
1396
+ const id = state.flattenedTagId;
1397
+ if (id === void 0 || !closeable.has(id)) return;
1398
+ clearFlattenedElements(state);
1399
+ }
1348
1400
  while (state.currentNode) {
1349
1401
  const id = state.currentNode.tagId;
1350
1402
  if (id === void 0 || !closeable.has(id)) break;
@@ -1352,6 +1404,14 @@ function closeTableContext(state, closeable, handleEvent) {
1352
1404
  }
1353
1405
  }
1354
1406
  function closeSelectTo(state, targetId, handleEvent) {
1407
+ if (state.flattenedTagName) {
1408
+ const id = state.flattenedTagId;
1409
+ if (id === targetId) {
1410
+ clearFlattenedElements(state);
1411
+ return true;
1412
+ }
1413
+ if (id === 65 && targetId !== 65 || id === 78) return false;
1414
+ }
1355
1415
  let target;
1356
1416
  for (let node = state.currentNode; node; node = node.parent) {
1357
1417
  if (node.tagId === targetId) {
@@ -1361,10 +1421,21 @@ function closeSelectTo(state, targetId, handleEvent) {
1361
1421
  if (node.tagId === 65 || node.tagId === 78) break;
1362
1422
  }
1363
1423
  if (!target) return false;
1424
+ clearFlattenedElements(state);
1364
1425
  while (state.currentNode && state.currentNode !== target) closeNode(state.currentNode, state, handleEvent);
1365
1426
  closeNode(target, state, handleEvent);
1366
1427
  return true;
1367
1428
  }
1429
+ function parserTagDepth(state, tagId) {
1430
+ return (state.depthMap[tagId] || 0) + (state.flattenedTagId === tagId ? state.flattenedTagDepth || 0 : 0);
1431
+ }
1432
+ function parserLastTagId(state) {
1433
+ return state.flattenedTagName ? state.flattenedTagId : state.currentNode?.tagId;
1434
+ }
1435
+ function popParserTop(state, handleEvent) {
1436
+ if (state.flattenedTagName) clearFlattenedElements(state);
1437
+ else closeNode(state.currentNode, state, handleEvent);
1438
+ }
1368
1439
  function finalizeParse(leftover, state, handleEvent) {
1369
1440
  if (state.currentNode && isScriptElement(state.currentNode)) {
1370
1441
  pushScriptTextChunk(state, leftover);
@@ -1400,7 +1471,7 @@ var ParsedElementNode = class {
1400
1471
  }
1401
1472
  get depthMap() {
1402
1473
  if (this.cachedDepthMap) return this.cachedDepthMap;
1403
- const depthMap = /* @__PURE__ */ new Uint8Array(112);
1474
+ const depthMap = /* @__PURE__ */ new Uint16Array(112);
1404
1475
  if (this.tagId >= 0 && this.tagId < 112) depthMap[this.tagId] = 1;
1405
1476
  let node = this.parent;
1406
1477
  while (node) {
@@ -1523,7 +1594,7 @@ function matchesClosingTag(node, tagName, tagId, closingIsAlias) {
1523
1594
  function parseHtml(html, options = {}) {
1524
1595
  const events = [];
1525
1596
  const state = {
1526
- depthMap: /* @__PURE__ */ new Uint8Array(112),
1597
+ depthMap: /* @__PURE__ */ new Uint16Array(112),
1527
1598
  depth: 0,
1528
1599
  resolvedPlugins: options.resolvedPlugins || []
1529
1600
  };
@@ -1540,10 +1611,9 @@ function parseHtmlStream(html, state, onEvent) {
1540
1611
  return parseHtmlInternal(html, state, onEvent);
1541
1612
  }
1542
1613
  function parseHtmlInternal(htmlChunk, state, handleEvent) {
1543
- if (state.depthLimitReached) return "";
1544
1614
  let textBuffer = "";
1545
1615
  let runStart = 0;
1546
- state.depthMap ??= /* @__PURE__ */ new Uint8Array(112);
1616
+ state.depthMap ??= /* @__PURE__ */ new Uint16Array(112);
1547
1617
  state.depth ??= 0;
1548
1618
  state.lastCharWasWhitespace ??= true;
1549
1619
  state.justClosedTag ??= false;
@@ -1557,7 +1627,7 @@ function parseHtmlInternal(htmlChunk, state, handleEvent) {
1557
1627
  textBuffer = takeScriptText(state);
1558
1628
  i = scanResult;
1559
1629
  }
1560
- while (i < chunkLength && !state.depthLimitReached) {
1630
+ while (i < chunkLength) {
1561
1631
  const currentCharCode = htmlChunk.charCodeAt(i);
1562
1632
  if (currentCharCode !== LT_CHAR) {
1563
1633
  if (currentCharCode === AMPERSAND_CHAR) state.hasEncodedHtmlEntity = true;
@@ -1580,11 +1650,7 @@ function parseHtmlInternal(htmlChunk, state, handleEvent) {
1580
1650
  state.textBufferContainsNonWhitespace = true;
1581
1651
  state.lastCharWasWhitespace = false;
1582
1652
  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];
1653
+ textBuffer += htmlChunk[i];
1588
1654
  }
1589
1655
  i++;
1590
1656
  continue;
@@ -1594,6 +1660,25 @@ function parseHtmlInternal(htmlChunk, state, handleEvent) {
1594
1660
  break;
1595
1661
  }
1596
1662
  const nextCharCode = htmlChunk.charCodeAt(i + 1);
1663
+ if (state.flattenedRawTagName) if (nextCharCode === SLASH_CHAR) {
1664
+ let peekEnd = i + 2;
1665
+ while (peekEnd < chunkLength) {
1666
+ const c = htmlChunk.charCodeAt(peekEnd);
1667
+ if (c === GT_CHAR || c === SLASH_CHAR || isWhitespace(c)) break;
1668
+ peekEnd++;
1669
+ }
1670
+ if (peekEnd === chunkLength) {
1671
+ textBuffer += htmlChunk.substring(i);
1672
+ break;
1673
+ }
1674
+ if (normalizeTagName(htmlChunk.substring(i + 2, peekEnd)) !== state.flattenedRawTagName) {
1675
+ textBuffer += htmlChunk[i++];
1676
+ continue;
1677
+ }
1678
+ } else {
1679
+ textBuffer += htmlChunk[i++];
1680
+ continue;
1681
+ }
1597
1682
  if (nextCharCode === EXCLAMATION_CHAR) {
1598
1683
  if (htmlChunk.charCodeAt(i + 2) === OPEN_BRACKET_CHAR) {
1599
1684
  if (htmlChunk.startsWith("<![CDATA[", i)) {
@@ -1757,7 +1842,7 @@ function processTextBuffer(textBuffer, state, handleEvent) {
1757
1842
  state.lastTextNode = rootTextNode;
1758
1843
  return;
1759
1844
  }
1760
- const excludesTextNodes = state.currentNode?.tagHandler?.excludesTextNodes || state.currentNode?.excludedFromMarkdown;
1845
+ const excludesTextNodes = (state.flattenedOpaqueDepth || 0) > 0 || state.flattenedRawExcludesText || state.currentNode?.tagHandler?.excludesTextNodes || state.currentNode?.excludedFromMarkdown;
1761
1846
  const inPreTag = (state.depthMap[34] || 0) > 0;
1762
1847
  if (!inPreTag && !containsNonWhitespace && !state.currentNode.childTextNodeIndex) return;
1763
1848
  let text = textBuffer;
@@ -1829,19 +1914,88 @@ function processClosingTag(htmlChunk, position, state, handleEvent) {
1829
1914
  const mappedTagId = TagIdMap[tagName];
1830
1915
  const tagId = effectiveTagId(tagName, typeof mappedTagId === "number" ? mappedTagId : -1, state);
1831
1916
  const closingIsAlias = tagHandler?.aliasTagId !== void 0;
1917
+ if ((state.flattenedOpaqueDepth || 0) > 0) {
1918
+ if (state.flattenedRawTagName !== void 0) {
1919
+ state.flattenedRawTagName = void 0;
1920
+ state.flattenedRawExcludesText = void 0;
1921
+ }
1922
+ if (tagName === state.flattenedOpaqueTagName) {
1923
+ state.flattenedOpaqueDepth--;
1924
+ if (!state.flattenedOpaqueDepth) {
1925
+ state.flattenedOpaqueTagName = void 0;
1926
+ if (tagName === state.flattenedTagName) {
1927
+ state.flattenedTagDepth--;
1928
+ if (!state.flattenedTagDepth) clearFlattenedElements(state);
1929
+ }
1930
+ }
1931
+ }
1932
+ state.justClosedTag = true;
1933
+ return {
1934
+ complete: true,
1935
+ newPosition: i + 1,
1936
+ remainingText: ""
1937
+ };
1938
+ }
1939
+ const stopAtTemplate = tagId !== 78 && (state.depthMap[78] || 0) > 0;
1940
+ let curr;
1941
+ if (state.flattenedTagName) {
1942
+ const closedRaw = state.flattenedRawTagName !== void 0;
1943
+ if (closedRaw) {
1944
+ state.flattenedRawTagName = void 0;
1945
+ state.flattenedRawExcludesText = void 0;
1946
+ }
1947
+ if (tagName === state.flattenedTagName) {
1948
+ state.flattenedTagDepth--;
1949
+ if (!state.flattenedTagDepth) clearFlattenedElements(state);
1950
+ state.justClosedTag = true;
1951
+ return {
1952
+ complete: true,
1953
+ newPosition: i + 1,
1954
+ remainingText: ""
1955
+ };
1956
+ }
1957
+ if (!closedRaw) {
1958
+ curr = state.currentNode;
1959
+ while (curr && !matchesClosingTag(curr, tagName, tagId, closingIsAlias)) {
1960
+ if (stopAtTemplate && curr.tagId === 78) {
1961
+ curr = null;
1962
+ break;
1963
+ }
1964
+ curr = curr.parent;
1965
+ }
1966
+ if (curr) clearFlattenedElements(state);
1967
+ else {
1968
+ state.justClosedTag = true;
1969
+ return {
1970
+ complete: true,
1971
+ newPosition: i + 1,
1972
+ remainingText: ""
1973
+ };
1974
+ }
1975
+ }
1976
+ if (closedRaw) {
1977
+ state.justClosedTag = true;
1978
+ return {
1979
+ complete: true,
1980
+ newPosition: i + 1,
1981
+ remainingText: ""
1982
+ };
1983
+ }
1984
+ }
1832
1985
  if (state.currentNode?.tagHandler?.isNonNesting && !matchesClosingTag(state.currentNode, tagName, tagId, closingIsAlias)) return {
1833
1986
  complete: false,
1834
1987
  newPosition: position,
1835
1988
  remainingText: htmlChunk.substring(position)
1836
1989
  };
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;
1990
+ if (!curr) {
1991
+ curr = state.currentNode;
1992
+ while (curr && !matchesClosingTag(curr, tagName, tagId, closingIsAlias)) {
1993
+ if (stopAtTemplate && curr.tagId === 78) {
1994
+ curr = null;
1995
+ break;
1996
+ }
1997
+ curr = curr.parent;
1843
1998
  }
1844
- curr = curr.parent;
1845
1999
  }
1846
2000
  if (curr) {
1847
2001
  while (state.currentNode && state.currentNode !== curr) closeNode(state.currentNode, state, handleEvent);
@@ -1884,6 +2038,33 @@ function closeNode(node, state, handleEvent) {
1884
2038
  state.hasEncodedHtmlEntity = false;
1885
2039
  state.justClosedTag = true;
1886
2040
  }
2041
+ function clearFlattenedElements(state) {
2042
+ state.flattenedTagName = void 0;
2043
+ state.flattenedTagId = void 0;
2044
+ state.flattenedTagDepth = void 0;
2045
+ state.flattenedRawTagName = void 0;
2046
+ state.flattenedRawExcludesText = void 0;
2047
+ }
2048
+ function startsOpaqueFlattening(state, tagName, tagId, attributes, tagHandler) {
2049
+ if (tagId === 78) return true;
2050
+ const plugins = state.resolvedPlugins;
2051
+ if (!plugins) return false;
2052
+ let node;
2053
+ for (const plugin of plugins) {
2054
+ if (!plugin.excludesOverflowSubtree) continue;
2055
+ node ||= new ParsedElementNode(tagName, attributes, state.currentNode, state.depth + 1, 0, tagId, tagHandler);
2056
+ if (plugin.excludesOverflowSubtree(node)) return true;
2057
+ }
2058
+ return false;
2059
+ }
2060
+ function enterOpaqueFlattening(state, tagName, tagHandler) {
2061
+ state.flattenedOpaqueTagName = tagName;
2062
+ state.flattenedOpaqueDepth = 1;
2063
+ if (tagHandler?.isNonNesting) {
2064
+ state.flattenedRawTagName = tagName;
2065
+ state.flattenedRawExcludesText = tagHandler.excludesTextNodes;
2066
+ }
2067
+ }
1887
2068
  function processCommentOrDoctype(htmlChunk, position) {
1888
2069
  let i = position;
1889
2070
  const chunkLength = htmlChunk.length;
@@ -1959,12 +2140,26 @@ function processOpeningTag(tagName, tagId, htmlChunk, i, state, handleEvent) {
1959
2140
  remainingText: `<${tagName}${result.attrBuffer}`,
1960
2141
  selfClosing: false
1961
2142
  };
1962
- if ((state.depthMap[1] || 0) > 0 && (state.depthMap[78] || 0) === 0 && !HEAD_CONTENT_TAGS.has(tagId)) {
2143
+ if ((state.flattenedOpaqueDepth || 0) > 0) {
2144
+ if (!result.selfClosing && tagName === state.flattenedOpaqueTagName) state.flattenedOpaqueDepth++;
2145
+ if (!result.selfClosing && tagHandler?.isNonNesting) {
2146
+ state.flattenedRawTagName = tagName;
2147
+ state.flattenedRawExcludesText = tagHandler.excludesTextNodes;
2148
+ }
2149
+ return {
2150
+ complete: true,
2151
+ newPosition: result.newPosition,
2152
+ remainingText: "",
2153
+ selfClosing: result.selfClosing,
2154
+ skip: true
2155
+ };
2156
+ }
2157
+ if (!state.flattenedTagName && (state.depthMap[1] || 0) > 0 && (state.depthMap[78] || 0) === 0 && !HEAD_CONTENT_TAGS.has(tagId)) {
1963
2158
  while (state.currentNode && state.currentNode.tagId !== 1) closeNode(state.currentNode, state, handleEvent);
1964
2159
  const headNode = state.currentNode;
1965
2160
  if (headNode && headNode.tagId === 1) closeNode(headNode, state, handleEvent);
1966
2161
  }
1967
- if (tagId >= 0 && tagId < 112 && NEEDS_IMPLIED_END_RECOVERY[tagId] === 1) if (tagId === 65 && (state.depthMap[65] || 0) > 0) {
2162
+ if (tagId >= 0 && tagId < 112 && NEEDS_IMPLIED_END_RECOVERY[tagId] === 1) if (tagId === 65 && parserTagDepth(state, 65) > 0) {
1968
2163
  if (closeSelectTo(state, 65, handleEvent)) return {
1969
2164
  complete: true,
1970
2165
  newPosition: result.newPosition,
@@ -1973,32 +2168,54 @@ function processOpeningTag(tagName, tagId, htmlChunk, i, state, handleEvent) {
1973
2168
  skip: true
1974
2169
  };
1975
2170
  } 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) {
2171
+ if (parserTagDepth(state, 65) > 0) closeSelectTo(state, 67, handleEvent);
2172
+ else if (parserLastTagId(state) === 67) popParserTop(state, handleEvent);
2173
+ } else if (tagId === 109 && parserTagDepth(state, 65) > 0) {
1979
2174
  closeSelectTo(state, 67, handleEvent);
1980
2175
  closeSelectTo(state, 109, handleEvent);
1981
2176
  } else if (tagId === 26) {
1982
- if ((state.depthMap[26] || 0) > 0) closeImpliedTo(state, SINGLE_A, A_SCOPE_BOUNDARY, handleEvent);
2177
+ if (parserTagDepth(state, 26) > 0) closeImpliedTo(state, SINGLE_A, A_SCOPE_BOUNDARY, handleEvent);
1983
2178
  } 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);
2179
+ if (parserTagDepth(state, 28) > 0) if (tagId === 32 || tagId === 31) {
2180
+ if (parserTagDepth(state, 32) > 0 || parserTagDepth(state, 31) > 0) closeImpliedTo(state, TD_TH, CELL_SCOPE_BOUNDARY, handleEvent);
1986
2181
  } else if (tagId === 30) {
1987
- if ((state.depthMap[30] || 0) > 0) closeTableContext(state, TR_CELLS, handleEvent);
2182
+ if (parserTagDepth(state, 30) > 0) closeTableContext(state, TR_CELLS, handleEvent);
1988
2183
  } else closeTableContext(state, SECTION_CELLS, handleEvent);
1989
2184
  } else {
1990
- if ((state.depthMap[35] || 0) > 0) closeImpliedTo(state, SINGLE_P, P_SCOPE_BOUNDARY, handleEvent);
2185
+ if (parserTagDepth(state, 35) > 0) closeImpliedTo(state, SINGLE_P, P_SCOPE_BOUNDARY, handleEvent);
1991
2186
  if (HEADINGS.has(tagId)) {
1992
- const top = state.currentNode;
1993
- if (top && top.tagId !== void 0 && HEADINGS.has(top.tagId)) closeNode(top, state, handleEvent);
2187
+ const topId = parserLastTagId(state);
2188
+ if (topId !== void 0 && HEADINGS.has(topId)) popParserTop(state, handleEvent);
1994
2189
  } else if (tagId === 25) {
1995
- if ((state.depthMap[25] || 0) > 0) closeImpliedTo(state, SINGLE_LI, LI_SCOPE_BOUNDARY, handleEvent);
2190
+ if (parserTagDepth(state, 25) > 0) closeImpliedTo(state, SINGLE_LI, LI_SCOPE_BOUNDARY, handleEvent);
1996
2191
  } 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);
2192
+ if (parserTagDepth(state, 99) > 0 || parserTagDepth(state, 98) > 0) closeImpliedTo(state, DT_DD, DL_SCOPE_BOUNDARY, handleEvent);
2193
+ }
2194
+ }
2195
+ if (state.flattenedTagName && !result.selfClosing) {
2196
+ if (tagName === state.flattenedTagName) state.flattenedTagDepth++;
2197
+ if (startsOpaqueFlattening(state, tagName, tagId, result.attributes, tagHandler)) enterOpaqueFlattening(state, tagName, tagHandler);
2198
+ else if (tagHandler?.isNonNesting) {
2199
+ state.flattenedRawTagName = tagName;
2200
+ state.flattenedRawExcludesText = tagHandler.excludesTextNodes;
1998
2201
  }
2202
+ return {
2203
+ complete: true,
2204
+ newPosition: result.newPosition,
2205
+ remainingText: "",
2206
+ selfClosing: false,
2207
+ skip: true
2208
+ };
1999
2209
  }
2000
2210
  if (!result.selfClosing && state.depth >= MAX_ELEMENT_DEPTH) {
2001
- state.depthLimitReached = true;
2211
+ state.flattenedTagName = tagName;
2212
+ state.flattenedTagId = tagId;
2213
+ state.flattenedTagDepth = 1;
2214
+ if (startsOpaqueFlattening(state, tagName, tagId, result.attributes, tagHandler)) enterOpaqueFlattening(state, tagName, tagHandler);
2215
+ else if (tagHandler?.isNonNesting) {
2216
+ state.flattenedRawTagName = tagName;
2217
+ state.flattenedRawExcludesText = tagHandler.excludesTextNodes;
2218
+ }
2002
2219
  return {
2003
2220
  complete: true,
2004
2221
  newPosition: result.newPosition,
@@ -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
@@ -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.7",
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",