@mdream/js 1.5.2 → 1.5.3

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;
@@ -161,6 +175,18 @@ function hasNonWhitespace(value) {
161
175
  }
162
176
  return false;
163
177
  }
178
+ function dropEmptyMarker(buffer, packed, markerType) {
179
+ const idx = packed >> 3;
180
+ if ((packed & 7) === markerType && idx < buffer.length) {
181
+ for (let i = idx + 1; i < buffer.length; i++) {
182
+ const fragment = buffer[i];
183
+ if (fragment && hasNonWhitespace(fragment)) return -1;
184
+ }
185
+ buffer.length = idx;
186
+ return idx;
187
+ }
188
+ return -1;
189
+ }
164
190
  function flushPreFence(state) {
165
191
  if (state.plainText) {
166
192
  state.preFencePending = false;
@@ -205,6 +231,8 @@ function createMarkdownProcessor(options = {}, resolvedPlugins = [], tagOverride
205
231
  listIndentWidths: [],
206
232
  plainText: options.format === "text"
207
233
  };
234
+ const openMarkers = [];
235
+ let openMarkerCount = 0;
208
236
  let lastYieldedLength = 0;
209
237
  let hasYieldedContent = false;
210
238
  let preserveLeadingWhitespace = false;
@@ -258,6 +286,7 @@ function createMarkdownProcessor(options = {}, resolvedPlugins = [], tagOverride
258
286
  state.buffer.push(textNode.value);
259
287
  state.lastContentCache = textNode.value;
260
288
  }
289
+ if (openMarkerCount && hasNonWhitespace(textNode.value)) openMarkerCount = 0;
261
290
  }
262
291
  state.lastTextNode = textNode;
263
292
  return;
@@ -275,17 +304,32 @@ function createMarkdownProcessor(options = {}, resolvedPlugins = [], tagOverride
275
304
  if (secondLastChar === "\n") lastNewLines++;
276
305
  const eventFn = eventType === 0 ? "enter" : "exit";
277
306
  const handler = node.tagHandler;
307
+ const isInlineElement = handler?.isInline === true;
308
+ let handlerOutput;
278
309
  if (!output && handler?.[eventFn]) {
279
310
  const res = state.plainText ? getPlainTextOutput(element, eventType, state) : handler[eventFn]({
280
311
  node: element,
281
312
  state
282
313
  });
283
- if (res) output = [res];
314
+ if (res) {
315
+ output = [res];
316
+ handlerOutput = res;
317
+ }
318
+ }
319
+ if (eventType === 1 && openMarkerCount) {
320
+ const markerType = handlerOutput === void 0 ? 0 : INLINE_MARKER_TYPE[element.tagId];
321
+ if (markerType && (element.tagId !== 23 || !state.depthMap[34]) && !handler?.literalExit) {
322
+ const idx = dropEmptyMarker(buff, openMarkers[--openMarkerCount], markerType);
323
+ if (idx >= 0) {
324
+ state.lastContentCache = idx > 0 ? buff[idx - 1] : void 0;
325
+ return;
326
+ }
327
+ openMarkerCount = 0;
328
+ }
284
329
  }
285
330
  if (element.tagId === 6 && !state.depthMap[34] && output?.length === 1 && output[0] === "\n" && lastChar === "\n" && secondLastChar === "\n") output = void 0;
286
331
  const configuredNewLines = calculateNewLineConfig(node, state.depthMap, state.plainText === true)[eventType] || 0;
287
332
  const newLines = Math.max(0, configuredNewLines - lastNewLines);
288
- const isInlineElement = node.tagHandler?.isInline === true;
289
333
  if (state.pendingInlineWhitespace) {
290
334
  const firstOutput = output?.[0]?.[0] || "";
291
335
  if (eventType === 0) {
@@ -351,6 +395,11 @@ function createMarkdownProcessor(options = {}, resolvedPlugins = [], tagOverride
351
395
  state.lastContentCache = fragment;
352
396
  }
353
397
  }
398
+ if (eventType === 0 && handlerOutput !== void 0 && isInlineElement && !handler?.literalEnter) {
399
+ const markerType = INLINE_MARKER_TYPE[element.tagId];
400
+ if (markerType && (element.tagId !== 23 || !state.depthMap[34]) && buff[buff.length - 1] === handlerOutput) openMarkers[openMarkerCount++] = buff.length - 1 << 3 | markerType;
401
+ else if (openMarkerCount && hasNonWhitespace(handlerOutput)) openMarkerCount = 0;
402
+ } else if (openMarkerCount && (handler?.literalExit || element.tagId !== -1 && !isInlineElement || output?.some(hasNonWhitespace))) openMarkerCount = 0;
354
403
  updateListIndent(state, element, eventType);
355
404
  }
356
405
  function processHtml(html) {
@@ -377,19 +426,30 @@ function createMarkdownProcessor(options = {}, resolvedPlugins = [], tagOverride
377
426
  const hasMutableTrailingSpace = state.lastTextNode?.containsWhitespace && !state.depthMap[34] && currentContent.endsWith(" ");
378
427
  let stableLength = currentContent.length;
379
428
  if (hasMutableTrailingSpace) while (stableLength > 0 && currentContent[stableLength - 1] === " ") stableLength--;
429
+ const markerHeld = openMarkerCount > 0;
430
+ if (markerHeld) {
431
+ const openFragment = openMarkers[0] >> 3;
432
+ let markerPos = 0;
433
+ for (let i = 0; i < openFragment; i++) markerPos += state.buffer[i].length;
434
+ markerPos -= content.length - currentContent.length;
435
+ if (markerPos < lastYieldedLength) markerPos = lastYieldedLength;
436
+ if (markerPos < stableLength) stableLength = markerPos;
437
+ }
380
438
  const newContent = currentContent.slice(lastYieldedLength, stableLength);
381
439
  lastYieldedLength = stableLength;
382
440
  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);
441
+ if (!markerHeld && !hasMutableTrailingSpace) {
442
+ if (!resolvedPlugins.length && !options.wrapWidth && !state.depthMap[26]) {
443
+ const tailStart = Math.max(0, stableLength - 2);
444
+ const emittedTail = currentContent.slice(tailStart, stableLength);
445
+ const mutableTail = currentContent.slice(stableLength);
446
+ state.buffer.length = 0;
447
+ if (emittedTail || mutableTail) state.buffer.push(emittedTail + mutableTail);
448
+ lastYieldedLength = emittedTail.length;
449
+ } else if (state.buffer.length > 1) {
450
+ state.buffer.length = 0;
451
+ state.buffer.push(currentContent);
452
+ }
393
453
  }
394
454
  return newContent;
395
455
  }
@@ -1026,6 +1026,7 @@ function buildTagOverrideHandlers(overrides) {
1026
1026
  if (override.exit !== void 0) {
1027
1027
  const exitStr = override.exit;
1028
1028
  handler.exit = () => exitStr;
1029
+ handler.literalExit = true;
1029
1030
  }
1030
1031
  if (override.spacing !== void 0) handler.spacing = override.spacing;
1031
1032
  if (override.isInline !== void 0) handler.isInline = override.isInline;
@@ -1065,6 +1066,7 @@ const SCRIPT_DATA_DOUBLE_ESCAPED_DASH_DASH = 6;
1065
1066
  const SCRIPT_SEQUENCE_NO_MATCH = -1;
1066
1067
  const SCRIPT_SEQUENCE_INCOMPLETE = -2;
1067
1068
  const SCRIPT_SCAN_COMPLETE = -1;
1069
+ const MAX_ELEMENT_DEPTH = 512;
1068
1070
  const HEAD_CONTENT_TAGS = /* @__PURE__ */ new Set([
1069
1071
  4,
1070
1072
  5,
@@ -1452,6 +1454,7 @@ function parseHtmlStream(html, state, onEvent) {
1452
1454
  return parseHtmlInternal(html, state, onEvent);
1453
1455
  }
1454
1456
  function parseHtmlInternal(htmlChunk, state, handleEvent) {
1457
+ if (state.depthLimitReached) return "";
1455
1458
  let textBuffer = "";
1456
1459
  state.depthMap ??= /* @__PURE__ */ new Uint8Array(110);
1457
1460
  state.depth ??= 0;
@@ -1467,7 +1470,7 @@ function parseHtmlInternal(htmlChunk, state, handleEvent) {
1467
1470
  textBuffer = takeScriptText(state);
1468
1471
  i = scanResult;
1469
1472
  }
1470
- while (i < chunkLength) {
1473
+ while (i < chunkLength && !state.depthLimitReached) {
1471
1474
  const currentCharCode = htmlChunk.charCodeAt(i);
1472
1475
  if (currentCharCode !== LT_CHAR) {
1473
1476
  if (currentCharCode === AMPERSAND_CHAR) state.hasEncodedHtmlEntity = true;
@@ -1819,7 +1822,7 @@ function processCommentOrDoctype(htmlChunk, position) {
1819
1822
  function processCdataSection(content, state, handleEvent) {
1820
1823
  if (!state.tagOverrideHandlers?.has("#cdata-section")) return;
1821
1824
  const open = processOpeningTag("#cdata-section", -1, ">", 0, state, handleEvent);
1822
- if (!open.complete || open.selfClosing) return;
1825
+ if (!open.complete || open.selfClosing || open.skip) return;
1823
1826
  const node = state.currentNode;
1824
1827
  if (content.length > 0 && !node.tagHandler?.excludesTextNodes) {
1825
1828
  const textNode = {
@@ -1888,6 +1891,16 @@ function processOpeningTag(tagName, tagId, htmlChunk, i, state, handleEvent) {
1888
1891
  if ((state.depthMap[99] || 0) > 0 || (state.depthMap[98] || 0) > 0) closeImpliedTo(state, DT_DD, DL_SCOPE_BOUNDARY, handleEvent);
1889
1892
  }
1890
1893
  }
1894
+ if (!result.selfClosing && state.depth >= MAX_ELEMENT_DEPTH) {
1895
+ state.depthLimitReached = true;
1896
+ return {
1897
+ complete: true,
1898
+ newPosition: result.newPosition,
1899
+ remainingText: "",
1900
+ selfClosing: false,
1901
+ skip: true
1902
+ };
1903
+ }
1891
1904
  if (tagId >= 0 && tagId < 110) state.depthMap[tagId] = (state.depthMap[tagId] || 0) + 1;
1892
1905
  state.depth++;
1893
1906
  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.3",
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",