@mdream/js 2.0.3 → 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,8 +1,6 @@
1
- import { DEFAULT_BLOCK_SPACING, LIST_ITEM_SPACING, NO_SPACING, TABLE_ROW_SPACING } from "./const.mjs";
2
- import { figcaptionOwnsBlockSpacing, isDataUrl, isInsideHeading, isInsideTableCell, lastOutputChar, markRenderedChildContent, trimOutputStart, trimTextAtLineStart } from "./utils.mjs";
3
- import { resolveUrl } from "./url.mjs";
1
+ import { DEFAULT_BLOCK_SPACING, LIST_ITEM_SPACING, NO_SPACING, TABLE_ROW_SPACING, isInsideRawHtmlBlock } from "./const.mjs";
2
+ import { figcaptionOwnsBlockSpacing, isBlockChildOfOpenVoid, isDataUrl, isInsideHeading, isInsideTableCell, isOpenVoidTag, lastOutputChar, markRenderedChildContent, needsSpacing, parserTagHandlers, resolveUrl, trimOutputStart, trimTextAtLineStart } from "./url.mjs";
4
3
  import { assertEngineOptions, buildTagOverrideHandlers, checkClean } from "./tag-overrides.mjs";
5
- import { parserTagHandlers } from "./parse.mjs";
6
4
  import { resolvePlugins } from "./plugin.mjs";
7
5
  import { processHtmlOutput, streamHtmlOutput } from "./output-runner.mjs";
8
6
  function trimAsciiWhitespaceEnd(value) {
@@ -16,7 +14,7 @@ function trimAsciiWhitespaceEnd(value) {
16
14
  }
17
15
  function shouldAddSpacingBeforeText(lastChar, lastNode, textNode) {
18
16
  if (!lastChar || "\n [>".includes(lastChar) || textNode.value[0] === " ") return false;
19
- if (textNode.joinsPrevious || lastNode && (lastNode.tagHandler ? lastNode.tagHandler.isInline : lastNode.tagId === -1)) return false;
17
+ if (textNode.joinsPrevious || lastNode && (lastNode.tagHandler ? lastNode.tagHandler.isInline || isOpenVoidTag(lastNode.tagHandler, lastNode.tagId) : lastNode.tagId === -1)) return false;
20
18
  const firstChar = textNode.value[0];
21
19
  return Boolean(firstChar && !".,!?:;_*`)]".includes(firstChar));
22
20
  }
@@ -30,7 +28,7 @@ function currentColumn(buffer, base, scan) {
30
28
  scan[1] = columnAt(value, value.length, scan[1]);
31
29
  }
32
30
  const tail = buffer.at(-1);
33
- return tail ? columnAt(tail, tail.length, scan[1]) : base;
31
+ return tail ? columnAt(tail, tail.length, scan[1]) : scan[1];
34
32
  }
