@mdream/js 2.1.0 → 2.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,5 @@
1
1
  import { BLOCKQUOTE_SPACING, DEFAULT_BLOCK_SPACING, LIST_ITEM_SPACING, NO_SPACING, TABLE_ROW_SPACING, isInsideRawHtmlBlock } from "./const.mjs";
2
- import { blockOpenPrefix, endsAtHardBreak, figcaptionOwnsBlockSpacing, getLanguageFromAttributes, hasNonWhitespace, isCharacterReferenceTail, isDataUrl, isEmptyLinkHref, isInsideHeading, isInsideTableCell, lastOutputChar, listMarkerLineStart, markRenderedChildContent, needsSpacing, noteBufferRewrite, orderedItemNumber, parseUnsignedInteger, pendingMarkerIndent, resolveUrl, safeAnchorOutput, suppressesFormattingInPre, traverseUpToFirstBlockNode, trimOutputStart, trimTextAtLineStart } from "./url.mjs";
2
+ import { blockOpenPrefix, dropSourceLead, endsAtHardBreak, figcaptionOwnsBlockSpacing, getLanguageFromAttributes, hasNonWhitespace, isBlockChildOfOpenVoid, isCharacterReferenceTail, isDataUrl, isEmptyLinkHref, isInsideHeading, isInsideTableCell, isOpenVoidTag, lastOutputChar, listMarkerLineStart, markRenderedChildContent, needsSpacing, noteBufferRewrite, orderedItemNumber, outputLeadLength, parseUnsignedInteger, parserTagHandlers, pendingMarkerIndent, resolveUrl, safeAnchorOutput, suppressesFormattingInPre, traverseUpToFirstBlockNode, trimOutputStart, trimTextAtLineStart } from "./url.mjs";
3
3
  function serializeMarkdownDestination(destination) {
4
4
  if (!/[\t\n\f\r ()\\<>]/.test(destination)) return destination;
5
5
  return `<${/[\\<>]/.test(destination) ? destination.replace(/[\\<>]/g, "\\$&") : destination}>`;
@@ -32,6 +32,7 @@ function isAutolinkUri(s) {
32
32
  }
33
33
  function renderBreak(state) {
34
34
  const depthMap = state.depthMap;
35
+ if (!depthMap[34] && state.codeText?.kind === "span") return " ";
35
36
  if (isInsideTableCell(state) || isInsideRawHtmlBlock(depthMap) || isInsideHeading(depthMap)) return "<br>";
36
37
  if (depthMap[34] || depthMap[23]) return "\n";
37
38
  return ` \n${state.listIndent ?? ""}`;
@@ -43,6 +44,26 @@ const breakHandler = {
43
44
  collapsesInnerWhiteSpace: true,
44
45
  isInline: true
45
46
  };
47
+ const CODE_SPAN_ENTER = {
48
+ _tag: "CodeSpanEnter",
49
+ output: "`"
50
+ };
51
+ const SPACED_CODE_SPAN_ENTER = {
52
+ _tag: "CodeSpanEnter",
53
+ output: " `"
54
+ };
55
+ const CODE_SPAN_EXIT = { _tag: "CodeSpanExit" };
56
+ function inlineCodeEnter(state, separateFromListText = false) {
57
+ const last = lastOutputChar(state.buffer);
58
+ return last === 96 || separateFromListText && last !== -1 && last !== 32 && last !== 10 && last !== 9 && last !== 42 && last !== 95 && last !== 126 && last !== 91 && last !== 62 ? SPACED_CODE_SPAN_ENTER : CODE_SPAN_ENTER;
59
+ }
60
+ const codeLikeHandler = {
61
+ enter: ({ state }) => isInsideRawHtmlBlock(state.depthMap) ? "`" : inlineCodeEnter(state),
62
+ exit: ({ state }) => isInsideRawHtmlBlock(state.depthMap) ? "`" : CODE_SPAN_EXIT,
63
+ collapsesInnerWhiteSpace: true,
64
+ spacing: NO_SPACING,
65
+ isInline: true
66
+ };
46
67
  const LEAD_GAP = 0;
47
68
  const LEAD_DIGITS = 1;
48
69
  const LEAD_INDENT = 2;
@@ -101,11 +122,20 @@ function markerOpensBlockLine(state, index, marker) {
101
122
  function isLeadChar(code) {
102
123
  return code === 32 || code >= 48 && code <= 57 || code === 45 || code === 42 || code === 43 || code === 46 || code === 41;
103
124
  }
125
+ function isBlankEntry(entry) {
126
+ for (let i = 0; i < entry.length; i++) {
127
+ const code = entry.charCodeAt(i);
128
+ if (code !== 32 && code !== 9) return false;
129
+ }
130
+ return true;
131
+ }
104
132
  function escapeTrailingHeadingHashes(state) {
105
133
  const buffer = state.buffer;
106
134
  let index = buffer.length - 1;
107
135
  while (index >= 0 && buffer[index] === "") index--;
108
136
  if (index < 0) return;
137
+ const last = index;
138
+ while (index > 0 && isBlankEntry(buffer[index])) index--;
109
139
  const entry = buffer[index];
110
140
  let end = entry.length;
111
141
  while (end > 0 && (entry[end - 1] === " " || entry[end - 1] === " ")) end--;
@@ -114,6 +144,7 @@ function escapeTrailingHeadingHashes(state) {
114
144
  if (run === end) return;
115
145
  let replacement;
116
146
  if (opensBlockLine(buffer, index, run)) {
147
+ if (index !== last) return;
117
148
  let trimmed = entry.length;
118
149
  while (trimmed > end && entry.charCodeAt(trimmed - 1) === 32) trimmed--;
119
150
  if (trimmed === entry.length) return;
@@ -218,10 +249,12 @@ function handleHeading(depth) {
218
249
  }
219
250
  const Strong = {
220
251
  enter: ({ state }) => {
252
+ if (state.codeText) return void 0;
221
253
  if ((state.depthMap?.[15] || 0) > 1) return "";
222
254
  return "**";
223
255
  },
224
256
  exit: ({ node, state }) => {
257
+ if (state.codeText) return void 0;
225
258
  if ((state.depthMap?.[15] || 0) + (node.tagId === 15 ? 1 : 0) > 1) return "";
226
259
  return "**";
227
260
  },
@@ -231,10 +264,12 @@ const Strong = {
231
264
  };
232
265
  const Emphasis = {
233
266
  enter: ({ state }) => {
267
+ if (state.codeText) return void 0;
234
268
  if ((state.depthMap?.[17] || 0) > 1) return "";
235
269
  return "*";
236
270
  },
237
271
  exit: ({ node, state }) => {
272
+ if (state.codeText) return void 0;
238
273
  if ((state.depthMap?.[17] || 0) + (node.tagId === 17 ? 1 : 0) > 1) return "";
239
274
  return "*";
240
275
  },
@@ -304,7 +339,7 @@ const tagHandlers = {
304
339
  const end = enterOutputEnd(node, state);
305
340
  if (state.codeFence?.fragment === end - 1 && state.buffer[end - 1].endsWith("\n")) return `${prefix}***`;
306
341
  const open = blockOpenPrefix(state, prefix, end);
307
- return open === void 0 && prefix ? "***" : `${open ?? ""}---`;
342
+ return open === void 0 && prefix && !state.lastItemStar ? "***" : `${open ?? ""}---`;
308
343
  },
309
344
  isSelfClosing: true
310
345
  },
@@ -381,18 +416,7 @@ const tagHandlers = {
381
416
  };
382
417
  }
383
418
  if (isInsideRawHtmlBlock(state.depthMap)) return "<code>";
384
- if ((state.depthMap?.[25] || 0) > 0) {
385
- const lastEntry = state.buffer.at(-1);
386
- const lastChar = lastEntry?.charAt(lastEntry.length - 1) || "";
387
- if (lastChar && lastChar !== " " && lastChar !== "\n" && lastChar !== " " && lastChar !== "*" && lastChar !== "_" && lastChar !== "~" && lastChar !== "[" && lastChar !== ">") return {
388
- _tag: "CodeSpanEnter",
389
- output: ` \``
390
- };
391
- }
392
- return {
393
- _tag: "CodeSpanEnter",
394
- output: "`"
395
- };
419
+ return inlineCodeEnter(state, !!state.depthMap?.[25]);
396
420
  },
397
421
  exit: ({ state }) => {
398
422
  if ((state.depthMap?.[34] || 0) > 0) {
@@ -400,7 +424,7 @@ const tagHandlers = {
400
424
  return;
401
425
  }
402
426
  if (isInsideRawHtmlBlock(state.depthMap)) return "</code>";
403
- return { _tag: "CodeSpanExit" };
427
+ return CODE_SPAN_EXIT;
404
428
  },
405
429
  collapsesInnerWhiteSpace: true,
406
430
  spacing: NO_SPACING,
@@ -417,7 +441,10 @@ const tagHandlers = {
417
441
  [25]: {
418
442
  enter: ({ node, state }) => {
419
443
  if (isInsideTableCell(state)) return "<li>";
420
- const marker = node.parent?.tagId === 33 ? `${orderedItemNumber(node.parent, node.index)}. ` : "- ";
444
+ const isOrdered = node.parent?.tagId === 33;
445
+ const markers = state.listMarkers;
446
+ const character = markers && markers.length > 0 ? markers[markers.length - 1] & 255 : 0;
447
+ const marker = isOrdered ? `${orderedItemNumber(node.parent, node.index)}${character === 41 ? ") " : ". "}` : character === 42 ? "* " : "- ";
421
448
  return `${state.listIndent}${marker}`;
422
449
  },
423
450
  exit: ({ state }) => isInsideTableCell(state) ? "</li>" : void 0,
@@ -437,7 +464,15 @@ const tagHandlers = {
437
464
  if (isInsideRawHtmlBlock(state.depthMap) && !node.tagHandler?.literalEnter) return safeAnchorOutput(node, state.options, false, true);
438
465
  const href = resolveUrl(node.attributes.href, state.options?.origin, state.options?.clean);
439
466
  let title = node.attributes?.title;
440
- if (state.lastContentCache === title) title = "";
467
+ let preservesCodeWhitespace = false;
468
+ if (state.codeText?.kind === "span" && !state.depthMap?.[34] && !state.lastTextNode?.containsWhitespace) {
469
+ const buffer = state.buffer;
470
+ let tail = buffer.length - 1;
471
+ while (tail >= 0 && !buffer[tail]) tail--;
472
+ preservesCodeWhitespace = buffer[tail] === state.lastContentCache;
473
+ }
474
+ const cachedText = preservesCodeWhitespace ? state.lastContentCache : state.lastContentCache?.replace(/[ \t\n\f\r]+$/, "");
475
+ if (title && !state.cacheFollowsSeparator && state.droppedPairAt !== state.buffer.length && cachedText === title.replace(/[ \t\n\f\r]+$/, "")) title = "";
441
476
  if (!title && isAutolinkUri(href)) {
442
477
  const buf = state.buffer;
443
478
  let i = buf.length - 1;
@@ -573,9 +608,8 @@ const tagHandlers = {
573
608
  },
574
609
  [35]: { enter: ({ node, state }) => {
575
610
  if ((state.depthMap?.[25] || 0) > 0 && !isInsideTableCell(state) && !state.preFencePending && state.preFenceOpener !== node) {
576
- const lastEntry = state.buffer[enterOutputEnd(node, state) - 1];
577
- const lastChar = lastEntry?.charAt(lastEntry.length - 1) || "";
578
- if (lastChar && lastChar !== " " && lastChar !== "\n") return `\n\n${state.listIndent}`;
611
+ const last = lastOutputChar(state.buffer, enterOutputEnd(node, state));
612
+ if (last !== -1 && last !== 32 && last !== 10) return `\n\n${state.listIndent}`;
579
613
  }
580
614
  } },
581
615
  [36]: { enter: ({ state }) => {
@@ -623,13 +657,7 @@ const tagHandlers = {
623
657
  spacing: TABLE_ROW_SPACING,
624
658
  excludesTextNodes: true
625
659
  },
626
- [46]: {
627
- enter: () => "`",
628
- exit: () => "`",
629
- collapsesInnerWhiteSpace: true,
630
- spacing: NO_SPACING,
631
- isInline: true
632
- },
660
+ [46]: codeLikeHandler,
633
661
  [47]: { spacing: NO_SPACING },
634
662
  [40]: { spacing: NO_SPACING },
635
663
  [54]: {
@@ -731,13 +759,7 @@ const tagHandlers = {
731
759
  spacing: NO_SPACING,
732
760
  isInline: true
733
761
  },
734
- [82]: {
735
- enter: () => "`",
736
- exit: () => "`",
737
- collapsesInnerWhiteSpace: true,
738
- spacing: NO_SPACING,
739
- isInline: true
740
- },
762
+ [82]: codeLikeHandler,
741
763
  [83]: {
742
764
  enter: () => "",
743
765
  exit: () => "",
@@ -774,26 +796,20 @@ const tagHandlers = {
774
796
  isInline: true
775
797
  },
776
798
  [90]: {
777
- enter: () => "*",
778
- exit: () => "*",
799
+ enter: ({ state }) => state.codeText ? void 0 : "*",
800
+ exit: ({ state }) => state.codeText ? void 0 : "*",
779
801
  collapsesInnerWhiteSpace: true,
780
802
  spacing: NO_SPACING,
781
803
  isInline: true
782
804
  },
783
805
  [91]: {
784
- enter: () => "**",
785
- exit: () => "**",
786
- collapsesInnerWhiteSpace: true,
787
- spacing: NO_SPACING,
788
- isInline: true
789
- },
790
- [92]: {
791
- enter: () => "`",
792
- exit: () => "`",
806
+ enter: ({ state }) => state.codeText ? void 0 : "**",
807
+ exit: ({ state }) => state.codeText ? void 0 : "**",
793
808
  collapsesInnerWhiteSpace: true,
794
809
  spacing: NO_SPACING,
795
810
  isInline: true
796
811
  },
812
+ [92]: codeLikeHandler,
797
813
  [93]: {
798
814
  enter: () => "",
799
815
  exit: () => "",
@@ -868,6 +884,7 @@ const CAPTION_FRAME_SIZE = 4;
868
884
  const CAPTION_OPEN = 1;
869
885
  const CAPTION_INTERNAL_BREAK = 2;
870
886
  const CAPTION_HEADING_BREAK = 4;
887
+ const CAPTION_BLOCK_PREFIX = 8;
871
888
  const CAPTION_DEFAULT_SPACING = -1;
872
889
  const CAPTION_NO_ANCHOR = -1;
873
890
  INLINE_MARKER_TYPE[14] = 1;
@@ -884,6 +901,19 @@ INLINE_MARKER_TYPE[23] = 5;
884
901
  INLINE_MARKER_TYPE[82] = 5;
885
902
  INLINE_MARKER_TYPE[92] = 5;
886
903
  INLINE_MARKER_TYPE[81] = 6;
904
+ function isAsciiPunctuation(code) {
905
+ return code >= 33 && code <= 126 && !isAsciiAlphanumeric(code);
906
+ }
907
+ function isAsciiAlphanumeric(code) {
908
+ return code >= 48 && code <= 57 || code >= 65 && code <= 90 || code >= 97 && code <= 122;
909
+ }
910
+ function breaksPendingCloser(output, entering, closer) {
911
+ for (let index = 0; index < output.length; index++) {
912
+ const fragment = output[index];
913
+ if (fragment && (entering || fragment.charCodeAt(0) !== closer || fragment.charCodeAt(fragment.length - 1) !== closer)) return true;
914
+ }
915
+ return false;
916
+ }
887
917
  function newlineRunBefore(buffer, index) {
888
918
  let run = 0;
889
919
  for (let i = index - 1; i >= 0; i--) {
@@ -982,21 +1012,32 @@ function resolveItemMarker(state, atExit, unresolvedCaptionFragment = -1, linkOp
982
1012
  buffer[fragment] = replacement;
983
1013
  noteBufferRewrite(state);
984
1014
  }
985
- function updateListIndent(state, element, eventType) {
986
- if (element.tagId !== 25) return;
1015
+ function updateListIndent(state, element, eventType, output, pluginWrote) {
987
1016
  if (isInsideTableCell(state)) return;
988
1017
  if (eventType === 0) {
989
1018
  const width = element.parent?.tagId === 33 ? String(orderedItemNumber(element.parent, element.index)).length + 2 : 2;
990
1019
  state.listIndentWidths.push(width);
991
1020
  state.listIndent += " ".repeat(width);
1021
+ const markers = state.listMarkers;
1022
+ const top = markers.length - 1;
1023
+ const selected = top >= 0 ? markers[top] & 255 : 0;
1024
+ let written = 0;
1025
+ if (pluginWrote || element.tagHandler?.literalEnter) written = output ? writtenListMarker(output.join("")) : 0;
1026
+ else if (output?.some(hasNonWhitespace)) written = selected;
1027
+ if (written !== 0 && top >= 0) markers[top] = selected | written << 8;
1028
+ state.lastItemStar = written === 42;
992
1029
  } else if (eventType === 1) {
993
1030
  const width = state.listIndentWidths.pop() ?? 0;
994
1031
  state.listIndent = state.listIndent.slice(0, state.listIndent.length - width);
995
1032
  }
996
1033
  }
1034
+ function needsMarkdownSpacing(lastChar, firstChar, openMarkerCount) {
1035
+ if (lastChar === "~" && openMarkerCount) return firstChar === "~";
1036
+ return needsSpacing(lastChar, firstChar);
1037
+ }
997
1038
  function shouldAddSpacingBeforeText(lastChar, lastNode, textNode) {
998
1039
  if (!lastChar || "\n [>".includes(lastChar)) return false;
999
- if (textNode.joinsPrevious || lastNode && (lastNode.tagHandler ? lastNode.tagHandler.isInline : lastNode.tagId === -1)) return false;
1040
+ if (textNode.joinsPrevious || lastNode && (lastNode.tagHandler ? lastNode.tagHandler.isInline || isOpenVoidTag(lastNode.tagHandler, lastNode.tagId) : lastNode.tagId === -1)) return false;
1000
1041
  const firstChar = textNode.value[0];
1001
1042
  if (firstChar === " ") return false;
1002
1043
  if (firstChar && ".,!?:;_*`)]".includes(firstChar)) return false;
@@ -1149,7 +1190,9 @@ function isEqualsLine(value, start) {
1149
1190
  return true;
1150
1191
  }
1151
1192
  const GFM_TEXT_NATIVE_TRIGGER = /[\\*_~`[<\r\n]/;
1152
- function lineStartDigits(buffer, indent, value) {
1193
+ function lineStartDigits(state, value) {
1194
+ const buffer = state.buffer;
1195
+ const indent = state.listIndent;
1153
1196
  let lead = 0;
1154
1197
  while (value.charCodeAt(lead) >= 48 && value.charCodeAt(lead) <= 57) lead++;
1155
1198
  let one = lead === 0 || value.charCodeAt(lead - 1) === 49;
@@ -1165,7 +1208,7 @@ function lineStartDigits(buffer, indent, value) {
1165
1208
  offset = fragment >= 0 ? buffer[fragment].length : 0;
1166
1209
  }
1167
1210
  const code = fragment >= 0 ? buffer[fragment].charCodeAt(--offset) : -1;
1168
- if (newline) return code === -1 || code === 10 || code === 32 || code === 9 || code === 92 ? digits : 0;
1211
+ if (newline) return code === -1 || code === 10 || code === 32 || code === 9 || code === 92 || outputStartsAt(state, fragment, offset + 1) ? digits : 0;
1169
1212
  if (spaces === 0 && code >= 48 && code <= 57) {
1170
1213
  if (++digits > 9) return 0;
1171
1214
  one &&= code === (digits === 1 && lead === 0 ? 49 : 48);
@@ -1173,7 +1216,7 @@ function lineStartDigits(buffer, indent, value) {
1173
1216
  }
1174
1217
  if (digits === 0) return 0;
1175
1218
  if (code === 32) {
1176
- if (++spaces > indent.length + 3) return 0;
1219
+ if (++spaces > indent.length + 3) return outputStartsAt(state, fragment, offset) ? digits : 0;
1177
1220
  continue;
1178
1221
  }
1179
1222
  if (code === -1 || code === 10) {
@@ -1182,8 +1225,27 @@ function lineStartDigits(buffer, indent, value) {
1182
1225
  newline = true;
1183
1226
  continue;
1184
1227
  }
1185
- return spaces > 0 && spaces <= 3 && listMarkerLineStart(buffer, fragment, offset) ? digits : 0;
1228
+ return spaces > 0 && spaces <= 3 && listMarkerLineStart(buffer, fragment, offset) || outputStartsAt(state, fragment, offset + 1) ? digits : 0;
1229
+ }
1230
+ }
1231
+ function outputStartsAt(state, fragment, offset) {
1232
+ if (state.drainedContent || state.blockquotes.length !== 0) return false;
1233
+ for (let index = fragment; index >= 0; index--) {
1234
+ const value = state.buffer[index];
1235
+ if (outputLeadLength(value) < (index === fragment ? offset : value.length)) return false;
1186
1236
  }
1237
+ return true;
1238
+ }
1239
+ function outputStartMarker(value, state) {
1240
+ const lead = outputLeadLength(value);
1241
+ const marker = value.charCodeAt(lead);
1242
+ if (marker !== 35 && marker !== 43 && marker !== 45 && marker !== 61 && marker !== 62 && !(marker >= 48 && marker <= 57)) return -1;
1243
+ if (state.depthMap[28]) for (let index = 0; index < lead; index++) {
1244
+ const code = value.charCodeAt(index);
1245
+ if (code === 10 || code === 13) return -1;
1246
+ }
1247
+ const last = state.buffer.length - 1;
1248
+ return outputStartsAt(state, last, last < 0 ? 0 : state.buffer[last].length) ? lead : -1;
1187
1249
  }
1188
1250
  function mayNeedGfmTextEscape(value, buffer, depthMap, structuralIndent) {
1189
1251
  if (value.search(GFM_TEXT_NATIVE_TRIGGER) !== -1) return true;
@@ -1201,14 +1263,14 @@ function mayNeedGfmTextEscape(value, buffer, depthMap, structuralIndent) {
1201
1263
  const code = value.charCodeAt(index);
1202
1264
  return "#+->=".includes(value[index]) || code >= 48 && code <= 57;
1203
1265
  }
1204
- function escapeGfmText(value, buffer, depthMap, structuralIndent, orderedDigits = 0) {
1266
+ function escapeGfmText(value, buffer, depthMap, structuralIndent, orderedDigits = 0, outputStart = -1) {
1205
1267
  const inTable = Boolean(depthMap[28]);
1206
1268
  const inLink = Boolean(depthMap[26]);
1207
1269
  const inBlockquote = Boolean(depthMap[22]);
1208
- let lineIndent = markdownLineIndent(buffer, structuralIndent);
1270
+ let lineIndent = outputStart === -1 ? markdownLineIndent(buffer, structuralIndent) : 0;
1209
1271
  let copiedUntil = 0;
1210
1272
  let escaped = "";
1211
- for (let index = 0; index < value.length; index++) {
1273
+ for (let index = Math.max(0, outputStart); index < value.length; index++) {
1212
1274
  const code = value.charCodeAt(index);
1213
1275
  const character = value[index];
1214
1276
  const next = index + 1 < value.length ? value.charCodeAt(index + 1) : 0;
@@ -1336,16 +1398,19 @@ function collapsedByAncestor(node, state, eventType) {
1336
1398
  currParent = currParent.parent;
1337
1399
  continue;
1338
1400
  }
1339
- return true;
1401
+ return !isBlockChildOfOpenVoid(node);
1340
1402
  }
1341
1403
  currParent = currParent.parent;
1342
1404
  }
1343
1405
  return false;
1344
1406
  }
1345
1407
  function headingKeptInlineInItem(heading) {
1408
+ let blockKept = false;
1346
1409
  let parent = heading.parent;
1347
1410
  while (parent) {
1411
+ if (blockKept && parent.tagId !== void 0 && parserTagHandlers[parent.tagId]?.isSelfClosing) return false;
1348
1412
  if (parent.tagId !== 37 && parent.tagHandler?.collapsesInnerWhiteSpace && wrapperWritesDelimiters(parent)) return true;
1413
+ blockKept ||= isBlockChildOfOpenVoid(parent);
1349
1414
  parent = parent.parent;
1350
1415
  }
1351
1416
  return false;
@@ -1367,6 +1432,31 @@ function trimLeadingSpaces(value) {
1367
1432
  while (value.charCodeAt(start) === 32) start++;
1368
1433
  return start === 0 ? value : value.slice(start);
1369
1434
  }
1435
+ function exitListMarker(output, continuesItem) {
1436
+ let end = output.length;
1437
+ while (end > 0 && isAsciiWhitespace(output.charCodeAt(end - 1))) end--;
1438
+ if (end === 0) return void 0;
1439
+ for (;;) {
1440
+ let start = end;
1441
+ while (start > 0 && output.charCodeAt(start - 1) !== 10 && output.charCodeAt(start - 1) !== 13) start--;
1442
+ if (start === 0 && continuesItem) return void 0;
1443
+ const marker = writtenListMarker(output.slice(start, end));
1444
+ if (marker !== 0 || start === 0 || start < end && !isAsciiWhitespace(output.charCodeAt(start))) return marker;
1445
+ end = start - 1;
1446
+ }
1447
+ }
1448
+ function writtenListMarker(output) {
1449
+ let last = output.length - 1;
1450
+ while (last >= 0 && isAsciiWhitespace(output.charCodeAt(last))) last--;
1451
+ if (last < 0) return 0;
1452
+ let first = output.lastIndexOf("\n", last) + 1;
1453
+ while (isAsciiWhitespace(output.charCodeAt(first))) first++;
1454
+ let end = first;
1455
+ for (let code = output.charCodeAt(end); code >= 48 && code <= 57; code = output.charCodeAt(end)) end++;
1456
+ const digits = end - first;
1457
+ const marker = output.charCodeAt(end);
1458
+ return (digits === 0 ? marker === 45 || marker === 42 || marker === 43 : digits <= 9 && (marker === 46 || marker === 41)) && (end + 1 === output.length || isAsciiWhitespace(output.charCodeAt(end + 1))) ? marker : 0;
1459
+ }
1370
1460
  function insideOpenInlineDelimiter(node, state) {
1371
1461
  const rawHtml = isInsideRawHtmlBlock(state.depthMap);
1372
1462
  for (let parent = node.parent; parent; parent = parent.parent) {
@@ -1378,6 +1468,14 @@ function insideOpenInlineDelimiter(node, state) {
1378
1468
  function isAsciiWhitespace(code) {
1379
1469
  return code === 32 || code === 9 || code === 10 || code === 12 || code === 13;
1380
1470
  }
1471
+ const CLOSED_MARKER_REACH = 32;
1472
+ function isTransientOutput(fragment) {
1473
+ for (let index = 0; index < fragment.length; index++) {
1474
+ const code = fragment.charCodeAt(index);
1475
+ if (code !== 42 && code !== 126 && code !== 96 && code !== 34 && !isAsciiWhitespace(code)) return false;
1476
+ }
1477
+ return true;
1478
+ }
1381
1479
  function trimAsciiWhitespaceEnd(value) {
1382
1480
  let end = value.length;
1383
1481
  while (end > 0 && isAsciiWhitespace(value.charCodeAt(end - 1))) end--;
@@ -1405,12 +1503,26 @@ function dropVerticalTabMarker(buffer, packed, markerType, marker, positions) {
1405
1503
  buffer[idx] = kept;
1406
1504
  return true;
1407
1505
  }
1506
+ function hasLabelText(value) {
1507
+ for (let index = 0; index < value.length; index++) {
1508
+ const code = value.charCodeAt(index);
1509
+ if (code === 32 || code >= 9 && code <= 13) continue;
1510
+ if (code < 133 || !(code === 133 || code === 160 || code === 5760 || code >= 8192 && code <= 8202 || code === 8232 || code === 8233 || code === 8239 || code === 8287 || code === 12288)) return true;
1511
+ }
1512
+ return false;
1513
+ }
1514
+ function dropBlankLink(buffer, openFragment, positions) {
1515
+ for (let index = openFragment + 1; index < buffer.length; index++) if (hasLabelText(buffer[index])) return false;
1516
+ positions?.replace(openFragment, buffer.length, []);
1517
+ buffer.length = openFragment;
1518
+ return true;
1519
+ }
1408
1520
  function resetRetractedCaptionFrames(frames, count, fragment) {
1409
1521
  if (!frames) return;
1410
1522
  for (let index = 0; index < count; index++) {
1411
1523
  const offset = index * CAPTION_FRAME_SIZE;
1412
1524
  if (frames[offset + 3] >= fragment) {
1413
- frames[offset + 2] = frames[offset + 2] & -2;
1525
+ frames[offset + 2] = frames[offset + 2] & -10;
1414
1526
  frames[offset + 3] = CAPTION_NO_ANCHOR;
1415
1527
  }
1416
1528
  }
@@ -1460,15 +1572,33 @@ function maxLineLeadingRun(value, marker, indent) {
1460
1572
  return max;
1461
1573
  }
1462
1574
  function finalizeCodeSpan(state, span) {
1463
- let content = state.buffer.slice(span.fragment + 1).join("");
1464
- if ((state.depthMap[28] || 0) > 0 && content.includes("|")) {
1465
- const original = content;
1466
- content = content.replaceAll("|", "\\|");
1467
- state.outputPositions?.replace(span.fragment + 1, state.buffer.length, [content], (offset) => {
1468
- let added = 0;
1469
- for (let index = 0; index < offset; index++) if (original.charCodeAt(index) === 124) added++;
1470
- return offset + added;
1575
+ const original = state.buffer.slice(span.fragment + 1).join("");
1576
+ const escapePipes = !!state.depthMap[28] && original.includes("|");
1577
+ const offsetChanges = state.outputPositions ? [] : void 0;
1578
+ let delta = 0;
1579
+ const content = original.replace(escapePipes ? /\r\n?|\n|\|/g : /\r\n?|\n/g, (match, offset) => {
1580
+ if (offsetChanges && match === "|") offsetChanges.push({
1581
+ at: offset,
1582
+ delta: ++delta
1583
+ });
1584
+ else if (offsetChanges && match.length === 2) offsetChanges.push({
1585
+ at: offset + 1,
1586
+ delta: --delta
1471
1587
  });
1588
+ return match === "|" ? "\\|" : " ";
1589
+ });
1590
+ if (content !== original) {
1591
+ const mapOffset = offsetChanges?.length ? (offset) => {
1592
+ let low = 0;
1593
+ let high = offsetChanges.length;
1594
+ while (low < high) {
1595
+ const mid = low + high >>> 1;
1596
+ if (offsetChanges[mid].at < offset) low = mid + 1;
1597
+ else high = mid;
1598
+ }
1599
+ return offset + (offsetChanges[low - 1]?.delta ?? 0);
1600
+ } : void 0;
1601
+ state.outputPositions?.replace(span.fragment + 1, state.buffer.length, [content], mapOffset);
1472
1602
  state.buffer.length = span.fragment + 1;
1473
1603
  state.buffer.push(content);
1474
1604
  noteBufferRewrite(state);
@@ -1543,13 +1673,31 @@ function quotedOffset(content, listIndent, offset) {
1543
1673
  sourceStart = lineEnd + 1;
1544
1674
  }
1545
1675
  }
1676
+ function onItemMarkerLine(state, end = state.buffer.length) {
1677
+ if (!state.listIndent) return false;
1678
+ for (let fragment = end - 1; fragment >= 0; fragment--) {
1679
+ const value = state.buffer[fragment];
1680
+ for (let index = value.length - 1; index >= 0; index--) if (value.charCodeAt(index) !== 32) return listMarkerLineStart(state.buffer, fragment, index, pendingMarkerIndent(state));
1681
+ }
1682
+ return false;
1683
+ }
1546
1684
  function finalizeBlockquote(state) {
1547
1685
  const frame = state.blockquotes.pop();
1548
- if (!frame) return;
1686
+ if (!frame) return false;
1549
1687
  state.bufferedBlockquoteDepth = state.blockquotes.length;
1550
1688
  const buffer = state.buffer;
1551
1689
  const content = trimAsciiWhitespaceEnd(buffer.slice(frame.fragment).join(""));
1552
- if (!content && frame.followsWhitespace) return;
1690
+ if (!content && frame.followsWhitespace) {
1691
+ if (frame.itemSeparatorFragments && !endsAtHardBreak(state.buffer)) {
1692
+ const start = frame.fragment - frame.itemSeparatorFragments;
1693
+ state.outputPositions?.replace(start, buffer.length, []);
1694
+ buffer.length = start;
1695
+ state.lastContentCache = buffer.at(-1);
1696
+ state.lastTextNode = void 0;
1697
+ noteBufferRewrite(state);
1698
+ }
1699
+ return false;
1700
+ }
1553
1701
  const prefix = `${frame.listIndent}>`;
1554
1702
  const quoted = content.split("\n").map((line) => {
1555
1703
  const unindented = stripBlockquoteListIndent(line, frame.listIndent);
@@ -1595,12 +1743,13 @@ function finalizeBlockquote(state) {
1595
1743
  if (end < quoted.length) buffer.push(quoted.slice(end));
1596
1744
  state.lastContentCache = buffer.at(-1);
1597
1745
  noteBufferRewrite(state);
1598
- return;
1746
+ return true;
1599
1747
  }
1600
1748
  state.outputPositions?.replace(frame.fragment, buffer.length, [quoted], mapPosition);
1601
1749
  buffer.splice(frame.fragment, buffer.length - frame.fragment, quoted);
1602
1750
  state.lastContentCache = quoted;
1603
1751
  noteBufferRewrite(state);
1752
+ return true;
1604
1753
  }
1605
1754
  function collapseNestedBlockquoteSeparator(buffer, positions) {
1606
1755
  if (newlineRunBefore(buffer, buffer.length) < 2) return;
@@ -1645,7 +1794,7 @@ function consumeGfmAction(action, state, lifecycle) {
1645
1794
  switch (action._tag) {
1646
1795
  case "BlockquoteEnter": return action.output;
1647
1796
  case "BlockquoteExit":
1648
- finalizeBlockquote(state);
1797
+ lifecycle.quoteWritten = finalizeBlockquote(state);
1649
1798
  return;
1650
1799
  case "PreEnter":
1651
1800
  if (!state.preFenceOwnerDepth && !state.preFencePending) {
@@ -1686,11 +1835,13 @@ function commitGfmAction(action, state, lifecycle, outputStart, owner, keepsQuot
1686
1835
  if (state.blockquotes.length > 0 && !keepsQuoteSeparator && (!rawHtmlQuoteBoundary || newlineRunBefore(state.buffer, state.buffer.length) > 2)) {
1687
1836
  collapseNestedBlockquoteSeparator(state.buffer, state.outputPositions);
1688
1837
  for (const frame of state.blockquotes) frame.fragment = Math.min(frame.fragment, state.buffer.length);
1838
+ outputStart = Math.min(outputStart, state.buffer.length);
1689
1839
  noteBufferRewrite(state);
1690
1840
  }
1691
1841
  const before = lastOutputChar(state.buffer);
1692
1842
  state.blockquotes.push({
1693
1843
  fragment: state.buffer.length,
1844
+ itemSeparatorFragments: onItemMarkerLine(state, outputStart) ? state.buffer.length - outputStart : 0,
1694
1845
  listIndent: isInsideTableCell(state) ? "" : state.listIndent,
1695
1846
  followsWhitespace: before === -1 ? state.lastContentCache !== void 0 && isAsciiWhitespace(state.lastContentCache.charCodeAt(state.lastContentCache.length - 1)) : isAsciiWhitespace(before)
1696
1847
  });
@@ -1698,11 +1849,20 @@ function commitGfmAction(action, state, lifecycle, outputStart, owner, keepsQuot
1698
1849
  break;
1699
1850
  }
1700
1851
  case "CodeSpanEnter":
1701
- lifecycle.openCodeSpans.push({
1702
- owner,
1703
- fragment: outputStart,
1704
- prefix: action.output.slice(0, -1)
1705
- });
1852
+ for (let index = outputStart; index < state.buffer.length; index++) {
1853
+ const output = state.buffer[index];
1854
+ if (output.endsWith("`")) {
1855
+ const span = {
1856
+ kind: "span",
1857
+ owner,
1858
+ fragment: index,
1859
+ prefix: output.slice(0, -1)
1860
+ };
1861
+ lifecycle.openCodeSpans.push(span);
1862
+ if (!owner.tagHandler?.literalEnter) state.codeText ??= span;
1863
+ break;
1864
+ }
1865
+ }
1706
1866
  break;
1707
1867
  case "CodeFenceEnter": {
1708
1868
  const buffer = state.buffer;
@@ -1723,6 +1883,7 @@ function createMarkdownProcessor(options = {}, createStream) {
1723
1883
  outputFormat: "markdown",
1724
1884
  buffer: [],
1725
1885
  outputPositions: void 0,
1886
+ codeText: void 0,
1726
1887
  depthMap: /* @__PURE__ */ new Uint16Array(113),
1727
1888
  depth: 0,
1728
1889
  listIndent: "",
@@ -1732,6 +1893,11 @@ function createMarkdownProcessor(options = {}, createStream) {
1732
1893
  emptyItemFragment: void 0,
1733
1894
  headingBreakPending: false,
1734
1895
  emptyItemAfterCr: false,
1896
+ pendingLeadingBlank: false,
1897
+ pendingPairSeparator: false,
1898
+ pendingDecodedBlank: false,
1899
+ separatorPending: false,
1900
+ cacheFollowsSeparator: false,
1735
1901
  tableRenderedTable: false,
1736
1902
  tableCurrentRowCells: 0,
1737
1903
  tableColumnAlignments: [],
@@ -1739,9 +1905,20 @@ function createMarkdownProcessor(options = {}, createStream) {
1739
1905
  preFencePending: false,
1740
1906
  preFencePendingDepth: 0,
1741
1907
  preFenceOwnerDepth: 0,
1908
+ listMarkers: [],
1909
+ lastItemStar: false,
1742
1910
  fencesUnreliable: hasLiteralTagOverride(options.tagOverrides),
1743
1911
  preFenceOpener: void 0
1744
1912
  };
1913
+ let siblingListMarker = 0;
1914
+ let siblingListDepth = 0;
1915
+ let siblingListQuotes = 0;
1916
+ const listMarkerOwners = options.plugins?.length ? [] : void 0;
1917
+ const listMarkerDepths = listMarkerOwners ? [] : void 0;
1918
+ const eventPath = listMarkerOwners ? [] : void 0;
1919
+ const missingAncestors = listMarkerOwners ? [] : void 0;
1920
+ let inGapEvent = false;
1921
+ let listClosedInGap = false;
1745
1922
  const bufferScan = [
1746
1923
  false,
1747
1924
  0,
@@ -1756,6 +1933,42 @@ function createMarkdownProcessor(options = {}, createStream) {
1756
1933
  let pendingRawHtmlBoundary = false;
1757
1934
  const openMarkers = [];
1758
1935
  let openMarkerCount = 0;
1936
+ let pendingCloser = 0;
1937
+ const openMarkerClosers = [];
1938
+ const closedMarkers = [];
1939
+ let closedMarkerCount = 0;
1940
+ let reopenedDelimiter = "";
1941
+ let reopenedType = 0;
1942
+ let unmergedOpener = -1;
1943
+ let unmergedSlot = -1;
1944
+ function heldClosedMarker() {
1945
+ if (closedMarkerCount === 0) return Infinity;
1946
+ const buffer = state.buffer;
1947
+ let reach = CLOSED_MARKER_REACH;
1948
+ for (let index = closedMarkers[closedMarkerCount - 1] + 1; index < buffer.length; index++) {
1949
+ const fragment = buffer[index];
1950
+ reach -= fragment.length;
1951
+ if (reach < 0 || !isTransientOutput(fragment)) {
1952
+ closedMarkerCount = 0;
1953
+ return Infinity;
1954
+ }
1955
+ }
1956
+ if (closedMarkers[closedMarkerCount - 1] >= buffer.length) {
1957
+ closedMarkerCount = 0;
1958
+ return Infinity;
1959
+ }
1960
+ return closedMarkers[0];
1961
+ }
1962
+ const openMarkerFollowsSpace = [];
1963
+ const openMarkerSeparatorTrimmed = [];
1964
+ const openMarkerRunLength = [];
1965
+ let silentMarkerCount = 0;
1966
+ let blankMarkerCount = 0;
1967
+ let breakAfterLastText = false;
1968
+ let pendingSpaceFragment = -1;
1969
+ let pendingSpaceOwned = false;
1970
+ let pendingSpaceTrimmed = false;
1971
+ const literalOpeners = [];
1759
1972
  let captionFrames;
1760
1973
  let captionFrameCount = 0;
1761
1974
  let captionBoundary = 0;
@@ -1767,11 +1980,15 @@ function createMarkdownProcessor(options = {}, createStream) {
1767
1980
  let captionLineFragment = -1;
1768
1981
  let captionLineHead = "";
1769
1982
  let captionLineOpens = false;
1770
- const gfmLifecycle = { openCodeSpans: [] };
1983
+ const gfmLifecycle = {
1984
+ openCodeSpans: [],
1985
+ quoteWritten: false
1986
+ };
1771
1987
  let openLinkFragment = -1;
1772
1988
  let openLinkHeadingBreak = false;
1773
1989
  let openLinkCaptionBreakRun = 0;
1774
1990
  let openLinkCaptionFlags;
1991
+ let blankCaptionFrames = 0;
1775
1992
  const clean = options.clean;
1776
1993
  const cleanRules = typeof clean === "object" && clean !== null ? clean : void 0;
1777
1994
  const cleanEmptyLinkText = clean === true || cleanRules?.emptyLinkText === true;
@@ -1860,32 +2077,46 @@ function createMarkdownProcessor(options = {}, createStream) {
1860
2077
  let changed = false;
1861
2078
  if (captionBoundary === 1 || captionBoundary === 3) changed = appendCaptionSpacing(captionBoundarySpacing, includeBoundaryListIndent);
1862
2079
  const frames = captionFrames;
1863
- if (frames) for (let index = 0; index < captionFrameCount; index++) {
1864
- const offset = index * CAPTION_FRAME_SIZE;
1865
- let flags = frames[offset + 2];
1866
- if (flags & CAPTION_OPEN) continue;
1867
- const anchor = frames[offset + 3];
1868
- const beforeFragment = anchor === CAPTION_NO_ANCHOR ? state.buffer.length : anchor;
1869
- let opening = flags & CAPTION_INTERNAL_BREAK ? "" : captionSpacingPrefix(frames[offset], true, beforeFragment);
1870
- flags |= CAPTION_OPEN;
1871
- if (!explicitTop || index !== captionFrameCount - 1) opening += "*";
1872
- if (opening) {
1873
- if (beforeFragment === state.buffer.length) {
1874
- state.buffer.push(opening);
1875
- state.lastContentCache = opening;
1876
- } else {
1877
- const anchored = state.buffer[beforeFragment];
1878
- const replacement = opening + anchored;
1879
- state.outputPositions?.replace(beforeFragment, beforeFragment + 1, [replacement], (offset) => offset + opening.length);
1880
- state.buffer[beforeFragment] = replacement;
1881
- noteBufferRewrite(state);
1882
- const codeSpan = gfmLifecycle.openCodeSpans.at(-1);
1883
- if (codeSpan?.fragment === beforeFragment) codeSpan.prefix = opening + codeSpan.prefix;
1884
- if (state.lastContentCache === anchored) state.lastContentCache = replacement;
2080
+ if (frames) {
2081
+ let insertedFragment = -1;
2082
+ let insertedLength = 0;
2083
+ for (let index = 0; index < captionFrameCount; index++) {
2084
+ const offset = index * CAPTION_FRAME_SIZE;
2085
+ let flags = frames[offset + 2];
2086
+ if (flags & CAPTION_OPEN) continue;
2087
+ const anchor = frames[offset + 3];
2088
+ const beforeFragment = anchor === CAPTION_NO_ANCHOR ? state.buffer.length : anchor;
2089
+ let opening = flags & CAPTION_INTERNAL_BREAK && !(flags & CAPTION_BLOCK_PREFIX) ? "" : captionSpacingPrefix(frames[offset], true, beforeFragment);
2090
+ if (state.pendingInlineWhitespace) {
2091
+ if (!opening) {
2092
+ const last = lastOutputChar(state.buffer, beforeFragment);
2093
+ if (last !== -1 && last !== 32 && last !== 10 && last !== 9 && last !== 13) opening = " ";
2094
+ }
2095
+ state.pendingInlineWhitespace = false;
2096
+ }
2097
+ flags |= CAPTION_OPEN;
2098
+ if (!explicitTop || index !== captionFrameCount - 1) opening += "*";
2099
+ if (opening) {
2100
+ if (beforeFragment === state.buffer.length) {
2101
+ state.buffer.push(opening);
2102
+ state.lastContentCache = opening;
2103
+ } else {
2104
+ const anchored = state.buffer[beforeFragment];
2105
+ const at = insertedFragment === beforeFragment ? insertedLength : 0;
2106
+ const replacement = anchored.slice(0, at) + opening + anchored.slice(at);
2107
+ state.outputPositions?.replace(beforeFragment, beforeFragment + 1, [replacement], (offset) => offset >= at ? offset + opening.length : offset);
2108
+ state.buffer[beforeFragment] = replacement;
2109
+ insertedFragment = beforeFragment;
2110
+ insertedLength = at + opening.length;
2111
+ noteBufferRewrite(state);
2112
+ const codeSpan = gfmLifecycle.openCodeSpans.at(-1);
2113
+ if (codeSpan?.fragment === beforeFragment) codeSpan.prefix = codeSpan.prefix.slice(0, at) + opening + codeSpan.prefix.slice(at);
2114
+ if (state.lastContentCache === anchored) state.lastContentCache = replacement;
2115
+ }
2116
+ changed = true;
1885
2117
  }
1886
- changed = true;
2118
+ frames[offset + 2] = flags;
1887
2119
  }
1888
- frames[offset + 2] = flags;
1889
2120
  }
1890
2121
  return changed;
1891
2122
  }
@@ -1913,15 +2144,31 @@ function createMarkdownProcessor(options = {}, createStream) {
1913
2144
  quoteTentativeSpacing = void 0;
1914
2145
  } else if (captionBoundary < 4) captionBoundary = 2;
1915
2146
  }
2147
+ function settleBlankCaption() {
2148
+ const frames = captionFrames;
2149
+ const end = Math.min(blankCaptionFrames, captionFrameCount);
2150
+ for (let index = 0; index < end; index++) if ((frames[index * CAPTION_FRAME_SIZE + 2] & CAPTION_OPEN) === 0) {
2151
+ state.headingBreakPending = false;
2152
+ flushCaptionBreakRun();
2153
+ prepareCaptionOutput(false);
2154
+ commitCaptionFrames();
2155
+ return;
2156
+ }
2157
+ if (openLinkFragment < 0 && openMarkerCount === 0) commitCaptionFrames();
2158
+ }
1916
2159
  function anchorPendingCaptions(fragment) {
1917
2160
  const frames = captionFrames;
1918
2161
  if (!frames) return;
1919
2162
  for (let index = 0; index < captionFrameCount; index++) {
1920
2163
  const offset = index * CAPTION_FRAME_SIZE;
1921
- if ((frames[offset + 2] & CAPTION_OPEN) === 0 && frames[offset + 3] === CAPTION_NO_ANCHOR) frames[offset + 3] = fragment;
2164
+ if ((frames[offset + 2] & CAPTION_OPEN) === 0 && frames[offset + 3] === CAPTION_NO_ANCHOR) {
2165
+ frames[offset + 3] = fragment;
2166
+ if (!(frames[offset + 2] & CAPTION_INTERNAL_BREAK)) frames[offset + 2] |= CAPTION_BLOCK_PREFIX;
2167
+ }
1922
2168
  }
1923
2169
  }
1924
2170
  function commitCaptionFrames() {
2171
+ blankCaptionFrames = 0;
1925
2172
  const frames = captionFrames;
1926
2173
  if (!frames) return;
1927
2174
  for (let index = 0; index < captionFrameCount; index++) {
@@ -1929,7 +2176,7 @@ function createMarkdownProcessor(options = {}, createStream) {
1929
2176
  if (frames[offset + 2] & CAPTION_OPEN) frames[offset + 3] = CAPTION_NO_ANCHOR;
1930
2177
  }
1931
2178
  }
1932
- function flushCaptionBreakRun() {
2179
+ function flushCaptionBreakRun(beforeText = false) {
1933
2180
  if (captionBreakRun === 0) return false;
1934
2181
  let index = state.buffer.length;
1935
2182
  while (index !== 0) {
@@ -1948,8 +2195,36 @@ function createMarkdownProcessor(options = {}, createStream) {
1948
2195
  state.buffer.length = index;
1949
2196
  noteBufferRewrite(state);
1950
2197
  }
1951
- const output = ` \n${state.listIndent}`.repeat(captionBreakRun);
2198
+ const output = (gfmLifecycle.openCodeSpans.length ? "\n" : ` \n${state.listIndent}`).repeat(captionBreakRun);
1952
2199
  captionBreakRun = 0;
2200
+ if (beforeText && !gfmLifecycle.openCodeSpans.length && openMarkerCount && (openMarkers[openMarkerCount - 1] & MARKER_TYPE_MASK) <= 4) for (let frame = 0; frame < captionFrameCount; frame++) {
2201
+ const offset = frame * CAPTION_FRAME_SIZE;
2202
+ const anchor = captionFrames[offset + 3];
2203
+ if (captionFrames[offset + 2] & CAPTION_OPEN || anchor === CAPTION_NO_ANCHOR) continue;
2204
+ let onlyOpeners = true;
2205
+ for (let index = anchor; index < state.buffer.length && onlyOpeners; index++) {
2206
+ const fragment = state.buffer[index];
2207
+ for (let at = 0; at < fragment.length; at++) {
2208
+ const code = fragment.charCodeAt(at);
2209
+ if (code !== 42 && code !== 126) {
2210
+ onlyOpeners = false;
2211
+ break;
2212
+ }
2213
+ }
2214
+ }
2215
+ if (onlyOpeners) {
2216
+ prepareCaptionOutput(false);
2217
+ const fragment = state.buffer[anchor];
2218
+ let at = fragment.length;
2219
+ while (at > 0 && (fragment.charCodeAt(at - 1) === 42 || fragment.charCodeAt(at - 1) === 126)) at--;
2220
+ const replacement = fragment.slice(0, at) + output + fragment.slice(at);
2221
+ state.outputPositions?.replace(anchor, anchor + 1, [replacement], (position) => position >= at ? position + output.length : position);
2222
+ state.buffer[anchor] = replacement;
2223
+ state.lastContentCache = state.buffer.at(-1);
2224
+ noteBufferRewrite(state);
2225
+ return true;
2226
+ }
2227
+ }
1953
2228
  state.buffer.push(output);
1954
2229
  state.lastContentCache = output;
1955
2230
  return true;
@@ -1975,6 +2250,75 @@ function createMarkdownProcessor(options = {}, createStream) {
1975
2250
  restoreQuoteParentBoundary();
1976
2251
  } else if (captionBreakRun !== 0 && captionFrameCount === 0 && captionBoundary < 3) captionBoundary = 2;
1977
2252
  }
2253
+ function hoistOpenerSpace() {
2254
+ const buffer = state.buffer;
2255
+ const depthMap = state.depthMap;
2256
+ const index = openMarkerCount - 1;
2257
+ let fragment = openMarkers[index] >> 3;
2258
+ const reopened = (openMarkers[index] & MARKER_TYPE_MASK) === MARKER_TYPE_MASK;
2259
+ if ((reopened ? fragment !== buffer.length : fragment !== buffer.length - 1 || (openMarkers[index] & MARKER_TYPE_MASK) > 4) || depthMap[23] || depthMap[34] || depthMap[46] || depthMap[82] || depthMap[92]) return false;
2260
+ if (reopened) {
2261
+ const before = lastOutputChar(buffer);
2262
+ if (before !== -1 && before !== 32 && before !== 10 && before !== 9) {
2263
+ buffer.push(" ");
2264
+ openMarkers[index] = buffer.length << 3 | MARKER_TYPE_MASK;
2265
+ openMarkerFollowsSpace[index] = true;
2266
+ }
2267
+ return true;
2268
+ }
2269
+ if (state.blockquotes.at(-1)?.fragment === fragment + 1) return false;
2270
+ fragment -= openMarkerRunLength[index] - 1;
2271
+ if (literalOpeners.length) {
2272
+ const link = openLinkFragment < -1 ? -openLinkFragment - 2 : openLinkFragment;
2273
+ let at = fragment;
2274
+ while (at > 0 && !buffer[at - 1]) at--;
2275
+ if (followsLiteralOpener(link >= 0 && link === at - 1 ? link : fragment)) return true;
2276
+ }
2277
+ if (buffer[fragment].charCodeAt(0) === 32) return true;
2278
+ let previous = fragment;
2279
+ let before = -1;
2280
+ while (before === -1 && --previous >= 0) {
2281
+ const value = buffer[previous];
2282
+ if (value) before = value.charCodeAt(value.length - 1);
2283
+ }
2284
+ if (previous >= 0 && previous === (openLinkFragment < -1 ? -openLinkFragment - 2 : openLinkFragment)) before = lastOutputChar(buffer, previous);
2285
+ if (before !== -1 && before !== 32 && before !== 10 && before !== 9) {
2286
+ const opener = ` ${buffer[fragment]}`;
2287
+ state.outputPositions?.replace(fragment, fragment + 1, [opener], (offset) => offset > 0 ? offset + 1 : offset);
2288
+ buffer[fragment] = opener;
2289
+ noteBufferRewrite(state);
2290
+ }
2291
+ return true;
2292
+ }
2293
+ function placeLeadingBlank() {
2294
+ const depthMap = state.depthMap;
2295
+ if (depthMap[23] || depthMap[34] || depthMap[46] || depthMap[82] || depthMap[92] || openMarkerCount && hoistOpenerSpace()) return;
2296
+ const end = state.buffer.length - 1;
2297
+ const link = openLinkFragment < -1 ? -openLinkFragment - 2 : openLinkFragment;
2298
+ const opener = openMarkerCount ? openMarkers[openMarkerCount - 1] : MARKER_TYPE_MASK;
2299
+ if (!((opener & MARKER_TYPE_MASK) !== MARKER_TYPE_MASK && opener >> 3 === end) && !(link >= 0 && link === end) && !(literalOpeners.length && followsLiteralOpener(end + 1))) {
2300
+ state.pendingInlineWhitespace = true;
2301
+ state.pendingLeadingBlank = true;
2302
+ state.pendingPairSeparator = false;
2303
+ state.pendingDecodedBlank = false;
2304
+ }
2305
+ }
2306
+ function followsLiteralOpener(fragment) {
2307
+ const end = literalOpeners[literalOpeners.length - 1];
2308
+ if (end < 0 || end > fragment || fragment > state.buffer.length) return false;
2309
+ for (let index = end; index < fragment; index++) if (state.buffer[index]) return false;
2310
+ return true;
2311
+ }
2312
+ function holdLiteralOpener(held) {
2313
+ const end = literalOpeners[literalOpeners.length - 1];
2314
+ if (end < 0 || end >= held) return held;
2315
+ const limit = Math.min(held, state.buffer.length);
2316
+ for (let index = end; index < limit; index++) if (state.buffer[index]) {
2317
+ for (let pair = 1; pair < literalOpeners.length; pair += 2) literalOpeners[pair] = -1;
2318
+ return held;
2319
+ }
2320
+ return end;
2321
+ }
1978
2322
  function captionExitOpensBlockLine(buff) {
1979
2323
  let fragment = buff.length - 1;
1980
2324
  let cursor = buff[fragment].length;
@@ -2042,27 +2386,50 @@ function createMarkdownProcessor(options = {}, createStream) {
2042
2386
  start++;
2043
2387
  }
2044
2388
  if (start !== 0) textNode.value = value.slice(start);
2389
+ lastChar = "\n";
2045
2390
  }
2046
2391
  }
2047
2392
  if (textNode.trimsAtLineStart) trimTextAtLineStart(textNode, state.buffer);
2393
+ if (textNode.sourceLead && openLinkFragment !== -1 && (openLinkFragment < -1 ? -openLinkFragment - 2 : openLinkFragment) === state.buffer.length - 1) dropSourceLead(textNode);
2048
2394
  if (textNode.value) {
2049
2395
  if (textNode.excludedFromMarkdown) return;
2050
2396
  const ownsCaptionSpace = captionNeedsPreparation();
2051
2397
  if (ownsCaptionSpace) {
2052
2398
  if (captionBoundary !== 5 && textNode.value.charCodeAt(0) === 32) textNode.value = textNode.value.slice(1);
2053
2399
  if (!hasNonWhitespace(textNode.value)) return;
2054
- flushCaptionBreakRun();
2400
+ flushCaptionBreakRun(true);
2055
2401
  prepareCaptionOutput(false);
2056
- commitCaptionFrames();
2402
+ if (openLinkFragment >= 0 && !hasLabelText(textNode.value)) blankCaptionFrames = captionFrameCount;
2403
+ else commitCaptionFrames();
2057
2404
  captionBoundary = 0;
2058
2405
  }
2059
2406
  if (state.pendingInlineWhitespace) {
2060
- if (!textNode.value.trim()) return;
2061
- if (lastChar && !" \n \r".includes(lastChar) && !" \n \r".includes(textNode.value[0] || "")) textNode.value = ` ${textNode.value}`;
2407
+ const blank = !hasNonWhitespace(textNode.value);
2408
+ if (blank) {
2409
+ if (!state.pendingLeadingBlank || state.pendingPairSeparator) return;
2410
+ if (textNode.value !== " " || !textNode.containsWhitespace) {
2411
+ state.pendingDecodedBlank = true;
2412
+ return;
2413
+ }
2414
+ }
2415
+ if (!blank && lastChar && !" \n \r".includes(lastChar) && !" \n \r".includes(textNode.value[0] || "") && !(literalOpeners.length && followsLiteralOpener(state.buffer.length)) && !(openMarkerCount && hoistOpenerSpace())) {
2416
+ if (textNode.generatedMarkdown?.prefix || state.pendingLeadingBlank) {
2417
+ state.buffer.push(" ");
2418
+ state.lastContentCache = " ";
2419
+ state.separatorPending = true;
2420
+ lastChar = " ";
2421
+ } else textNode.value = ` ${textNode.value}`;
2422
+ }
2062
2423
  state.pendingInlineWhitespace = false;
2063
2424
  }
2064
2425
  if (textNode.value === " " && (lastChar === " " || lastChar === "\n" || lastChar === " " || lastChar === "\r")) return;
2065
2426
  if (!ownsCaptionSpace && shouldAddSpacingBeforeText(lastChar, lastNode, textNode)) textNode.value = ` ${textNode.value}`;
2427
+ if (openMarkerCount && textNode.value.charCodeAt(0) === 32 && hoistOpenerSpace()) {
2428
+ let start = 1;
2429
+ while (textNode.value.charCodeAt(start) === 32) start++;
2430
+ textNode.value = textNode.value.slice(start);
2431
+ if (!textNode.value) return;
2432
+ }
2066
2433
  if ((state.depthMap[34] || 0) > 0 && (state.depthMap[25] || 0) > 0) {
2067
2434
  const indent = state.listIndent;
2068
2435
  let value = textNode.value.replace(/\n(?!\n|$)/g, `\n${indent}`);
@@ -2080,14 +2447,24 @@ function createMarkdownProcessor(options = {}, createStream) {
2080
2447
  }
2081
2448
  const last = lastChar.charCodeAt(0);
2082
2449
  const first = last >= 48 && last <= 57 ? textNode.value.charCodeAt(0) : 0;
2083
- const orderedDigits = (first >= 48 && first <= 57 || first === 46 || first === 41) && !textNode.generatedMarkdown?.prefix ? lineStartDigits(state.buffer, state.listIndent, textNode.value) : 0;
2084
- if (!state.depthMap[34] && !state.depthMap[23] && (!insideRawHtmlBlock || rawHtmlMarkdown) && (orderedDigits > 0 || mayNeedGfmTextEscape(textNode.value, state.buffer, state.depthMap, state.listIndent.length)) && (!insideRawHtmlBlock || !lineOpensRawHtmlBlock(state.buffer, bufferScan))) textNode.value = escapeGfmText(textNode.value, state.buffer, state.depthMap, state.listIndent.length, orderedDigits);
2450
+ const orderedDigits = (first >= 48 && first <= 57 || first === 46 || first === 41) && !textNode.generatedMarkdown?.prefix ? lineStartDigits(state, textNode.value) : 0;
2451
+ const outputStart = textNode.generatedMarkdown?.prefix ? -1 : outputStartMarker(textNode.value, state);
2452
+ if (!state.depthMap[34] && !state.depthMap[23] && state.codeText?.kind !== "span" && (!insideRawHtmlBlock || rawHtmlMarkdown) && (orderedDigits > 0 || outputStart !== -1 || mayNeedGfmTextEscape(textNode.value, state.buffer, state.depthMap, state.listIndent.length)) && (!insideRawHtmlBlock || !lineOpensRawHtmlBlock(state.buffer, bufferScan))) textNode.value = escapeGfmText(textNode.value, state.buffer, state.depthMap, state.listIndent.length, orderedDigits, outputStart);
2085
2453
  if (textNode.generatedMarkdown && textNode.value) {
2086
2454
  noteGeneratedFences(state, textNode.generatedMarkdown);
2087
2455
  textNode.value = `${textNode.generatedMarkdown.prefix}${textNode.value}${textNode.generatedMarkdown.suffix}`;
2088
2456
  }
2457
+ if (pendingCloser && textNode.value) {
2458
+ const code = textNode.value.charCodeAt(0);
2459
+ const written = ownsCaptionSpace ? lastOutputChar(state.buffer) : -1;
2460
+ if ((written === -1 ? lastChar.charCodeAt(0) : written) === pendingCloser && isAsciiAlphanumeric(code)) textNode.value = `&#${code};${textNode.value.slice(1)}`;
2461
+ pendingCloser = 0;
2462
+ }
2463
+ state.cacheFollowsSeparator = state.separatorPending === true;
2464
+ state.separatorPending = false;
2465
+ state.droppedPairAt = void 0;
2089
2466
  const wrapWidth = state.options?.wrapWidth;
2090
- if (wrapWidth && canWrapHere(state.depthMap)) {
2467
+ if (wrapWidth && state.codeText?.kind !== "span" && canWrapHere(state.depthMap)) {
2091
2468
  const column = currentColumn(state.buffer, Math.max(wrapWidth, state.listIndent.length + 1));
2092
2469
  const wrapped = wrapText(textNode.value, column, wrapWidth, state.listIndent);
2093
2470
  state.buffer.push(wrapped);
@@ -2098,15 +2475,96 @@ function createMarkdownProcessor(options = {}, createStream) {
2098
2475
  }
2099
2476
  if (openMarkerCount || openLinkFragment >= 0) {
2100
2477
  const hasVisibleText = textNode.containsWhitespace === false || hasNonWhitespace(textNode.value);
2101
- if (openLinkFragment >= 0 && hasVisibleText) openLinkFragment = -openLinkFragment - 2;
2102
- if (openMarkerCount && hasVisibleText) {
2478
+ if (openLinkFragment >= 0 && hasVisibleText && hasLabelText(textNode.value)) openLinkFragment = -openLinkFragment - 2;
2479
+ if (openMarkerCount && hasNonWhitespace(textNode.value)) {
2103
2480
  const first = textNode.value.charCodeAt(0);
2104
- if (first !== 11 && !isAsciiWhitespace(first) || (openMarkers[openMarkerCount - 1] & MARKER_TYPE_MASK) > 4 || !isVerticalTabRun(textNode.value)) openMarkerCount = 0;
2105
- commitCaptionFrames();
2481
+ if (first !== 11 && !isAsciiWhitespace(first) || (openMarkers[openMarkerCount - 1] + 1 & MARKER_TYPE_MASK) > 5 || !isVerticalTabRun(textNode.value)) openMarkerCount = 0;
2482
+ if (openLinkFragment < 0) commitCaptionFrames();
2483
+ } else if (openMarkerCount && textNode.containsWhitespace !== true && textNode.value && (openMarkers[openMarkerCount - 1] + 1 & MARKER_TYPE_MASK) > 5) {
2484
+ if (silentMarkerCount >= openMarkerCount) openMarkerCount = 0;
2485
+ else blankMarkerCount = openMarkerCount;
2106
2486
  }
2107
2487
  }
2108
2488
  }
2109
2489
  state.lastTextNode = textNode;
2490
+ breakAfterLastText = false;
2491
+ }
2492
+ function nextListMarker(list, ordered) {
2493
+ const sibling = siblingListMarker;
2494
+ if (sibling !== 0 && siblingListDepth === state.depthMap[25] && siblingListQuotes === state.blockquotes.length && (sibling === 46 || sibling === 41) === ordered) {
2495
+ if (sibling === 45 && listItemsOpenLines(list)) return 42;
2496
+ if (sibling === 46) return 41;
2497
+ }
2498
+ return ordered ? 46 : 45;
2499
+ }
2500
+ function listItemsOpenLines(list) {
2501
+ if (captionFrameCount !== 0) return false;
2502
+ let ancestor = list;
2503
+ while (ancestor && (!ancestor.tagHandler?.collapsesInnerWhiteSpace || ancestor.tagId === 37)) ancestor = ancestor.parent;
2504
+ if (!ancestor) return true;
2505
+ const last = lastOutputChar(state.buffer);
2506
+ return last === 10 || last === -1;
2507
+ }
2508
+ function reconcileListPath(event) {
2509
+ const node = event.node;
2510
+ const path = eventPath;
2511
+ let ancestor = node.parent;
2512
+ let top = path.at(-1);
2513
+ while (ancestor !== top) if (top && (!ancestor || top.depth >= ancestor.depth)) {
2514
+ path.pop();
2515
+ if (top !== node && top === listMarkerOwners.at(-1)) {
2516
+ listMarkerOwners.pop();
2517
+ const depth = listMarkerDepths.pop();
2518
+ const marker = state.listMarkers.pop();
2519
+ if (marker !== void 0 && marker > 255) {
2520
+ siblingListMarker = marker >> 8;
2521
+ siblingListDepth = depth;
2522
+ siblingListQuotes = state.blockquotes.length;
2523
+ }
2524
+ }
2525
+ top = path.at(-1);
2526
+ } else if (ancestor) {
2527
+ missingAncestors.push(ancestor);
2528
+ ancestor = ancestor.parent;
2529
+ } else break;
2530
+ while (missingAncestors.length) path.push(missingAncestors.pop());
2531
+ if (node.type === 1 && event.type === 0) path.push(node);
2532
+ }
2533
+ function closeListMarker(element, exitOutput) {
2534
+ if (listMarkerOwners) {
2535
+ if (listMarkerOwners.at(-1) !== element) return;
2536
+ listMarkerOwners.pop();
2537
+ listMarkerDepths.pop();
2538
+ }
2539
+ let marker = state.listMarkers.pop();
2540
+ if (exitOutput && marker !== void 0) {
2541
+ const written = exitListMarker(exitOutput, marker > 255);
2542
+ if (written !== void 0) {
2543
+ marker = marker & 255 | written << 8;
2544
+ siblingListMarker = 0;
2545
+ }
2546
+ }
2547
+ if (marker !== void 0 && marker > 255) {
2548
+ siblingListMarker = marker >> 8;
2549
+ siblingListDepth = state.depthMap[25];
2550
+ siblingListQuotes = state.blockquotes.length;
2551
+ listClosedInGap = true;
2552
+ }
2553
+ }
2554
+ function processGapEvent(event) {
2555
+ const fragments = state.buffer.length;
2556
+ inGapEvent = true;
2557
+ listClosedInGap = false;
2558
+ processEvent(event);
2559
+ inGapEvent = false;
2560
+ if (listClosedInGap) return;
2561
+ const buffer = state.buffer;
2562
+ if (openMarkerCount === 0 && openLinkFragment < 0 && gfmLifecycle.openCodeSpans.length === 0) {
2563
+ for (let index = Math.min(fragments, buffer.length); index < buffer.length; index++) if (hasNonWhitespace(buffer[index])) {
2564
+ siblingListMarker = 0;
2565
+ break;
2566
+ }
2567
+ }
2110
2568
  }
2111
2569
  function settleHeadingBreakAtEvent(node, eventType) {
2112
2570
  if (node.type === 2 && eventType === 0) {
@@ -2127,10 +2585,25 @@ function createMarkdownProcessor(options = {}, createStream) {
2127
2585
  return false;
2128
2586
  }
2129
2587
  function processEvent(event) {
2588
+ if (eventPath && !inGapEvent) reconcileListPath(event);
2589
+ if (siblingListMarker !== 0 && !inGapEvent) return processGapEvent(event);
2130
2590
  const { type: eventType, node } = event;
2131
- if (pendingRawHtmlBoundary && eventType === 0 && node.type === 1 && node.tagId === 34 && !node.tagHandler?.literalEnter) pendingRawHtmlBoundary = false;
2132
2591
  state.depth = node.depth;
2592
+ if (blankCaptionFrames !== 0) settleBlankCaption();
2133
2593
  if (node.type === 1 ? node.excludedFromMarkdown : node.parent?.excludedFromMarkdown) return;
2594
+ const codeCaption = state.codeText !== void 0 && node.type === 1 && node.tagId === 106;
2595
+ if (codeCaption) {
2596
+ const element = node;
2597
+ const literalOutput = eventType === 0 ? element.tagHandler?.literalEnter : element.tagHandler?.literalExit;
2598
+ if (!element.pluginOutput?.length && !literalOutput) return;
2599
+ }
2600
+ if (closedMarkerCount) heldClosedMarker();
2601
+ if (pendingRawHtmlBoundary && eventType === 0 && node.type === 1 && node.tagId === 34 && !node.tagHandler?.literalEnter) pendingRawHtmlBoundary = false;
2602
+ const blank = node.type === 2 && node.leadingBlank ? node.value : void 0;
2603
+ if (blank !== void 0 && !hasNonWhitespace(blank)) {
2604
+ if (blank && !node.excludedFromMarkdown && !state.pendingInlineWhitespace && !captionNeedsPreparation()) placeLeadingBlank();
2605
+ return;
2606
+ }
2134
2607
  if (state.tableRowOpenerPending && state.depthMap[28] <= 1) {
2135
2608
  const tagId = node.type === 1 ? node.tagId : void 0;
2136
2609
  const cellEvent = tagId === 32 || tagId === 31;
@@ -2157,6 +2630,7 @@ function createMarkdownProcessor(options = {}, createStream) {
2157
2630
  const lastNode = state.lastNode;
2158
2631
  state.lastNode = event.node;
2159
2632
  const buff = state.buffer;
2633
+ const closerBefore = pendingCloser;
2160
2634
  if (state.preFencePending) {
2161
2635
  const preparePendingPreFence = () => {
2162
2636
  if (captionNeedsPreparation()) {
@@ -2194,9 +2668,24 @@ function createMarkdownProcessor(options = {}, createStream) {
2194
2668
  const element = node;
2195
2669
  const handler = node.tagHandler;
2196
2670
  const tagId = element.tagId;
2671
+ if (eventType === 1 && state.codeText?.owner === element) state.codeText = void 0;
2672
+ if ((tagId === 24 || tagId === 33) && eventType === 0) {
2673
+ state.listMarkers.push(nextListMarker(element, tagId === 33));
2674
+ listMarkerOwners?.push(element);
2675
+ listMarkerDepths?.push(state.depthMap[25]);
2676
+ }
2677
+ if (eventType === 1) while (literalOpeners.length && literalOpeners[literalOpeners.length - 2] >= element.depth) literalOpeners.length -= 2;
2197
2678
  const insideRawHtmlRegion = isInsideRawHtmlBlock(state.depthMap);
2679
+ if (captionBoundary === 3 && tagId === 22 && eventType === 0 && onItemMarkerLine(state)) captionBoundary = 0;
2680
+ let emptyQuoteItemExit = false;
2198
2681
  if (captionBoundary >= 3 && tagId === 25) {
2199
2682
  restoreQuoteParentBoundary();
2683
+ if (eventType === 1 && onItemMarkerLine(state)) {
2684
+ buff.push("\n");
2685
+ state.lastContentCache = "\n";
2686
+ lastChar = "\n";
2687
+ emptyQuoteItemExit = true;
2688
+ }
2200
2689
  captionBoundary = 0;
2201
2690
  }
2202
2691
  if (eventType === 0 && state.depthMap[25] && !insideRawHtmlRegion && (tagId === 35 || tagId === 36 || tagId === 34 || tagId === 28 || tagId === 13 && !state.listIndent) && !handler?.literalEnter && endsAtHardBreak(buff)) {
@@ -2230,13 +2719,16 @@ function createMarkdownProcessor(options = {}, createStream) {
2230
2719
  lastChar = lastBuffEntry?.charAt(lastBuffEntry.length - 1) || "";
2231
2720
  }
2232
2721
  }
2233
- if (state.pendingInlineWhitespace) {
2722
+ if (pendingRawHtmlBoundary && breakOutput) finishRawHtmlBlock(breakOutput, true);
2723
+ const captionHasAnchor = captionFrameCount !== 0 && (captionFrames[(captionFrameCount - 1) * CAPTION_FRAME_SIZE + 2] & CAPTION_OPEN) === 0 && captionFrames[(captionFrameCount - 1) * CAPTION_FRAME_SIZE + 3] !== CAPTION_NO_ANCHOR;
2724
+ if (state.pendingInlineWhitespace && !captionHasAnchor) {
2234
2725
  state.pendingInlineWhitespace = false;
2235
2726
  if (breakOutput?.charCodeAt(0) === 60) {
2236
2727
  const last = lastOutputChar(buff);
2237
2728
  if (last !== -1 && last !== 32 && last !== 10 && last !== 9 && last !== 13) {
2238
2729
  buff.push(" ");
2239
2730
  state.lastContentCache = " ";
2731
+ state.separatorPending = true;
2240
2732
  }
2241
2733
  }
2242
2734
  }
@@ -2260,7 +2752,7 @@ function createMarkdownProcessor(options = {}, createStream) {
2260
2752
  parent = parent.parent;
2261
2753
  }
2262
2754
  if (!parentInPre || element.parent?.tagId === 34) {
2263
- if (breakOutput?.endsWith("\n") && !parentInPre) {
2755
+ if (breakOutput?.endsWith("\n") && !parentInPre && !breakAfterLastText) {
2264
2756
  const trimmed = trimAsciiWhitespaceEnd(lastFragment);
2265
2757
  if (trimmed.length !== lastFragment.length && buff.at(-1) === lastFragment) {
2266
2758
  state.outputPositions?.replace(buff.length - 1, buff.length, [trimmed]);
@@ -2272,7 +2764,7 @@ function createMarkdownProcessor(options = {}, createStream) {
2272
2764
  }
2273
2765
  if (!inPre && breakOutput?.endsWith("\n") && lastOutputChar(buff) === 32) trimTrailingSpaces(buff);
2274
2766
  const pendingCaption = captionFrameCount !== 0 && (captionFrames[(captionFrameCount - 1) * CAPTION_FRAME_SIZE + 2] & CAPTION_OPEN) === 0;
2275
- if (breakOutput && pendingCaption && !hasNonWhitespace(breakOutput)) {
2767
+ if (breakOutput && breakOutput !== " " && pendingCaption && !hasNonWhitespace(breakOutput)) {
2276
2768
  for (let index = 0; index < captionFrameCount; index++) {
2277
2769
  const offset = index * CAPTION_FRAME_SIZE + 2;
2278
2770
  if ((captionFrames[offset] & CAPTION_OPEN) === 0) captionFrames[offset] = captionFrames[offset] | CAPTION_INTERNAL_BREAK;
@@ -2287,10 +2779,11 @@ function createMarkdownProcessor(options = {}, createStream) {
2287
2779
  return;
2288
2780
  }
2289
2781
  let output;
2290
- const hasPluginOutput = !!element.pluginOutput?.length;
2291
- if (hasPluginOutput) {
2292
- notePluginFences(state, element.pluginOutput);
2293
- output = element.pluginOutput;
2782
+ const pluginOutput = element.pluginOutput;
2783
+ const hasPluginOutput = !!pluginOutput?.length;
2784
+ if (pluginOutput?.length) {
2785
+ notePluginFences(state, pluginOutput);
2786
+ output = pluginOutput;
2294
2787
  element.pluginOutput = void 0;
2295
2788
  }
2296
2789
  let captionBufferChanged = false;
@@ -2301,13 +2794,13 @@ function createMarkdownProcessor(options = {}, createStream) {
2301
2794
  let keepsQuoteSeparator = false;
2302
2795
  let gfmAction;
2303
2796
  let handlerOutput;
2304
- const suppressedInPre = (state.depthMap[34] || 0) > 0 && suppressesFormattingInPre(tagId) && !(eventType === 0 ? handler?.literalEnter : handler?.literalExit);
2797
+ const suppressedInPre = !!state.depthMap[34] && suppressesFormattingInPre(tagId) && !(eventType === 0 ? handler?.literalEnter : handler?.literalExit);
2305
2798
  const emptyTableRow = eventType === 1 && tagId === 30 && state.tableRowOpenerPending && !output && !handler?.literalExit;
2306
2799
  if (eventType === 1 && (handler?.literalExit || output) && gfmLifecycle.openCodeSpans.at(-1)?.owner === element) gfmLifecycle.openCodeSpans.pop();
2307
2800
  if (cleanPass && eventType === 1) cleanPass.exit(element);
2308
2801
  if (!output && !suppressedInPre && handler?.[eventFn] && (tagId !== 36 || state.depthMap[25] || handler[eventFn] !== tagHandlers[36].enter)) {
2309
2802
  if (openLinkFragment >= 0 && eventType === 1 && tagId === 26) {
2310
- if (dropEmptyLinkText(buff, openLinkFragment, state.outputPositions)) {
2803
+ if (dropBlankLink(buff, openLinkFragment, state.outputPositions)) {
2311
2804
  noteBufferRewrite(state);
2312
2805
  if (openLinkHeadingBreak) {
2313
2806
  state.outputPositions?.replace(openLinkFragment - 1, openLinkFragment, []);
@@ -2345,6 +2838,9 @@ function createMarkdownProcessor(options = {}, createStream) {
2345
2838
  const cache = state.lastContentCache;
2346
2839
  if (cache) state.lastContentCache = cache.slice(0, Math.max(0, cache.length - removed));
2347
2840
  state.pendingInlineWhitespace = true;
2841
+ state.pendingLeadingBlank = false;
2842
+ state.pendingPairSeparator = false;
2843
+ state.pendingDecodedBlank = false;
2348
2844
  lastBuffEntry = buff.at(-1);
2349
2845
  lastChar = String.fromCharCode(lastOutputChar(buff));
2350
2846
  }
@@ -2366,11 +2862,14 @@ function createMarkdownProcessor(options = {}, createStream) {
2366
2862
  else {
2367
2863
  gfmAction = res;
2368
2864
  if (res._tag === "BlockquoteExit") closesQuote = state.blockquotes.length > 0;
2369
- if (res._tag === "CodeSpanEnter" && captionBoundary === 3 && res.output === " `") res.output = "`";
2370
- handlerOutput = consumeGfmAction(res, state, gfmLifecycle);
2865
+ if (res._tag === "CodeSpanEnter" && res.output === " `" && (captionBoundary === 3 || captionFrameCount !== 0 && (captionFrames[(captionFrameCount - 1) * CAPTION_FRAME_SIZE + 2] & CAPTION_OPEN) === 0)) gfmAction = CODE_SPAN_ENTER;
2866
+ handlerOutput = consumeGfmAction(gfmAction, state, gfmLifecycle);
2371
2867
  }
2372
2868
  if (handlerOutput) output = [handlerOutput];
2373
- if (res._tag === "BlockquoteExit") captionBufferChanged = true;
2869
+ if (res._tag === "BlockquoteExit") {
2870
+ captionBufferChanged = true;
2871
+ if (gfmLifecycle.quoteWritten || siblingListQuotes > state.blockquotes.length) siblingListMarker = 0;
2872
+ }
2374
2873
  }
2375
2874
  }
2376
2875
  if (eventType === 0 && (tagId === 32 || tagId === 31) && state.tableRowOpenerPending && state.depthMap[28] <= 1) {
@@ -2380,13 +2879,13 @@ function createMarkdownProcessor(options = {}, createStream) {
2380
2879
  else output = [opener];
2381
2880
  }
2382
2881
  if (emptyTableRow) return;
2383
- if (eventType === 0 && output && handler?.collapsesInnerWhiteSpace && (hasPluginOutput || handler !== tagHandlers[tagId] || !INLINE_MARKER_TYPE[tagId] && tagId !== 26)) element.hasEnterOutput = output.some((fragment) => fragment.length > 0);
2384
- if (captionBreakRun && !(tagId === 106 && eventType === 1) && output) {
2882
+ if (eventType === 0 && output && handler?.collapsesInnerWhiteSpace && (hasPluginOutput || handler.literalEnter)) element.hasEnterOutput = output.some((fragment) => fragment.length > 0);
2883
+ const captionEvent = tagId === 106 && !codeCaption;
2884
+ if (captionBreakRun && !(captionEvent && eventType === 1) && output) {
2385
2885
  flushCaptionBreakRun();
2386
2886
  if (gfmAction?._tag !== "BlockquoteEnter") anchorPendingCaptions(state.buffer.length);
2387
2887
  captionBufferChanged = true;
2388
2888
  }
2389
- const captionEvent = tagId === 106;
2390
2889
  let captionTransition = false;
2391
2890
  if (captionEvent || captionBoundary !== 0 || captionFrameCount !== 0) {
2392
2891
  if (captionBoundary === 3 && eventType === 0 && output?.[0]?.startsWith("\n\n")) captionBoundary = 2;
@@ -2486,21 +2985,77 @@ function createMarkdownProcessor(options = {}, createStream) {
2486
2985
  if (eventType === 1 && openMarkerCount) {
2487
2986
  const markerType = handlerOutput === void 0 ? 0 : INLINE_MARKER_TYPE[tagId];
2488
2987
  if (markerType && (tagId !== 23 || !state.depthMap[34] && !isInsideRawHtmlBlock(state.depthMap)) && !handler?.literalExit) {
2489
- const packed = openMarkers[--openMarkerCount];
2490
- const restoresHeadingBreak = buff[packed >> 3]?.includes("\n");
2491
- const idx = dropEmptyMarker(buff, packed, markerType, state.outputPositions);
2492
- if (idx >= 0) {
2493
- noteBufferRewrite(state);
2494
- if (restoresHeadingBreak) state.headingBreakPending = true;
2495
- resetRetractedCaptionFrames(captionFrames, captionFrameCount, idx);
2496
- state.lastContentCache = idx > 0 ? buff[idx - 1] : void 0;
2497
- restoreQuoteParentBoundary();
2498
- return;
2499
- }
2500
- if (markerType <= 4 && dropVerticalTabMarker(buff, packed, markerType, handlerOutput, state.outputPositions)) {
2501
- noteBufferRewrite(state);
2502
- state.lastContentCache = buff[buff.length - 1];
2503
- return;
2988
+ const slot = --openMarkerCount;
2989
+ const packed = openMarkers[slot];
2990
+ if ((packed & MARKER_TYPE_MASK) === MARKER_TYPE_MASK) {
2991
+ const start = packed >> 3;
2992
+ let index = start;
2993
+ while (index < buff.length && !hasNonWhitespace(buff[index])) index++;
2994
+ if (index === buff.length && (markerType === 1 || markerType === 2 || markerType === 4)) {
2995
+ const owesSeparator = openMarkerFollowsSpace[slot] && buff[start - 1] === " ";
2996
+ const at = owesSeparator ? start - 1 : start;
2997
+ state.outputPositions?.replace(at, buff.length, [handlerOutput]);
2998
+ buff.length = at;
2999
+ buff.push(handlerOutput);
3000
+ noteBufferRewrite(state);
3001
+ state.lastContentCache = handlerOutput;
3002
+ pendingCloser = openMarkerClosers[slot];
3003
+ if (closedMarkerCount && closedMarkers[closedMarkerCount - 1] !== at - 1) closedMarkerCount = 0;
3004
+ closedMarkers[closedMarkerCount++] = at;
3005
+ if (owesSeparator) {
3006
+ state.pendingInlineWhitespace = true;
3007
+ state.pendingLeadingBlank = true;
3008
+ state.pendingPairSeparator = openMarkerSeparatorTrimmed[slot];
3009
+ state.pendingDecodedBlank = false;
3010
+ }
3011
+ return;
3012
+ }
3013
+ } else {
3014
+ const hoisted = markerType <= 4 && buff[packed >> 3]?.charCodeAt(0) === 32;
3015
+ const owned = markerType > 4 && openMarkerFollowsSpace[slot] && buff[packed >> 3]?.charCodeAt(0) === 32;
3016
+ const restoresHeadingBreak = buff[packed >> 3]?.includes("\n");
3017
+ let idx = dropEmptyMarker(buff, packed, markerType, state.outputPositions);
3018
+ if (idx >= 0) {
3019
+ pendingCloser = openMarkerClosers[slot];
3020
+ noteBufferRewrite(state);
3021
+ if (hoisted) {
3022
+ state.pendingInlineWhitespace = true;
3023
+ state.pendingLeadingBlank = true;
3024
+ state.pendingPairSeparator = false;
3025
+ state.pendingDecodedBlank = false;
3026
+ }
3027
+ if (openMarkerFollowsSpace[slot] && (owned || buff[idx - 1] === " ")) {
3028
+ if (!owned) {
3029
+ idx--;
3030
+ state.outputPositions?.replace(idx, buff.length, []);
3031
+ buff.length = idx;
3032
+ }
3033
+ state.pendingInlineWhitespace = true;
3034
+ state.pendingLeadingBlank = true;
3035
+ state.pendingPairSeparator = openMarkerSeparatorTrimmed[slot];
3036
+ state.pendingDecodedBlank = false;
3037
+ }
3038
+ if (restoresHeadingBreak) state.headingBreakPending = true;
3039
+ resetRetractedCaptionFrames(captionFrames, captionFrameCount, idx);
3040
+ state.lastContentCache = idx > 0 ? buff[idx - 1] : void 0;
3041
+ state.droppedPairAt = buff.length;
3042
+ restoreQuoteParentBoundary();
3043
+ if (unmergedSlot === openMarkerCount - 1 && unmergedOpener === buff.length - 1 && openMarkers[unmergedSlot] >> 3 === unmergedOpener && closedMarkerCount && closedMarkers[closedMarkerCount - 1] === unmergedOpener - 1 && buff[unmergedOpener] === buff[unmergedOpener - 1]) {
3044
+ closedMarkerCount--;
3045
+ state.outputPositions?.replace(unmergedOpener - 1, buff.length, []);
3046
+ buff.length = unmergedOpener - 1;
3047
+ noteBufferRewrite(state);
3048
+ state.lastContentCache = buff[buff.length - 1];
3049
+ openMarkers[unmergedSlot] = buff.length << 3 | MARKER_TYPE_MASK;
3050
+ unmergedSlot = -1;
3051
+ }
3052
+ return;
3053
+ }
3054
+ if (markerType <= 4 && dropVerticalTabMarker(buff, packed, markerType, handlerOutput, state.outputPositions)) {
3055
+ noteBufferRewrite(state);
3056
+ state.lastContentCache = buff[buff.length - 1];
3057
+ return;
3058
+ }
2504
3059
  }
2505
3060
  openMarkerCount = 0;
2506
3061
  }
@@ -2525,9 +3080,29 @@ function createMarkdownProcessor(options = {}, createStream) {
2525
3080
  rawHtmlNeedsBoundary = false;
2526
3081
  } else if (rawBlockTag && eventType === 0) pendingRawHtmlBoundary = false;
2527
3082
  if (eventType === 1 && (tagId === 25 || tagId === 22)) pendingRawHtmlBoundary = false;
3083
+ const voidSpacingAtEnter = isOpenVoidTag(handler, tagId) && !handler?.literalExitHasOutput && !(eventType === 1 && pluginOutput?.some(hasNonWhitespace));
3084
+ let openVoidLines = 0;
3085
+ if (eventType === 0 && voidSpacingAtEnter) {
3086
+ const exitLines = suppressedInPre ? 0 : calculateNewLineConfig(element, state, 1)[1];
3087
+ let enterNewlines = 0;
3088
+ let visible = false;
3089
+ for (let i = (output?.length || 0) - 1; i >= 0 && !visible; i--) {
3090
+ const fragment = output[i];
3091
+ let end = fragment.length;
3092
+ while (end > 0 && fragment.charCodeAt(end - 1) === 10) {
3093
+ end--;
3094
+ enterNewlines++;
3095
+ }
3096
+ visible = end > 0;
3097
+ }
3098
+ const cappedLines = tagId === 13 && state.blockquotes.length > 0 ? Math.min(1, exitLines) : exitLines;
3099
+ const owedLines = Math.max(0, cappedLines - enterNewlines);
3100
+ if (!visible) openVoidLines = owedLines;
3101
+ else if (owedLines > 0) output = [...output, "\n".repeat(owedLines)];
3102
+ }
2528
3103
  const newLineConfig = suppressedInPre ? NO_SPACING : calculateNewLineConfig(node, state, eventType);
2529
3104
  const quoteAtStart = eventType === 0 && state.blockquotes.at(-1)?.fragment === state.buffer.length;
2530
- const configuredNewLines = quoteAtStart || captionEvent || captionTransition ? 0 : eventType === 1 && tagId === 13 && state.blockquotes.length > 0 ? Math.min(1, newLineConfig[eventType] || 0) : newLineConfig[eventType] || 0;
3105
+ const configuredNewLines = quoteAtStart || captionEvent || captionTransition || eventType === 1 && voidSpacingAtEnter ? 0 : eventType === 1 && tagId === 13 && state.blockquotes.length > 0 ? Math.min(1, newLineConfig[eventType] || 0) : openVoidLines ? Math.max(newLineConfig[eventType] || 0, openVoidLines) : newLineConfig[eventType] || 0;
2531
3106
  let measureFromOutputTail = false;
2532
3107
  if (eventType === 1 && gfmAction?._tag === "PreExit" && output) for (let i = output.length - 1; i >= 0; i--) {
2533
3108
  const frag = output[i];
@@ -2538,21 +3113,37 @@ function createMarkdownProcessor(options = {}, createStream) {
2538
3113
  }
2539
3114
  let newLines = Math.max(0, configuredNewLines - (measureFromOutputTail ? 0 : lastNewLines));
2540
3115
  if (newLines > 0 && buff.length === 0 && !hasYieldedContent) newLines = 0;
2541
- if (state.pendingInlineWhitespace) {
3116
+ pendingSpaceFragment = -1;
3117
+ pendingSpaceOwned = false;
3118
+ if (state.pendingInlineWhitespace && !(captionFrameCount !== 0 && (captionFrames[(captionFrameCount - 1) * CAPTION_FRAME_SIZE + 2] & CAPTION_OPEN) === 0 && (captionTransition || captionFrames[(captionFrameCount - 1) * CAPTION_FRAME_SIZE + 3] !== CAPTION_NO_ANCHOR || !output && newLines === 0 && (eventType === 1 || tagId === 49 || tagId === 52 || tagId === 53)))) {
2542
3119
  const firstOutput = output?.[0]?.[0] || "";
3120
+ const opensQuote = eventType === 0 && tagId === 22;
3121
+ const pairOwed = !!state.pendingPairSeparator && !opensQuote;
3122
+ const owed = pairOwed || !!state.pendingDecodedBlank && !opensQuote;
2543
3123
  if (eventType === 0) {
2544
- if (!isInlineElement || newLines > 0 || firstOutput === "\n" || firstOutput === "\r") state.pendingInlineWhitespace = false;
3124
+ if (!isInlineElement && (tagId !== 36 || firstOutput) && !owed || newLines > 0 || firstOutput === "\n" || firstOutput === "\r") state.pendingInlineWhitespace = false;
2545
3125
  else if (firstOutput) {
2546
- if (lastChar && !" \n \r".includes(lastChar) && !" \n \r".includes(firstOutput)) {
2547
- state.buffer.push(" ");
2548
- state.lastContentCache = " ";
2549
- lastChar = " ";
3126
+ if (lastChar && !" \n \r".includes(lastChar) && !" \n \r".includes(firstOutput) && !(literalOpeners.length && followsLiteralOpener(state.buffer.length))) {
3127
+ if (openMarkerCount && hoistOpenerSpace()) {
3128
+ if ((openMarkers[openMarkerCount - 1] & MARKER_TYPE_MASK) === MARKER_TYPE_MASK) lastChar = " ";
3129
+ } else {
3130
+ pendingCloser = 0;
3131
+ state.buffer.push(" ");
3132
+ state.lastContentCache = " ";
3133
+ lastChar = " ";
3134
+ pendingSpaceFragment = state.buffer.length - 1;
3135
+ pendingSpaceTrimmed = !state.pendingLeadingBlank || pairOwed;
3136
+ }
3137
+ } else if (firstOutput === " ") {
3138
+ pendingSpaceOwned = true;
3139
+ pendingSpaceTrimmed = !state.pendingLeadingBlank || pairOwed;
2550
3140
  }
2551
3141
  state.pendingInlineWhitespace = false;
2552
3142
  }
2553
- } else if (!isInlineElement || newLines > 0) state.pendingInlineWhitespace = false;
3143
+ } else if (!isInlineElement && !owed || newLines > 0) state.pendingInlineWhitespace = false;
2554
3144
  }
2555
3145
  if (newLines > 0) {
3146
+ pendingCloser = 0;
2556
3147
  if (!buff.length && !captionEvent && !captionTransition) {
2557
3148
  if (output) {
2558
3149
  for (const fragment of output) if (fragment) {
@@ -2561,7 +3152,8 @@ function createMarkdownProcessor(options = {}, createStream) {
2561
3152
  }
2562
3153
  }
2563
3154
  if (gfmAction) commitGfmAction(gfmAction, state, gfmLifecycle, state.buffer.length, element, keepsQuoteSeparator);
2564
- updateListIndent(state, element, eventType);
3155
+ if (eventType === 1 && (tagId === 24 || tagId === 33)) closeListMarker(element, hasPluginOutput || handler?.literalExit ? output?.join("") : void 0);
3156
+ if (tagId === 25) updateListIndent(state, element, eventType, output, hasPluginOutput);
2565
3157
  return;
2566
3158
  }
2567
3159
  if (lastChar === " " && buff?.length) {
@@ -2570,12 +3162,13 @@ function createMarkdownProcessor(options = {}, createStream) {
2570
3162
  if (!measureFromOutputTail) newLines = Math.max(0, configuredNewLines - newlineRunBefore(buff, buff.length));
2571
3163
  }
2572
3164
  const newlinesStr = "\n".repeat(newLines);
3165
+ if (eventType === 1 || !output) breakAfterLastText = true;
2573
3166
  if (eventType === 0) {
2574
3167
  if (output) output.unshift(newlinesStr);
2575
3168
  else output = [newlinesStr];
2576
3169
  } else if (output) output.push(newlinesStr);
2577
3170
  else output = [newlinesStr];
2578
- } else if (lastFragment && state.lastTextNode?.containsWhitespace && (!!node.parent || isInlineElement) && "value" in state.lastTextNode && typeof state.lastTextNode.value === "string") {
3171
+ } else if (lastFragment && state.lastTextNode?.containsWhitespace && !(breakAfterLastText && !(output && eventType === 1)) && (!!node.parent || isInlineElement) && "value" in state.lastTextNode && typeof state.lastTextNode.value === "string") {
2579
3172
  let parent = node.parent;
2580
3173
  let parentInPre = false;
2581
3174
  while (parent) {
@@ -2588,7 +3181,7 @@ function createMarkdownProcessor(options = {}, createStream) {
2588
3181
  if (!parentInPre || node.parent?.tagId === 34) {
2589
3182
  const collapsesWhiteSpace = node.tagHandler?.collapsesInnerWhiteSpace;
2590
3183
  const hasSpacing = node.tagHandler?.spacing && Array.isArray(node.tagHandler.spacing);
2591
- const isBlockElement = !isInlineElement && !collapsesWhiteSpace && configuredNewLines > 0;
3184
+ const isBlockElement = !isInlineElement && configuredNewLines > 0 && !(eventType === 1 ? node.parent?.tagHandler?.collapsesInnerWhiteSpace : collapsesWhiteSpace);
2592
3185
  if (!quoteAtStart && !(tagId === 34 && eventType === 1 && isInsideTableCell(state)) && (tagId === 6 && output?.[0]?.endsWith("\n") && !parentInPre || (!isInlineElement || eventType === 1) && !isBlockElement && !(collapsesWhiteSpace && eventType === 0) && !(hasSpacing && eventType === 0))) {
2593
3186
  const originalLength = lastFragment.length;
2594
3187
  const trimmed = trimAsciiWhitespaceEnd(lastFragment);
@@ -2602,7 +3195,12 @@ function createMarkdownProcessor(options = {}, createStream) {
2602
3195
  buff.length--;
2603
3196
  }
2604
3197
  }
2605
- if (eventType === 1 && isInlineElement) state.pendingInlineWhitespace = true;
3198
+ if (eventType === 1 && isInlineElement) {
3199
+ state.pendingInlineWhitespace = true;
3200
+ state.pendingLeadingBlank = false;
3201
+ state.pendingPairSeparator = false;
3202
+ state.pendingDecodedBlank = false;
3203
+ }
2606
3204
  }
2607
3205
  }
2608
3206
  state.lastTextNode = void 0;
@@ -2610,9 +3208,9 @@ function createMarkdownProcessor(options = {}, createStream) {
2610
3208
  }
2611
3209
  let linkHeadingBreak = false;
2612
3210
  if (state.headingBreakPending && eventType === 0) {
2613
- if (tagId === 25 || tagId === 106 || output?.[0] && trimLeadingSpaces(output[0]).startsWith("\n")) {
3211
+ if (tagId === 25 || captionEvent || output?.[0] && trimLeadingSpaces(output[0]).startsWith("\n")) {
2614
3212
  state.headingBreakPending = false;
2615
- if (tagId === 106 && !state.depthMap[34] && captionFrameCount !== 0) captionFrames[(captionFrameCount - 1) * CAPTION_FRAME_SIZE + 2] |= CAPTION_HEADING_BREAK;
3213
+ if (captionEvent && !state.depthMap[34] && captionFrameCount !== 0) captionFrames[(captionFrameCount - 1) * CAPTION_FRAME_SIZE + 2] |= CAPTION_HEADING_BREAK;
2616
3214
  } else if (output?.some(hasNonWhitespace)) {
2617
3215
  state.headingBreakPending = false;
2618
3216
  const separator = `\n\n${state.listIndent}`;
@@ -2643,10 +3241,19 @@ function createMarkdownProcessor(options = {}, createStream) {
2643
3241
  output[0] = `\n\n${state.listIndent}${output[0]}`;
2644
3242
  handlerOutput = output[0];
2645
3243
  }
2646
- if (!captionTransition && output?.[0]?.[0] && eventType === 0 && !handler?.literalEnter && lastChar && needsSpacing(lastChar, output[0][0])) {
3244
+ if (!captionTransition && output?.[0]?.[0] && (tagId !== 6 || output[0][0] === "<") && eventType === 0 && !handler?.literalEnter && lastChar && needsMarkdownSpacing(lastChar, output[0][0], openMarkerCount) && !(pendingRawHtmlBoundary && newLines < 2 && !isInsideRawHtmlBlock(state.depthMap))) {
3245
+ pendingCloser = 0;
2647
3246
  state.buffer.push(" ");
2648
3247
  state.lastContentCache = " ";
2649
3248
  }
3249
+ if (eventType === 1 && handlerOutput) {
3250
+ const markerType = INLINE_MARKER_TYPE[tagId];
3251
+ if (markerType && markerType <= 4) {
3252
+ const mark = handlerOutput.charCodeAt(0);
3253
+ const previous = lastOutputChar(buff);
3254
+ if (previous !== mark && isAsciiPunctuation(previous) && !handler?.literalExit && !options.tagOverrides && !captionFrameCount && !insideRawHtmlRegion && !state.depthMap[34] && !state.depthMap[23] && !state.depthMap[46] && !state.depthMap[82] && !state.depthMap[92]) pendingCloser = mark;
3255
+ }
3256
+ }
2650
3257
  let trailingEmphasisWhitespace = "";
2651
3258
  if (eventType === 1 && !handler?.literalExit && INLINE_MARKER_TYPE[tagId] >= 1 && INLINE_MARKER_TYPE[tagId] <= 4 && (output?.[0]?.charCodeAt(0) === 42 || output?.[0]?.charCodeAt(0) === 126)) {
2652
3259
  let firstFragment = buff.length;
@@ -2683,6 +3290,7 @@ function createMarkdownProcessor(options = {}, createStream) {
2683
3290
  }
2684
3291
  }
2685
3292
  }
3293
+ if (pendingCloser && output && breaksPendingCloser(output, eventType === 0, pendingCloser)) pendingCloser = 0;
2686
3294
  if (eventType === 1 && tagId >= 7 && tagId <= 12 && state.depthMap[25] && !state.depthMap[34] && captionFrameCount === 0 && !state.depthMap[26] && !isInsideTableCell(state) && (!handler?.literalEnter || handler.literalEnterHeading) && !headingKeptInlineInItem(element)) {
2687
3295
  if (handler?.literalEnterHeading) {
2688
3296
  if (handler.literalExit) escapeTrailingHeadingHashes(state);
@@ -2693,7 +3301,7 @@ function createMarkdownProcessor(options = {}, createStream) {
2693
3301
  if (eventType === 0 && tagId === 6 && !state.depthMap[34] && !handler?.literalEnter && output?.at(-1)?.endsWith("\n") && lastOutputChar(buff) === 32) trimTrailingSpaces(buff);
2694
3302
  const rawHtmlQuoteBoundary = pendingRawHtmlBoundary && gfmAction?._tag === "BlockquoteEnter";
2695
3303
  if (rawHtmlQuoteBoundary) finishRawHtmlBlock(`${output?.join("") ?? ""}>`, true);
2696
- if (pendingRawHtmlBoundary && !endsRawHtmlBlock && output) {
3304
+ if (pendingRawHtmlBoundary && !endsRawHtmlBlock && output && eventType === 0) {
2697
3305
  if (newLines >= 2) pendingRawHtmlBoundary = false;
2698
3306
  else finishRawHtmlBlock(output.join(""), true);
2699
3307
  }
@@ -2705,38 +3313,121 @@ function createMarkdownProcessor(options = {}, createStream) {
2705
3313
  state.lastContentCache = fragment;
2706
3314
  }
2707
3315
  }
3316
+ if (eventType === 0 && handler?.literalEnter && (handler.isInline ?? tagId === -1) && outputStart < buff.length) literalOpeners.push(element.depth, buff.length);
2708
3317
  if (trailingEmphasisWhitespace) {
2709
3318
  state.buffer.push(trailingEmphasisWhitespace);
2710
3319
  state.lastContentCache = trailingEmphasisWhitespace;
2711
3320
  }
2712
3321
  if (gfmAction) commitGfmAction(gfmAction, state, gfmLifecycle, outputStart, element, keepsQuoteSeparator, rawHtmlQuoteBoundary);
3322
+ else if (eventType === 0 && (tagId === 23 && handlerOutput === "<code>" || (tagId === 46 || tagId === 82 || tagId === 92) && handlerOutput === "`") && !state.codeText && !handler?.literalEnter) state.codeText = {
3323
+ owner: element,
3324
+ kind: "raw"
3325
+ };
2713
3326
  if (tagId === 25 && !isInsideTableCell(state)) {
2714
3327
  if (eventType === 0) recordItemMarker(state, element.index, handler?.literalEnter ? "" : lastChar);
2715
3328
  else resolveItemMarker(state, true, -1, openLinkFragment !== -1);
2716
3329
  }
2717
3330
  if (eventType === 0 && tagId === 26 && outputStart < buff.length && buff.at(-1) === "[") openLink(buff.length - 1, linkHeadingBreak);
2718
3331
  if (cleanPass) {
2719
- if (eventType === 0) cleanPass.enter(element, outputStart);
3332
+ if (eventType === 0) cleanPass.enter(element, outputStart, closerBefore);
2720
3333
  else if (handlerOutput) {
2721
- cleanPass.closed(element, outputStart, handlerOutput);
3334
+ cleanPass.closed(element, outputStart, handlerOutput, closerBefore);
2722
3335
  if (tagId === 26) noteBufferRewrite(state);
2723
3336
  }
2724
3337
  }
3338
+ if (eventType === 1 && handlerOutput !== void 0 && !handler?.literalExit && !handler?.literalEnter) {
3339
+ const closedType = INLINE_MARKER_TYPE[tagId];
3340
+ if ((closedType === 1 || closedType === 2 || closedType === 4) && buff[buff.length - 1] === handlerOutput) {
3341
+ let previous = buff.length - 2;
3342
+ while (previous >= 0 && buff[previous] === "") previous--;
3343
+ if (closedMarkerCount && closedMarkers[closedMarkerCount - 1] !== previous) closedMarkerCount = 0;
3344
+ const before = lastOutputChar(buff, buff.length - 1);
3345
+ if (closedMarkerCount && buff[closedMarkers[closedMarkerCount - 1]] === handlerOutput) closedMarkerCount = 0;
3346
+ else if (before !== -1 && before !== 96 && !isAsciiWhitespace(before)) closedMarkers[closedMarkerCount++] = buff.length - 1;
3347
+ }
3348
+ }
2725
3349
  if (eventType === 0 && handlerOutput !== void 0 && isInlineElement && !handler?.literalEnter) {
2726
3350
  const markerType = INLINE_MARKER_TYPE[tagId];
2727
- if (markerType && (tagId !== 23 || !state.depthMap[34] && !isInsideRawHtmlBlock(state.depthMap)) && buff[buff.length - 1] === handlerOutput) openMarkers[openMarkerCount++] = buff.length - 1 << 3 | markerType;
2728
- else if (openMarkerCount && !(tagId === 26 && cleanEmptyLinkText) && hasNonWhitespace(handlerOutput)) {
3351
+ if (markerType && (tagId !== 23 || !state.depthMap[34] && !isInsideRawHtmlBlock(state.depthMap)) && buff[buff.length - 1] === handlerOutput) {
3352
+ openMarkerClosers[openMarkerCount] = closerBefore;
3353
+ if (silentMarkerCount > openMarkerCount) silentMarkerCount = openMarkerCount;
3354
+ if (blankMarkerCount > openMarkerCount) blankMarkerCount = 0;
3355
+ let closer = buff.length - 2;
3356
+ while (closer >= 0 && buff[closer] === "") closer--;
3357
+ const merges = (markerType === 1 || markerType === 2 || markerType === 4) && (handlerOutput === "*" || handlerOutput === "**") && !handler?.literalExit && !(state.blockquotes.length && state.blockquotes[state.blockquotes.length - 1].fragment > closer) && !captionNeedsPreparation();
3358
+ const reopened = openMarkerCount ? openMarkers[openMarkerCount - 1] : 0;
3359
+ if (merges && (reopened & MARKER_TYPE_MASK) === MARKER_TYPE_MASK && closer < reopened >> 3 && reopenedDelimiter === handlerOutput && !openMarkerFollowsSpace[openMarkerCount - 1]) {
3360
+ const opener = buff.length - 1;
3361
+ state.outputPositions?.replace(opener, opener, [handlerOutput, handlerOutput]);
3362
+ buff.splice(opener, 0, handlerOutput, handlerOutput);
3363
+ noteBufferRewrite(state);
3364
+ if (closedMarkerCount && closedMarkers[closedMarkerCount - 1] !== closer) closedMarkerCount = 0;
3365
+ closedMarkers[closedMarkerCount++] = opener;
3366
+ unmergedOpener = opener + 1;
3367
+ unmergedSlot = openMarkerCount - 1;
3368
+ openMarkers[unmergedSlot] = unmergedOpener << 3 | reopenedType;
3369
+ openMarkerRunLength[unmergedSlot] = 1;
3370
+ openMarkerFollowsSpace[unmergedSlot] = false;
3371
+ openMarkerSeparatorTrimmed[unmergedSlot] = false;
3372
+ openMarkerRunLength[openMarkerCount] = 2;
3373
+ openMarkerFollowsSpace[openMarkerCount] = false;
3374
+ openMarkerSeparatorTrimmed[openMarkerCount] = false;
3375
+ openMarkers[openMarkerCount++] = buff.length - 1 << 3 | markerType;
3376
+ } else if (merges && closedMarkerCount && closedMarkers[closedMarkerCount - 1] === closer && buff[closer] === handlerOutput && (markerType === 1 ? state.depthMap[14] + state.depthMap[15] + state.depthMap[91] : state.depthMap[16] + state.depthMap[17] + state.depthMap[90] + state.depthMap[106]) < 2) {
3377
+ reopenedDelimiter = handlerOutput;
3378
+ reopenedType = markerType;
3379
+ closedMarkerCount--;
3380
+ state.outputPositions?.replace(closer, buff.length, []);
3381
+ buff.length = closer;
3382
+ noteBufferRewrite(state);
3383
+ state.lastContentCache = buff[buff.length - 1];
3384
+ for (let index = openMarkerCount - 1; index >= 0 && openMarkers[index] >> 3 > buff.length; index--) openMarkers[index] = buff.length << 3 | MARKER_TYPE_MASK;
3385
+ if (unmergedSlot >= openMarkerCount) unmergedSlot = -1;
3386
+ openMarkerRunLength[openMarkerCount] = 1;
3387
+ openMarkerFollowsSpace[openMarkerCount] = false;
3388
+ openMarkerSeparatorTrimmed[openMarkerCount] = false;
3389
+ openMarkers[openMarkerCount++] = buff.length << 3 | MARKER_TYPE_MASK;
3390
+ } else {
3391
+ if (unmergedSlot >= openMarkerCount) unmergedSlot = -1;
3392
+ const previous = openMarkers[openMarkerCount - 1];
3393
+ openMarkerRunLength[openMarkerCount] = openMarkerCount > 0 && markerType <= 4 && (previous & MARKER_TYPE_MASK) <= 4 && previous >> 3 === buff.length - 2 ? openMarkerRunLength[openMarkerCount - 1] + 1 : 1;
3394
+ const followsSpace = pendingSpaceFragment === buff.length - 2 || pendingSpaceOwned && handlerOutput.charCodeAt(0) === 32;
3395
+ openMarkerFollowsSpace[openMarkerCount] = followsSpace;
3396
+ openMarkerSeparatorTrimmed[openMarkerCount] = followsSpace && pendingSpaceTrimmed;
3397
+ openMarkers[openMarkerCount++] = buff.length - 1 << 3 | markerType;
3398
+ }
3399
+ } else if (openMarkerCount && !(tagId === 26 && cleanEmptyLinkText) && hasNonWhitespace(handlerOutput)) {
2729
3400
  openMarkerCount = 0;
2730
3401
  commitCaptionFrames();
2731
3402
  }
2732
- } else if (openMarkerCount && (eventType === 1 && handler?.literalExit || tagId !== -1 && !isInlineElement || output?.some(hasNonWhitespace))) openMarkerCount = 0;
3403
+ } else if (openMarkerCount && (eventType === 1 && handler?.literalExit || tagId !== -1 && !isInlineElement && (output !== void 0 || configuredNewLines > 0) || output?.some(hasNonWhitespace))) openMarkerCount = 0;
3404
+ else if (openMarkerCount && eventType === 1 && tagId !== -1 && !isInlineElement) {
3405
+ if (blankMarkerCount && blankMarkerCount <= openMarkerCount) {
3406
+ const kept = openMarkerCount - blankMarkerCount;
3407
+ for (let i = 0; i < kept; i++) {
3408
+ openMarkers[i] = openMarkers[i + blankMarkerCount];
3409
+ openMarkerClosers[i] = openMarkerClosers[i + blankMarkerCount];
3410
+ openMarkerFollowsSpace[i] = openMarkerFollowsSpace[i + blankMarkerCount];
3411
+ openMarkerSeparatorTrimmed[i] = openMarkerSeparatorTrimmed[i + blankMarkerCount];
3412
+ openMarkerRunLength[i] = openMarkerRunLength[i + blankMarkerCount];
3413
+ }
3414
+ openMarkerCount = kept;
3415
+ blankMarkerCount = 0;
3416
+ }
3417
+ silentMarkerCount = openMarkerCount;
3418
+ }
2733
3419
  if (eventType === 1 && tagId === 26) openLinkFragment = -1;
2734
3420
  if (closesQuote && state.depthMap[25] && !state.depthMap[34] && !insideRawHtmlRegion && !isInsideTableCell(state)) {
2735
3421
  captionBoundary = 3;
2736
3422
  captionBoundarySpacing = 2;
2737
3423
  state.listRulePending = void 0;
2738
3424
  }
2739
- updateListIndent(state, element, eventType);
3425
+ if (eventType === 1 && (tagId === 24 || tagId === 33)) closeListMarker(element, hasPluginOutput || handler?.literalExit ? output?.join("") : void 0);
3426
+ if (tagId === 25) updateListIndent(state, element, eventType, output, hasPluginOutput);
3427
+ if (emptyQuoteItemExit) {
3428
+ captionBoundary = 3;
3429
+ captionBoundarySpacing = 2;
3430
+ }
2740
3431
  if (!insideRawHtmlRegion && inRawHtmlRegion) {
2741
3432
  bufferScan[0] = false;
2742
3433
  bufferScan[1] = state.buffer.length;
@@ -2749,7 +3440,8 @@ function createMarkdownProcessor(options = {}, createStream) {
2749
3440
  return cleanPass ? cleanPass.finish(result).markdown : result;
2750
3441
  }
2751
3442
  function getNonQuoteHeldOutputFragment(final = false) {
2752
- let heldFragment = final ? Infinity : Math.min(openMarkerCount ? openMarkers[0] >> 3 : Infinity, gfmLifecycle.openCodeSpans[0]?.fragment ?? Infinity, state.emptyItemFragment ?? Infinity, state.codeFence?.fragment ?? Infinity, openLinkFragment === -1 ? Infinity : openLinkFragment < -1 ? -openLinkFragment - 2 : openLinkFragment - Number(openLinkHeadingBreak));
3443
+ let heldFragment = final ? Infinity : Math.min(openMarkerCount ? openMarkers[0] >> 3 : Infinity, heldClosedMarker(), gfmLifecycle.openCodeSpans[0]?.fragment ?? Infinity, state.emptyItemFragment ?? Infinity, state.codeFence?.fragment ?? Infinity, openLinkFragment === -1 ? Infinity : openLinkFragment < -1 ? -openLinkFragment - 2 : openLinkFragment - Number(openLinkHeadingBreak));
3444
+ if (!final && literalOpeners.length) heldFragment = holdLiteralOpener(heldFragment);
2753
3445
  if (cleanPass && !final) heldFragment = Math.min(heldFragment, cleanPass.held());
2754
3446
  const captionHoldCount = final ? 0 : captionFrameCount;
2755
3447
  for (let index = 0; index < captionHoldCount; index++) {
@@ -2767,13 +3459,22 @@ function createMarkdownProcessor(options = {}, createStream) {
2767
3459
  let heldFragment = getNonQuoteHeldOutputFragment(final);
2768
3460
  const quoteHoldCount = final ? 0 : state.blockquotes.length;
2769
3461
  for (let index = 0; index < quoteHoldCount; index++) {
2770
- const fragment = state.blockquotes[index].fragment;
3462
+ const frame = state.blockquotes[index];
3463
+ const fragment = frame.fragment - frame.itemSeparatorFragments;
2771
3464
  if (fragment < heldFragment) heldFragment = fragment;
2772
3465
  }
2773
3466
  return heldFragment > state.buffer.length ? Infinity : heldFragment;
2774
3467
  }
2775
3468
  const processor = {
2776
3469
  processEvent,
3470
+ skipEvent(event) {
3471
+ if (event.type !== 1) return;
3472
+ if (state.codeText?.owner === event.node) state.codeText = void 0;
3473
+ const span = gfmLifecycle.openCodeSpans.at(-1);
3474
+ if (span?.owner !== event.node) return;
3475
+ gfmLifecycle.openCodeSpans.pop();
3476
+ if (openMarkerCount && openMarkers[openMarkerCount - 1] === (span.fragment << 3 | 5)) openMarkerCount--;
3477
+ },
2777
3478
  getMarkdown,
2778
3479
  state,
2779
3480
  getHeldOutputFragment: cleanPass ? getHeldOutputFragment : void 0,
@@ -2791,6 +3492,20 @@ function createMarkdownProcessor(options = {}, createStream) {
2791
3492
  inRawHtmlRegion: () => inRawHtmlRegion,
2792
3493
  getNonQuoteHeldOutputFragment,
2793
3494
  getHeldOutputFragment,
3495
+ compactPrefix(fragment, retained) {
3496
+ if (!closedMarkerCount || cleanPass && cleanPass.held() !== Infinity || state.outputPositions || state.blockquotes.length || gfmLifecycle.openCodeSpans.length || openLinkFragment !== -1 || captionFrameCount || literalOpeners.length || state.codeFence || state.emptyItemFragment !== void 0) return false;
3497
+ const removed = fragment - 1;
3498
+ state.buffer.splice(0, fragment, retained);
3499
+ for (let index = 0; index < openMarkerCount; index++) openMarkers[index] = openMarkers[index] - (removed << 3);
3500
+ for (let index = 0; index < closedMarkerCount; index++) closedMarkers[index] -= removed;
3501
+ if (unmergedOpener >= fragment) unmergedOpener -= removed;
3502
+ else unmergedOpener = unmergedSlot = -1;
3503
+ pendingSpaceFragment = pendingSpaceFragment >= fragment ? pendingSpaceFragment - removed : -1;
3504
+ captionLineEnd = 0;
3505
+ captionLineFragment = -1;
3506
+ noteBufferRewrite(state);
3507
+ return true;
3508
+ },
2794
3509
  settleItemMarker() {
2795
3510
  let unresolvedCaptionFragment = -1;
2796
3511
  if (state.emptyItemFragment !== void 0 && captionFrames) for (let index = 0; index < captionFrameCount; index++) {