@mdream/js 1.5.2 → 1.5.4

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.
@@ -35,6 +35,20 @@ function processPluginsForEvent(event, plugins, state, processEvent) {
35
35
  processEvent(event);
36
36
  return false;
37
37
  }
38
+ const INLINE_MARKER_TYPE = /* @__PURE__ */ new Uint8Array(110);
39
+ INLINE_MARKER_TYPE[14] = 1;
40
+ INLINE_MARKER_TYPE[15] = 1;
41
+ INLINE_MARKER_TYPE[91] = 1;
42
+ INLINE_MARKER_TYPE[16] = 2;
43
+ INLINE_MARKER_TYPE[17] = 2;
44
+ INLINE_MARKER_TYPE[106] = 2;
45
+ INLINE_MARKER_TYPE[18] = 3;
46
+ INLINE_MARKER_TYPE[90] = 4;
47
+ INLINE_MARKER_TYPE[46] = 5;
48
+ INLINE_MARKER_TYPE[23] = 5;
49
+ INLINE_MARKER_TYPE[82] = 5;
50
+ INLINE_MARKER_TYPE[92] = 5;
51
+ INLINE_MARKER_TYPE[81] = 6;
38
52
  function updateListIndent(state, element, eventType) {
39
53
  if (element.tagId !== 25) return;
40
54
  if ((state.depthMap[32] || 0) > 0 || (state.depthMap[31] || 0) > 0) return;
@@ -90,6 +104,33 @@ function canWrapHere(depthMap) {
90
104
  for (let h = 7; h <= 12; h++) if (depthMap[h]) return false;
91
105
  return true;
92
106
  }
107
+ function foldPreLinesToBr(value) {
108
+ let start = 0;
109
+ while (start < value.length) {
110
+ const c = value.charCodeAt(start);
111
+ if (c !== 10 && c !== 13) break;
112
+ start++;
113
+ }
114
+ let end = value.length;
115
+ while (end > start) {
116
+ const c = value.charCodeAt(end - 1);
117
+ if (c !== 10 && c !== 13) break;
118
+ end--;
119
+ }
120
+ let out = "";
121
+ for (let i = start; i < end; i++) {
122
+ const c = value.charCodeAt(i);
123
+ if (c === 13) {
124
+ out += "<br>";
125
+ if (i + 1 < end && value.charCodeAt(i + 1) === 10) i++;
126
+ } else if (c === 10) out += "<br>";
127
+ else if (c === 38) out += "&amp;";
128
+ else if (c === 60) out += "&lt;";
129
+ else if (c === 62) out += "&gt;";
130
+ else out += value[i];
131
+ }
132
+ return out;
133
+ }
93
134
  function currentColumn(buffer) {
94
135
  let col = 0;
95
136
  for (let i = buffer.length - 1; i >= 0; i--) {
@@ -161,6 +202,18 @@ function hasNonWhitespace(value) {
161
202
  }
162
203
  return false;
163
204
  }
205
+ function dropEmptyMarker(buffer, packed, markerType) {
206
+ const idx = packed >> 3;
207
+ if ((packed & 7) === markerType && idx < buffer.length) {
208
+ for (let i = idx + 1; i < buffer.length; i++) {
209
+ const fragment = buffer[i];
210
+ if (fragment && hasNonWhitespace(fragment)) return -1;
211
+ }
212
+ buffer.length = idx;
213
+ return idx;
214
+ }
215
+ return -1;
216
+ }
164
217
  function flushPreFence(state) {
165
218
  if (state.plainText) {
166
219
  state.preFencePending = false;
@@ -205,6 +258,8 @@ function createMarkdownProcessor(options = {}, resolvedPlugins = [], tagOverride
205
258
  listIndentWidths: [],
206
259
  plainText: options.format === "text"
207
260
  };
261
+ const openMarkers = [];
262
+ let openMarkerCount = 0;
208
263
  let lastYieldedLength = 0;
209
264
  let hasYieldedContent = false;
210
265
  let preserveLeadingWhitespace = false;
@@ -249,6 +304,7 @@ function createMarkdownProcessor(options = {}, resolvedPlugins = [], tagOverride
249
304
  if (lastChar === "\n" && value[0] && value[0] !== "\n") value = indent + value;
250
305
  textNode.value = value;
251
306
  }
307
+ if (state.depthMap[34] > 0 && (state.depthMap[32] > 0 || state.depthMap[31] > 0)) textNode.value = foldPreLinesToBr(textNode.value);
252
308
  const wrapWidth = state.options?.wrapWidth;
253
309
  if (wrapWidth && canWrapHere(state.depthMap)) {
254
310
  const wrapped = wrapText(textNode.value, currentColumn(state.buffer), wrapWidth, continuationPrefix(textNode, state.listIndentWidths));
@@ -258,6 +314,7 @@ function createMarkdownProcessor(options = {}, resolvedPlugins = [], tagOverride
258
314
  state.buffer.push(textNode.value);
259
315
  state.lastContentCache = textNode.value;
260
316
  }
317
+ if (openMarkerCount && hasNonWhitespace(textNode.value)) openMarkerCount = 0;
261
318
  }
262
319
  state.lastTextNode = textNode;
263
320
  return;
@@ -275,17 +332,40 @@ function createMarkdownProcessor(options = {}, resolvedPlugins = [], tagOverride
275
332
  if (secondLastChar === "\n") lastNewLines++;
276
333
  const eventFn = eventType === 0 ? "enter" : "exit";
277
334
  const handler = node.tagHandler;
335
+ const isInlineElement = handler?.isInline === true;
336
+ let handlerOutput;
278
337
  if (!output && handler?.[eventFn]) {
279
338
  const res = state.plainText ? getPlainTextOutput(element, eventType, state) : handler[eventFn]({
280
339
  node: element,
281
340
  state
282
341
  });
283
- if (res) output = [res];
342
+ if (res) {
343
+ output = [res];
344
+ handlerOutput = res;
345
+ }
346
+ }
347
+ if (eventType === 1 && openMarkerCount) {
348
+ const markerType = handlerOutput === void 0 ? 0 : INLINE_MARKER_TYPE[element.tagId];
349
+ if (markerType && (element.tagId !== 23 || !state.depthMap[34]) && !handler?.literalExit) {
350
+ const idx = dropEmptyMarker(buff, openMarkers[--openMarkerCount], markerType);
351
+ if (idx >= 0) {
352
+ state.lastContentCache = idx > 0 ? buff[idx - 1] : void 0;
353
+ return;
354
+ }
355
+ openMarkerCount = 0;
356
+ }
284
357
  }
285
358
  if (element.tagId === 6 && !state.depthMap[34] && output?.length === 1 && output[0] === "\n" && lastChar === "\n" && secondLastChar === "\n") output = void 0;
286
359
  const configuredNewLines = calculateNewLineConfig(node, state.depthMap, state.plainText === true)[eventType] || 0;
287
- const newLines = Math.max(0, configuredNewLines - lastNewLines);
288
- const isInlineElement = node.tagHandler?.isInline === true;
360
+ let effectiveLastNewLines = lastNewLines;
361
+ if (eventType === 1 && output) for (let i = output.length - 1; i >= 0; i--) {
362
+ const frag = output[i];
363
+ if (frag) {
364
+ if (frag.charCodeAt(frag.length - 1) === 96) effectiveLastNewLines = 0;
365
+ break;
366
+ }
367
+ }
368
+ const newLines = Math.max(0, configuredNewLines - effectiveLastNewLines);
289
369
  if (state.pendingInlineWhitespace) {
290
370
  const firstOutput = output?.[0]?.[0] || "";
291
371
  if (eventType === 0) {
@@ -351,6 +431,11 @@ function createMarkdownProcessor(options = {}, resolvedPlugins = [], tagOverride
351
431
  state.lastContentCache = fragment;
352
432
  }
353
433
  }
434
+ if (eventType === 0 && handlerOutput !== void 0 && isInlineElement && !handler?.literalEnter) {
435
+ const markerType = INLINE_MARKER_TYPE[element.tagId];
436
+ if (markerType && (element.tagId !== 23 || !state.depthMap[34]) && buff[buff.length - 1] === handlerOutput) openMarkers[openMarkerCount++] = buff.length - 1 << 3 | markerType;
437
+ else if (openMarkerCount && hasNonWhitespace(handlerOutput)) openMarkerCount = 0;
438
+ } else if (openMarkerCount && (handler?.literalExit || element.tagId !== -1 && !isInlineElement || output?.some(hasNonWhitespace))) openMarkerCount = 0;
354
439
  updateListIndent(state, element, eventType);
355
440
  }
356
441
  function processHtml(html) {
@@ -377,19 +462,30 @@ function createMarkdownProcessor(options = {}, resolvedPlugins = [], tagOverride
377
462
  const hasMutableTrailingSpace = state.lastTextNode?.containsWhitespace && !state.depthMap[34] && currentContent.endsWith(" ");
378
463
  let stableLength = currentContent.length;
379
464
  if (hasMutableTrailingSpace) while (stableLength > 0 && currentContent[stableLength - 1] === " ") stableLength--;
465
+ const markerHeld = openMarkerCount > 0;
466
+ if (markerHeld) {
467
+ const openFragment = openMarkers[0] >> 3;
468
+ let markerPos = 0;
469
+ for (let i = 0; i < openFragment; i++) markerPos += state.buffer[i].length;
470
+ markerPos -= content.length - currentContent.length;
471
+ if (markerPos < lastYieldedLength) markerPos = lastYieldedLength;
472
+ if (markerPos < stableLength) stableLength = markerPos;
473
+ }
380
474
  const newContent = currentContent.slice(lastYieldedLength, stableLength);
381
475
  lastYieldedLength = stableLength;
382
476
  if (newContent) hasYieldedContent = true;
383
- if (!hasMutableTrailingSpace && !resolvedPlugins.length && !options.wrapWidth && !state.depthMap[26]) {
384
- const tailStart = Math.max(0, stableLength - 2);
385
- const emittedTail = currentContent.slice(tailStart, stableLength);
386
- const mutableTail = currentContent.slice(stableLength);
387
- state.buffer.length = 0;
388
- if (emittedTail || mutableTail) state.buffer.push(emittedTail + mutableTail);
389
- lastYieldedLength = emittedTail.length;
390
- } else if (state.buffer.length > 1 && !hasMutableTrailingSpace) {
391
- state.buffer.length = 0;
392
- state.buffer.push(currentContent);
477
+ if (!markerHeld && !hasMutableTrailingSpace) {
478
+ if (!resolvedPlugins.length && !options.wrapWidth && !state.depthMap[26]) {
479
+ const tailStart = Math.max(0, stableLength - 2);
480
+ const emittedTail = currentContent.slice(tailStart, stableLength);
481
+ const mutableTail = currentContent.slice(stableLength);
482
+ state.buffer.length = 0;
483
+ if (emittedTail || mutableTail) state.buffer.push(emittedTail + mutableTail);
484
+ lastYieldedLength = emittedTail.length;
485
+ } else if (state.buffer.length > 1) {
486
+ state.buffer.length = 0;
487
+ state.buffer.push(currentContent);
488
+ }
393
489
  }
394
490
  return newContent;
395
491
  }
@@ -381,7 +381,7 @@ function isAutolinkUri(s) {
381
381
  }
382
382
  function isInsideTableCell(state) {
383
383
  const depthMap = state.depthMap;
384
- return (depthMap[32] || 0) > 0 || (depthMap[31] || 0) > 0;
384
+ return depthMap[32] > 0 || depthMap[31] > 0;
385
385
  }
386
386
  function isInsideRawHtmlBlock(state) {
387
387
  const depthMap = state.depthMap;
@@ -437,11 +437,11 @@ const tagHandlers = {
437
437
  },
438
438
  [2]: {
439
439
  enter: () => "<details>",
440
- exit: () => "</details>\n\n"
440
+ exit: ({ state }) => isInsideTableCell(state) ? "</details>" : "</details>\n\n"
441
441
  },
442
442
  [3]: {
443
443
  enter: () => "<summary>",
444
- exit: () => "</summary>\n\n"
444
+ exit: ({ state }) => isInsideTableCell(state) ? "</summary>" : "</summary>\n\n"
445
445
  },
446
446
  [4]: {
447
447
  collapsesInnerWhiteSpace: true,
@@ -525,11 +525,13 @@ const tagHandlers = {
525
525
  },
526
526
  [34]: {
527
527
  enter: ({ node, state }) => {
528
+ if (isInsideTableCell(state)) return "<pre>";
528
529
  state.preFencePending = true;
529
530
  state.preOwnFence = false;
530
531
  state.preFenceLang = getLanguageFromClass(node.attributes?.class);
531
532
  },
532
533
  exit: ({ state }) => {
534
+ if (isInsideTableCell(state)) return "</pre>";
533
535
  const ownFence = state.preOwnFence;
534
536
  state.preFencePending = false;
535
537
  state.preOwnFence = false;
@@ -544,6 +546,7 @@ const tagHandlers = {
544
546
  [23]: {
545
547
  enter: ({ node, state }) => {
546
548
  if ((state.depthMap?.[34] || 0) > 0) {
549
+ if (isInsideTableCell(state)) return "<code>";
547
550
  if (state.preOwnFence) return;
548
551
  state.preFencePending = false;
549
552
  const language = getLanguageFromClass(node.attributes?.class);
@@ -559,6 +562,7 @@ const tagHandlers = {
559
562
  },
560
563
  exit: ({ state }) => {
561
564
  if ((state.depthMap?.[34] || 0) > 0) {
565
+ if (isInsideTableCell(state)) return "</code>";
562
566
  if (state.preOwnFence) return;
563
567
  if ((state.depthMap?.[25] || 0) > 0) {
564
568
  const indent = state.listIndent;
@@ -1026,6 +1030,7 @@ function buildTagOverrideHandlers(overrides) {
1026
1030
  if (override.exit !== void 0) {
1027
1031
  const exitStr = override.exit;
1028
1032
  handler.exit = () => exitStr;
1033
+ handler.literalExit = true;
1029
1034
  }
1030
1035
  if (override.spacing !== void 0) handler.spacing = override.spacing;
1031
1036
  if (override.isInline !== void 0) handler.isInline = override.isInline;
@@ -1065,6 +1070,7 @@ const SCRIPT_DATA_DOUBLE_ESCAPED_DASH_DASH = 6;
1065
1070
  const SCRIPT_SEQUENCE_NO_MATCH = -1;
1066
1071
  const SCRIPT_SEQUENCE_INCOMPLETE = -2;
1067
1072
  const SCRIPT_SCAN_COMPLETE = -1;
1073
+ const MAX_ELEMENT_DEPTH = 512;
1068
1074
  const HEAD_CONTENT_TAGS = /* @__PURE__ */ new Set([
1069
1075
  4,
1070
1076
  5,
@@ -1452,6 +1458,7 @@ function parseHtmlStream(html, state, onEvent) {
1452
1458
  return parseHtmlInternal(html, state, onEvent);
1453
1459
  }
1454
1460
  function parseHtmlInternal(htmlChunk, state, handleEvent) {
1461
+ if (state.depthLimitReached) return "";
1455
1462
  let textBuffer = "";
1456
1463
  state.depthMap ??= /* @__PURE__ */ new Uint8Array(110);
1457
1464
  state.depth ??= 0;
@@ -1467,7 +1474,7 @@ function parseHtmlInternal(htmlChunk, state, handleEvent) {
1467
1474
  textBuffer = takeScriptText(state);
1468
1475
  i = scanResult;
1469
1476
  }
1470
- while (i < chunkLength) {
1477
+ while (i < chunkLength && !state.depthLimitReached) {
1471
1478
  const currentCharCode = htmlChunk.charCodeAt(i);
1472
1479
  if (currentCharCode !== LT_CHAR) {
1473
1480
  if (currentCharCode === AMPERSAND_CHAR) state.hasEncodedHtmlEntity = true;
@@ -1819,7 +1826,7 @@ function processCommentOrDoctype(htmlChunk, position) {
1819
1826
  function processCdataSection(content, state, handleEvent) {
1820
1827
  if (!state.tagOverrideHandlers?.has("#cdata-section")) return;
1821
1828
  const open = processOpeningTag("#cdata-section", -1, ">", 0, state, handleEvent);
1822
- if (!open.complete || open.selfClosing) return;
1829
+ if (!open.complete || open.selfClosing || open.skip) return;
1823
1830
  const node = state.currentNode;
1824
1831
  if (content.length > 0 && !node.tagHandler?.excludesTextNodes) {
1825
1832
  const textNode = {
@@ -1888,6 +1895,16 @@ function processOpeningTag(tagName, tagId, htmlChunk, i, state, handleEvent) {
1888
1895
  if ((state.depthMap[99] || 0) > 0 || (state.depthMap[98] || 0) > 0) closeImpliedTo(state, DT_DD, DL_SCOPE_BOUNDARY, handleEvent);
1889
1896
  }
1890
1897
  }
1898
+ if (!result.selfClosing && state.depth >= MAX_ELEMENT_DEPTH) {
1899
+ state.depthLimitReached = true;
1900
+ return {
1901
+ complete: true,
1902
+ newPosition: result.newPosition,
1903
+ remainingText: "",
1904
+ selfClosing: false,
1905
+ skip: true
1906
+ };
1907
+ }
1891
1908
  if (tagId >= 0 && tagId < 110) state.depthMap[tagId] = (state.depthMap[tagId] || 0) + 1;
1892
1909
  state.depth++;
1893
1910
  i = result.newPosition;
@@ -317,6 +317,8 @@ interface TagHandler {
317
317
  * strings so markers like `^`/`~` attach to adjacent content (issue #93).
318
318
  */
319
319
  literalEnter?: boolean;
320
+ /** When true, the `exit` string is a user-supplied tagOverride, exempt from empty-pair cleanup. */
321
+ literalExit?: boolean;
320
322
  /**
321
323
  * Built-in tag id used by declarative string aliases.
322
324
  * @internal
package/dist/parse.d.mts CHANGED
@@ -21,6 +21,8 @@ interface ParseState {
21
21
  depthMap: Uint8Array;
22
22
  /** Current overall nesting depth */
23
23
  depth: number;
24
+ /** Whether parsing stopped after reaching the practical browser depth limit. */
25
+ depthLimitReached?: boolean;
24
26
  /** Currently processing element node */
25
27
  currentNode?: ElementNode | null;
26
28
  /** Whether current content contains HTML entities that need decoding */
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@mdream/js",
3
3
  "type": "module",
4
- "version": "1.5.2",
4
+ "version": "1.5.4",
5
5
  "description": "JavaScript HTML-to-Markdown engine for mdream. Escape hatch for hooks and edge runtimes.",
6
6
  "author": {
7
7
  "name": "Harlan Wilton",