@mdream/js 1.5.7 → 1.5.10

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,
@@ -1,5 +1,5 @@
1
1
  import { DEFAULT_BLOCK_SPACING, NO_SPACING, isInsideRawHtmlBlock } from "./const.mjs";
2
- import { breakHandler, continuationPrefix, finalizeParse, parseHtmlStream, renderBreak, resolveUrl } from "./parse.mjs";
2
+ import { blockOpenPrefix, breakHandler, continuationPrefix, finalizeParse, isCharacterReferenceTail, isInsideHeading, isInsideTableCell, listMarkerLineStart, orderedItemNumber, parseHtmlStream, renderBreak, resolveUrl } from "./parse.mjs";
3
3
  function processPluginsForEvent(event, plugins, state, processEvent) {
4
4
  if (plugins?.length) {
5
5
  if (event.node.type === 1 && event.type === 0) {
@@ -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;
@@ -51,12 +51,53 @@ INLINE_MARKER_TYPE[23] = 5;
51
51
  INLINE_MARKER_TYPE[82] = 5;
52
52
  INLINE_MARKER_TYPE[92] = 5;
53
53
  INLINE_MARKER_TYPE[81] = 6;
54
+ function suppressesFormattingInPre(tagId) {
55
+ return tagId === 26 || tagId === 46 || tagId === 110 || tagId === 111 || tagId >= 14 && tagId <= 21 || tagId >= 79 && tagId <= 83 || tagId >= 89 && tagId <= 94;
56
+ }
57
+ function newlineRunBefore(buffer, index) {
58
+ let run = 0;
59
+ for (let i = index - 1; i >= 0; i--) {
60
+ const fragment = buffer[i];
61
+ for (let j = fragment.length - 1; j >= 0; j--) {
62
+ if (fragment.charCodeAt(j) !== 10) return run;
63
+ run++;
64
+ }
65
+ }
66
+ return run;
67
+ }
68
+ function recordItemMarker(state, index) {
69
+ if (index !== 0 || newlineRunBefore(state.buffer, state.buffer.length - 1) !== 1) {
70
+ state.emptyItemFragment = void 0;
71
+ return;
72
+ }
73
+ state.emptyItemFragment = state.buffer.length - 1;
74
+ }
75
+ function resolveItemMarker(state, atExit) {
76
+ const fragment = state.emptyItemFragment;
77
+ if (fragment === void 0) return;
78
+ const buffer = state.buffer;
79
+ let first = -1;
80
+ for (let i = fragment + 1; i < buffer.length && first === -1; i++) {
81
+ const fragment = buffer[i];
82
+ for (let j = 0; j < fragment.length; j++) {
83
+ const code = fragment.charCodeAt(j);
84
+ if (code !== 32) {
85
+ first = code;
86
+ break;
87
+ }
88
+ }
89
+ }
90
+ if (first === -1 && !atExit) return;
91
+ state.emptyItemFragment = void 0;
92
+ if (first !== -1 && first !== 10) return;
93
+ buffer[fragment] = `\n${buffer[fragment]}`;
94
+ }
54
95
  function updateListIndent(state, element, eventType) {
55
96
  if (element.tagId !== 25) return;
56
- if ((state.depthMap[32] || 0) > 0 || (state.depthMap[31] || 0) > 0) return;
97
+ if (isInsideTableCell(state)) return;
57
98
  if (eventType === 0) {
58
99
  const isOrdered = element.parent?.tagId === 33;
59
- const width = state.plainText ? 0 : isOrdered ? String(element.index + 1).length + 2 : 2;
100
+ const width = state.plainText ? 0 : isOrdered ? String(orderedItemNumber(element.parent, element.index)).length + 2 : 2;
60
101
  state.listIndentWidths.push(width);
61
102
  state.listIndent += " ".repeat(width);
62
103
  } else if (eventType === 1) {
@@ -65,46 +106,21 @@ function updateListIndent(state, element, eventType) {
65
106
  }
66
107
  }
67
108
  function needsSpacing(lastChar, firstChar, state) {
68
- if (lastChar === " " || lastChar === "\n" || lastChar === " ") return false;
69
- if (firstChar === " " || firstChar === "\n" || firstChar === " ") return false;
70
- const noSpaceAfter = /* @__PURE__ */ new Set([
71
- "[",
72
- "(",
73
- ">",
74
- "*",
75
- "_",
76
- "`"
77
- ]);
78
- const noSpaceBefore = /* @__PURE__ */ new Set([
79
- "]",
80
- ")",
81
- "<",
82
- ".",
83
- ",",
84
- "!",
85
- "?",
86
- ":",
87
- ";",
88
- "*",
89
- "_",
90
- "`"
91
- ]);
109
+ if (" \n ".includes(lastChar) || " \n ".includes(firstChar)) return false;
92
110
  if (lastChar === "|" && firstChar === "<" && state && (state.depthMap[28] || 0) > 0) return true;
93
- if (noSpaceAfter.has(lastChar) || noSpaceBefore.has(firstChar)) return false;
94
- return true;
111
+ return !"[(>*_`".includes(lastChar) && !"])<.,!?:;*_`".includes(firstChar);
95
112
  }
96
113
  function shouldAddSpacingBeforeText(lastChar, lastNode, textNode) {
97
- if (!lastChar || lastChar === "\n" || lastChar === " " || lastChar === " " || lastChar === "[" || lastChar === ">") return false;
114
+ if (!lastChar || "\n [>".includes(lastChar)) return false;
98
115
  if (lastNode?.tagHandler?.isInline) return false;
99
116
  const firstChar = textNode.value[0];
100
117
  if (firstChar === " ") return false;
101
- if (firstChar === "." || firstChar === "," || firstChar === "!" || firstChar === "?" || firstChar === ":" || firstChar === ";" || firstChar === "_" || firstChar === "*" || firstChar === "`" || firstChar === ")" || firstChar === "]") return false;
118
+ if (firstChar && ".,!?:;_*`)]".includes(firstChar)) return false;
102
119
  return true;
103
120
  }
104
121
  function canWrapHere(depthMap) {
105
122
  if (depthMap[34] || depthMap[23] || depthMap[32] || depthMap[31]) return false;
106
- for (let h = 7; h <= 12; h++) if (depthMap[h]) return false;
107
- return true;
123
+ return !isInsideHeading(depthMap);
108
124
  }
109
125
  function foldPreLinesToBr(value) {
110
126
  let start = 0;
@@ -159,18 +175,70 @@ function escapeRawHtmlText(value, depthMap) {
159
175
  }
160
176
  return copiedUntil === 0 ? value : escaped + value.slice(copiedUntil);
161
177
  }
178
+ function resetBufferScanCursors(scan) {
179
+ scan[1] = 0;
180
+ scan[2] = 0;
181
+ scan[3] = 0;
182
+ scan[4] = 0;
183
+ }
184
+ function trackRawHtmlMarkdownContext(buffer, scan) {
185
+ if (scan[0]) return true;
186
+ let index = scan[1];
187
+ if (index > buffer.length) index = 0;
188
+ let previous = index > 0 ? buffer[index - 1].charCodeAt(buffer[index - 1].length - 1) : -1;
189
+ for (; index < buffer.length; index++) {
190
+ const fragment = buffer[index];
191
+ if (!fragment) continue;
192
+ if (fragment.includes("\n\n") || previous === 10 && fragment.charCodeAt(0) === 10) {
193
+ scan[0] = true;
194
+ break;
195
+ }
196
+ previous = fragment.charCodeAt(fragment.length - 1);
197
+ }
198
+ scan[1] = buffer.length;
199
+ return scan[0];
200
+ }
201
+ function lineOpensRawHtmlBlock(buffer, scan) {
202
+ let scanned = scan[2];
203
+ if (scanned > buffer.length) {
204
+ scanned = 0;
205
+ scan[3] = 0;
206
+ scan[4] = 0;
207
+ }
208
+ for (let index = scanned; index < buffer.length; index++) {
209
+ const newline = buffer[index].lastIndexOf("\n");
210
+ if (newline !== -1) {
211
+ scan[3] = index;
212
+ scan[4] = newline + 1;
213
+ }
214
+ }
215
+ scan[2] = buffer.length;
216
+ let spaces = 0;
217
+ let offset = scan[4];
218
+ for (let index = scan[3]; index < buffer.length; index++) {
219
+ const fragment = buffer[index];
220
+ while (offset < fragment.length) {
221
+ const code = fragment.charCodeAt(offset);
222
+ if (code !== 32) return code === 60;
223
+ if (++spaces > 3) return false;
224
+ offset++;
225
+ }
226
+ offset = 0;
227
+ }
228
+ return false;
229
+ }
162
230
  function markdownLineIndent(buffer) {
163
231
  let spaces = 0;
164
232
  for (let fragmentIndex = buffer.length - 1; fragmentIndex >= 0; fragmentIndex--) {
165
233
  const fragment = buffer[fragmentIndex];
166
234
  for (let index = fragment.length - 1; index >= 0; index--) {
167
235
  const code = fragment.charCodeAt(index);
168
- if (code === 10) return spaces <= 3 ? spaces : -1;
169
- if (code !== 32) return -1;
170
- if (++spaces > 3) return -1;
236
+ if (code === 10) return spaces;
237
+ if (code !== 32 || spaces === 3) return listMarkerLineStart(buffer, fragmentIndex, index) ? 0 : -1;
238
+ spaces++;
171
239
  }
172
240
  }
173
- return spaces <= 3 ? spaces : -1;
241
+ return spaces;
174
242
  }
175
243
  function isMarkdownMarkerWhitespace(code) {
176
244
  return code === 0 || code === 9 || code === 10 || code === 13 || code === 32;
@@ -188,32 +256,12 @@ function isThematicBreak(value, start, marker) {
188
256
  }
189
257
  return count >= 3;
190
258
  }
191
- function isEntityReferenceAfterAmpersand(value, ampersand) {
192
- let index = ampersand + 1;
193
- if (value.charCodeAt(index) === 35) {
194
- index++;
195
- const hex = value.charCodeAt(index) === 120 || value.charCodeAt(index) === 88;
196
- if (hex) index++;
197
- const start = index;
198
- while (index < value.length) {
199
- const code = value.charCodeAt(index);
200
- if (!(code >= 48 && code <= 57 || hex && (code >= 65 && code <= 70 || code >= 97 && code <= 102))) break;
201
- index++;
202
- }
203
- return index > start && value.charCodeAt(index) === 59;
204
- }
205
- const start = index;
206
- while (index < value.length) {
207
- const code = value.charCodeAt(index);
208
- if (!(code >= 48 && code <= 57 || code >= 65 && code <= 90 || code >= 97 && code <= 122)) break;
209
- index++;
210
- }
211
- return index > start && value.charCodeAt(index) === 59;
212
- }
213
259
  const GFM_TEXT_NATIVE_TRIGGER = /[\\*_~`[<\r\n]/;
214
260
  function mayNeedGfmTextEscape(value, buffer, depthMap) {
215
261
  if (value.search(GFM_TEXT_NATIVE_TRIGGER) !== -1) return true;
216
262
  if (depthMap[28] && value.includes("|") || depthMap[26] && value.includes("]") || depthMap[22] && value.includes(">")) return true;
263
+ const first = value.charCodeAt(0);
264
+ if (!" #+->".includes(value[0]) && !(first >= 48 && first <= 57)) return false;
217
265
  let lineIndent = markdownLineIndent(buffer);
218
266
  if (lineIndent < 0) return false;
219
267
  let index = 0;
@@ -223,7 +271,7 @@ function mayNeedGfmTextEscape(value, buffer, depthMap) {
223
271
  }
224
272
  if (index >= value.length) return false;
225
273
  const code = value.charCodeAt(index);
226
- return code === 35 || code === 43 || code === 45 || code === 62 || code >= 48 && code <= 57;
274
+ return "#+->".includes(value[index]) || code >= 48 && code <= 57;
227
275
  }
228
276
  function escapeGfmText(value, buffer, depthMap) {
229
277
  const inTable = Boolean(depthMap[28]);
@@ -235,8 +283,9 @@ function escapeGfmText(value, buffer, depthMap) {
235
283
  let escaped = "";
236
284
  for (let index = 0; index < value.length; index++) {
237
285
  const code = value.charCodeAt(index);
286
+ const character = value[index];
238
287
  const next = index + 1 < value.length ? value.charCodeAt(index + 1) : 0;
239
- if (code === 92 && next === 38 && isEntityReferenceAfterAmpersand(value, index + 1)) {
288
+ if (code === 92 && next === 38 && isCharacterReferenceTail(value, index + 2)) {
240
289
  lineIndent = -1;
241
290
  orderedDigits = 0;
242
291
  index++;
@@ -248,7 +297,7 @@ function escapeGfmText(value, buffer, depthMap) {
248
297
  copiedUntil = index + 1;
249
298
  continue;
250
299
  }
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));
300
+ let shouldEscape = "\\*_~`[".includes(character) || code === 93 && inLink || code === 124 && inTable || code === 62 && inBlockquote || code === 60 && canStartGfmAngleConstruct(value.charCodeAt(index + 1));
252
301
  if (!shouldEscape && lineIndent >= 0) {
253
302
  if (code === 35) {
254
303
  let end = index + 1;
@@ -259,7 +308,7 @@ function escapeGfmText(value, buffer, depthMap) {
259
308
  } else if (!shouldEscape && orderedDigits > 0 && (code === 46 || code === 41)) shouldEscape = index + 1 === value.length || isMarkdownMarkerWhitespace(value.charCodeAt(index + 1));
260
309
  if (shouldEscape) {
261
310
  escaped += value.slice(copiedUntil, index);
262
- escaped += `\\${value[index]}`;
311
+ escaped += `\\${character}`;
263
312
  copiedUntil = index + 1;
264
313
  }
265
314
  if (code === 10) {
@@ -323,7 +372,7 @@ function calculateNewLineConfig(node, depthMap, plainText) {
323
372
  if (plainText && tagId === 34) return [1, 1];
324
373
  return NO_SPACING;
325
374
  }
326
- const isBlockElement = tagId !== void 0 && (tagId >= 7 && tagId <= 12 || tagId === 35 || tagId === 36);
375
+ const isBlockElement = tagId !== void 0 && (tagId >= 7 && tagId <= 12 || tagId === 35 || tagId === 36 || tagId === 25);
327
376
  let currParent = node.parent;
328
377
  while (currParent) {
329
378
  if (currParent.tagHandler?.collapsesInnerWhiteSpace) {
@@ -410,7 +459,12 @@ function maxLineLeadingRun(value, marker, indent) {
410
459
  return max;
411
460
  }
412
461
  function finalizeCodeSpan(state, span) {
413
- const content = state.buffer.slice(span.fragment + 1).join("");
462
+ let content = state.buffer.slice(span.fragment + 1).join("");
463
+ if ((state.depthMap[28] || 0) > 0 && content.includes("|")) {
464
+ content = content.replaceAll("|", "\\|");
465
+ state.buffer.length = span.fragment + 1;
466
+ state.buffer.push(content);
467
+ }
414
468
  const delimiter = "`".repeat(Math.max(1, maxBacktickRun(content) + 1));
415
469
  const padded = content.startsWith("`") || content.endsWith("`");
416
470
  state.buffer[span.fragment] = `${span.prefix}${delimiter}${padded ? " " : ""}`;
@@ -454,12 +508,7 @@ function finalizeBlockquote(state) {
454
508
  state.lastContentCache = quoted;
455
509
  }
456
510
  function collapseNestedBlockquoteSeparator(buffer) {
457
- let trailingNewlines = 0;
458
- for (let fragmentIndex = buffer.length - 1; fragmentIndex >= 0 && trailingNewlines < 2; fragmentIndex--) {
459
- const fragment = buffer[fragmentIndex];
460
- for (let index = fragment.length - 1; index >= 0 && fragment.charCodeAt(index) === 10; index--) trailingNewlines++;
461
- }
462
- if (trailingNewlines < 2) return;
511
+ if (newlineRunBefore(buffer, buffer.length) < 2) return;
463
512
  const last = buffer.at(-1);
464
513
  if (last.length === 1) buffer.pop();
465
514
  else buffer[buffer.length - 1] = last.slice(0, -1);
@@ -467,13 +516,12 @@ function collapseNestedBlockquoteSeparator(buffer) {
467
516
  function flushPreFence(state) {
468
517
  if (state.plainText) {
469
518
  state.preFencePending = false;
470
- state.preOwnFence = false;
471
519
  return;
472
520
  }
473
521
  state.preFencePending = false;
474
- state.preOwnFence = true;
522
+ state.preFenceOpen = true;
475
523
  const lang = state.preFenceLang || "";
476
- const fence = (state.depthMap[25] || 0) > 0 ? `\n\n${state.listIndent}\`\`\`${lang}\n${state.listIndent}` : `\`\`\`${lang}\n`;
524
+ const fence = (state.depthMap[25] || 0) > 0 ? `${blockOpenPrefix(state.buffer, state.listIndent) ?? ""}\`\`\`${lang}\n${state.listIndent}` : `\`\`\`${lang}\n`;
477
525
  state.buffer.push(fence);
478
526
  startCodeFence(state, state.buffer.length - 1, lang, state.listIndent);
479
527
  state.lastContentCache = fence;
@@ -500,14 +548,14 @@ function consumeGfmAction(action, state, lifecycle) {
500
548
  return;
501
549
  case "PreEnter":
502
550
  state.preFencePending = true;
503
- state.preOwnFence = false;
551
+ state.preFenceOpen = false;
504
552
  state.preFenceLang = action.language;
505
553
  return;
506
554
  case "PreExit": {
507
- const ownFence = state.preOwnFence;
555
+ const fenceOpen = state.preFenceOpen;
508
556
  state.preFencePending = false;
509
- state.preOwnFence = false;
510
- if (!ownFence) return void 0;
557
+ state.preFenceOpen = false;
558
+ if (!fenceOpen) return void 0;
511
559
  const indent = state.listIndent;
512
560
  const output = (state.depthMap[25] || 0) > 0 ? `\n${indent}\`\`\`\n\n${indent}` : `\n\`\`\``;
513
561
  const delimiter = finalizeCodeFence(state);
@@ -520,11 +568,8 @@ function consumeGfmAction(action, state, lifecycle) {
520
568
  }
521
569
  case "CodeFenceEnter":
522
570
  state.preFencePending = false;
571
+ state.preFenceOpen = true;
523
572
  return action.output;
524
- case "CodeFenceExit": {
525
- const delimiter = finalizeCodeFence(state);
526
- return delimiter ? action.output.replace("```", delimiter) : action.output;
527
- }
528
573
  }
529
574
  }
530
575
  function commitGfmAction(action, state, lifecycle, outputStart) {
@@ -561,7 +606,7 @@ function getPlainTextOutput(node, eventType, state) {
561
606
  const depthMap = state.depthMap;
562
607
  if (eventType === 0) {
563
608
  if (tagId === 6) return "\n";
564
- if (tagId === 35 && ((depthMap[22] || 0) > 0 || (depthMap[25] || 0) > 0 && !(depthMap[32] || 0) && !(depthMap[31] || 0))) {
609
+ if (tagId === 35 && ((depthMap[22] || 0) > 0 || (depthMap[25] || 0) > 0 && !isInsideTableCell(state))) {
565
610
  const lastEntry = state.buffer.at(-1);
566
611
  const lastChar = lastEntry?.charAt(lastEntry.length - 1) || "";
567
612
  if (lastChar && lastChar !== " " && lastChar !== "\n") return "\n\n";
@@ -581,13 +626,22 @@ function createMarkdownProcessor(options = {}, resolvedPlugins = [], tagOverride
581
626
  const state = {
582
627
  options,
583
628
  buffer: [],
584
- depthMap: /* @__PURE__ */ new Uint16Array(112),
629
+ depthMap: /* @__PURE__ */ new Uint16Array(113),
585
630
  listIndent: "",
586
631
  listIndentWidths: [],
587
632
  blockquotes: [],
588
633
  bufferedBlockquoteDepth: 0,
589
- plainText: options.format === "text"
634
+ plainText: options.format === "text",
635
+ emptyItemFragment: void 0
590
636
  };
637
+ const bufferScan = [
638
+ false,
639
+ 0,
640
+ 0,
641
+ 0,
642
+ 0
643
+ ];
644
+ let inRawHtmlRegion = false;
591
645
  const openMarkers = [];
592
646
  let openMarkerCount = 0;
593
647
  const gfmLifecycle = { openCodeSpans: [] };
@@ -612,10 +666,12 @@ function createMarkdownProcessor(options = {}, resolvedPlugins = [], tagOverride
612
666
  if (lastChar === "\n" && value[0] && value[0] !== "\n") value = indent + value;
613
667
  textNode.value = value;
614
668
  }
615
- if (state.depthMap[34] > 0 && (state.depthMap[32] > 0 || state.depthMap[31] > 0)) textNode.value = foldPreLinesToBr(textNode.value);
669
+ if (state.depthMap[34] > 0 && isInsideTableCell(state)) textNode.value = foldPreLinesToBr(textNode.value);
616
670
  const insideRawHtmlBlock = isInsideRawHtmlBlock(state.depthMap);
671
+ let rawHtmlMarkdown = false;
672
+ if (insideRawHtmlBlock) rawHtmlMarkdown = trackRawHtmlMarkdownContext(state.buffer, bufferScan);
617
673
  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);
674
+ if (!state.plainText && !state.depthMap[34] && !state.depthMap[23] && (!insideRawHtmlBlock || rawHtmlMarkdown) && mayNeedGfmTextEscape(textNode.value, state.buffer, state.depthMap) && (!insideRawHtmlBlock || !lineOpensRawHtmlBlock(state.buffer, bufferScan))) textNode.value = escapeGfmText(textNode.value, state.buffer, state.depthMap);
619
675
  if (textNode.generatedMarkdown && textNode.value) textNode.value = `${textNode.generatedMarkdown.prefix}${textNode.value}${textNode.generatedMarkdown.suffix}`;
620
676
  const wrapWidth = state.options?.wrapWidth;
621
677
  if (wrapWidth && canWrapHere(state.depthMap)) {
@@ -634,6 +690,17 @@ function createMarkdownProcessor(options = {}, resolvedPlugins = [], tagOverride
634
690
  const { type: eventType, node } = event;
635
691
  state.depth = node.depth;
636
692
  if (node.type === 1 ? node.excludedFromMarkdown : node.parent?.excludedFromMarkdown) return;
693
+ if (state.listRulePending !== void 0) {
694
+ const isVisibleText = node.type === 2 && eventType === 0 && hasNonWhitespace(node.value);
695
+ const opensVisibleElement = node.type === 1 && eventType === 0 && !node.tagHandler?.excludesTextNodes;
696
+ if (isVisibleText || opensVisibleElement) {
697
+ const separator = blockOpenPrefix(state.buffer, state.listRulePending);
698
+ state.buffer.push(separator);
699
+ state.lastContentCache = separator;
700
+ state.listRulePending = void 0;
701
+ } else if (node.type === 1 && eventType === 1 && node.tagId === 25) state.listRulePending = void 0;
702
+ else if (node.type === 2) return;
703
+ }
637
704
  const lastNode = state.lastNode;
638
705
  state.lastNode = event.node;
639
706
  const buff = state.buffer;
@@ -647,6 +714,12 @@ function createMarkdownProcessor(options = {}, resolvedPlugins = [], tagOverride
647
714
  if (node.type !== 1) return;
648
715
  const element = node;
649
716
  const handler = node.tagHandler;
717
+ const insideRawHtmlRegion = isInsideRawHtmlBlock(state.depthMap);
718
+ if (insideRawHtmlRegion && !inRawHtmlRegion) {
719
+ bufferScan[0] = false;
720
+ bufferScan[1] = state.buffer.length;
721
+ inRawHtmlRegion = true;
722
+ }
650
723
  if (eventType === 0 && handler === breakHandler && !element.pluginOutput?.length) {
651
724
  const inPre = state.depthMap[34] !== 0;
652
725
  let breakOutput = state.plainText ? "\n" : renderBreak(element, state);
@@ -697,7 +770,8 @@ function createMarkdownProcessor(options = {}, resolvedPlugins = [], tagOverride
697
770
  const isInlineElement = handler?.isInline === true;
698
771
  let gfmAction;
699
772
  let handlerOutput;
700
- if (!output && handler?.[eventFn]) {
773
+ const suppressedInPre = !state.plainText && (state.depthMap[34] || 0) > 0 && suppressesFormattingInPre(element.tagId);
774
+ if (!output && !suppressedInPre && handler?.[eventFn]) {
701
775
  const res = state.plainText ? getPlainTextOutput(element, eventType, state) : handler[eventFn]({
702
776
  node: element,
703
777
  state
@@ -815,6 +889,8 @@ function createMarkdownProcessor(options = {}, resolvedPlugins = [], tagOverride
815
889
  }
816
890
  }
817
891
  if (gfmAction) commitGfmAction(gfmAction, state, gfmLifecycle, outputStart);
892
+ if (element.tagId === 25 && !state.plainText && !isInsideTableCell(state)) if (eventType === 0) recordItemMarker(state, element.index);
893
+ else resolveItemMarker(state, true);
818
894
  if (eventType === 0 && element.tagId === 26 && handlerOutput === "[" && buff.at(-1) === "[") openLinkFragment = buff.length - 1;
819
895
  if (eventType === 0 && handlerOutput !== void 0 && isInlineElement && !handler?.literalEnter) {
820
896
  const markerType = INLINE_MARKER_TYPE[element.tagId];
@@ -823,6 +899,11 @@ function createMarkdownProcessor(options = {}, resolvedPlugins = [], tagOverride
823
899
  } else if (openMarkerCount && (handler?.literalExit || element.tagId !== -1 && !isInlineElement || output?.some(hasNonWhitespace))) openMarkerCount = 0;
824
900
  if (eventType === 1 && element.tagId === 26) openLinkFragment = -1;
825
901
  updateListIndent(state, element, eventType);
902
+ if (!insideRawHtmlRegion && inRawHtmlRegion) {
903
+ bufferScan[0] = false;
904
+ bufferScan[1] = state.buffer.length;
905
+ inRawHtmlRegion = false;
906
+ }
826
907
  }
827
908
  function processHtml(html) {
828
909
  const parseState = {
@@ -843,6 +924,7 @@ function createMarkdownProcessor(options = {}, resolvedPlugins = [], tagOverride
843
924
  return result.trimEnd();
844
925
  }
845
926
  function getMarkdownChunk() {
927
+ resolveItemMarker(state, false);
846
928
  const content = state.buffer.join("");
847
929
  const currentContent = hasYieldedContent || state.plainText && preserveLeadingWhitespace ? content : content.trimStart();
848
930
  const inPre = state.depthMap[34] !== 0;
@@ -877,6 +959,12 @@ function createMarkdownProcessor(options = {}, resolvedPlugins = [], tagOverride
877
959
  const spanPos = Math.max(lastYieldedLength, trimBufferedWhitespacePosition(currentContent, fragmentPosition(state.buffer, gfmLifecycle.openCodeSpans[0].fragment) - leadingTrimmed));
878
960
  if (spanPos < stableLength) stableLength = spanPos;
879
961
  }
962
+ const emptyItemFragment = state.emptyItemFragment;
963
+ const emptyItemHeld = emptyItemFragment !== void 0;
964
+ if (emptyItemHeld) {
965
+ const itemPos = Math.max(lastYieldedLength, trimBufferedWhitespacePosition(currentContent, fragmentPosition(state.buffer, emptyItemFragment) - leadingTrimmed));
966
+ if (itemPos < stableLength) stableLength = itemPos;
967
+ }
880
968
  const codeFenceHeld = state.codeFence !== void 0;
881
969
  if (codeFenceHeld) {
882
970
  const fencePos = Math.max(lastYieldedLength, trimBufferedWhitespacePosition(currentContent, fragmentPosition(state.buffer, state.codeFence.fragment) - leadingTrimmed));
@@ -892,11 +980,21 @@ function createMarkdownProcessor(options = {}, resolvedPlugins = [], tagOverride
892
980
  const linkPos = Math.max(lastYieldedLength, trimBufferedWhitespacePosition(currentContent, fragmentPosition(state.buffer, openLinkFragment) - leadingTrimmed));
893
981
  if (linkPos < stableLength) stableLength = linkPos;
894
982
  }
983
+ const headingHeld = isInsideHeading(state.depthMap);
984
+ if (headingHeld) {
985
+ let headingPos = currentContent.length;
986
+ while (headingPos > 0) {
987
+ const code = currentContent.charCodeAt(headingPos - 1);
988
+ if (code !== 35 && code !== 32 && code !== 9) break;
989
+ headingPos--;
990
+ }
991
+ if (headingPos < stableLength) stableLength = headingPos;
992
+ }
895
993
  if (stableLength < lastYieldedLength) stableLength = lastYieldedLength;
896
994
  const newContent = currentContent.slice(lastYieldedLength, stableLength);
897
995
  lastYieldedLength = stableLength;
898
996
  if (newContent) hasYieldedContent = true;
899
- if (!markerHeld && !codeSpanHeld && !codeFenceHeld && !blockquoteHeld && !linkHeld && (!retainMutableFragments || !inPre)) {
997
+ if (!markerHeld && !codeSpanHeld && !codeFenceHeld && !blockquoteHeld && !linkHeld && !emptyItemHeld && !headingHeld && (!retainMutableFragments || !inPre)) {
900
998
  if (!resolvedPlugins.length && !options.wrapWidth && !state.depthMap[26]) {
901
999
  if (retainMutableFragments && leadingTrimmed === 0) {
902
1000
  const lastFragment = state.buffer.at(-1);
@@ -904,6 +1002,7 @@ function createMarkdownProcessor(options = {}, resolvedPlugins = [], tagOverride
904
1002
  const tailStart = Math.max(0, Math.min(stableLength - 2, fragmentStart));
905
1003
  const emittedTail = currentContent.slice(tailStart, fragmentStart);
906
1004
  state.buffer.length = 0;
1005
+ resetBufferScanCursors(bufferScan);
907
1006
  if (emittedTail) state.buffer.push(emittedTail);
908
1007
  state.buffer.push(lastFragment);
909
1008
  lastYieldedLength = stableLength - tailStart;
@@ -911,11 +1010,13 @@ function createMarkdownProcessor(options = {}, resolvedPlugins = [], tagOverride
911
1010
  const tailStart = Math.max(0, stableLength - 2);
912
1011
  const emittedTail = currentContent.slice(tailStart, stableLength);
913
1012
  state.buffer.length = 0;
1013
+ resetBufferScanCursors(bufferScan);
914
1014
  if (emittedTail) state.buffer.push(emittedTail);
915
1015
  lastYieldedLength = emittedTail.length;
916
1016
  }
917
1017
  } else if (!retainMutableFragments && state.buffer.length > 1) {
918
1018
  state.buffer.length = 0;
1019
+ resetBufferScanCursors(bufferScan);
919
1020
  state.buffer.push(currentContent);
920
1021
  }
921
1022
  }