@mdream/js 1.5.4 → 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) {
@@ -202,6 +202,28 @@ function hasNonWhitespace(value) {
202
202
  }
203
203
  return false;
204
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
+ }
205
227
  function dropEmptyMarker(buffer, packed, markerType) {
206
228
  const idx = packed >> 3;
207
229
  if ((packed & 7) === markerType && idx < buffer.length) {
@@ -243,7 +265,11 @@ function getPlainTextOutput(node, eventType, state) {
243
265
  if (lastChar && lastChar !== " " && lastChar !== "\n") return "\n\n";
244
266
  }
245
267
  if (tagId === 32 || tagId === 31) return (depthMap[28] || 0) > 1 || node.index === 0 ? "" : " ";
246
- 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
+ }
247
273
  if (tagId === 81) return "\"";
248
274
  return;
249
275
  }
@@ -260,6 +286,7 @@ function createMarkdownProcessor(options = {}, resolvedPlugins = [], tagOverride
260
286
  };
261
287
  const openMarkers = [];
262
288
  let openMarkerCount = 0;
289
+ let openLinkFragment = -1;
263
290
  let lastYieldedLength = 0;
264
291
  let hasYieldedContent = false;
265
292
  let preserveLeadingWhitespace = false;
@@ -412,7 +439,7 @@ function createMarkdownProcessor(options = {}, resolvedPlugins = [], tagOverride
412
439
  const isBlockElement = !isInlineElement && !collapsesWhiteSpace && configuredNewLines > 0;
413
440
  if (element.tagId === 6 && output?.[0]?.[0] === "\n" && !parentInPre || (!isInlineElement || eventType === 1) && !isBlockElement && !(collapsesWhiteSpace && eventType === 0) && !(hasSpacing && eventType === 0)) {
414
441
  const originalLength = lastFragment.length;
415
- const trimmed = lastFragment.trimEnd();
442
+ const trimmed = trimAsciiWhitespaceEnd(lastFragment);
416
443
  if (originalLength - trimmed.length > 0) {
417
444
  if (buff?.length && buff.at(-1) === lastFragment) buff[buff.length - 1] = trimmed;
418
445
  if (eventType === 1 && isInlineElement) state.pendingInlineWhitespace = true;
@@ -431,11 +458,13 @@ function createMarkdownProcessor(options = {}, resolvedPlugins = [], tagOverride
431
458
  state.lastContentCache = fragment;
432
459
  }
433
460
  }
461
+ if (eventType === 0 && element.tagId === 26 && handlerOutput === "[" && buff.at(-1) === "[") openLinkFragment = buff.length - 1;
434
462
  if (eventType === 0 && handlerOutput !== void 0 && isInlineElement && !handler?.literalEnter) {
435
463
  const markerType = INLINE_MARKER_TYPE[element.tagId];
436
464
  if (markerType && (element.tagId !== 23 || !state.depthMap[34]) && buff[buff.length - 1] === handlerOutput) openMarkers[openMarkerCount++] = buff.length - 1 << 3 | markerType;
437
465
  else if (openMarkerCount && hasNonWhitespace(handlerOutput)) openMarkerCount = 0;
438
466
  } else if (openMarkerCount && (handler?.literalExit || element.tagId !== -1 && !isInlineElement || output?.some(hasNonWhitespace))) openMarkerCount = 0;
467
+ if (eventType === 1 && element.tagId === 26) openLinkFragment = -1;
439
468
  updateListIndent(state, element, eventType);
440
469
  }
441
470
  function processHtml(html) {
@@ -459,30 +488,61 @@ function createMarkdownProcessor(options = {}, resolvedPlugins = [], tagOverride
459
488
  function getMarkdownChunk() {
460
489
  const content = state.buffer.join("");
461
490
  const currentContent = hasYieldedContent || state.plainText && preserveLeadingWhitespace ? content : content.trimStart();
462
- const hasMutableTrailingSpace = state.lastTextNode?.containsWhitespace && !state.depthMap[34] && currentContent.endsWith(" ");
463
- let stableLength = currentContent.length;
464
- 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;
465
512
  const markerHeld = openMarkerCount > 0;
466
513
  if (markerHeld) {
467
514
  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;
515
+ const markerPos = Math.max(lastYieldedLength, trimBufferedWhitespacePosition(currentContent, fragmentPosition(state.buffer, openFragment) - leadingTrimmed));
472
516
  if (markerPos < stableLength) stableLength = markerPos;
473
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;
474
524
  const newContent = currentContent.slice(lastYieldedLength, stableLength);
475
525
  lastYieldedLength = stableLength;
476
526
  if (newContent) hasYieldedContent = true;
477
- if (!markerHeld && !hasMutableTrailingSpace) {
527
+ if (!markerHeld && !linkHeld && (!retainMutableFragments || !inPre)) {
478
528
  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) {
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) {
486
546
  state.buffer.length = 0;
487
547
  state.buffer.push(currentContent);
488
548
  }
@@ -1460,6 +1460,7 @@ function parseHtmlStream(html, state, onEvent) {
1460
1460
  function parseHtmlInternal(htmlChunk, state, handleEvent) {
1461
1461
  if (state.depthLimitReached) return "";
1462
1462
  let textBuffer = "";
1463
+ let runStart = 0;
1463
1464
  state.depthMap ??= /* @__PURE__ */ new Uint8Array(110);
1464
1465
  state.depth ??= 0;
1465
1466
  state.lastCharWasWhitespace ??= true;
@@ -1485,6 +1486,7 @@ function parseHtmlInternal(htmlChunk, state, handleEvent) {
1485
1486
  state.lastCharWasWhitespace = false;
1486
1487
  }
1487
1488
  if (!inPreTag && state.lastCharWasWhitespace) {
1489
+ if (textBuffer.length === 0) runStart = i + 1;
1488
1490
  i++;
1489
1491
  continue;
1490
1492
  }
@@ -1522,9 +1524,11 @@ function parseHtmlInternal(htmlChunk, state, handleEvent) {
1522
1524
  if (textBuffer.length > 0) {
1523
1525
  processTextBuffer(textBuffer, state, handleEvent);
1524
1526
  textBuffer = "";
1527
+ runStart = i;
1525
1528
  }
1526
1529
  processCdataSection(htmlChunk.substring(i + 9, end), state, handleEvent);
1527
1530
  i = end + 3;
1531
+ runStart = i;
1528
1532
  continue;
1529
1533
  }
1530
1534
  if (chunkLength - i < 9 && "<![CDATA[".startsWith(htmlChunk.substring(i))) {
@@ -1535,10 +1539,13 @@ function parseHtmlInternal(htmlChunk, state, handleEvent) {
1535
1539
  if (textBuffer.length > 0) {
1536
1540
  processTextBuffer(textBuffer, state, handleEvent);
1537
1541
  textBuffer = "";
1542
+ runStart = i;
1538
1543
  }
1539
1544
  const result = processCommentOrDoctype(htmlChunk, i);
1540
- if (result.complete) i = result.newPosition;
1541
- else {
1545
+ if (result.complete) {
1546
+ i = result.newPosition;
1547
+ runStart = i;
1548
+ } else {
1542
1549
  textBuffer += result.remainingText;
1543
1550
  break;
1544
1551
  }
@@ -1561,10 +1568,13 @@ function parseHtmlInternal(htmlChunk, state, handleEvent) {
1561
1568
  if (textBuffer.length > 0) {
1562
1569
  processTextBuffer(textBuffer, state, handleEvent);
1563
1570
  textBuffer = "";
1571
+ runStart = i;
1564
1572
  }
1565
1573
  const result = processClosingTag(htmlChunk, i, state, handleEvent);
1566
- if (result.complete) i = result.newPosition;
1567
- else {
1574
+ if (result.complete) {
1575
+ i = result.newPosition;
1576
+ runStart = i;
1577
+ } else {
1568
1578
  textBuffer += result.remainingText;
1569
1579
  break;
1570
1580
  }
@@ -1601,31 +1611,39 @@ function parseHtmlInternal(htmlChunk, state, handleEvent) {
1601
1611
  if (textBuffer.length > 0) {
1602
1612
  processTextBuffer(textBuffer, state, handleEvent);
1603
1613
  textBuffer = "";
1614
+ runStart = i;
1604
1615
  }
1605
1616
  const result = processOpeningTag(tagName, tagId, htmlChunk, i2, state, handleEvent);
1606
- if (result.skip) i = result.newPosition;
1607
- else if (result.complete) {
1617
+ if (result.skip) {
1618
+ i = result.newPosition;
1619
+ runStart = i;
1620
+ } else if (result.complete) {
1608
1621
  i = result.newPosition;
1609
1622
  if (!result.selfClosing) {
1610
1623
  state.isFirstTextInElement = true;
1611
1624
  if (tagId === 52 && tagName === "script") {
1612
1625
  const scanResult = scanScriptChunk(htmlChunk, i, state);
1613
1626
  if (scanResult < 0) {
1614
- 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 = "";
1615
1631
  break;
1616
1632
  }
1617
1633
  textBuffer = takeScriptText(state);
1618
1634
  i = scanResult;
1619
1635
  }
1620
1636
  }
1637
+ if (textBuffer.length === 0) runStart = i;
1621
1638
  } else {
1622
1639
  textBuffer += result.remainingText;
1623
1640
  break;
1624
1641
  }
1625
1642
  }
1626
1643
  }
1627
- if (textBuffer.length > 0 && isWhitespace(textBuffer.charCodeAt(0))) state.lastCharWasWhitespace = false;
1628
- 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;
1629
1647
  }
1630
1648
  function processTextBuffer(textBuffer, state, handleEvent) {
1631
1649
  const containsNonWhitespace = state.textBufferContainsNonWhitespace;
@@ -2065,4 +2083,4 @@ function parseAttributes(attrStr) {
2065
2083
  }
2066
2084
  return result;
2067
2085
  }
2068
- 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.4",
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",