@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,7 +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, needsSpacing, resolveUrl, trimOutputStart, trimTextAtLineStart } 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";
3
3
  import { assertEngineOptions, buildTagOverrideHandlers, checkClean } from "./tag-overrides.mjs";
4
- import { parserTagHandlers } from "./parse.mjs";
5
4
  import { resolvePlugins } from "./plugin.mjs";
6
5
  import { processHtmlOutput, streamHtmlOutput } from "./output-runner.mjs";
7
6
  function trimAsciiWhitespaceEnd(value) {
@@ -15,7 +14,7 @@ function trimAsciiWhitespaceEnd(value) {
15
14
  }
16
15
  function shouldAddSpacingBeforeText(lastChar, lastNode, textNode) {
17
16
  if (!lastChar || "\n [>".includes(lastChar) || textNode.value[0] === " ") return false;
18
- 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;
19
18
  const firstChar = textNode.value[0];
20
19
  return Boolean(firstChar && !".,!?:;_*`)]".includes(firstChar));
21
20
  }
@@ -111,11 +110,42 @@ function canWrapHere(depthMap) {
111
110
  if (depthMap[34] || depthMap[23] || depthMap[32] || depthMap[31]) return false;
112
111
  return !isInsideHeading(depthMap);
113
112
  }
114
- 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) {
115
143
  const tagId = node.tagId;
116
144
  if (tagId === 106) return NO_SPACING;
117
145
  if (tagId !== 25 && depthMap[25] || tagId !== 22 && depthMap[22]) return tagId === 34 ? [1, 1] : NO_SPACING;
118
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;
119
149
  let parent = node.parent;
120
150
  while (parent) {
121
151
  if (parent.tagHandler?.collapsesInnerWhiteSpace) {
@@ -123,10 +153,17 @@ function newlineConfig(node, depthMap) {
123
153
  parent = parent.parent;
124
154
  continue;
125
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
+ }
126
162
  return NO_SPACING;
127
163
  }
128
164
  parent = parent.parent;
129
165
  }
166
+ if (closesBlock) return [0, (node.tagHandler?.spacing ?? DEFAULT_BLOCK_SPACING)[1]];
130
167
  if (node.tagHandler?.spacing) return node.tagHandler.spacing;
131
168
  return tagId === -1 ? NO_SPACING : DEFAULT_BLOCK_SPACING;
132
169
  }
@@ -169,30 +206,43 @@ function trailingNewlines(buffer) {
169
206
  }
170
207
  return count;
171
208
  }
172
- 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) {
173
213
  const buffer = state.buffer;
174
- 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
+ }
175
224
  if (wantedNewlines > 0 && buffer.length > 0 && !(state.depthMap[34] > 0 && element.tagId !== 34)) {
176
225
  const tail = buffer[buffer.length - 1];
177
226
  const trimmed = trimSpacesEnd(tail, state.depthMap[28] > 0);
178
227
  if (trimmed.length !== tail.length) buffer[buffer.length - 1] = trimmed;
179
228
  }
180
229
  const missingNewlines = Math.max(0, wantedNewlines - trailingNewlines(buffer));
181
- const isInline = element.tagHandler ? element.tagHandler.isInline === true : element.tagId === -1;
230
+ const isInline = isInlineElement(element);
182
231
  if (buffer.length === 0) {
183
232
  if (output) buffer.push(output);
184
233
  return;
185
234
  }
186
235
  if (state.pendingInlineWhitespace) {
187
236
  const firstOutput = output?.[0] || "";
237
+ const owed = !!state.pendingDecodedBlank && !(eventType === 0 && element.tagId === 22);
188
238
  if (eventType === 0) {
189
- 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;
190
240
  else if (firstOutput) {
191
241
  const last = charOf(lastOutputChar(buffer));
192
- 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(" ");
193
243
  state.pendingInlineWhitespace = false;
194
244
  }
195
- } else if (!isInline || missingNewlines > 0) state.pendingInlineWhitespace = false;
245
+ } else if (!isInline && !owed || missingNewlines > 0) state.pendingInlineWhitespace = false;
196
246
  }
197
247
  const closesPre = element.tagId === 34;
198
248
  const inPre = state.depthMap[34] > 0 && !closesPre;
@@ -206,7 +256,11 @@ function appendOutput(state, element, eventType, output) {
206
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
266
  if (!keepsOwnSpaces) state.lastTextNode = void 0;
@@ -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,7 +417,16 @@ 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;
@@ -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));
@@ -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;
@@ -331,6 +364,13 @@ interface MdreamRuntimeState extends Partial<MdreamProcessingState>, PluginState
331
364
  }>;
332
365
  /** Content-column prefix deferred after a list item rule. */
333
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;
334
374
  /** Whether output should omit Markdown/HTML markup */
335
375
  plainText?: boolean;
336
376
  /**
@@ -390,7 +430,7 @@ type GfmAction = {
390
430
  _tag: 'PreExit';
391
431
  } | {
392
432
  _tag: 'CodeSpanEnter';
393
- output: string;
433
+ readonly output: string;
394
434
  } | {
395
435
  _tag: 'CodeSpanExit';
396
436
  } | {