@mdream/js 1.7.0 → 1.7.2

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.
@@ -3,5 +3,4 @@ import { EngineOptions } from "./types.mjs";
3
3
  * Creates a configurable minimal preset with advanced options.
4
4
  * Returns declarative plugin config that works with both JS and Rust engines.
5
5
  */
6
- declare function withMinimalPreset<T extends EngineOptions>(options?: T): T;
7
- export { withMinimalPreset };
6
+ export declare function withMinimalPreset<T extends EngineOptions>(options?: T): T;
@@ -309,6 +309,25 @@ function isSafeHtmlUrl(href, image = false) {
309
309
  }
310
310
  return true;
311
311
  }
312
+ function escapeHtml(value, attribute = false, protectMarkdown = false) {
313
+ let escaped = "";
314
+ let copiedUntil = 0;
315
+ for (let index = 0; index < value.length; index++) {
316
+ const code = value.charCodeAt(index);
317
+ let replacement;
318
+ if (code === 38) replacement = "&amp;";
319
+ else if (code === 60) replacement = "&lt;";
320
+ else if (code === 62) replacement = "&gt;";
321
+ else if (attribute && code === 34) replacement = "&quot;";
322
+ else if (protectMarkdown && code === 91) replacement = "&#91;";
323
+ else if (protectMarkdown && code === 93) replacement = "&#93;";
324
+ if (replacement !== void 0) {
325
+ escaped += value.slice(copiedUntil, index) + replacement;
326
+ copiedUntil = index + 1;
327
+ }
328
+ }
329
+ return copiedUntil === 0 ? value : escaped + value.slice(copiedUntil);
330
+ }
312
331
  function continuationPrefix(node, listIndentWidths, includeBlockquotes = true) {
313
332
  const chain = [];
314
333
  let current = node.parent;
@@ -384,18 +403,18 @@ function parseUnsignedInteger(raw) {
384
403
  const value = Number(raw);
385
404
  return value <= 4294967295 ? value : void 0;
386
405
  }
387
- function lastOutputChar(buffer) {
388
- for (let index = buffer.length - 1; index >= 0; index--) {
406
+ function lastOutputChar(buffer, end = buffer.length) {
407
+ for (let index = end - 1; index >= 0; index--) {
389
408
  const fragment = buffer[index];
390
409
  if (fragment.length > 0) return fragment.charCodeAt(fragment.length - 1);
391
410
  }
392
411
  return -1;
393
412
  }
394
- function blockOpenPrefix(buffer, prefix) {
395
- switch (lastOutputChar(buffer)) {
413
+ function blockOpenPrefix(buffer, prefix, end = buffer.length) {
414
+ switch (lastOutputChar(buffer, end)) {
396
415
  case -1: return;
397
416
  case 32: {
398
- let fragment = buffer.length - 1;
417
+ let fragment = end - 1;
399
418
  let index = buffer[fragment].length;
400
419
  for (;;) {
401
420
  if (index === 0) {
@@ -411,6 +430,15 @@ function blockOpenPrefix(buffer, prefix) {
411
430
  default: return `\n\n${prefix ?? ""}`;
412
431
  }
413
432
  }
433
+ function figcaptionOwnsBlockSpacing(node, plainText) {
434
+ let parent = node.parent;
435
+ while (parent) {
436
+ if (parent.tagId === 32 || parent.tagId === 31) return false;
437
+ if (!plainText && parent.tagId !== 37 && parent.tagHandler?.collapsesInnerWhiteSpace) return false;
438
+ parent = parent.parent;
439
+ }
440
+ return true;
441
+ }
414
442
  function listMarkerLineStart(buffer, fragmentIndex, markerIndex) {
415
443
  const marker = buffer[fragmentIndex].charCodeAt(markerIndex);
416
444
  const ordered = marker === 46 || marker === 41;
@@ -523,7 +551,8 @@ function decodeHTMLEntities(text, inAttribute = false, protectDecodedEntityRefer
523
551
  let legacyEnd = Math.min(nameEnd, nameStart + 6);
524
552
  let decodedLegacy = false;
525
553
  while (legacyEnd > nameStart) {
526
- const replacement = HTML_ENTITIES[text.slice(nameStart, legacyEnd)];
554
+ const name = text.slice(nameStart, legacyEnd);
555
+ const replacement = HTML_ENTITIES[name];
527
556
  if (typeof replacement === "string") {
528
557
  const next = text.charCodeAt(legacyEnd);
529
558
  if (replacement !== void 0 && !(inAttribute && (next === 61 || isAsciiAlphaNumeric(next)))) {
@@ -541,18 +570,44 @@ function decodeHTMLEntities(text, inAttribute = false, protectDecodedEntityRefer
541
570
  }
542
571
  return result;
543
572
  }
573
+ function isInlineNode(node) {
574
+ const isInline = node.tagHandler?.isInline;
575
+ return isInline === void 0 ? node.tagId === -1 : isInline;
576
+ }
544
577
  function traverseUpToFirstBlockNode(node) {
545
578
  let firstBlockParent = node;
546
579
  const parentsToIncrement = [firstBlockParent];
547
- while (firstBlockParent.tagHandler?.isInline) {
580
+ while (isInlineNode(firstBlockParent)) {
548
581
  if (!firstBlockParent.parent) break;
549
582
  firstBlockParent = firstBlockParent.parent;
550
583
  parentsToIncrement.push(firstBlockParent);
551
584
  }
552
585
  return parentsToIncrement;
553
586
  }
587
+ function markRenderedChildContent(node) {
588
+ let parent = node.parent;
589
+ let anchorInScope = false;
590
+ while (parent) {
591
+ parent.childTextNodeIndex = (parent.childTextNodeIndex || 0) + 1;
592
+ if (parent.tagId === 26) anchorInScope = true;
593
+ if (!isInlineNode(parent)) break;
594
+ parent = parent.parent;
595
+ }
596
+ if (anchorInScope) return;
597
+ let anchorFound = false;
598
+ while (parent?.parent) {
599
+ parent = parent.parent;
600
+ if (!anchorFound) {
601
+ if (parent.tagId !== 26) continue;
602
+ anchorFound = true;
603
+ }
604
+ parent.childTextNodeIndex = (parent.childTextNodeIndex || 0) + 1;
605
+ if (!isInlineNode(parent)) return;
606
+ }
607
+ }
554
608
  const TRACKING_PARAM_RE = /^(?:utm_|fbclid|gclid|mc_eid|msclkid|oly_)/;
555
- const URL_SCHEME_RE = /^[\dA-Z+.-]+:/i;
609
+ const URL_SCHEME_RE = /^[A-Z][\dA-Z+.-]*:/i;
610
+ const SLASH_CHAR$1 = 47;
556
611
  function stripTrackingParams(url) {
557
612
  const queryStart = url.indexOf("?");
558
613
  const fragmentStart = url.indexOf("#");
@@ -561,17 +616,83 @@ function stripTrackingParams(url) {
561
616
  const query = url.slice(queryStart + 1, queryEnd).split("&").filter((parameter) => !TRACKING_PARAM_RE.test(parameter)).join("&");
562
617
  return `${url.slice(0, queryStart)}${query ? `?${query}` : ""}${url.slice(queryEnd)}`;
563
618
  }
619
+ function pathEnd(value, from) {
620
+ const query = value.indexOf("?", from);
621
+ const fragment = value.indexOf("#", from);
622
+ if (query === -1) return fragment === -1 ? value.length : fragment;
623
+ return fragment === -1 ? query : Math.min(query, fragment);
624
+ }
625
+ function hasDotSegment(path) {
626
+ return path.includes(".") && (path === "." || path === ".." || path.startsWith("./") || path.startsWith("../") || path.includes("/./") || path.includes("/../") || path.endsWith("/.") || path.endsWith("/.."));
627
+ }
628
+ function mergeDotSegments(dir, rest) {
629
+ let out = dir;
630
+ let index = 0;
631
+ for (;;) {
632
+ let end = rest.indexOf("/", index);
633
+ const last = end === -1;
634
+ if (last) end = rest.length;
635
+ const segment = rest.slice(index, end);
636
+ if (segment === "..") out = out.slice(0, out.lastIndexOf("/", out.length - 2) + 1);
637
+ else if (segment !== ".") out += last ? segment : `${segment}/`;
638
+ if (last) return out;
639
+ index = end + 1;
640
+ }
641
+ }
642
+ let baseOrigin;
643
+ let baseRoot = "";
644
+ let basePath = "";
645
+ let baseDir = "";
646
+ function loadBase(origin) {
647
+ if (origin !== baseOrigin) {
648
+ baseOrigin = origin;
649
+ baseRoot = "";
650
+ const authority = origin.indexOf("://");
651
+ if (authority !== -1) {
652
+ const end = pathEnd(origin, authority + 3);
653
+ const slash = origin.indexOf("/", authority + 3);
654
+ const rootEnd = slash === -1 || slash > end ? end : slash;
655
+ baseRoot = origin.slice(0, rootEnd);
656
+ basePath = origin.slice(rootEnd, end);
657
+ baseDir = basePath.slice(0, basePath.lastIndexOf("/") + 1) || "/";
658
+ }
659
+ }
660
+ return baseRoot !== "";
661
+ }
662
+ function resolveAgainstBase(origin, url) {
663
+ if (!loadBase(origin)) {
664
+ let prefix = origin;
665
+ while (prefix.endsWith("/")) prefix = prefix.slice(0, -1);
666
+ const path = url.startsWith("./") ? url.slice(2) : url;
667
+ return `${prefix}${path.charCodeAt(0) === SLASH_CHAR$1 ? "" : "/"}${path}`;
668
+ }
669
+ const end = pathEnd(url, 0);
670
+ const path = url.slice(0, end);
671
+ const suffix = end === url.length ? "" : url.slice(end);
672
+ if (!path) return `${baseRoot}${basePath}${suffix}`;
673
+ const dot = hasDotSegment(path);
674
+ if (path.charCodeAt(0) === SLASH_CHAR$1) return `${baseRoot}${dot ? mergeDotSegments("/", path.slice(1)) : path}${suffix}`;
675
+ return `${baseRoot}${dot ? mergeDotSegments(baseDir, path) : baseDir + path}${suffix}`;
676
+ }
564
677
  function resolveUrl(url, origin, clean) {
565
678
  if (!url || url[0] === "#") return url;
679
+ const isSlash = url.charCodeAt(0) === SLASH_CHAR$1;
566
680
  let resolved = url;
567
- if (url.startsWith("//")) resolved = `https:${url}`;
568
- else if (origin && !URL_SCHEME_RE.test(url)) {
569
- const path = url.startsWith("./") ? url.slice(2) : url;
570
- while (origin.endsWith("/")) origin = origin.slice(0, -1);
571
- resolved = `${origin}${path[0] === "/" ? "" : "/"}${path}`;
572
- }
681
+ if (isSlash && url.charCodeAt(1) === SLASH_CHAR$1) {
682
+ let scheme = "https";
683
+ if (origin && loadBase(origin)) scheme = baseRoot.slice(0, baseRoot.indexOf(":"));
684
+ resolved = `${scheme}:${url}`;
685
+ } else if (origin && (isSlash || !URL_SCHEME_RE.test(url))) resolved = resolveAgainstBase(origin, url);
573
686
  return (clean === true || !!clean && clean.urls === true) && resolved.includes("?") ? stripTrackingParams(resolved) : resolved;
574
687
  }
688
+ function safeAnchorOutput(node, options, entering, protectMarkdown = false) {
689
+ const href = node.attributes?.href;
690
+ if (!href || !isSafeHtmlUrl(href)) return;
691
+ const resolved = resolveUrl(href, options?.origin, options?.clean);
692
+ if (!isSafeHtmlUrl(resolved)) return;
693
+ if (!entering) return "</a>";
694
+ return `<a href="${escapeHtml(resolved, true, protectMarkdown)}"${node.attributes?.title === void 0 ? "" : ` title="${escapeHtml(node.attributes.title, true, protectMarkdown)}"`}>`;
695
+ }
575
696
  function serializeMarkdownDestination(destination) {
576
697
  if (!/[\t\n\f\r ()\\<>]/.test(destination)) return destination;
577
698
  return `<${/[\\<>]/.test(destination) ? destination.replace(/[\\<>]/g, "\\$&") : destination}>`;
@@ -903,12 +1024,14 @@ const tagHandlers = {
903
1024
  enter: ({ node, state }) => {
904
1025
  if (node.attributes?.href !== void 0) {
905
1026
  if (stripsEmptyLink(state, node.attributes.href)) return;
1027
+ if (isInsideRawHtmlBlock(state.depthMap)) return safeAnchorOutput(node, state.options, true, true);
906
1028
  return "[";
907
1029
  }
908
1030
  },
909
1031
  exit: ({ node, state }) => {
910
1032
  if (node.attributes?.href === void 0) return "";
911
1033
  if (stripsEmptyLink(state, node.attributes.href)) return "";
1034
+ if (isInsideRawHtmlBlock(state.depthMap) && !node.tagHandler?.literalEnter) return safeAnchorOutput(node, state.options, false, true);
912
1035
  const href = resolveUrl(node.attributes.href, state.options?.origin, state.options?.clean);
913
1036
  let title = node.attributes?.title;
914
1037
  if (state.lastContentCache === title) title = "";
@@ -916,14 +1039,26 @@ const tagHandlers = {
916
1039
  const buf = state.buffer;
917
1040
  let i = buf.length - 1;
918
1041
  let textLen = 0;
1042
+ let captionBracket = -1;
1043
+ let captionTextLen = 0;
919
1044
  while (i >= 0) {
920
1045
  const entry = buf[i];
921
1046
  if (entry === "[") break;
1047
+ if (captionBracket < 0 && state.depthMap?.[106] && entry.endsWith("*[")) {
1048
+ captionBracket = i;
1049
+ captionTextLen = textLen;
1050
+ }
922
1051
  textLen += entry.length;
923
1052
  i--;
924
1053
  }
1054
+ const captionLink = i < 0 && captionBracket >= 0;
1055
+ if (captionLink) {
1056
+ i = captionBracket;
1057
+ textLen = captionTextLen;
1058
+ }
925
1059
  if (i >= 0 && textLen === href.length && buf.slice(i + 1).join("") === href) {
926
- buf.length = i;
1060
+ if (captionLink) buf[i] = buf[i].slice(0, -1);
1061
+ buf.length = i + (captionLink ? 1 : 0);
927
1062
  const auto = `<${href}>`;
928
1063
  buf.push(auto);
929
1064
  state.lastContentCache = auto;
@@ -940,6 +1075,10 @@ const tagHandlers = {
940
1075
  enter: ({ node, state }) => {
941
1076
  const alt = node.attributes?.alt || "";
942
1077
  const src = resolveUrl(node.attributes?.src || "", state.options?.origin, state.options?.clean);
1078
+ const clean = state.options?.clean;
1079
+ if ((clean === true || clean !== void 0 && clean !== false && clean.emptyImages === true) && !alt.trim()) {
1080
+ if (state.depthMap?.[106]) return void 0;
1081
+ } else markRenderedChildContent(node);
943
1082
  return `![${serializeImageDescription(alt)}]${serializeMarkdownResource(src, node.attributes?.title)}`;
944
1083
  },
945
1084
  collapsesInnerWhiteSpace: true,
@@ -1299,10 +1438,8 @@ const tagHandlers = {
1299
1438
  [104]: {},
1300
1439
  [102]: {},
1301
1440
  [106]: {
1302
- enter: () => "*",
1303
1441
  exit: () => "*",
1304
1442
  collapsesInnerWhiteSpace: true,
1305
- spacing: NO_SPACING,
1306
1443
  isInline: true
1307
1444
  }
1308
1445
  };
@@ -1872,24 +2009,26 @@ function parseHtmlInternal(htmlChunk, state, handleEvent) {
1872
2009
  break;
1873
2010
  }
1874
2011
  const nextCharCode = htmlChunk.charCodeAt(i + 1);
1875
- if (state.flattenedRawTagName) if (nextCharCode === SLASH_CHAR) {
1876
- let peekEnd = i + 2;
1877
- while (peekEnd < chunkLength) {
1878
- const c = htmlChunk.charCodeAt(peekEnd);
1879
- if (c === GT_CHAR || c === SLASH_CHAR || isWhitespace(c)) break;
1880
- peekEnd++;
1881
- }
1882
- if (peekEnd === chunkLength) {
1883
- textBuffer += htmlChunk.substring(i);
1884
- break;
1885
- }
1886
- if (normalizeTagName(htmlChunk.substring(i + 2, peekEnd)) !== state.flattenedRawTagName) {
2012
+ if (state.flattenedRawTagName) {
2013
+ if (nextCharCode === SLASH_CHAR) {
2014
+ let peekEnd = i + 2;
2015
+ while (peekEnd < chunkLength) {
2016
+ const c = htmlChunk.charCodeAt(peekEnd);
2017
+ if (c === GT_CHAR || c === SLASH_CHAR || isWhitespace(c)) break;
2018
+ peekEnd++;
2019
+ }
2020
+ if (peekEnd === chunkLength) {
2021
+ textBuffer += htmlChunk.substring(i);
2022
+ break;
2023
+ }
2024
+ if (normalizeTagName(htmlChunk.substring(i + 2, peekEnd)) !== state.flattenedRawTagName) {
2025
+ textBuffer += htmlChunk[i++];
2026
+ continue;
2027
+ }
2028
+ } else {
1887
2029
  textBuffer += htmlChunk[i++];
1888
2030
  continue;
1889
2031
  }
1890
- } else {
1891
- textBuffer += htmlChunk[i++];
1892
- continue;
1893
2032
  }
1894
2033
  if (nextCharCode === EXCLAMATION_CHAR) {
1895
2034
  if (htmlChunk.charCodeAt(i + 2) === OPEN_BRACKET_CHAR) {
@@ -2395,37 +2534,41 @@ function processOpeningTag(tagName, tagId, htmlChunk, i, state, handleEvent) {
2395
2534
  const headNode = state.currentNode;
2396
2535
  if (headNode && headNode.tagId === 1) closeNode(headNode, state, handleEvent);
2397
2536
  }
2398
- if (tagId >= 0 && tagId < 113 && NEEDS_IMPLIED_END_RECOVERY[tagId] === 1) if (tagId === 65 && parserTagDepth(state, 65) > 0) {
2399
- if (closeSelectTo(state, 65, handleEvent)) return {
2400
- complete: true,
2401
- newPosition: result.newPosition,
2402
- remainingText: "",
2403
- selfClosing: false,
2404
- skip: true
2405
- };
2406
- } else if (tagId === 67) {
2407
- if (parserTagDepth(state, 65) > 0) closeSelectTo(state, 67, handleEvent);
2408
- else if (parserLastTagId(state) === 67) popParserTop(state, handleEvent);
2409
- } else if (tagId === 109 && parserTagDepth(state, 65) > 0) {
2410
- closeSelectTo(state, 67, handleEvent);
2411
- closeSelectTo(state, 109, handleEvent);
2412
- } else if (tagId === 26) {
2413
- if (parserTagDepth(state, 26) > 0) closeImpliedTo(state, SINGLE_A, A_SCOPE_BOUNDARY, handleEvent);
2414
- } else if (tagId === 32 || tagId === 31 || tagId === 30 || tagId === 29 || tagId === 38 || tagId === 39) {
2415
- if (parserTagDepth(state, 28) > 0) if (tagId === 32 || tagId === 31) {
2416
- if (parserTagDepth(state, 32) > 0 || parserTagDepth(state, 31) > 0) closeImpliedTo(state, TD_TH, CELL_SCOPE_BOUNDARY, handleEvent);
2417
- } else if (tagId === 30) {
2418
- if (parserTagDepth(state, 30) > 0) closeTableContext(state, TR_CELLS, handleEvent);
2419
- } else closeTableContext(state, SECTION_CELLS, handleEvent);
2420
- } else {
2421
- if (parserTagDepth(state, 35) > 0) closeImpliedTo(state, SINGLE_P, P_SCOPE_BOUNDARY, handleEvent);
2422
- if (HEADINGS.has(tagId)) {
2423
- const topId = parserLastTagId(state);
2424
- if (topId !== void 0 && HEADINGS.has(topId)) popParserTop(state, handleEvent);
2425
- } else if (tagId === 25) {
2426
- if (parserTagDepth(state, 25) > 0) closeImpliedTo(state, SINGLE_LI, LI_SCOPE_BOUNDARY, handleEvent);
2427
- } else if (tagId === 99 || tagId === 98) {
2428
- if (parserTagDepth(state, 99) > 0 || parserTagDepth(state, 98) > 0) closeImpliedTo(state, DT_DD, DL_SCOPE_BOUNDARY, handleEvent);
2537
+ if (tagId >= 0 && tagId < 113 && NEEDS_IMPLIED_END_RECOVERY[tagId] === 1) {
2538
+ if (tagId === 65 && parserTagDepth(state, 65) > 0) {
2539
+ if (closeSelectTo(state, 65, handleEvent)) return {
2540
+ complete: true,
2541
+ newPosition: result.newPosition,
2542
+ remainingText: "",
2543
+ selfClosing: false,
2544
+ skip: true
2545
+ };
2546
+ } else if (tagId === 67) {
2547
+ if (parserTagDepth(state, 65) > 0) closeSelectTo(state, 67, handleEvent);
2548
+ else if (parserLastTagId(state) === 67) popParserTop(state, handleEvent);
2549
+ } else if (tagId === 109 && parserTagDepth(state, 65) > 0) {
2550
+ closeSelectTo(state, 67, handleEvent);
2551
+ closeSelectTo(state, 109, handleEvent);
2552
+ } else if (tagId === 26) {
2553
+ if (parserTagDepth(state, 26) > 0) closeImpliedTo(state, SINGLE_A, A_SCOPE_BOUNDARY, handleEvent);
2554
+ } else if (tagId === 32 || tagId === 31 || tagId === 30 || tagId === 29 || tagId === 38 || tagId === 39) {
2555
+ if (parserTagDepth(state, 28) > 0) {
2556
+ if (tagId === 32 || tagId === 31) {
2557
+ if (parserTagDepth(state, 32) > 0 || parserTagDepth(state, 31) > 0) closeImpliedTo(state, TD_TH, CELL_SCOPE_BOUNDARY, handleEvent);
2558
+ } else if (tagId === 30) {
2559
+ if (parserTagDepth(state, 30) > 0) closeTableContext(state, TR_CELLS, handleEvent);
2560
+ } else closeTableContext(state, SECTION_CELLS, handleEvent);
2561
+ }
2562
+ } else {
2563
+ if (parserTagDepth(state, 35) > 0) closeImpliedTo(state, SINGLE_P, P_SCOPE_BOUNDARY, handleEvent);
2564
+ if (HEADINGS.has(tagId)) {
2565
+ const topId = parserLastTagId(state);
2566
+ if (topId !== void 0 && HEADINGS.has(topId)) popParserTop(state, handleEvent);
2567
+ } else if (tagId === 25) {
2568
+ if (parserTagDepth(state, 25) > 0) closeImpliedTo(state, SINGLE_LI, LI_SCOPE_BOUNDARY, handleEvent);
2569
+ } else if (tagId === 99 || tagId === 98) {
2570
+ if (parserTagDepth(state, 99) > 0 || parserTagDepth(state, 98) > 0) closeImpliedTo(state, DT_DD, DL_SCOPE_BOUNDARY, handleEvent);
2571
+ }
2429
2572
  }
2430
2573
  }
2431
2574
  if (state.flattenedTagName && !result.selfClosing) {
@@ -2559,12 +2702,10 @@ function scanAttributes(source, position, selfClosing, stopAtTagEnd) {
2559
2702
  state = ATTRIBUTE_GAP;
2560
2703
  }
2561
2704
  break;
2562
- case ATTRIBUTE_UNQUOTED_VALUE:
2563
- if (isSpace || !stopAtTagEnd && charCode === GT_CHAR) {
2564
- attributes = setAttribute(attributes, source.substring(nameStart, nameEnd), source.substring(valueStart, i));
2565
- state = ATTRIBUTE_GAP;
2566
- }
2567
- break;
2705
+ case ATTRIBUTE_UNQUOTED_VALUE: if (isSpace || !stopAtTagEnd && charCode === GT_CHAR) {
2706
+ attributes = setAttribute(attributes, source.substring(nameStart, nameEnd), source.substring(valueStart, i));
2707
+ state = ATTRIBUTE_GAP;
2708
+ }
2568
2709
  }
2569
2710
  i++;
2570
2711
  }
@@ -2581,9 +2722,7 @@ function scanAttributes(source, position, selfClosing, stopAtTagEnd) {
2581
2722
  attributes = setAttribute(attributes, source.substring(nameStart, nameEnd), "");
2582
2723
  break;
2583
2724
  case ATTRIBUTE_QUOTED_VALUE:
2584
- case ATTRIBUTE_UNQUOTED_VALUE:
2585
- attributes = setAttribute(attributes, source.substring(nameStart, nameEnd), source.substring(valueStart, valueEnd));
2586
- break;
2725
+ case ATTRIBUTE_UNQUOTED_VALUE: attributes = setAttribute(attributes, source.substring(nameStart, nameEnd), source.substring(valueStart, valueEnd));
2587
2726
  }
2588
2727
  return {
2589
2728
  _tag: "complete",
@@ -2601,4 +2740,4 @@ function parseAttributes(attrStr) {
2601
2740
  if (result._tag === "incomplete") throw new Error("Isolated attribute scans always complete at EOF");
2602
2741
  return result.attributes === EMPTY_ATTRIBUTES ? {} : result.attributes;
2603
2742
  }
2604
- export { blockOpenPrefix, breakHandler, buildTagOverrideHandlers, continuationPrefix, finalizeParse, getLanguageFromClass, isCharacterReferenceTail, isEmptyLinkHref, isInsideHeading, isInsideTableCell, isSafeHtmlUrl, listMarkerLineStart, orderedItemNumber, parseAttributes, parseHtml, parseHtmlStream, parseUnsignedInteger, renderBreak, resolveUrl };
2743
+ export { blockOpenPrefix, breakHandler, buildTagOverrideHandlers, continuationPrefix, escapeHtml, figcaptionOwnsBlockSpacing, finalizeParse, getLanguageFromClass, isCharacterReferenceTail, isEmptyLinkHref, isInsideHeading, isInsideTableCell, isSafeHtmlUrl, lastOutputChar, listMarkerLineStart, markRenderedChildContent, orderedItemNumber, parseAttributes, parseHtml, parseHtmlStream, parseUnsignedInteger, renderBreak, resolveUrl, safeAnchorOutput };
@@ -19,13 +19,15 @@ async function* streamHtmlToMarkdown(htmlStream, options = {}, resolvedPlugins =
19
19
  const { done, value } = await reader.read();
20
20
  if (done) break;
21
21
  const decoded = typeof value === "string" ? decoder.decode() + value : decoder.decode(value, { stream: true });
22
- remainingHtml = parseHtmlStream(`${remainingHtml}${decoded}`, parseState, handleEvent);
22
+ const htmlContent = `${remainingHtml}${decoded}`;
23
+ remainingHtml = parseHtmlStream(htmlContent, parseState, handleEvent);
23
24
  const chunk = processor.getMarkdownChunk();
24
25
  if (chunk) yield chunk;
25
26
  }
26
27
  const decoderTail = decoder.decode();
27
28
  const finalHtml = remainingHtml + decoderTail;
28
- finalizeParse(finalHtml ? parseHtmlStream(finalHtml, parseState, handleEvent) : "", parseState, handleEvent);
29
+ const leftover = finalHtml ? parseHtmlStream(finalHtml, parseState, handleEvent) : "";
30
+ finalizeParse(leftover, parseState, handleEvent);
29
31
  const finalChunk = processor.getMarkdownChunk();
30
32
  if (finalChunk) yield finalChunk;
31
33
  } finally {
@@ -1,11 +1,11 @@
1
- declare const TAG_H1 = 7;
2
- declare const TAG_H2 = 8;
3
- declare const TAG_H3 = 9;
4
- declare const TAG_H4 = 10;
5
- declare const TAG_H5 = 11;
6
- declare const TAG_H6 = 12;
7
- declare const ELEMENT_NODE = 1;
8
- declare const TEXT_NODE = 2;
1
+ export declare const TAG_H1 = 7;
2
+ export declare const TAG_H2 = 8;
3
+ export declare const TAG_H3 = 9;
4
+ export declare const TAG_H4 = 10;
5
+ export declare const TAG_H5 = 11;
6
+ export declare const TAG_H6 = 12;
7
+ export declare const ELEMENT_NODE = 1;
8
+ export declare const TEXT_NODE = 2;
9
9
  declare const NodeEventEnter$1 = 0;
10
10
  declare const NodeEventExit$1 = 1;
11
11
  /**
@@ -13,7 +13,7 @@ declare const NodeEventExit$1 = 1;
13
13
  * When transforms are provided, the JS engine is used regardless of engine selection.
14
14
  * For declarative config that works with both engines, use `BuiltinPlugins`.
15
15
  */
16
- interface TransformPlugin {
16
+ export interface TransformPlugin {
17
17
  /**
18
18
  * Whether a flattened element starts an output-excluded subtree.
19
19
  * @internal
@@ -51,7 +51,7 @@ interface TransformPlugin {
51
51
  * Declarative tag override configuration.
52
52
  * When a string value is provided, it acts as an alias (e.g. `{ "x-heading": "h2" }`).
53
53
  */
54
- interface TagOverride {
54
+ export interface TagOverride {
55
55
  enter?: string;
56
56
  exit?: string;
57
57
  spacing?: [number, number];
@@ -62,7 +62,7 @@ interface TagOverride {
62
62
  /**
63
63
  * Frontmatter configuration options.
64
64
  */
65
- interface FrontmatterConfig {
65
+ export interface FrontmatterConfig {
66
66
  additionalFields?: Record<string, string>;
67
67
  metaFields?: string[];
68
68
  /**
@@ -75,7 +75,7 @@ interface FrontmatterConfig {
75
75
  * Declarative configuration for built-in plugins.
76
76
  * Works with both the JavaScript and Rust engines.
77
77
  */
78
- interface BuiltinPlugins {
78
+ export interface BuiltinPlugins {
79
79
  /** Filter elements by CSS selectors, tag names, or TAG_* constants */
80
80
  filter?: {
81
81
  include?: (string | number)[];
@@ -109,7 +109,7 @@ interface BuiltinPlugins {
109
109
  * Shared engine options that work with both JS and Rust engines.
110
110
  * This is the contract that `MarkdownEngine` methods accept.
111
111
  */
112
- interface CleanOptions {
112
+ export interface CleanOptions {
113
113
  /** Strip tracking query parameters (utm_*, fbclid, gclid, etc.) from URLs */
114
114
  urls?: boolean;
115
115
  /** Strip fragment-only links that don't match any heading in the output */
@@ -127,7 +127,7 @@ interface CleanOptions {
127
127
  /** Drop links that produce no visible text: [](url) → nothing */
128
128
  emptyLinkText?: boolean;
129
129
  }
130
- interface EngineOptions {
130
+ export interface EngineOptions {
131
131
  /**
132
132
  * Origin URL for resolving relative image paths and internal links.
133
133
  */
@@ -155,7 +155,7 @@ interface EngineOptions {
155
155
  */
156
156
  format?: 'markdown' | 'text' | 'html';
157
157
  }
158
- interface ElementNode extends Node {
158
+ export interface ElementNode extends Node {
159
159
  /** Element tag name (for ELEMENT_NODE) */
160
160
  name: string;
161
161
  /** HTML attributes (for ELEMENT_NODE) */
@@ -169,7 +169,7 @@ interface ElementNode extends Node {
169
169
  /** Plugin outputs collected during processing */
170
170
  pluginOutput?: string[];
171
171
  }
172
- interface TextNode extends Node {
172
+ export interface TextNode extends Node {
173
173
  /** Text content (for TEXT_NODE) */
174
174
  value: string;
175
175
  /** Markdown delimiters generated by a plugin around this literal text. */
@@ -184,7 +184,7 @@ interface TextNode extends Node {
184
184
  * Base DOM node interface
185
185
  * Optimized for streaming HTML parsing with minimal memory footprint
186
186
  */
187
- interface Node {
187
+ export interface Node {
188
188
  /** Node type (ELEMENT_NODE or TEXT_NODE) */
189
189
  type: number;
190
190
  /** Current nesting depth in the DOM tree */
@@ -194,7 +194,7 @@ interface Node {
194
194
  index: number;
195
195
  /** Current walk index for child traversal during streaming */
196
196
  currentWalkIndex?: number;
197
- /** Count of text child nodes - used for whitespace handling */
197
+ /** Child-content counter used by whitespace and empty-link handling */
198
198
  childTextNodeIndex?: number;
199
199
  /** Whether node contains whitespace - used for whitespace optimization */
200
200
  containsWhitespace?: boolean;
@@ -250,7 +250,7 @@ interface MdreamProcessingState {
250
250
  * Runtime state for markdown generation
251
251
  * Extended state that includes output tracking and options
252
252
  */
253
- interface MdreamRuntimeState extends Partial<MdreamProcessingState> {
253
+ export interface MdreamRuntimeState extends Partial<MdreamProcessingState> {
254
254
  /** Number of newlines at end of most recent output */
255
255
  lastNewLines?: number;
256
256
  /** Configuration options for conversion */
@@ -296,7 +296,7 @@ type NodeEventExit = 1;
296
296
  * Node event for DOM traversal
297
297
  * Used in the event-based traversal system for streaming processing
298
298
  */
299
- interface NodeEvent {
299
+ export interface NodeEvent {
300
300
  /** Event type - enter (start tag) or exit (end tag) */
301
301
  type: NodeEventEnter | NodeEventExit;
302
302
  /** The node being processed */
@@ -340,7 +340,7 @@ type TagHandlerResult = string | GfmAction | undefined | void;
340
340
  * Tag handler interface for HTML elements
341
341
  * Used by plugins to extend or customize tag handling
342
342
  */
343
- interface TagHandler {
343
+ export interface TagHandler {
344
344
  enter?: (context: HandlerContext) => TagHandlerResult;
345
345
  exit?: (context: HandlerContext) => TagHandlerResult;
346
346
  isSelfClosing?: boolean;
@@ -368,7 +368,7 @@ interface TailwindContext {
368
368
  prefix?: string;
369
369
  suffix?: string;
370
370
  }
371
- interface PluginContext {
371
+ export interface PluginContext {
372
372
  score?: number;
373
373
  tagCount?: number;
374
374
  linkTextLength?: number;
@@ -380,7 +380,7 @@ interface PluginContext {
380
380
  /**
381
381
  * Element extracted during conversion by the extraction plugin.
382
382
  */
383
- interface ExtractedElement {
383
+ export interface ExtractedElement {
384
384
  /** The CSS selector that matched this element */
385
385
  selector: string;
386
386
  /** The HTML tag name */
@@ -394,7 +394,7 @@ interface ExtractedElement {
394
394
  * Top-level options for the mdream JS engine.
395
395
  * Extends the shared `EngineOptions` with JS-specific concerns.
396
396
  */
397
- interface MdreamOptions extends EngineOptions {
397
+ export interface MdreamOptions extends EngineOptions {
398
398
  /**
399
399
  * Imperative hook-based transform plugins.
400
400
  * When provided, a new JS engine is created with these hooks.
@@ -405,7 +405,7 @@ interface MdreamOptions extends EngineOptions {
405
405
  * Markdown chunk with content and metadata
406
406
  * Compatible with LangChain Document structure
407
407
  */
408
- interface MarkdownChunk {
408
+ export interface MarkdownChunk {
409
409
  /** The markdown content of the chunk */
410
410
  content: string;
411
411
  /** Metadata extracted during chunking */
@@ -427,7 +427,7 @@ interface MarkdownChunk {
427
427
  * Options for HTML to Markdown chunking
428
428
  * Extends EngineOptions with chunking-specific settings
429
429
  */
430
- interface SplitterOptions extends EngineOptions {
430
+ export interface SplitterOptions extends EngineOptions {
431
431
  /**
432
432
  * Header tag IDs to split on (TAG_H1, TAG_H2, etc.)
433
433
  * @example [TAG_H1, TAG_H2]
@@ -466,4 +466,4 @@ interface SplitterOptions extends EngineOptions {
466
466
  */
467
467
  keepSeparator?: boolean;
468
468
  }
469
- export { BuiltinPlugins, CleanOptions, ELEMENT_NODE, ElementNode, EngineOptions, ExtractedElement, FrontmatterConfig, MarkdownChunk, MdreamOptions, MdreamRuntimeState, Node, NodeEvent, NodeEventEnter$1 as NodeEventEnter, NodeEventExit$1 as NodeEventExit, PluginContext, SplitterOptions, TAG_H1, TAG_H2, TAG_H3, TAG_H4, TAG_H5, TAG_H6, TEXT_NODE, TagHandler, TagOverride, TextNode, TransformPlugin };
469
+ export { NodeEventEnter$1 as NodeEventEnter, NodeEventExit$1 as NodeEventExit };
package/dist/cli.d.mts CHANGED
@@ -1 +1 @@
1
- export { };
1
+ export {}
package/dist/cli.mjs CHANGED
@@ -1,5 +1,5 @@
1
- import { streamHtmlToMarkdown } from "./_chunks/src.mjs";
2
1
  import { withMinimalPreset } from "./preset/minimal.mjs";
2
+ import { streamHtmlToMarkdown } from "./index.mjs";
3
3
  import { readFileSync } from "node:fs";
4
4
  import { Readable } from "node:stream";
5
5
  import { fileURLToPath } from "node:url";
@@ -21,7 +21,8 @@ async function streamingConvert(options = {}) {
21
21
  const markdownGenerator = streamHtmlToMarkdown(Readable.toWeb(process.stdin), conversionOptions);
22
22
  for await (const markdownChunk of markdownGenerator) if (markdownChunk && markdownChunk.length > 0) process.stdout.write(markdownChunk);
23
23
  }
24
- const packageJsonPath = join(dirname(fileURLToPath(import.meta.url)), "..", "package.json");
24
+ const __dirname = dirname(fileURLToPath(import.meta.url));
25
+ const packageJsonPath = join(__dirname, "..", "package.json");
25
26
  const packageJson = JSON.parse(readFileSync(packageJsonPath, "utf-8"));
26
27
  const cli = cac();
27
28
  cli.command("[options]", "Convert HTML from stdin to Markdown on stdout (JS engine)").option("--origin <url>", "Origin URL for resolving relative image paths").option("--preset <preset>", "Conversion presets: minimal").option("--wrap-width <n>", "Hard-wrap prose at <n> characters on word boundaries").option("--format <format>", "Output format: markdown, text, html").option("--text", "Alias for --format text").action(async (_, opts) => {