35
33
  function columnAt(value, end, column) {
36
34
  const newline = end > 0 ? value.lastIndexOf("\n", end - 1) : -1;
@@ -112,11 +110,42 @@ function canWrapHere(depthMap) {
112
110
  if (depthMap[34] || depthMap[23] || depthMap[32] || depthMap[31]) return false;
113
111
  return !isInsideHeading(depthMap);
114
112
  }
115
- function newlineConfig(node, depthMap) {
113
+ const WRITES_INLINE_MARKERS = /* @__PURE__ */ new Uint8Array(114);
114
+ for (const tagId of [
115
+ 14,
116
+ 15,
117
+ 16,
118
+ 17,
119
+ 18,
120
+ 110,
121
+ 111,
122
+ 21,
123
+ 19,
124
+ 20,
125
+ 23,
126
+ 46,
127
+ 82,
128
+ 92,
129
+ 80,
130
+ 89,
131
+ 90,
132
+ 91,
133
+ 81
134
+ ]) WRITES_INLINE_MARKERS[tagId] = 1;
135
+ function wrapperWritesInlineMarkers(node, options, depthMap) {
136
+ const override = options.tagOverrides?.[node.name];
137
+ if (override && typeof override !== "string" && (override.enter || override.exit)) return true;
138
+ const tagId = node.tagId;
139
+ if (tagId === 26) return node.attributes?.href !== void 0 || isInsideRawHtmlBlock(depthMap);
140
+ return tagId !== void 0 && tagId >= 0 && WRITES_INLINE_MARKERS[tagId] === 1;
141
+ }
142
+ function newlineConfig(node, depthMap, options) {
116
143
  const tagId = node.tagId;
117
144
  if (tagId === 106) return NO_SPACING;
118
145
  if (tagId !== 25 && depthMap[25] || tagId !== 22 && depthMap[22]) return tagId === 34 ? [1, 1] : NO_SPACING;
119
146
  const block = tagId !== void 0 && (tagId >= 7 && tagId <= 12 || tagId === 35 || tagId === 36 || tagId === 25);
147
+ const heading = tagId !== void 0 && tagId >= 7 && tagId <= 12;
148
+ let closesBlock = false;
120
149
  let parent = node.parent;
121
150
  while (parent) {
122
151
  if (parent.tagHandler?.collapsesInnerWhiteSpace) {
@@ -124,10 +153,17 @@ function newlineConfig(node, depthMap) {
124
153
  parent = parent.parent;
125
154
  continue;
126
155
  }
156
+ if (isBlockChildOfOpenVoid(node)) break;
157
+ if (heading && !closesBlock && !depthMap[32] && !depthMap[31] && !wrapperWritesInlineMarkers(parent, options, depthMap)) {
158
+ closesBlock = true;
159
+ parent = parent.parent;
160
+ continue;
161
+ }
127
162
  return NO_SPACING;
128
163
  }
129
164
  parent = parent.parent;
130
165
  }
166
+ if (closesBlock) return [0, (node.tagHandler?.spacing ?? DEFAULT_BLOCK_SPACING)[1]];
131
167
  if (node.tagHandler?.spacing) return node.tagHandler.spacing;
132
168
  return tagId === -1 ? NO_SPACING : DEFAULT_BLOCK_SPACING;
133
169
  }
@@ -152,11 +188,11 @@ function elementOutput(node, eventType, state) {
152
188
  const output = alt !== void 0 ? alt || void 0 : node.attributes.title || resolveUrl(isDataUrl(src) ? "" : src, state.options.origin, state.options.clean) || void 0;
153
189
  if (output && hasVisibleContent(output)) {
154
190
  markRenderedChildContent(node);
155
- if (lastOutputChar(state.buffer) > 32) return ` ${output}`;
191
+ if (needsSpacing(charOf(lastOutputChar(state.buffer)), output[0])) return ` ${output}`;
156
192
  }
157
193
  return output;
158
194
  }
159
- if (node.tagId === 81) return lastOutputChar(state.buffer) > 32 ? " \"" : "\"";
195
+ if (node.tagId === 81) return needsSpacing(charOf(lastOutputChar(state.buffer)), "\"") ? " \"" : "\"";
160
196
  } else if (node.tagId === 81) return "\"";
161
197
  }
162
198
  function trailingNewlines(buffer) {
@@ -170,46 +206,64 @@ function trailingNewlines(buffer) {
170
206
  }
171
207
  return count;
172
208
  }
