@mdream/js 1.5.3 → 1.5.5

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 { DEFAULT_BLOCK_SPACING, NO_SPACING } from "./const.mjs";
2
- import { continuationPrefix, finalizeParse, parseHtmlStream } from "./parse.mjs";
2
+ import { continuationPrefix, finalizeParse, parseHtmlStream, resolveUrl } from "./parse.mjs";
3
3
  function processPluginsForEvent(event, plugins, state, processEvent) {
4
4
  if (plugins?.length) {
5
5
  if (event.node.type === 1 && event.type === 0) {
@@ -104,6 +104,33 @@ function canWrapHere(depthMap) {
104
104
  for (let h = 7; h <= 12; h++) if (depthMap[h]) return false;
105
105
  return true;
106
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
+ }
107
134
  function currentColumn(buffer) {
108
135
  let col = 0;
109
136
  for (let i = buffer.length - 1; i >= 0; i--) {
@@ -175,6 +202,28 @@ function hasNonWhitespace(value) {
175
202
  }
176
203
  return false;
177
204
  }
205
+ function isAsciiWhitespace(code) {
206
+ return code === 32 || code >= 9 && code <= 13;
207
+ }
208
+ function trimAsciiWhitespaceEnd(value) {
209
+ let end = value.length;
210
+ while (end > 0 && isAsciiWhitespace(value.charCodeAt(end - 1))) end--;
211
+ return end === value.length ? value : value.slice(0, end);
212
+ }
213
+ function fragmentPosition(buffer, fragment) {
214
+ let position = 0;
215
+ for (let index = 0; index < fragment; index++) position += buffer[index].length;
216
+ return position;
217
+ }
218
+ function trimBufferedWhitespacePosition(content, position) {
219
+ let end = Math.max(0, position);
220
+ while (end > 0) {
221
+ const code = content.charCodeAt(end - 1);
222
+ if (code !== 32 && code !== 10) break;
223
+ end--;
224
+ }
225
+ return end;
226
+ }
178
227
  function dropEmptyMarker(buffer, packed, markerType) {
179
228
  const idx = packed >> 3;
180
229
  if ((packed & 7) === markerType && idx < buffer.length) {
@@ -216,7 +265,11 @@ function getPlainTextOutput(node, eventType, state) {
216
265
  if (lastChar && lastChar !== " " && lastChar !== "\n") return "\n\n";
217
266
  }
218
267
  if (tagId === 32 || tagId === 31) return (depthMap[28] || 0) > 1 || node.index === 0 ? "" : " ";
219
- if (tagId === 27) return node.attributes?.alt || void 0;
268
+ if (tagId === 27) {
269
+ const alt = node.attributes?.alt;
270
+ if (alt !== void 0) return alt || void 0;
271
+ return node.attributes?.title || resolveUrl(node.attributes?.src || "", state.options?.origin) || void 0;
272
+ }
220
273
  if (tagId === 81) return "\"";
221
274
  return;
222
275
  }
@@ -233,6 +286,7 @@ function createMarkdownProcessor(options = {}, resolvedPlugins = [], tagOverride
233
286
  };
234
287
  const openMarkers = [];
235
288
  let openMarkerCount = 0;
289
+ let openLinkFragment = -1;
236
290
  let lastYieldedLength = 0;
237
291
  let hasYieldedContent = false;
238
292
  let preserveLeadingWhitespace = false;
@@ -277,6 +331,7 @@ function createMarkdownProcessor(options = {}, resolvedPlugins = [], tagOverride
277
331
  if (lastChar === "\n" && value[0] && value[0] !== "\n") value = indent + value;
278
332
  textNode.value = value;
279
333
  }
334
+ if (state.depthMap[34] > 0 && (state.depthMap[32] > 0 || state.depthMap[31] > 0)) textNode.value = foldPreLinesToBr(textNode.value);
280
335
  const wrapWidth = state.options?.wrapWidth;
281
336
  if (wrapWidth && canWrapHere(state.depthMap)) {
282
337
  const wrapped = wrapText(textNode.value, currentColumn(state.buffer), wrapWidth, continuationPrefix(textNode, state.listIndentWidths));
@@ -329,7 +384,15 @@ function createMarkdownProcessor(options = {}, resolvedPlugins = [], tagOverride
329
384
  }
330
385
  if (element.tagId === 6 && !state.depthMap[34] && output?.length === 1 && output[0] === "\n" && lastChar === "\n" && secondLastChar === "\n") output = void 0;
331
386
  const configuredNewLines = calculateNewLineConfig(node, state.depthMap, state.plainText === true)[eventType] || 0;
332
- const newLines = Math.max(0, configuredNewLines - lastNewLines);
387
+ let effectiveLastNewLines = lastNewLines;
388
+ if (eventType === 1 && output) for (let i = output.length - 1; i >= 0; i--) {
389
+ const frag = output[i];
390
+ if (frag) {
391
+ if (frag.charCodeAt(frag.length - 1) === 96) effectiveLastNewLines = 0;
392
+ break;
393
+ }
394
+ }
395
+ const newLines = Math.max(0, configuredNewLines - effectiveLastNewLines);
333
396
  if (state.pendingInlineWhitespace) {
334
397
  const firstOutput = output?.[0]?.[0] || "";
335
398
  if (eventType === 0) {
@@ -376,7 +439,7 @@ function createMarkdownProcessor(options = {}, resolvedPlugins = [], tagOverride
376
439
  const isBlockElement = !isInlineElement && !collapsesWhiteSpace && configuredNewLines > 0;
377
440
  if (element.tagId === 6 && output?.[0]?.[0] === "\n" && !parentInPre || (!isInlineElement || eventType === 1) && !isBlockElement && !(collapsesWhiteSpace && eventType === 0) && !(hasSpacing && eventType === 0)) {
378
441
  const originalLength = lastFragment.length;
379
- const trimmed = lastFragment.trimEnd();
442
+ const trimmed = trimAsciiWhitespaceEnd(lastFragment);
380
443
  if (originalLength - trimmed.length > 0) {
381
444
  if (buff?.length && buff.at(-1) === lastFragment) buff[buff.length - 1] = trimmed;
382
445
  if (eventType === 1 && isInlineElement) state.pendingInlineWhitespace = true;
@@ -395,11 +458,13 @@ function createMarkdownProcessor(options = {}, resolvedPlugins = [], tagOverride
395
458
  state.lastContentCache = fragment;
396
459
  }
397
460
  }
461
+ if (eventType === 0 && element.tagId === 26 && handlerOutput === "[" && buff.at(-1) === "[") openLinkFragment = buff.length - 1;
398
462
  if (eventType === 0 && handlerOutput !== void 0 && isInlineElement && !handler?.literalEnter) {
399
463
  const markerType = INLINE_MARKER_TYPE[element.tagId];
400
464
  if (markerType && (element.tagId !== 23 || !state.depthMap[34]) && buff[buff.length - 1] === handlerOutput) openMarkers[openMarkerCount++] = buff.length - 1 << 3 | markerType;
401
465
  else if (openMarkerCount && hasNonWhitespace(handlerOutput)) openMarkerCount = 0;
402
466
  } else if (openMarkerCount && (handler?.literalExit || element.tagId !== -1 && !isInlineElement || output?.some(hasNonWhitespace))) openMarkerCount = 0;
467
+ if (eventType === 1 && element.tagId === 26) openLinkFragment = -1;
403
468
  updateListIndent(state, element, eventType);
404
469
  }
405
470
  function processHtml(html) {
@@ -423,30 +488,61 @@ function createMarkdownProcessor(options = {}, resolvedPlugins = [], tagOverride
423
488
  function getMarkdownChunk() {
424
489
  const content = state.buffer.join("");
425
490
  const currentContent = hasYieldedContent || state.plainText && preserveLeadingWhitespace ? content : content.trimStart();
426
- const hasMutableTrailingSpace = state.lastTextNode?.containsWhitespace && !state.depthMap[34] && currentContent.endsWith(" ");
427
- let stableLength = currentContent.length;
428
- if (hasMutableTrailingSpace) while (stableLength > 0 && currentContent[stableLength - 1] === " ") stableLength--;
491
+ const inPre = state.depthMap[34] !== 0;
492
+ const trailingCode = currentContent.charCodeAt(currentContent.length - 1);
493
+ let trailingSpaceEnd = currentContent.length;
494
+ while (trailingSpaceEnd > 0 && currentContent.charCodeAt(trailingSpaceEnd - 1) === 32) trailingSpaceEnd--;
495
+ let stableLength = trailingSpaceEnd;
496
+ let retainMutableFragments = stableLength < currentContent.length;
497
+ if (inPre) {
498
+ if (state.lastTextNode?.containsWhitespace && isAsciiWhitespace(trailingCode)) {
499
+ stableLength = trimAsciiWhitespaceEnd(currentContent).length;
500
+ retainMutableFragments = stableLength < currentContent.length;
501
+ } else if (stableLength < currentContent.length) {
502
+ if (!(stableLength === 0 || currentContent.charCodeAt(stableLength - 1) === 10)) {
503
+ stableLength = currentContent.length;
504
+ retainMutableFragments = false;
505
+ }
506
+ }
507
+ } else {
508
+ while (stableLength > 0 && (currentContent[stableLength - 1] === " " || currentContent[stableLength - 1] === "\n")) stableLength--;
509
+ retainMutableFragments = stableLength < currentContent.length;
510
+ }
511
+ const leadingTrimmed = content.length - currentContent.length;
429
512
  const markerHeld = openMarkerCount > 0;
430
513
  if (markerHeld) {
431
514
  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;
515
+ const markerPos = Math.max(lastYieldedLength, trimBufferedWhitespacePosition(currentContent, fragmentPosition(state.buffer, openFragment) - leadingTrimmed));
436
516
  if (markerPos < stableLength) stableLength = markerPos;
437
517
  }
518
+ const linkHeld = openLinkFragment >= 0;
519
+ if (linkHeld) {
520
+ const linkPos = Math.max(lastYieldedLength, trimBufferedWhitespacePosition(currentContent, fragmentPosition(state.buffer, openLinkFragment) - leadingTrimmed));
521
+ if (linkPos < stableLength) stableLength = linkPos;
522
+ }
523
+ if (stableLength < lastYieldedLength) stableLength = lastYieldedLength;
438
524
  const newContent = currentContent.slice(lastYieldedLength, stableLength);
439
525
  lastYieldedLength = stableLength;
440
526
  if (newContent) hasYieldedContent = true;
441
- if (!markerHeld && !hasMutableTrailingSpace) {
527
+ if (!markerHeld && !linkHeld && (!retainMutableFragments || !inPre)) {
442
528
  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) {
529
+ if (retainMutableFragments && leadingTrimmed === 0) {
530
+ const lastFragment = state.buffer.at(-1);
531
+ const fragmentStart = currentContent.length - lastFragment.length;
532
+ const tailStart = Math.max(0, Math.min(stableLength - 2, fragmentStart));
533
+ const emittedTail = currentContent.slice(tailStart, fragmentStart);
534
+ state.buffer.length = 0;
535
+ if (emittedTail) state.buffer.push(emittedTail);
536
+ state.buffer.push(lastFragment);
537
+ lastYieldedLength = stableLength - tailStart;
538
+ } else if (!retainMutableFragments) {
539
+ const tailStart = Math.max(0, stableLength - 2);
540
+ const emittedTail = currentContent.slice(tailStart, stableLength);
541
+ state.buffer.length = 0;
542
+ if (emittedTail) state.buffer.push(emittedTail);
543
+ lastYieldedLength = emittedTail.length;
544
+ }
545
+ } else if (!retainMutableFragments && state.buffer.length > 1) {
450
546
  state.buffer.length = 0;
451
547
  state.buffer.push(currentContent);
452
548
  }
@@ -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;
@@ -1456,6 +1460,7 @@ function parseHtmlStream(html, state, onEvent) {
1456
1460
  function parseHtmlInternal(htmlChunk, state, handleEvent) {
1457
1461
  if (state.depthLimitReached) return "";
1458
1462
  let textBuffer = "";
1463
+ let runStart = 0;
1459
1464
  state.depthMap ??= /* @__PURE__ */ new Uint8Array(110);
1460
1465
  state.depth ??= 0;
1461
1466
  state.lastCharWasWhitespace ??= true;
@@ -1481,6 +1486,7 @@ function parseHtmlInternal(htmlChunk, state, handleEvent) {
1481
1486
  state.lastCharWasWhitespace = false;
1482
1487
  }
1483
1488
  if (!inPreTag && state.lastCharWasWhitespace) {
1489
+ if (textBuffer.length === 0) runStart = i + 1;
1484
1490
  i++;
1485
1491
  continue;
1486
1492
  }
@@ -1518,9 +1524,11 @@ function parseHtmlInternal(htmlChunk, state, handleEvent) {
1518
1524
  if (textBuffer.length > 0) {
1519
1525
  processTextBuffer(textBuffer, state, handleEvent);
1520
1526
  textBuffer = "";
1527
+ runStart = i;
1521
1528
  }
1522
1529
  processCdataSection(htmlChunk.substring(i + 9, end), state, handleEvent);
1523
1530
  i = end + 3;
1531
+ runStart = i;
1524
1532
  continue;
1525
1533
  }
1526
1534
  if (chunkLength - i < 9 && "<![CDATA[".startsWith(htmlChunk.substring(i))) {
@@ -1531,10 +1539,13 @@ function parseHtmlInternal(htmlChunk, state, handleEvent) {
1531
1539
  if (textBuffer.length > 0) {
1532
1540
  processTextBuffer(textBuffer, state, handleEvent);
1533
1541
  textBuffer = "";
1542
+ runStart = i;
1534
1543
  }
1535
1544
  const result = processCommentOrDoctype(htmlChunk, i);
1536
- if (result.complete) i = result.newPosition;
1537
- else {
1545
+ if (result.complete) {
1546
+ i = result.newPosition;
1547
+ runStart = i;
1548
+ } else {
1538
1549
  textBuffer += result.remainingText;
1539
1550
  break;
1540
1551
  }
@@ -1557,10 +1568,13 @@ function parseHtmlInternal(htmlChunk, state, handleEvent) {
1557
1568
  if (textBuffer.length > 0) {
1558
1569
  processTextBuffer(textBuffer, state, handleEvent);
1559
1570
  textBuffer = "";
1571
+ runStart = i;
1560
1572
  }
1561
1573
  const result = processClosingTag(htmlChunk, i, state, handleEvent);
1562
- if (result.complete) i = result.newPosition;
1563
- else {
1574
+ if (result.complete) {
1575
+ i = result.newPosition;
1576
+ runStart = i;
1577
+ } else {
1564
1578
  textBuffer += result.remainingText;
1565
1579
  break;
1566
1580
  }
@@ -1597,31 +1611,39 @@ function parseHtmlInternal(htmlChunk, state, handleEvent) {
1597
1611
  if (textBuffer.length > 0) {
1598
1612
  processTextBuffer(textBuffer, state, handleEvent);
1599
1613
  textBuffer = "";
1614
+ runStart = i;
1600
1615
  }
1601
1616
  const result = processOpeningTag(tagName, tagId, htmlChunk, i2, state, handleEvent);
1602
- if (result.skip) i = result.newPosition;
1603
- else if (result.complete) {
1617
+ if (result.skip) {
1618
+ i = result.newPosition;
1619
+ runStart = i;
1620
+ } else if (result.complete) {
1604
1621
  i = result.newPosition;
1605
1622
  if (!result.selfClosing) {
1606
1623
  state.isFirstTextInElement = true;
1607
1624
  if (tagId === 52 && tagName === "script") {
1608
1625
  const scanResult = scanScriptChunk(htmlChunk, i, state);
1609
1626
  if (scanResult < 0) {
1610
- textBuffer = scanResult <= SCRIPT_SEQUENCE_INCOMPLETE ? htmlChunk.substring(-scanResult - 2) : "";
1627
+ if (scanResult <= SCRIPT_SEQUENCE_INCOMPLETE) {
1628
+ runStart = -scanResult - 2;
1629
+ textBuffer = htmlChunk.substring(runStart);
1630
+ } else textBuffer = "";
1611
1631
  break;
1612
1632
  }
1613
1633
  textBuffer = takeScriptText(state);
1614
1634
  i = scanResult;
1615
1635
  }
1616
1636
  }
1637
+ if (textBuffer.length === 0) runStart = i;
1617
1638
  } else {
1618
1639
  textBuffer += result.remainingText;
1619
1640
  break;
1620
1641
  }
1621
1642
  }
1622
1643
  }
1623
- if (textBuffer.length > 0 && isWhitespace(textBuffer.charCodeAt(0))) state.lastCharWasWhitespace = false;
1624
- return textBuffer;
1644
+ const remainingHtml = textBuffer.length > 0 ? htmlChunk.substring(runStart) : "";
1645
+ if (remainingHtml.length > 0 && isWhitespace(remainingHtml.charCodeAt(0))) state.lastCharWasWhitespace = false;
1646
+ return remainingHtml;
1625
1647
  }
1626
1648
  function processTextBuffer(textBuffer, state, handleEvent) {
1627
1649
  const containsNonWhitespace = state.textBufferContainsNonWhitespace;
@@ -2061,4 +2083,4 @@ function parseAttributes(attrStr) {
2061
2083
  }
2062
2084
  return result;
2063
2085
  }
2064
- export { buildTagOverrideHandlers, continuationPrefix, finalizeParse, parseAttributes, parseHtml, parseHtmlStream };
2086
+ export { buildTagOverrideHandlers, continuationPrefix, finalizeParse, parseAttributes, parseHtml, parseHtmlStream, resolveUrl };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@mdream/js",
3
3
  "type": "module",
4
- "version": "1.5.3",
4
+ "version": "1.5.5",
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",