@mdream/js 2.0.2 → 2.1.0

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.
@@ -7,7 +7,8 @@ function createParseState(processor, options) {
7
7
  resolvedPlugins: options.plugins,
8
8
  tagHandlers: options.tagHandlers,
9
9
  tagOverrideHandlers: options.tagOverrideHandlers,
10
- plainText: options.plainText
10
+ plainText: options.plainText,
11
+ urlBaseTarget: processor.state
11
12
  };
12
13
  }
13
14
  function createEventHandler(processor, plugins) {
@@ -1,5 +1,5 @@
1
1
  import { NO_SPACING, TagIdMap, isInsideRawHtmlBlock } from "./const.mjs";
2
- import { decodeHTMLEntities, traverseUpToFirstBlockNode } from "./utils.mjs";
2
+ import { decodeHTMLEntities, resolveUrl, traverseUpToFirstBlockNode } from "./url.mjs";
3
3
  const selfClosing = { isSelfClosing: true };
4
4
  const rawText = { isNonNesting: true };
5
5
  const inertRawText = {
@@ -39,6 +39,22 @@ const parserTagHandlers = {
39
39
  [86]: rawText,
40
40
  [87]: rawText
41
41
  };
42
+ const HTTP_BASE_RE = /^https?:\/\/(?:[^/?#]*@)?(?![^/?#]*@)[^:/?#]/i;
43
+ const MAX_ELEMENT_DEPTH = 512;
44
+ function captureBaseHref(state, href) {
45
+ const target = state.urlBaseTarget;
46
+ if (!target || href === void 0 || state.depthMap[26] || state.depthMap[78] || state.depth >= MAX_ELEMENT_DEPTH) return;
47
+ state.baseHrefSeen = true;
48
+ let start = 0;
49
+ let end = href.length;
50
+ while (start < end && href.charCodeAt(start) <= 32) start++;
51
+ while (end > start && href.charCodeAt(end - 1) <= 32) end--;
52
+ const base = resolveUrl(href.slice(start, end), target.options.origin);
53
+ if (HTTP_BASE_RE.test(base)) target.options = {
54
+ ...target.options,
55
+ origin: base
56
+ };
57
+ }
42
58
  const UNKNOWN_TAG_HANDLER = {
43
59
  isInline: true,
44
60
  spacing: NO_SPACING
@@ -52,6 +68,7 @@ const APOS_CHAR = 39;
52
68
  const EXCLAMATION_CHAR = 33;
53
69
  const QUESTION_CHAR = 63;
54
70
  const AMPERSAND_CHAR = 38;
71
+ const HASH_CHAR = 35;
55
72
  const DASH_CHAR = 45;
56
73
  const SPACE_CHAR = 32;
57
74
  const TAB_CHAR = 9;
@@ -82,7 +99,6 @@ const SCRIPT_DATA_DOUBLE_ESCAPED_DASH_DASH = 6;
82
99
  const SCRIPT_SEQUENCE_NO_MATCH = -1;
83
100
  const SCRIPT_SEQUENCE_INCOMPLETE = -2;
84
101
  const SCRIPT_SCAN_COMPLETE = -1;
85
- const MAX_ELEMENT_DEPTH = 512;
86
102
  const HEAD_CONTENT_TAGS = /* @__PURE__ */ new Set([
87
103
  4,
88
104
  5,
@@ -528,6 +544,23 @@ function parseHtml(html, options = {}) {
528
544
  remainingHtml
529
545
  };
530
546
  }
547
+ function commitPlainTextPrefix(remainingHtml, state, onEvent) {
548
+ const text = state.trailingText;
549
+ if (!state.textBufferContainsWhitespace || remainingHtml.length < 65536 || !text || text.length < 65536 || state.currentNode?.tagId !== 35 && state.currentNode?.tagId !== 25 || state.hasEncodedHtmlEntity || state.flattenedTagName || state.depthMap[34] || state.depthMap[23] || state.depthMap[28] || isInsideRawHtmlBlock(state.depthMap)) return remainingHtml;
550
+ const boundary = text.lastIndexOf(" ") + 1;
551
+ if (!boundary) return remainingHtml;
552
+ for (let offset = 0; offset < remainingHtml.length; offset++) {
553
+ const code = remainingHtml.charCodeAt(offset);
554
+ if (code !== SPACE_CHAR && !isAsciiAlpha(code) && code !== 44) return remainingHtml;
555
+ }
556
+ const suffix = text.substring(boundary);
557
+ processTextBuffer(text.substring(0, boundary), state, onEvent);
558
+ state.trailingText = suffix;
559
+ state.textBufferContainsWhitespace = false;
560
+ state.textBufferContainsNonWhitespace = suffix.length > 0;
561
+ state.lastCharWasWhitespace = true;
562
+ return suffix;
563
+ }
531
564
  function parseHtmlStream(html, state, onEvent) {
532
565
  return parseHtmlInternal(html, state, onEvent);
533
566
  }
@@ -541,7 +574,9 @@ function parseHtmlInternal(htmlChunk, state, handleEvent) {
541
574
  state.justClosedTag ??= false;
542
575
  state.isFirstTextInElement ??= false;
543
576
  state.scriptDataState ??= SCRIPT_DATA;
577
+ state.listItemCounts ??= void 0;
544
578
  state.rawtextEndTagPending = false;
579
+ state.entitySeals = void 0;
545
580
  let i = 0;
546
581
  const chunkLength = htmlChunk.length;
547
582
  if (state.currentNode && isScriptElement(state.currentNode)) {
@@ -577,12 +612,7 @@ function parseHtmlInternal(htmlChunk, state, handleEvent) {
577
612
  state.lastCharWasWhitespace = false;
578
613
  state.justClosedTag = false;
579
614
  const textStart = i++;
580
- while (i < chunkLength) {
581
- const code = htmlChunk.charCodeAt(i);
582
- if (code === LT_CHAR || isWhitespace(code)) break;
583
- if (code === AMPERSAND_CHAR) state.hasEncodedHtmlEntity = true;
584
- i++;
585
- }
615
+ i = scanTextRun(htmlChunk, i, chunkLength, state);
586
616
  textBuffer += htmlChunk.substring(textStart, i);
587
617
  continue;
588
618
  }
@@ -615,10 +645,17 @@ function parseHtmlInternal(htmlChunk, state, handleEvent) {
615
645
  continue;
616
646
  }
617
647
  }
618
- if (nextCharCode === EXCLAMATION_CHAR) {
619
- const handler = state.currentNode?.tagHandler;
620
- const visibleRawText = state.flattenedRawTagName !== void 0 ? !state.flattenedRawExcludesText : handler?.isNonNesting && !handler.excludesTextNodes;
621
- const discardDeclaration = state.discardExcludedText && !visibleRawText;
648
+ if (state.currentNode?.tagHandler?.isNonNesting && nextCharCode !== SLASH_CHAR) {
649
+ state.textBufferContainsNonWhitespace = true;
650
+ state.lastCharWasWhitespace = false;
651
+ textBuffer += htmlChunk[i++];
652
+ continue;
653
+ }
654
+ if (nextCharCode === EXCLAMATION_CHAR && state.currentNode?.tagHandler?.isNonNesting) {
655
+ textBuffer += htmlChunk[i++];
656
+ continue;
657
+ } else if (nextCharCode === EXCLAMATION_CHAR) {
658
+ const discardDeclaration = state.discardExcludedText;
622
659
  if (htmlChunk.charCodeAt(i + 2) === OPEN_BRACKET_CHAR) {
623
660
  if (htmlChunk.startsWith("<![CDATA[", i)) {
624
661
  const end = htmlChunk.indexOf("]]>", i + 9);
@@ -649,26 +686,49 @@ function parseHtmlInternal(htmlChunk, state, handleEvent) {
649
686
  break;
650
687
  }
651
688
  }
652
- if (textBuffer.length > 0) {
653
- processTextBuffer(textBuffer, state, handleEvent);
654
- textBuffer = "";
655
- runStart = i;
689
+ const declaration = (htmlChunk.charCodeAt(i + 2) | 32) === 100 || i + 2 === chunkLength ? htmlChunk.substring(i, i + 9).toLowerCase() : "";
690
+ if (declaration && declaration.length < 9 && "<!doctype".startsWith(declaration)) {
691
+ declarationPending = true;
692
+ textBuffer += htmlChunk.substring(i);
693
+ break;
656
694
  }
695
+ if (htmlChunk.startsWith("<!--", i)) {
696
+ if (textBuffer.length > 0) {
697
+ processTextBuffer(textBuffer, state, handleEvent);
698
+ textBuffer = "";
699
+ }
700
+ runStart = i;
701
+ } else sealTextRun(textBuffer, state);
657
702
  const result = processCommentOrDoctype(htmlChunk, i);
658
703
  if (result.complete) {
659
704
  i = result.newPosition;
660
- runStart = i;
705
+ if (textBuffer.length === 0) runStart = i;
661
706
  } else {
662
707
  declarationPending = true;
663
708
  if (discardDeclaration && htmlChunk.length - i >= 4) {
664
709
  const boundary = htmlChunk.charCodeAt(i + 2) === DASH_CHAR && htmlChunk.charCodeAt(i + 3) === DASH_CHAR ? result.remainingText.length <= 7 ? result.remainingText : `<!--x${result.remainingText.slice(-3)}` : "<!x";
665
- state.trailingText = boundary;
666
- return boundary;
710
+ if (textBuffer.length > 0 && isWhitespace(htmlChunk.charCodeAt(runStart))) state.lastCharWasWhitespace = false;
711
+ state.trailingText = textBuffer;
712
+ return htmlChunk.substring(runStart, i) + boundary;
667
713
  }
668
714
  textBuffer += result.remainingText;
669
715
  break;
670
716
  }
717
+ } else if ((nextCharCode === QUESTION_CHAR || nextCharCode === SLASH_CHAR && i + 2 < chunkLength && htmlChunk.charCodeAt(i + 2) !== GT_CHAR && !isAsciiAlpha(htmlChunk.charCodeAt(i + 2))) && !state.currentNode?.tagHandler?.isNonNesting) {
718
+ sealTextRun(textBuffer, state);
719
+ const end = htmlChunk.indexOf(">", i + 2);
720
+ if (end === -1) {
721
+ if (textBuffer.length > 0 && isWhitespace(htmlChunk.charCodeAt(runStart))) state.lastCharWasWhitespace = false;
722
+ state.trailingText = textBuffer;
723
+ return `${htmlChunk.substring(runStart, i)}<!x`;
724
+ }
725
+ i = end + 1;
726
+ if (textBuffer.length === 0) runStart = i;
671
727
  } else if (nextCharCode === SLASH_CHAR) {
728
+ if (i + 2 === chunkLength) {
729
+ textBuffer += htmlChunk.substring(i);
730
+ break;
731
+ }
672
732
  if (state.currentNode?.tagHandler?.isNonNesting) {
673
733
  let peekEnd = i + 2;
674
734
  while (peekEnd < chunkLength) {
@@ -732,10 +792,6 @@ function parseHtmlInternal(htmlChunk, state, handleEvent) {
732
792
  const mappedTagId = TagIdMap[tagName];
733
793
  const tagId = effectiveTagId(tagName, typeof mappedTagId === "number" ? mappedTagId : -1, state);
734
794
  i2 = tagNameEnd;
735
- if (state.currentNode?.tagHandler?.isNonNesting) {
736
- textBuffer += htmlChunk[i++];
737
- continue;
738
- }
739
795
  if (!tagName) {
740
796
  state.textBufferContainsNonWhitespace = true;
741
797
  state.lastCharWasWhitespace = false;
@@ -802,12 +858,72 @@ function parseHtmlInternal(htmlChunk, state, handleEvent) {
802
858
  state.textBufferContainsWhitespace = false;
803
859
  state.textBufferContainsNonWhitespace = false;
804
860
  state.hasEncodedHtmlEntity = false;
861
+ state.entitySeals = void 0;
805
862
  }
806
863
  if (remainingHtml.length > 0 && isWhitespace(remainingHtml.charCodeAt(0))) state.lastCharWasWhitespace = false;
807
864
  state.trailingText = textBuffer;
808
865
  return remainingHtml;
809
866
  }
867
+ function scanTextRun(htmlChunk, i, chunkLength, state) {
868
+ while (i < chunkLength) {
869
+ const code = htmlChunk.charCodeAt(i);
870
+ if (code === LT_CHAR || isWhitespace(code)) break;
871
+ if (code === AMPERSAND_CHAR) state.hasEncodedHtmlEntity = true;
872
+ i++;
873
+ }
874
+ return i;
875
+ }
876
+ function sealTextRun(textBuffer, state) {
877
+ const at = textBuffer.length;
878
+ if (!state.hasEncodedHtmlEntity || at === 0) return;
879
+ const seals = state.entitySeals ??= [];
880
+ if (seals[seals.length - 1] !== at) seals.push(at);
881
+ }
882
+ function openReferenceStart(text, from, to) {
883
+ let amp = to - 1;
884
+ while (amp >= from && text.charCodeAt(amp) !== AMPERSAND_CHAR) amp--;
885
+ if (amp < from) return -1;
886
+ let k = amp + 1;
887
+ if (k < to && text.charCodeAt(k) === HASH_CHAR) {
888
+ k++;
889
+ const hex = k < to && (text.charCodeAt(k) | 32) === 120;
890
+ if (hex) k++;
891
+ for (; k < to; k++) {
892
+ const code = text.charCodeAt(k);
893
+ if (!(code >= 48 && code <= 57) && !(hex && (code | 32) >= 97 && (code | 32) <= 102)) return -1;
894
+ }
895
+ return amp;
896
+ }
897
+ for (; k < to; k++) {
898
+ const code = text.charCodeAt(k);
899
+ if (!(code >= 48 && code <= 57) && !isAsciiAlpha(code)) return -1;
900
+ }
901
+ return amp;
902
+ }
903
+ function endedReference(reference) {
904
+ if (reference.charCodeAt(1) === HASH_CHAR) {
905
+ const digitStart = (reference.charCodeAt(2) | 32) === 120 ? 3 : 2;
906
+ if (reference.length > digitStart) return `${reference};`;
907
+ }
908
+ const decoded = decodeHTMLEntities(reference, false, false);
909
+ return decoded === reference ? `&amp;${reference.slice(1)}` : decoded.replaceAll("&", "&amp;");
910
+ }
911
+ function endReferencesAtSeals(text, seals) {
912
+ let ended = "";
913
+ let from = 0;
914
+ for (let k = 0; k < seals.length; k++) {
915
+ const at = Math.min(seals[k], text.length);
916
+ const amp = openReferenceStart(text, from, at);
917
+ ended += amp === -1 ? text.slice(from, at) : text.slice(from, amp) + endedReference(text.slice(amp, at));
918
+ from = at;
919
+ }
920
+ return ended + text.slice(from);
921
+ }
810
922
  function processTextBuffer(textBuffer, state, handleEvent) {
923
+ if (state.entitySeals) {
924
+ textBuffer = endReferencesAtSeals(textBuffer, state.entitySeals);
925
+ state.entitySeals = void 0;
926
+ }
811
927
  const containsNonWhitespace = state.textBufferContainsNonWhitespace;
812
928
  const containsWhitespace = state.textBufferContainsWhitespace;
813
929
  state.textBufferContainsNonWhitespace = false;
@@ -1030,6 +1146,7 @@ function closeNode(node, state, handleEvent) {
1030
1146
  }
1031
1147
  const tagId = node.tagId;
1032
1148
  if (tagId !== void 0 && tagId >= 0 && tagId < 113) state.depthMap[tagId] = Math.max(0, (state.depthMap[tagId] || 0) - 1);
1149
+ if (tagId === 33 || tagId === 24) state.listItemCounts.pop();
1033
1150
  state.depth--;
1034
1151
  state.endTagClosedNothing = false;
1035
1152
  handleEvent({
@@ -1150,6 +1267,7 @@ function processCdataSection(content, state, handleEvent) {
1150
1267
  closeNode(state.currentNode ?? null, state, handleEvent);
1151
1268
  }
1152
1269
  function processOpeningTag(tagName, tagId, htmlChunk, i, state, handleEvent) {
1270
+ const isBase = tagId === 56;
1153
1271
  tagId = effectiveTagId(tagName, tagId, state);
1154
1272
  if (state.currentNode?.tagHandler?.isNonNesting) closeNode(state.currentNode, state, handleEvent);
1155
1273
  const tagHandler = state.tagOverrideHandlers?.get(tagName) ?? (tagId === -1 ? UNKNOWN_TAG_HANDLER : state.tagHandlers?.[tagId] ?? parserTagHandlers[tagId]);
@@ -1231,6 +1349,7 @@ function processOpeningTag(tagName, tagId, htmlChunk, i, state, handleEvent) {
1231
1349
  skip: true
1232
1350
  };
1233
1351
  }
1352
+ if (isBase && !state.baseHrefSeen) captureBaseHref(state, result.attributes.href);
1234
1353
  if (!result.selfClosing && state.depth >= MAX_ELEMENT_DEPTH) {
1235
1354
  state.flattenedTagName = tagName;
1236
1355
  state.flattenedTagId = tagId;
@@ -1252,8 +1371,10 @@ function processOpeningTag(tagName, tagId, htmlChunk, i, state, handleEvent) {
1252
1371
  state.depth++;
1253
1372
  i = result.newPosition;
1254
1373
  if (state.currentNode) state.currentNode.currentWalkIndex = state.currentNode.currentWalkIndex || 0;
1255
- const currentWalkIndex = state.currentNode ? state.currentNode.currentWalkIndex++ : 0;
1256
- const tag = new ParsedElementNode(tagName, result.attributes, state.currentNode, state.depth, currentWalkIndex, tagId, tagHandler);
1374
+ let index = state.currentNode ? state.currentNode.currentWalkIndex++ : 0;
1375
+ if (tagId === 33 || tagId === 24) (state.listItemCounts ||= []).push(0);
1376
+ else if (tagId === 25 && state.listItemCounts?.length && (state.currentNode?.tagId === 33 || state.currentNode?.tagId === 24)) index = state.listItemCounts[state.listItemCounts.length - 1]++;
1377
+ const tag = new ParsedElementNode(tagName, result.attributes, state.currentNode, state.depth, index, tagId, tagHandler);
1257
1378
  if (tagId === 78 || state.currentNode?.excludedFromMarkdown) tag.excludedFromMarkdown = true;
1258
1379
  state.lastTextNode = tag;
1259
1380
  state.endTagClosedNothing = false;
@@ -1381,9 +1502,9 @@ function scanTagAttributes(htmlChunk, position, tagHandler) {
1381
1502
  return scanAttributes(htmlChunk, position, tagHandler?.isSelfClosing || false, true);
1382
1503
  }
1383
1504
  function parseAttributes(attrStr) {
1384
- if (!attrStr) return EMPTY_ATTRIBUTES;
1505
+ if (!attrStr) return {};
1385
1506
  const result = scanAttributes(attrStr, 0, false, false);
1386
1507
  if (result._tag === "incomplete") throw new Error("Isolated attribute scans always complete at EOF");
1387
1508
  return result.attributes === EMPTY_ATTRIBUTES ? {} : result.attributes;
1388
1509
  }
1389
- export { finalizeParse, parseAttributes, parseHtml, parseHtmlStream, parserTagHandlers };
1510
+ export { commitPlainTextPrefix, finalizeParse, parseAttributes, parseHtml, parseHtmlStream, parserTagHandlers };
@@ -1,8 +1,8 @@
1
1
  import "./const.mjs";
2
- import { isInsideHeading, trimOutputStart } from "./utils.mjs";
2
+ import { isInsideHeading, noteBufferRewrite, trimOutputStart } from "./url.mjs";
3
3
  import { createMarkdownProcessor, isAsciiWhitespace, lineOpensRawHtmlBlock, stripBlockquoteListIndent, tagHandlers, trimAsciiWhitespaceEnd } from "./markdown-processor.mjs";
4
4
  import { assertEngineOptions, buildTagOverrideHandlers, checkClean, endPlugins, processPluginsForEvent } from "./tag-overrides.mjs";
5
- import { finalizeParse, parseHtmlStream } from "./parse.mjs";
5
+ import { commitPlainTextPrefix, finalizeParse, parseHtmlStream } from "./parse.mjs";
6
6
  import { resolvePlugins } from "./plugin.mjs";
7
7
  function resetBufferScanCursors(scan) {
8
8
  scan[1] = 0;
@@ -65,6 +65,7 @@ function flushBlockquoteLines(state) {
65
65
  frame.fragment = start + 1;
66
66
  frame.followsWhitespace = true;
67
67
  }
68
+ noteBufferRewrite(state);
68
69
  return true;
69
70
  }
70
71
  function prepareQuoteOutput(state, bufferScan, quoteScan) {
@@ -132,9 +133,10 @@ function createMarkdownDrain(context, _hasPlugins) {
132
133
  if (state.blockquotes.length === 0 || start <= 0 || state.outputPositions || context.getNonQuoteHeldOutputFragment() !== Infinity) return 0;
133
134
  const retained = content.slice(start);
134
135
  state.buffer.splice(0, fragment, retained);
136
+ noteBufferRewrite(state);
135
137
  for (const frame of state.blockquotes) frame.fragment -= fragment - 1;
136
138
  quoteScan = void 0;
137
- bufferScan[1] = 0;
139
+ bufferScan[1] = Math.max(0, bufferScan[1] - fragment + 1);
138
140
  bufferScan[2] = 1;
139
141
  bufferScan[3] = bufferScan[5] === void 0 ? 1 : 0;
140
142
  bufferScan[4] = 0;
@@ -160,9 +162,14 @@ function createMarkdownDrain(context, _hasPlugins) {
160
162
  retainMutableFragments = stableLength < currentContent.length;
161
163
  }
162
164
  } else {
165
+ let holdVerticalTabs;
163
166
  while (stableLength > 0) {
164
167
  const code = currentContent.charCodeAt(stableLength - 1);
165
- if (code !== 32 && code !== 10) break;
168
+ if (!isAsciiWhitespace(code)) {
169
+ if (code !== 11) break;
170
+ holdVerticalTabs ??= !final && !!(state.depthMap[16] || state.depthMap[17] || state.depthMap[14] || state.depthMap[15] || state.depthMap[90] || state.depthMap[91] || state.depthMap[18] || state.depthMap[110] || state.depthMap[111]);
171
+ if (!holdVerticalTabs) break;
172
+ }
166
173
  stableLength--;
167
174
  }
168
175
  retainMutableFragments = stableLength < currentContent.length;
@@ -204,10 +211,16 @@ function createMarkdownDrain(context, _hasPlugins) {
204
211
  const fragmentStart = currentContent.length - lastFragment.length;
205
212
  const tailStart = Math.max(0, Math.min(contextStart, fragmentStart));
206
213
  const emittedTail = currentContent.slice(tailStart, fragmentStart);
214
+ const stableTailLength = Math.max(0, Math.min(stableLength, fragmentStart) - tailStart);
215
+ const stableTail = emittedTail.slice(0, stableTailLength);
216
+ const mutableTail = emittedTail.slice(stableTailLength);
207
217
  state.buffer.length = 0;
208
218
  resetBufferScanCursors(bufferScan);
209
- if (emittedTail) state.buffer.push(emittedTail);
219
+ if (stableTail) state.buffer.push(stableTail);
220
+ bufferScan[1] = state.buffer.length;
221
+ if (mutableTail) state.buffer.push(mutableTail);
210
222
  state.buffer.push(lastFragment);
223
+ noteBufferRewrite(state);
211
224
  lastYieldedLength = stableLength - tailStart;
212
225
  } else if (!retainMutableFragments) {
213
226
  const tailStart = Math.max(0, contextStart);
@@ -215,8 +228,10 @@ function createMarkdownDrain(context, _hasPlugins) {
215
228
  state.buffer.length = 0;
216
229
  resetBufferScanCursors(bufferScan);
217
230
  if (emittedTail) state.buffer.push(emittedTail);
231
+ noteBufferRewrite(state);
218
232
  lastYieldedLength = emittedTail.length;
219
233
  }
234
+ if (!retainMutableFragments) bufferScan[1] = state.buffer.length;
220
235
  bufferScan[2] = state.buffer.length;
221
236
  if (bufferScan[5] === void 0) bufferScan[3] = state.buffer.length;
222
237
  }
@@ -236,9 +251,11 @@ async function* streamHtmlToMarkdown$1(htmlStream, options = {}, resolvedPlugins
236
251
  tagHandlers,
237
252
  tagOverrideHandlers,
238
253
  plainText: false,
254
+ urlBaseTarget: processor.state,
239
255
  discardExcludedText: resolvedPlugins.length === 0
240
256
  };
241
257
  const handleEvent = resolvedPlugins.length ? (event) => processPluginsForEvent(event, resolvedPlugins, processor.state, processor.processEvent) : processor.processEvent;
258
+ const incrementalPlainText = resolvedPlugins.length === 0 && !options.wrapWidth && !tagOverrideHandlers?.size;
242
259
  let remainingHtml = "";
243
260
  try {
244
261
  while (true) {
@@ -248,6 +265,7 @@ async function* streamHtmlToMarkdown$1(htmlStream, options = {}, resolvedPlugins
248
265
  const decoded = typeof value === "string" ? decoder.decode() + value : decoder.decode(value, { stream: true });
249
266
  const htmlContent = `${remainingHtml}${decoded}`;
250
267
  remainingHtml = parseHtmlStream(htmlContent, parseState, handleEvent);
268
+ if (incrementalPlainText) remainingHtml = commitPlainTextPrefix(remainingHtml, parseState, handleEvent);
251
269
  const chunk = processor.getMarkdownChunk();
252
270
  if (chunk) yield chunk;
253
271
  }
@@ -274,7 +292,8 @@ function htmlToMarkdown(html, options = {}) {
274
292
  resolvedPlugins: plugins,
275
293
  tagHandlers,
276
294
  tagOverrideHandlers,
277
- plainText: false
295
+ plainText: false,
296
+ urlBaseTarget: processor.state
278
297
  };
279
298
  const handleEvent = plugins.length ? (event) => processPluginsForEvent(event, plugins, processor.state, processor.processEvent) : processor.processEvent;
280
299
  const leftover = parseHtmlStream(html, parseState, handleEvent);
@@ -56,10 +56,17 @@ function processPluginsForEvent(event, plugins, state, processEvent) {
56
56
  if (plugins?.length) {
57
57
  if (event.node.type === 1 && event.type === 0) {
58
58
  const element = event.node;
59
+ let hasCheckedAttributes = false;
59
60
  for (let index = 0; index < plugins.length; index++) {
60
61
  const plugin = plugins[index];
61
62
  if (plugin === void 0) throw new TypeError("Plugin arrays must not contain undefined entries.");
62
- if (plugin.processAttributes) plugin.processAttributes(element, state);
63
+ if (plugin.processAttributes) {
64
+ if (!hasCheckedAttributes) {
65
+ hasCheckedAttributes = true;
66
+ if (!Object.isExtensible(element.attributes)) element.attributes = { ...element.attributes };
67
+ }
68
+ plugin.processAttributes(element, state);
69
+ }
63
70
  }
64
71
  }
65
72
  let shouldSkip = false;
@@ -124,11 +131,14 @@ function buildTagOverrideHandlers(overrides, tagHandlers) {
124
131
  }
125
132
  const baseId = TagIdMap[tagName];
126
133
  const baseHandler = baseId === void 0 ? void 0 : tagHandlers[baseId];
127
- const handler = baseHandler ? { ...baseHandler } : {};
134
+ const handler = baseHandler ? { ...baseHandler } : baseId === void 0 ? { isInline: true } : {};
128
135
  if (override.enter !== void 0) {
129
136
  const output = override.enter;
130
137
  handler.enter = () => output;
131
138
  handler.literalEnter = true;
139
+ let hashes = 0;
140
+ while (hashes < 7 && output.charCodeAt(hashes) === 35) hashes++;
141
+ handler.literalEnterHeading = hashes > 0 && hashes <= 6 && (output.charCodeAt(hashes) === 32 || output.charCodeAt(hashes) === 9);
132
142
  }
133
143
  if (override.exit !== void 0) {
134
144
  const output = override.exit;
@@ -13,21 +13,64 @@ function createIdSelector(selector) {
13
13
  toString: () => `#${id}`
14
14
  };
15
15
  }
16
+ function containsWhitespaceSeparatedToken(value, token) {
17
+ if (!token) return false;
18
+ let index = value.indexOf(token);
19
+ if (index === -1) return false;
20
+ for (let i = 0; i < token.length; i++) if (isCssWhitespace(token.charCodeAt(i))) return false;
21
+ while (index !== -1) {
22
+ const before = value.charCodeAt(index - 1);
23
+ if (index === 0 || isCssWhitespace(before)) {
24
+ const end = index + token.length;
25
+ const after = value.charCodeAt(end);
26
+ if (end === value.length || isCssWhitespace(after)) return true;
27
+ }
28
+ index = value.indexOf(token, index + 1);
29
+ }
30
+ return false;
31
+ }
16
32
  function createClassSelector(selector) {
17
33
  const className = selector.slice(1);
18
34
  return {
19
35
  matches: (element) => {
20
36
  if (!element.attributes?.class) return false;
21
- return element.attributes.class.trim().split(" ").filter(Boolean).includes(className);
37
+ return containsWhitespaceSeparatedToken(element.attributes.class, className);
22
38
  },
23
39
  toString: () => `.${className}`
24
40
  };
25
41
  }
42
+ function findAttributeEnd(selector, start) {
43
+ let quote = 0;
44
+ for (let i = start + 1; i < selector.length; i++) {
45
+ const char = selector.charCodeAt(i);
46
+ if (char === 92) {
47
+ i++;
48
+ continue;
49
+ }
50
+ if (quote) {
51
+ if (char === quote) quote = 0;
52
+ continue;
53
+ }
54
+ if (char === 34 || char === 39) quote = char;
55
+ else if (char === 93) return i;
56
+ }
57
+ return -1;
58
+ }
59
+ function isCssWhitespace(code) {
60
+ return code === 32 || code === 9 || code === 10 || code === 12 || code === 13;
61
+ }
62
+ function trimCssWhitespace(value) {
63
+ let start = 0;
64
+ let end = value.length;
65
+ while (start < end && isCssWhitespace(value.charCodeAt(start))) start++;
66
+ while (end > start && isCssWhitespace(value.charCodeAt(end - 1))) end--;
67
+ return value.slice(start, end);
68
+ }
26
69
  function parseAttributeSelector(selector) {
27
70
  if (selector.charCodeAt(0) !== 91) return null;
28
- const end = selector.indexOf("]", 1);
71
+ const end = findAttributeEnd(selector, 0);
29
72
  if (end === -1) return null;
30
- const inner = selector.slice(1, end);
73
+ const inner = trimCssWhitespace(selector.slice(1, end));
31
74
  let opIdx = -1;
32
75
  for (let i = 0; i < inner.length; i++) {
33
76
  const c = inner.charCodeAt(i);
@@ -37,12 +80,12 @@ function parseAttributeSelector(selector) {
37
80
  }
38
81
  }
39
82
  if (opIdx === -1) return { attr: inner };
40
- const attr = inner.slice(0, opIdx);
83
+ const attr = trimCssWhitespace(inner.slice(0, opIdx));
41
84
  let opEnd = opIdx + 1;
42
85
  if (opEnd < inner.length && inner.charCodeAt(opEnd) === 61) opEnd++;
43
86
  const op = inner.slice(opIdx, opEnd);
44
- let value = inner.slice(opEnd);
45
- if ((value.charCodeAt(0) === 34 || value.charCodeAt(0) === 39) && value.charCodeAt(value.length - 1) === value.charCodeAt(0)) value = value.slice(1, -1);
87
+ let value = trimCssWhitespace(inner.slice(opEnd));
88
+ if (value.length >= 2 && (value.charCodeAt(0) === 34 || value.charCodeAt(0) === 39) && value.charCodeAt(value.length - 1) === value.charCodeAt(0)) value = value.slice(1, -1);
46
89
  return {
47
90
  attr,
48
91
  op,
@@ -57,21 +100,21 @@ function createAttributeSelector(selector) {
57
100
  return {
58
101
  matches: (element) => {
59
102
  if (!(attrName in (element.attributes || {}))) return false;
60
- if (!operator || !attrValue) return true;
103
+ if (!operator || attrValue === void 0) return true;
61
104
  const value = element.attributes[attrName];
62
105
  if (value === void 0) return false;
63
106
  switch (operator) {
64
107
  case "=": return value === attrValue;
65
- case "^=": return value.startsWith(attrValue);
66
- case "$=": return value.endsWith(attrValue);
67
- case "*=": return value.includes(attrValue);
68
- case "~=": return value.trim().split(" ").filter(Boolean).includes(attrValue);
108
+ case "^=": return attrValue.length > 0 && value.startsWith(attrValue);
109
+ case "$=": return attrValue.length > 0 && value.endsWith(attrValue);
110
+ case "*=": return attrValue.length > 0 && value.includes(attrValue);
111
+ case "~=": return containsWhitespaceSeparatedToken(value, attrValue);
69
112
  case "|=": return value === attrValue || value.startsWith(`${attrValue}-`);
70
113
  default: return false;
71
114
  }
72
115
  },
73
116
  toString: () => {
74
- if (!operator || !attrValue) return `[${attrName}]`;
117
+ if (!operator || attrValue === void 0) return `[${attrName}]`;
75
118
  return `[${attrName}${operator}${attrValue}]`;
76
119
  }
77
120
  };
@@ -94,7 +137,6 @@ function createSelectorList(selectors) {
94
137
  function parseCompoundSelector(selector) {
95
138
  const selectorParts = [];
96
139
  let current = "";
97
- let inAttribute = false;
98
140
  for (let i = 0; i < selector.length; i++) {
99
141
  const char = selector[i];
100
142
  if ((char === "." || char === "#" || char === "[") && current) {
@@ -104,9 +146,12 @@ function parseCompoundSelector(selector) {
104
146
  else if (current) selectorParts.push(createTagSelector(current));
105
147
  current = char;
106
148
  } else current += char;
107
- if (char === "[") inAttribute = true;
108
- if (char === "]") inAttribute = false;
109
- if (inAttribute && char !== "[") {}
149
+ if (char === "[") {
150
+ const end = findAttributeEnd(selector, i);
151
+ if (end === -1) return createTagSelector(selector);
152
+ current += selector.slice(i + 1, end + 1);
153
+ i = end;
154
+ }
110
155
  }
111
156
  if (current) {
112
157
  if (current[0] === ".") selectorParts.push(createClassSelector(current));
@@ -122,24 +167,13 @@ function parseSelector(selector) {
122
167
  if (!selector) throw new Error("Empty selector");
123
168
  const selectors = [];
124
169
  let start = 0;
125
- let inAttribute = false;
126
- let quote = 0;
127
- let escaped = false;
128
170
  for (let i = 0; i < selector.length; i++) {
129
171
  const char = selector.charCodeAt(i);
130
- if (quote) {
131
- if (escaped) escaped = false;
132
- else if (char === 92) escaped = true;
133
- else if (char === quote) quote = 0;
134
- continue;
135
- }
136
- if (inAttribute) {
137
- if (char === 34 || char === 39) quote = char;
138
- else if (char === 93) inAttribute = false;
139
- continue;
140
- }
141
- if (char === 91) inAttribute = true;
142
- else if (char === 44) {
172
+ if (char === 91) {
173
+ const end = findAttributeEnd(selector, i);
174
+ if (end === -1) break;
175
+ i = end;
176
+ } else if (char === 44) {
143
177
  const part = selector.slice(start, i).trim();
144
178
  if (!part) return createTagSelector(selector);
145
179
  selectors.push(parseCompoundSelector(part));
@@ -478,10 +512,10 @@ function processTailwindClasses(classAttr) {
478
512
  weight = supersede(weight, breakpoint, true);
479
513
  break;
480
514
  case "line-through":
481
- case "underline":
482
515
  decoration = supersede(decoration, breakpoint, true);
483
516
  break;
484
517
  case "no-underline":
518
+ case "underline":
485
519
  decoration = supersede(decoration, breakpoint, false);
486
520
  break;
487
521
  case "hidden":