173
- function appendOutput(state, element, eventType, output) {
209
+ function isInlineElement(element) {
210
+ return element.tagHandler ? element.tagHandler.isInline === true : element.tagId === -1;
211
+ }
212
+ function appendOutput(state, element, eventType, output, hasVisiblePluginOutput = false, afterLiteralOpener = false) {
174
213
  const buffer = state.buffer;
175
- const wantedNewlines = newlineConfig(element, state.depthMap)[eventType] || 0;
214
+ const config = newlineConfig(element, state.depthMap, state.options);
215
+ const voidSpacingAtEnter = isOpenVoidTag(element.tagHandler, element.tagId) && !element.tagHandler?.literalExitHasOutput && !(eventType === 1 && hasVisiblePluginOutput);
216
+ let wantedNewlines = voidSpacingAtEnter && eventType === 1 ? 0 : config[eventType] || 0;
217
+ if (voidSpacingAtEnter && eventType === 0) {
218
+ let end = output ? output.length : 0;
219
+ while (end > 0 && output.charCodeAt(end - 1) === 10) end--;
220
+ const owed = (config[1] || 0) - ((output?.length || 0) - end);
221
+ if (end === 0) wantedNewlines = Math.max(wantedNewlines, owed);
222
+ else if (owed > 0) output += "\n".repeat(owed);
223
+ }
176
224
  if (wantedNewlines > 0 && buffer.length > 0 && !(state.depthMap[34] > 0 && element.tagId !== 34)) {
177
225
  const tail = buffer[buffer.length - 1];
178
226
  const trimmed = trimSpacesEnd(tail, state.depthMap[28] > 0);
179
227
  if (trimmed.length !== tail.length) buffer[buffer.length - 1] = trimmed;
180
228
  }
181
229
  const missingNewlines = Math.max(0, wantedNewlines - trailingNewlines(buffer));
182
- const isInline = element.tagHandler ? element.tagHandler.isInline === true : element.tagId === -1;
230
+ const isInline = isInlineElement(element);
183
231
  if (buffer.length === 0) {
184
232
  if (output) buffer.push(output);
185
233
  return;
186
234
  }
187
235
  if (state.pendingInlineWhitespace) {
188
236
  const firstOutput = output?.[0] || "";
237
+ const owed = !!state.pendingDecodedBlank && !(eventType === 0 && element.tagId === 22);
189
238
  if (eventType === 0) {
190
- if (!isInline || element.tagId === 6 || missingNewlines > 0 || firstOutput === "\n" || firstOutput === "\r") state.pendingInlineWhitespace = false;
239
+ if (!isInline && !owed || element.tagId === 6 || missingNewlines > 0 || firstOutput === "\n" || firstOutput === "\r") state.pendingInlineWhitespace = false;
191
240
  else if (firstOutput) {
192
241
  const last = charOf(lastOutputChar(buffer));
193
- if (last && !" \n \r".includes(last) && !" \n \r".includes(firstOutput)) buffer.push(" ");
242
+ if (last && !" \n \r".includes(last) && !" \n \r".includes(firstOutput) && !afterLiteralOpener) buffer.push(" ");
194
243
  state.pendingInlineWhitespace = false;
195
244
  }
196
- } else if (!isInline || missingNewlines > 0) state.pendingInlineWhitespace = false;
245
+ } else if (!isInline && !owed || missingNewlines > 0) state.pendingInlineWhitespace = false;
197
246
  }
198
247
  const closesPre = element.tagId === 34;
199
248
  const inPre = state.depthMap[34] > 0 && !closesPre;
249
+ const keepsOwnSpaces = closesPre && missingNewlines === 0 && state.lastTextNode?.parent === element && state.lastTextNode.index === element.currentWalkIndex - 1;
200
250
  if (buffer.length && state.lastTextNode?.containsWhitespace && !inPre) {
201
251
  const isBlock = !isInline && missingNewlines > 0;
202
252
  const collapses = element.tagHandler?.collapsesInnerWhiteSpace;
203
253
  const hasSpacing = Array.isArray(element.tagHandler?.spacing);
204
- if (element.tagId === 6 && output?.endsWith("\n") || isBlock || (!isInline || eventType === 1) && !(collapses && eventType === 0) && !(hasSpacing && eventType === 0)) {
254
+ if ((element.tagId === 6 && output?.endsWith("\n") || isBlock || (!isInline || eventType === 1) && !(collapses && eventType === 0) && !(hasSpacing && eventType === 0)) && !keepsOwnSpaces) {
205
255
  const last = buffer.at(-1);
206
- const trimmed = closesPre ? trimSpacesEnd(last) : trimAsciiWhitespaceEnd(last);
256
+ const trimmed = element.tagId === 6 && state.depthMap[28] > 0 ? trimSpacesEnd(last, true) : closesPre || state.lastTextNode.parent?.tagId === 34 ? trimSpacesEnd(last) : trimAsciiWhitespaceEnd(last);
207
257
  if (trimmed.length !== last.length) {
208
258
  buffer[buffer.length - 1] = trimmed;
209
- if (eventType === 1 && isInline) state.pendingInlineWhitespace = true;
259
+ if (eventType === 1 && isInline) {
260
+ state.pendingInlineWhitespace = true;
261
+ state.pendingLeadingBlank = false;
262
+ state.pendingDecodedBlank = false;
263
+ }
210
264
  }
211
265
  }
212
- state.lastTextNode = void 0;
266
+ if (!keepsOwnSpaces) state.lastTextNode = void 0;
213
267
  }
214
268
  const newline = missingNewlines ? "\n".repeat(missingNewlines) : "";
215
269
  if (eventType === 0 && newline) buffer.push(newline);
@@ -239,6 +293,14 @@ function createTextOutputProcessor(options, hasPlugins = false) {
239
293
  let captionBreakOwnsLine = false;
240
294
  const openQuotes = [];
241
295
  const openQuoteNodes = [];
296
+ const literalOpeners = [];
297
+ let imageTextPending = false;
298
+ function followsLiteralOpener() {
299
+ const end = literalOpeners[literalOpeners.length - 1];
300
+ if (end < 0 || end > state.buffer.length) return false;
301
+ for (let index = end; index < state.buffer.length; index++) if (state.buffer[index]) return false;
302
+ return true;
303
+ }
242
304
  function pushCaptionBoundary(newlines) {
243
305
  if (state.buffer.length === 0 || newlines === 0) return false;
244
306
  const tail = state.buffer[state.buffer.length - 1];
@@ -263,14 +325,29 @@ function createTextOutputProcessor(options, hasPlugins = false) {
263
325
  function processTextNode(node, lastNode) {
264
326
  if (node.trimsAtLineStart) trimTextAtLineStart(node, state.buffer);
265
327
  if (node.excludedFromMarkdown || !node.value) return;
328
+ if (node.leadingBlank && !hasNonWhitespace(node.value)) {
329
+ if (captionClosedSpacing === 0 && !(captionOpen > 0 && !captionContent) && openQuotes.at(-1) !== state.buffer.length - 1 && !(literalOpeners.length && followsLiteralOpener()) && !state.pendingInlineWhitespace) {
330
+ state.pendingInlineWhitespace = true;
331
+ state.pendingLeadingBlank = true;
332
+ state.pendingDecodedBlank = false;
333
+ }
334
+ return;
335
+ }
266
336
  if (hasVisibleContent(node.value) && openCaptionBoundary()) {
267
337
  node.value = trimAsciiWhitespaceStart(node.value);
268
338
  state.pendingInlineWhitespace = false;
269
339
  }
270
340
  const last = charOf(lastOutputChar(state.buffer));
271
341
  if (state.pendingInlineWhitespace) {
272
- if (!node.value.trim()) return;
273
- if (last && !" \n \r".includes(last) && !" \n \r".includes(node.value[0] || "")) node.value = ` ${node.value}`;
342
+ const blank = !hasNonWhitespace(node.value);
343
+ if (blank) {
344
+ if (!state.pendingLeadingBlank) return;
345
+ if (node.value !== " " || !node.containsWhitespace) {
346
+ state.pendingDecodedBlank = true;
347
+ return;
348
+ }
349
+ }
350
+ if (!blank && last && !" \n \r".includes(last) && !" \n \r".includes(node.value[0] || "") && !(literalOpeners.length && followsLiteralOpener())) node.value = ` ${node.value}`;
274
351
  state.pendingInlineWhitespace = false;
275
352
  }
276
353
  if (state.depthMap[34] && state.buffer.length === 0) preserveLeadingWhitespace = true;
@@ -279,7 +356,8 @@ function createTextOutputProcessor(options, hasPlugins = false) {
279
356
  captionBreakOwnsLine = false;
280
357
  if (last === "\n" && !state.depthMap[34]) node.value = trimAsciiWhitespaceStart(node.value);
281
358
  }
282
- if (!state.depthMap[34] && shouldAddSpacingBeforeText(last, lastNode, node)) node.value = ` ${node.value}`;
359
+ if (!state.depthMap[34] && shouldAddSpacingBeforeText(last, imageTextPending ? void 0 : lastNode, node)) node.value = ` ${node.value}`;
360
+ imageTextPending = false;
283
361
  const width = state.options.wrapWidth;
284
362
  if (hasPlugins) {
285
363
  columnScan[0] = 0;
@@ -305,7 +383,11 @@ function createTextOutputProcessor(options, hasPlugins = false) {
305
383
  }
306
384
  if (event.node.type !== 1) return;
307
385
  const element = event.node;
386
+ if (event.type === 1) while (literalOpeners.length && literalOpeners[literalOpeners.length - 2] >= element.depth) literalOpeners.length -= 2;
308
387
  let output;
388
+ let hasVisiblePluginOutput = false;
389
+ let literalEnter = false;
390
+ let imageText = false;
309
391
  if (element.tagId === 81 && event.type === 1 && openQuotes.length !== 0) {
310
392
  let record = openQuoteNodes.length - 1;
311
393
  while (record >= 0 && openQuoteNodes[record] !== element) record--;
@@ -335,14 +417,23 @@ function createTextOutputProcessor(options, hasPlugins = false) {
335
417
  if (element.pluginOutput?.length) {
336
418
  output = element.pluginOutput.join("");
337
419
  element.pluginOutput = void 0;
338
- } else output = elementOutput(element, event.type, state);
420
+ hasVisiblePluginOutput = hasNonWhitespace(output);
421
+ } else {
422
+ output = elementOutput(element, event.type, state);
423
+ if (event.type === 0 && output) {
424
+ const override = state.options.tagOverrides?.[element.name];
425
+ const overridden = typeof override === "object" && override.enter !== void 0;
426
+ literalEnter = overridden && (element.tagHandler?.isInline ?? element.tagId === -1);
427
+ imageText = !overridden && element.tagId === 27 && !isOpenVoidTag(element.tagHandler, 27);
428
+ }
429
+ }
339
430
  if (element.tagId === 6 && event.type === 0 && output) {
340
431
  const currentNewlines = trailingNewlines(state.buffer);
341
432
  if (!state.depthMap[34] && currentNewlines >= 2) output = void 0;
342
433
  state.pendingInlineWhitespace = false;
343
434
  if (output && !state.depthMap[34] && state.buffer.length > 0) {
344
435
  const tail = state.buffer[state.buffer.length - 1];
345
- const trimmed = trimSpacesEnd(tail);
436
+ const trimmed = trimSpacesEnd(tail, state.depthMap[28] > 0);
346
437
  if (trimmed.length !== tail.length) state.buffer[state.buffer.length - 1] = trimmed;
347
438
  }
348
439
  if (output && captionOpen > 0 && !captionContent) captionBreakRun++;
@@ -360,13 +451,17 @@ function createTextOutputProcessor(options, hasPlugins = false) {
360
451
  output = trimAsciiWhitespaceStart(output);
361
452
  state.pendingInlineWhitespace = false;
362
453
  }
363
- appendOutput(state, element, event.type, output);
454
+ const outputStart = state.buffer.length;
455
+ appendOutput(state, element, event.type, output, hasVisiblePluginOutput, literalOpeners.length !== 0 && followsLiteralOpener());
456
+ if (output) imageTextPending = imageText;
457
+ if (literalEnter && state.buffer.length > outputStart) literalOpeners.push(element.depth, state.buffer.length);
364
458
  if (element.tagId === 81 && event.type === 0 && (output === "\"" || output === " \"") && state.buffer.at(-1) === output) {
365
459
  openQuotes.push(state.buffer.length - 1);
366
460
  openQuoteNodes.push(element);
367
461
  } else if (openQuotes.length !== 0) {
368
- const isInline = element.tagHandler ? element.tagHandler.isInline === true : element.tagId === -1;
369
- if (output && hasNonWhitespace(output) || element.tagId !== -1 && !isInline) {
462
+ const isInline = isInlineElement(element);
463
+ const blockWrites = element.tagId !== -1 && !isInline && (output !== void 0 && output.length > 0 || newlineConfig(element, state.depthMap, state.options)[event.type] > 0);
464
+ if (output && hasNonWhitespace(output) || blockWrites) {
370
465
  openQuotes.length = 0;
371
466
  openQuoteNodes.length = 0;
372
467
  }
@@ -393,6 +488,15 @@ function createTextOutputProcessor(options, hasPlugins = false) {
393
488
  if (contextStart > 0 && first >= 56320 && first <= 57343) contextStart--;
394
489
  if (contextStart === 0) return;
395
490
  if (state.options.wrapWidth) bufferColumn = columnAt(content, contextStart, bufferColumn);
491
+ for (let pair = 1; pair < literalOpeners.length; pair += 2) {
492
+ let end = literalOpeners[pair];
493
+ if (end >= keepFrom) end -= keepFrom - 1;
494
+ else if (end >= 0) {
495
+ while (end < keepFrom && !buffer[end]) end++;
496
+ end = end === keepFrom ? 1 : -1;
497
+ }
498
+ literalOpeners[pair] = end;
499
+ }
396
500
  const kept = buffer.slice(keepFrom);
397
501
  buffer.length = 0;
398
502
  buffer.push(content.slice(contextStart, keptStart));
@@ -524,6 +628,7 @@ assign([6, 54], {
524
628
  collapsesInnerWhiteSpace: true,
525
629
  isInline: true
526
630
  });
631
+ assign([52, 53], { spacing: NO_SPACING });
527
632
  assign([
528
633
  88,
529
634
  70,
@@ -109,6 +109,8 @@ interface CleanTarget {
109
109
  depthMap: Uint16Array;
110
110
  /** The last buffer entry the converter wrote. */
111
111
  lastContentCache?: string;
112
+ /** Buffer length right after an empty marker pair was dropped. */
113
+ droppedPairAt?: number;
112
114
  options?: EngineOptions;
113
115
  /** @internal */
114
116
  outputPositions?: OutputPositions;
@@ -120,14 +122,20 @@ interface CleanTarget {
120
122
  interface CleanPass {
121
123
  /** Output waits for the whole document, because `fragments` needs every heading. */
122
124
  holdsOutput: boolean;
123
- /** An element's enter output starts at `outputStart` in the buffer. */
124
- enter: (element: ElementNode, outputStart: number) => void;
125
+ /**
126
+ * An element's enter output starts at `outputStart` in the buffer.
127
+ * `closer` is the emphasis closer pending before the event, or 0.
128
+ */
129
+ enter: (element: ElementNode, outputStart: number, closer?: number) => void;
125
130
  /** An element exits; called before its exit output. */
126
131
  exit: (element: ElementNode) => void;
127
132
  /** Rewrite the closing anchor to its text and skip its close. Returns true when it did. */
128
133
  unwrap: (element: ElementNode) => boolean;
129
- /** An element wrote its exit output `close` at `outputStart`. */
130
- closed: (element: ElementNode, outputStart: number, close: string) => void;
134
+ /**
135
+ * An element wrote its exit output `close` at `outputStart`.
136
+ * `closer` is the emphasis closer pending before the event, or 0.
137
+ */
138
+ closed: (element: ElementNode, outputStart: number, close: string, closer?: number) => void;
131
139
  /** The earliest buffer index a later hook may rewrite, or Infinity. */
132
140
  held: () => number;
133
141
  /** Apply the rules that need the whole document to the trimmed Markdown so far. */
@@ -202,11 +210,22 @@ export interface TextNode extends Node {
202
210
  context?: PluginContext;
203
211
  /** Follows an end tag that closed nothing, so it gets no separator. */
204
212
  joinsPrevious?: boolean;
213
+ /**
214
+ * Whitespace that opens an inline element, other than one with a literal
215
+ * `tagOverrides` opener. While it stays whitespace it is placed as a word
216
+ * separator, and is not written as text.
217
+ */
218
+ leadingBlank?: boolean;
205
219
  /**
206
220
  * First text with no block ancestor: it drops its leading whitespace when
207
221
  * the output is at the start of a line.
208
222
  */
209
223
  trimsAtLineStart?: boolean;
224
+ /**
225
+ * How many whitespace characters lead the text in the source. Whitespace
226
+ * decoded from a character reference is content and is not counted.
227
+ */
228
+ sourceLead?: number;
210
229
  }
211
230
  /**
212
231
  * Base DOM node interface
@@ -275,6 +294,11 @@ interface MdreamProcessingState {
275
294
  interface MdreamRuntimeState extends Partial<MdreamProcessingState>, PluginState {
276
295
  /** @internal */
277
296
  outputPositions?: OutputPositions;
297
+ /** The element whose built-in output opened a code context. */
298
+ codeText?: {
299
+ owner: ElementNode;
300
+ kind: 'span' | 'raw';
301
+ };
278
302
  /** Active output format for format-aware plugins. */
279
303
  outputFormat: OutputFormat;
280
304
  /** Nesting depth of the current node. */
@@ -297,6 +321,15 @@ interface MdreamRuntimeState extends Partial<MdreamProcessingState>, PluginState
297
321
  buffer: string[];
298
322
  /** Performance cache for last content to avoid iteration */
299
323
  lastContentCache?: string;
324
+ /** Buffer length right after an empty marker pair was dropped. */
325
+ droppedPairAt?: number;
326
+ /**
327
+ * A separator was just written on its own, so the next text does not start
328
+ * the run the redundant-title check compares.
329
+ */
330
+ separatorPending?: boolean;
331
+ /** The cached run follows such a separator, whether or not a drain took it. */
332
+ cacheFollowsSeparator?: boolean;
300
333
  /** Reference to the last processed node */
301
334
  lastNode?: Node;
302
335
  context?: PluginContext;
@@ -314,12 +347,51 @@ interface MdreamRuntimeState extends Partial<MdreamProcessingState>, PluginState
314
347
  preFencePendingDepth?: number;
315
348
  preFenceLang?: string;
316
349
  preFenceOwnerDepth?: number;
350
+ /** A fence may be misread by a parser; see MarkdownState. */
351
+ fencesUnreliable?: boolean;
352
+ preFenceInline?: boolean;
353
+ /** Element whose enter opened the deferred <pre> fence. */
354
+ preFenceOpener?: ElementNode;
355
+ /** Open fenced block, whose opener is the fragment at this index. */
356
+ codeFence?: {
357
+ fragment: number;
358
+ };
317
359
  /** Number of default blockquotes currently buffered for line prefixing. */
318
360
  bufferedBlockquoteDepth?: number;
361
+ /** Fragments the buffered blockquotes' content starts at, innermost last. */
362
+ blockquotes?: ReadonlyArray<{
363
+ readonly fragment: number;
364
+ }>;
319
365
  /** Content-column prefix deferred after a list item rule. */
320
366
  listRulePending?: string;
367
+ /**
368
+ * Marker character codes of each open `<ul>`/`<ol>`, innermost last, packed
369
+ * as `LIST_SELECTED_MASK` and `LIST_WRITTEN_SHIFT` describe.
370
+ */
371
+ listMarkers?: number[];
372
+ /** The latest item marker is `*`, where a rule on its line reads `* ***`. */
373
+ lastItemStar?: boolean;
321
374
  /** Whether output should omit Markdown/HTML markup */
322
375
  plainText?: boolean;
376
+ /**
377
+ * A `<div>`'s heading-marker check, kept with the entries the walk ends and
378
+ * stops in so divs that leave the line unchanged don't redo it.
379
+ * @internal
380
+ */
381
+ markerLine?: MarkerLineMemo;
382
+ }
383
+ /**
384
+ * A `<div>`'s heading-marker answer with the entries the check ended and
385
+ * stopped in. `noteBufferRewrite` drops it whenever output at or before
386
+ * `index` is rewritten.
387
+ * @internal
388
+ */
389
+ interface MarkerLineMemo {
390
+ index: number;
391
+ entry: string;
392
+ stop: number;
393
+ stopEntry: string;
394
+ opens: boolean;
323
395
  }
324
396
  type NodeEventEnter = 0;
325
397
  type NodeEventExit = 1;
@@ -358,7 +430,7 @@ type GfmAction = {
358
430
  _tag: 'PreExit';
359
431
  } | {
360
432
  _tag: 'CodeSpanEnter';
361
- output: string;
433
+ readonly output: string;
362
434
  } | {
363
435
  _tag: 'CodeSpanExit';
364
436
  } | {
@@ -386,6 +458,8 @@ export interface TagHandler {
386
458
  * strings so markers like `^`/`~` attach to adjacent content (issue #93).
387
459
  */
388
460
  literalEnter?: boolean;
461
+ /** Whether a declarative literal enter starts with an ATX heading prefix. */
462
+ literalEnterHeading?: boolean;
389
463
  /** When true, the `exit` string is a user-supplied tagOverride, exempt from empty-pair cleanup. */
390
464
  literalExit?: boolean;
391
465
  /** Whether a declarative literal exit string writes output. */