@mdream/js 2.1.0 → 2.2.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.
- package/dist/_chunks/clean.mjs +115 -19
- package/dist/_chunks/html.mjs +1 -2
- package/dist/_chunks/markdown-processor.mjs +883 -168
- package/dist/_chunks/output-runner.mjs +1 -0
- package/dist/_chunks/parse.mjs +85 -56
- package/dist/_chunks/src.mjs +27 -12
- package/dist/_chunks/tag-overrides.mjs +9 -3
- package/dist/_chunks/tailwind.mjs +3 -2
- package/dist/_chunks/text.mjs +123 -19
- package/dist/_chunks/types.d.mts +45 -5
- package/dist/_chunks/url.mjs +201 -260
- package/dist/parse.d.mts +6 -0
- package/dist/splitter.mjs +1 -1
- package/package.json +1 -1
|
@@ -8,6 +8,7 @@ function createParseState(processor, options) {
|
|
|
8
8
|
tagHandlers: options.tagHandlers,
|
|
9
9
|
tagOverrideHandlers: options.tagOverrideHandlers,
|
|
10
10
|
plainText: options.plainText,
|
|
11
|
+
keepLeadingBlanks: processor.state.outputFormat === "text",
|
|
11
12
|
urlBaseTarget: processor.state
|
|
12
13
|
};
|
|
13
14
|
}
|
package/dist/_chunks/parse.mjs
CHANGED
|
@@ -1,56 +1,14 @@
|
|
|
1
1
|
import { NO_SPACING, TagIdMap, isInsideRawHtmlBlock } from "./const.mjs";
|
|
2
|
-
import { decodeHTMLEntities, resolveUrl, traverseUpToFirstBlockNode } from "./url.mjs";
|
|
3
|
-
const selfClosing = { isSelfClosing: true };
|
|
4
|
-
const rawText = { isNonNesting: true };
|
|
5
|
-
const inertRawText = {
|
|
6
|
-
isNonNesting: true,
|
|
7
|
-
excludesTextNodes: true
|
|
8
|
-
};
|
|
9
|
-
const parserTagHandlers = {
|
|
10
|
-
[4]: inertRawText,
|
|
11
|
-
[52]: inertRawText,
|
|
12
|
-
[53]: inertRawText,
|
|
13
|
-
[5]: selfClosing,
|
|
14
|
-
[6]: selfClosing,
|
|
15
|
-
[13]: selfClosing,
|
|
16
|
-
[27]: selfClosing,
|
|
17
|
-
[29]: { excludesTextNodes: true },
|
|
18
|
-
[30]: { excludesTextNodes: true },
|
|
19
|
-
[38]: { excludesTextNodes: true },
|
|
20
|
-
[39]: { excludesTextNodes: true },
|
|
21
|
-
[54]: selfClosing,
|
|
22
|
-
[55]: selfClosing,
|
|
23
|
-
[56]: selfClosing,
|
|
24
|
-
[57]: selfClosing,
|
|
25
|
-
[58]: selfClosing,
|
|
26
|
-
[59]: selfClosing,
|
|
27
|
-
[60]: selfClosing,
|
|
28
|
-
[61]: selfClosing,
|
|
29
|
-
[62]: selfClosing,
|
|
30
|
-
[63]: selfClosing,
|
|
31
|
-
[64]: selfClosing,
|
|
32
|
-
[66]: rawText,
|
|
33
|
-
[73]: inertRawText,
|
|
34
|
-
[78]: { excludesTextNodes: true },
|
|
35
|
-
[84]: inertRawText,
|
|
36
|
-
[112]: inertRawText,
|
|
37
|
-
[108]: inertRawText,
|
|
38
|
-
[85]: inertRawText,
|
|
39
|
-
[86]: rawText,
|
|
40
|
-
[87]: rawText
|
|
41
|
-
};
|
|
2
|
+
import { decodeHTMLEntities, hasNonWhitespace, leadingWhitespaceLength, parserTagHandlers, resolveUrl, traverseUpToFirstBlockNode } from "./url.mjs";
|
|
42
3
|
const HTTP_BASE_RE = /^https?:\/\/(?:[^/?#]*@)?(?![^/?#]*@)[^:/?#]/i;
|
|
4
|
+
const MAX_BASE_HREF_LENGTH = 2048;
|
|
43
5
|
const MAX_ELEMENT_DEPTH = 512;
|
|
44
6
|
function captureBaseHref(state, href) {
|
|
45
7
|
const target = state.urlBaseTarget;
|
|
46
8
|
if (!target || href === void 0 || state.depthMap[26] || state.depthMap[78] || state.depth >= MAX_ELEMENT_DEPTH) return;
|
|
47
9
|
state.baseHrefSeen = true;
|
|
48
|
-
|
|
49
|
-
|
|
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 = {
|
|
10
|
+
const base = resolveUrl(href, target.options.origin);
|
|
11
|
+
if (HTTP_BASE_RE.test(base) && base.length <= MAX_BASE_HREF_LENGTH && new TextEncoder().encode(base).length <= MAX_BASE_HREF_LENGTH) target.options = {
|
|
54
12
|
...target.options,
|
|
55
13
|
origin: base
|
|
56
14
|
};
|
|
@@ -89,6 +47,9 @@ function shouldProtectDecodedEntityReferences(state) {
|
|
|
89
47
|
const depthMap = state.depthMap;
|
|
90
48
|
return !state.plainText && !depthMap[4] && !depthMap[34] && !depthMap[23] && !isInsideRawHtmlBlock(depthMap);
|
|
91
49
|
}
|
|
50
|
+
function keepsLeadingBlank(node) {
|
|
51
|
+
return node.tagHandler?.isInline ?? node.tagId === -1;
|
|
52
|
+
}
|
|
92
53
|
const SCRIPT_DATA = 0;
|
|
93
54
|
const SCRIPT_DATA_ESCAPED = 1;
|
|
94
55
|
const SCRIPT_DATA_ESCAPED_DASH = 2;
|
|
@@ -370,6 +331,7 @@ function finalizeParse(leftover, state, handleEvent) {
|
|
|
370
331
|
let text = trailing;
|
|
371
332
|
if (state.currentNode?.tagHandler?.isNonNesting) {
|
|
372
333
|
if (state.rawtextEndTagPending) text = "";
|
|
334
|
+
else if (!(state.discardExcludedText && state.currentNode.tagHandler.excludesTextNodes) && hasNonWhitespace(text)) state.textBufferContainsNonWhitespace = true;
|
|
373
335
|
} else {
|
|
374
336
|
const incompleteTagIndex = findIncompleteTagResidualIndex(trailing);
|
|
375
337
|
if (incompleteTagIndex !== -1) text = trailing.slice(0, incompleteTagIndex);
|
|
@@ -433,6 +395,11 @@ function scriptSequenceEnd(html, nameStart) {
|
|
|
433
395
|
const delimiter = html.charCodeAt(delimiterIndex);
|
|
434
396
|
return delimiter === GT_CHAR || delimiter === SLASH_CHAR || isWhitespace(delimiter) ? delimiterIndex + 1 : SCRIPT_SEQUENCE_NO_MATCH;
|
|
435
397
|
}
|
|
398
|
+
function inInertRawText(state) {
|
|
399
|
+
const node = state.currentNode;
|
|
400
|
+
const handler = node?.tagHandler;
|
|
401
|
+
return !!handler?.isNonNesting && !!handler.excludesTextNodes && node.tagId !== 4 && !isScriptElement(node);
|
|
402
|
+
}
|
|
436
403
|
function isScriptElement(node) {
|
|
437
404
|
return node.tagId === 52 && node.name === "script";
|
|
438
405
|
}
|
|
@@ -498,7 +465,14 @@ function pushScriptTextChunk(state, text) {
|
|
|
498
465
|
}
|
|
499
466
|
function scanScriptChunk(html, start, state) {
|
|
500
467
|
const result = findScriptEndTag(html, start, state);
|
|
501
|
-
if (state.discardExcludedText && state.currentNode?.tagHandler?.excludesTextNodes)
|
|
468
|
+
if (state.discardExcludedText && state.currentNode?.tagHandler?.excludesTextNodes) {
|
|
469
|
+
if ((result === SCRIPT_SCAN_COMPLETE ? html.length : result <= SCRIPT_SEQUENCE_INCOMPLETE ? -result - 2 : result) > start) {
|
|
470
|
+
state.lastCharWasWhitespace = false;
|
|
471
|
+
state.justClosedTag = false;
|
|
472
|
+
state.textBufferContainsWhitespace = false;
|
|
473
|
+
}
|
|
474
|
+
return result;
|
|
475
|
+
}
|
|
502
476
|
if (result === SCRIPT_SCAN_COMPLETE) pushScriptTextChunk(state, html.substring(start));
|
|
503
477
|
else if (result <= SCRIPT_SEQUENCE_INCOMPLETE) pushScriptTextChunk(state, html.substring(start, -result - 2));
|
|
504
478
|
else pushScriptTextChunk(state, html.substring(start, result));
|
|
@@ -737,8 +711,10 @@ function parseHtmlInternal(htmlChunk, state, handleEvent) {
|
|
|
737
711
|
peekEnd++;
|
|
738
712
|
}
|
|
739
713
|
if (peekEnd === chunkLength) {
|
|
740
|
-
state.
|
|
741
|
-
|
|
714
|
+
if (!state.currentNode.name.startsWith(normalizeTagName(htmlChunk.substring(i + 2, peekEnd)))) {
|
|
715
|
+
state.textBufferContainsNonWhitespace = true;
|
|
716
|
+
state.lastCharWasWhitespace = false;
|
|
717
|
+
}
|
|
742
718
|
textBuffer += htmlChunk.substring(i);
|
|
743
719
|
break;
|
|
744
720
|
}
|
|
@@ -749,6 +725,10 @@ function parseHtmlInternal(htmlChunk, state, handleEvent) {
|
|
|
749
725
|
textBuffer += htmlChunk[i++];
|
|
750
726
|
continue;
|
|
751
727
|
}
|
|
728
|
+
if (inInertRawText(state)) {
|
|
729
|
+
if (state.inertBodyBlankPending || i > 0 && isWhitespace(htmlChunk.charCodeAt(i - 1))) state.lastCharWasWhitespace = false;
|
|
730
|
+
state.inertBodyBlankPending = false;
|
|
731
|
+
}
|
|
752
732
|
}
|
|
753
733
|
if (textBuffer.length > 0) {
|
|
754
734
|
processTextBuffer(textBuffer, state, handleEvent);
|
|
@@ -861,6 +841,10 @@ function parseHtmlInternal(htmlChunk, state, handleEvent) {
|
|
|
861
841
|
state.entitySeals = void 0;
|
|
862
842
|
}
|
|
863
843
|
if (remainingHtml.length > 0 && isWhitespace(remainingHtml.charCodeAt(0))) state.lastCharWasWhitespace = false;
|
|
844
|
+
if (inInertRawText(state)) {
|
|
845
|
+
const last = chunkLength - remainingHtml.length - 1;
|
|
846
|
+
if (last >= 0 && isWhitespace(htmlChunk.charCodeAt(last))) state.inertBodyBlankPending = true;
|
|
847
|
+
}
|
|
864
848
|
state.trailingText = textBuffer;
|
|
865
849
|
return remainingHtml;
|
|
866
850
|
}
|
|
@@ -931,6 +915,7 @@ function processTextBuffer(textBuffer, state, handleEvent) {
|
|
|
931
915
|
if (!state.currentNode) {
|
|
932
916
|
let rootText = textBuffer;
|
|
933
917
|
if (rootText.length === 0) return;
|
|
918
|
+
const sourceLead = containsWhitespace ? leadingWhitespaceLength(rootText) : 0;
|
|
934
919
|
if (state.hasEncodedHtmlEntity) {
|
|
935
920
|
rootText = decodeHTMLEntities(String(rootText), false, shouldProtectDecodedEntityReferences(state));
|
|
936
921
|
state.hasEncodedHtmlEntity = false;
|
|
@@ -945,7 +930,10 @@ function processTextBuffer(textBuffer, state, handleEvent) {
|
|
|
945
930
|
excludedFromMarkdown: false
|
|
946
931
|
};
|
|
947
932
|
if (state.endTagClosedNothing) rootTextNode.joinsPrevious = true;
|
|
948
|
-
if (
|
|
933
|
+
if (sourceLead !== 0) {
|
|
934
|
+
rootTextNode.trimsAtLineStart = true;
|
|
935
|
+
rootTextNode.sourceLead = sourceLead;
|
|
936
|
+
}
|
|
949
937
|
state.endTagClosedNothing = false;
|
|
950
938
|
handleEvent({
|
|
951
939
|
type: 0,
|
|
@@ -956,10 +944,18 @@ function processTextBuffer(textBuffer, state, handleEvent) {
|
|
|
956
944
|
}
|
|
957
945
|
const excludesTextNodes = (state.flattenedOpaqueDepth || 0) > 0 || state.flattenedRawExcludesText || state.currentNode?.tagHandler?.excludesTextNodes || state.currentNode?.excludedFromMarkdown;
|
|
958
946
|
const inPreTag = (state.depthMap[34] || 0) > 0;
|
|
959
|
-
|
|
947
|
+
const isLeadingBlank = !inPreTag && !containsNonWhitespace && !state.currentNode.childTextNodeIndex;
|
|
948
|
+
if (isLeadingBlank && (state.plainText && !state.keepLeadingBlanks || !keepsLeadingBlank(state.currentNode))) return;
|
|
960
949
|
let text = textBuffer;
|
|
961
950
|
if (text.length === 0) return;
|
|
962
|
-
|
|
951
|
+
let parentsToIncrement;
|
|
952
|
+
if (isLeadingBlank && !state.resolvedPlugins?.length) {
|
|
953
|
+
if (state.leadingBlankParent !== state.currentNode) {
|
|
954
|
+
state.leadingBlankParent = state.currentNode;
|
|
955
|
+
state.leadingBlankAncestors = traverseUpToFirstBlockNode(state.currentNode);
|
|
956
|
+
}
|
|
957
|
+
parentsToIncrement = state.leadingBlankAncestors;
|
|
958
|
+
} else parentsToIncrement = traverseUpToFirstBlockNode(state.currentNode);
|
|
963
959
|
const firstBlockParent = parentsToIncrement.at(-1);
|
|
964
960
|
const firstInBlock = containsWhitespace && !firstBlockParent.childTextNodeIndex;
|
|
965
961
|
const inlineAtRoot = firstInBlock && !firstBlockParent.parent && (firstBlockParent.tagHandler?.isInline ?? firstBlockParent.tagId === -1);
|
|
@@ -968,6 +964,30 @@ function processTextBuffer(textBuffer, state, handleEvent) {
|
|
|
968
964
|
while (start < text.length && (inPreTag ? text.charCodeAt(start) === NEWLINE_CHAR || text.charCodeAt(start) === CARRIAGE_RETURN_CHAR : isWhitespace(text.charCodeAt(start)))) start++;
|
|
969
965
|
if (start > 0) text = text.substring(start);
|
|
970
966
|
}
|
|
967
|
+
if (isLeadingBlank) {
|
|
968
|
+
if (text) {
|
|
969
|
+
const blank = {
|
|
970
|
+
type: 2,
|
|
971
|
+
value: text,
|
|
972
|
+
parent: state.currentNode,
|
|
973
|
+
index: state.currentNode.currentWalkIndex,
|
|
974
|
+
depth: state.depth,
|
|
975
|
+
containsWhitespace,
|
|
976
|
+
excludedFromMarkdown: excludesTextNodes,
|
|
977
|
+
leadingBlank: true
|
|
978
|
+
};
|
|
979
|
+
handleEvent({
|
|
980
|
+
type: 0,
|
|
981
|
+
node: blank
|
|
982
|
+
});
|
|
983
|
+
if (!blank.excludedFromMarkdown && hasNonWhitespace(blank.value)) {
|
|
984
|
+
state.currentNode.currentWalkIndex++;
|
|
985
|
+
for (const parent of parentsToIncrement) parent.childTextNodeIndex = (parent.childTextNodeIndex || 0) + 1;
|
|
986
|
+
}
|
|
987
|
+
}
|
|
988
|
+
return;
|
|
989
|
+
}
|
|
990
|
+
const sourceLead = containsWhitespace && !inPreTag ? leadingWhitespaceLength(text) : 0;
|
|
971
991
|
if (state.hasEncodedHtmlEntity) {
|
|
972
992
|
text = decodeHTMLEntities(String(text), false, shouldProtectDecodedEntityReferences(state));
|
|
973
993
|
state.hasEncodedHtmlEntity = false;
|
|
@@ -983,6 +1003,7 @@ function processTextBuffer(textBuffer, state, handleEvent) {
|
|
|
983
1003
|
};
|
|
984
1004
|
if (state.endTagClosedNothing) textNode.joinsPrevious = true;
|
|
985
1005
|
if (inlineAtRoot) textNode.trimsAtLineStart = true;
|
|
1006
|
+
if (sourceLead !== 0) textNode.sourceLead = sourceLead;
|
|
986
1007
|
state.endTagClosedNothing = false;
|
|
987
1008
|
for (const parent of parentsToIncrement) parent.childTextNodeIndex = (parent.childTextNodeIndex || 0) + 1;
|
|
988
1009
|
handleEvent({
|
|
@@ -1322,7 +1343,7 @@ function processOpeningTag(tagName, tagId, htmlChunk, i, state, handleEvent) {
|
|
|
1322
1343
|
if (parserTagDepth(state, 30) > 0) closeTableContext(state, TR_CELLS, handleEvent);
|
|
1323
1344
|
} else closeTableContext(state, SECTION_CELLS, handleEvent);
|
|
1324
1345
|
}
|
|
1325
|
-
} else {
|
|
1346
|
+
} else if (CLOSES_P[tagId] === 1) {
|
|
1326
1347
|
if (parserTagDepth(state, 35) > 0) closeImpliedTo(state, SINGLE_P, P_SCOPE_BOUNDARY, handleEvent);
|
|
1327
1348
|
if (HEADINGS.has(tagId)) {
|
|
1328
1349
|
const topId = parserLastTagId(state);
|
|
@@ -1418,13 +1439,21 @@ function scanAttributes(source, position, selfClosing, stopAtTagEnd) {
|
|
|
1418
1439
|
let i = position;
|
|
1419
1440
|
while (i < length) {
|
|
1420
1441
|
const charCode = source.charCodeAt(i);
|
|
1421
|
-
if (
|
|
1422
|
-
if (
|
|
1442
|
+
if (charCode === SLASH_CHAR && state <= ATTRIBUTE_AFTER_NAME) {
|
|
1443
|
+
if (stopAtTagEnd && i + 1 < length && source.charCodeAt(i + 1) === GT_CHAR) {
|
|
1423
1444
|
selfClosing = true;
|
|
1424
1445
|
valueEnd = i;
|
|
1425
1446
|
newPosition = i + 2;
|
|
1426
1447
|
break;
|
|
1427
1448
|
}
|
|
1449
|
+
if (state !== ATTRIBUTE_GAP) {
|
|
1450
|
+
attributes = setAttribute(attributes, source.substring(nameStart, state === ATTRIBUTE_NAME ? i : nameEnd), "");
|
|
1451
|
+
state = ATTRIBUTE_GAP;
|
|
1452
|
+
}
|
|
1453
|
+
i++;
|
|
1454
|
+
continue;
|
|
1455
|
+
}
|
|
1456
|
+
if (stopAtTagEnd && state !== ATTRIBUTE_QUOTED_VALUE) {
|
|
1428
1457
|
if (charCode === GT_CHAR) {
|
|
1429
1458
|
valueEnd = i;
|
|
1430
1459
|
newPosition = i + 1;
|
|
@@ -1507,4 +1536,4 @@ function parseAttributes(attrStr) {
|
|
|
1507
1536
|
if (result._tag === "incomplete") throw new Error("Isolated attribute scans always complete at EOF");
|
|
1508
1537
|
return result.attributes === EMPTY_ATTRIBUTES ? {} : result.attributes;
|
|
1509
1538
|
}
|
|
1510
|
-
export { commitPlainTextPrefix, finalizeParse, parseAttributes, parseHtml, parseHtmlStream
|
|
1539
|
+
export { commitPlainTextPrefix, finalizeParse, parseAttributes, parseHtml, parseHtmlStream };
|
package/dist/_chunks/src.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import "./const.mjs";
|
|
2
|
-
import { isInsideHeading, noteBufferRewrite, trimOutputStart } from "./url.mjs";
|
|
2
|
+
import { isInsideHeading, noteBufferRewrite, outputLeadLength, 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
5
|
import { commitPlainTextPrefix, finalizeParse, parseHtmlStream } from "./parse.mjs";
|
|
@@ -63,6 +63,7 @@ function flushBlockquoteLines(state) {
|
|
|
63
63
|
for (const fragment of tail) buffer.push(fragment);
|
|
64
64
|
for (const frame of frames) {
|
|
65
65
|
frame.fragment = start + 1;
|
|
66
|
+
frame.itemSeparatorFragments = 0;
|
|
66
67
|
frame.followsWhitespace = true;
|
|
67
68
|
}
|
|
68
69
|
noteBufferRewrite(state);
|
|
@@ -129,12 +130,14 @@ function createMarkdownDrain(context, _hasPlugins) {
|
|
|
129
130
|
const blankLine = content.indexOf("\n\n", Math.max(0, start - 1));
|
|
130
131
|
if (blankLine !== -1 && blankLine + 2 <= end) bufferScan[0] = true;
|
|
131
132
|
}
|
|
132
|
-
function
|
|
133
|
-
if (
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
133
|
+
function compactHeldPrefix(fragment, content, start) {
|
|
134
|
+
if (start <= 0) return 0;
|
|
135
|
+
if (state.blockquotes.length > 0) {
|
|
136
|
+
if (state.outputPositions || context.getNonQuoteHeldOutputFragment() !== Infinity) return 0;
|
|
137
|
+
state.buffer.splice(0, fragment, content.slice(start));
|
|
138
|
+
noteBufferRewrite(state);
|
|
139
|
+
for (const frame of state.blockquotes) frame.fragment -= fragment - 1;
|
|
140
|
+
} else if (!context.compactPrefix(fragment, content.slice(start))) return 0;
|
|
138
141
|
quoteScan = void 0;
|
|
139
142
|
bufferScan[1] = Math.max(0, bufferScan[1] - fragment + 1);
|
|
140
143
|
bufferScan[2] = 1;
|
|
@@ -142,6 +145,9 @@ function createMarkdownDrain(context, _hasPlugins) {
|
|
|
142
145
|
bufferScan[4] = 0;
|
|
143
146
|
return start;
|
|
144
147
|
}
|
|
148
|
+
function noteDrained(content, end) {
|
|
149
|
+
if (!state.drainedContent && end > outputLeadLength(content)) state.drainedContent = true;
|
|
150
|
+
}
|
|
145
151
|
function getMarkdownChunk(final = false) {
|
|
146
152
|
if (context.holdsOutput) return final ? context.getMarkdown() : "";
|
|
147
153
|
const heldFragment = prepareDrain(final);
|
|
@@ -192,11 +198,12 @@ function createMarkdownDrain(context, _hasPlugins) {
|
|
|
192
198
|
context.markYielded();
|
|
193
199
|
}
|
|
194
200
|
const wrapping = !!options.wrapWidth;
|
|
195
|
-
if (hasYieldedContent && fragmentHeld &&
|
|
201
|
+
if (hasYieldedContent && fragmentHeld && !headingHeld && !inPre && (!wrapping || leadingTrimmed === 0)) {
|
|
196
202
|
observeStableOutput(content, stableLength + leadingTrimmed);
|
|
197
203
|
let retainedStart = stableLength + leadingTrimmed - 17;
|
|
198
204
|
if (wrapping) retainedStart = Math.min(retainedStart, content.lastIndexOf("\n", stableLength + leadingTrimmed - 1));
|
|
199
|
-
const removed =
|
|
205
|
+
const removed = compactHeldPrefix(heldFragment, content, Math.max(0, retainedStart));
|
|
206
|
+
noteDrained(content, removed);
|
|
200
207
|
lastYieldedLength -= removed;
|
|
201
208
|
}
|
|
202
209
|
if (hasYieldedContent && !fragmentHeld && !headingHeld && (!retainMutableFragments || !inPre) && (!wrapping || leadingTrimmed === 0)) {
|
|
@@ -214,6 +221,7 @@ function createMarkdownDrain(context, _hasPlugins) {
|
|
|
214
221
|
const stableTailLength = Math.max(0, Math.min(stableLength, fragmentStart) - tailStart);
|
|
215
222
|
const stableTail = emittedTail.slice(0, stableTailLength);
|
|
216
223
|
const mutableTail = emittedTail.slice(stableTailLength);
|
|
224
|
+
noteDrained(currentContent, tailStart);
|
|
217
225
|
state.buffer.length = 0;
|
|
218
226
|
resetBufferScanCursors(bufferScan);
|
|
219
227
|
if (stableTail) state.buffer.push(stableTail);
|
|
@@ -225,6 +233,7 @@ function createMarkdownDrain(context, _hasPlugins) {
|
|
|
225
233
|
} else if (!retainMutableFragments) {
|
|
226
234
|
const tailStart = Math.max(0, contextStart);
|
|
227
235
|
const emittedTail = currentContent.slice(tailStart, stableLength);
|
|
236
|
+
noteDrained(currentContent, tailStart);
|
|
228
237
|
state.buffer.length = 0;
|
|
229
238
|
resetBufferScanCursors(bufferScan);
|
|
230
239
|
if (emittedTail) state.buffer.push(emittedTail);
|
|
@@ -237,7 +246,13 @@ function createMarkdownDrain(context, _hasPlugins) {
|
|
|
237
246
|
}
|
|
238
247
|
return newContent;
|
|
239
248
|
}
|
|
240
|
-
|
|
249
|
+
function drainKeepingDroppedPair(final = false) {
|
|
250
|
+
const pending = state.droppedPairAt === state.buffer.length;
|
|
251
|
+
const chunk = getMarkdownChunk(final);
|
|
252
|
+
state.droppedPairAt = pending ? state.buffer.length : void 0;
|
|
253
|
+
return chunk;
|
|
254
|
+
}
|
|
255
|
+
return { getMarkdownChunk: drainKeepingDroppedPair };
|
|
241
256
|
}
|
|
242
257
|
async function* streamHtmlToMarkdown$1(htmlStream, options = {}, resolvedPlugins = [], tagOverrideHandlers) {
|
|
243
258
|
if (!htmlStream) throw new Error("Invalid HTML stream provided");
|
|
@@ -254,7 +269,7 @@ async function* streamHtmlToMarkdown$1(htmlStream, options = {}, resolvedPlugins
|
|
|
254
269
|
urlBaseTarget: processor.state,
|
|
255
270
|
discardExcludedText: resolvedPlugins.length === 0
|
|
256
271
|
};
|
|
257
|
-
const handleEvent = resolvedPlugins.length ? (event) => processPluginsForEvent(event, resolvedPlugins, processor.state, processor.processEvent) : processor.processEvent;
|
|
272
|
+
const handleEvent = resolvedPlugins.length ? (event) => processPluginsForEvent(event, resolvedPlugins, processor.state, processor.processEvent, processor.skipEvent) : processor.processEvent;
|
|
258
273
|
const incrementalPlainText = resolvedPlugins.length === 0 && !options.wrapWidth && !tagOverrideHandlers?.size;
|
|
259
274
|
let remainingHtml = "";
|
|
260
275
|
try {
|
|
@@ -295,7 +310,7 @@ function htmlToMarkdown(html, options = {}) {
|
|
|
295
310
|
plainText: false,
|
|
296
311
|
urlBaseTarget: processor.state
|
|
297
312
|
};
|
|
298
|
-
const handleEvent = plugins.length ? (event) => processPluginsForEvent(event, plugins, processor.state, processor.processEvent) : processor.processEvent;
|
|
313
|
+
const handleEvent = plugins.length ? (event) => processPluginsForEvent(event, plugins, processor.state, processor.processEvent, processor.skipEvent) : processor.processEvent;
|
|
299
314
|
const leftover = parseHtmlStream(html, parseState, handleEvent);
|
|
300
315
|
finalizeParse(leftover, parseState, handleEvent);
|
|
301
316
|
endPlugins(plugins, processor.state);
|
|
@@ -51,7 +51,7 @@ function assertEngineOptions(options, entry = "converter") {
|
|
|
51
51
|
const plugins = options.plugins;
|
|
52
52
|
if (plugins != null && !Array.isArray(plugins)) throw new TypeError("@mdream/js takes `plugins` as an array, such as { plugins: [frontmatterPlugin()] } with frontmatterPlugin from '@mdream/js/plugins'.");
|
|
53
53
|
}
|
|
54
|
-
function processPluginsForEvent(event, plugins, state, processEvent) {
|
|
54
|
+
function processPluginsForEvent(event, plugins, state, processEvent, onSkipped) {
|
|
55
55
|
state.depth = event.node.depth;
|
|
56
56
|
if (plugins?.length) {
|
|
57
57
|
if (event.node.type === 1 && event.type === 0) {
|
|
@@ -76,7 +76,10 @@ function processPluginsForEvent(event, plugins, state, processEvent) {
|
|
|
76
76
|
const res = plugin.beforeNodeProcess?.(event, state);
|
|
77
77
|
if (typeof res === "object") shouldSkip = res.skip;
|
|
78
78
|
}
|
|
79
|
-
if (shouldSkip)
|
|
79
|
+
if (shouldSkip) {
|
|
80
|
+
onSkipped?.(event);
|
|
81
|
+
return true;
|
|
82
|
+
}
|
|
80
83
|
if (event.node.type === 1) {
|
|
81
84
|
const element = event.node;
|
|
82
85
|
const fn = event.type === 0 ? "onNodeEnter" : "onNodeExit";
|
|
@@ -98,7 +101,10 @@ function processPluginsForEvent(event, plugins, state, processEvent) {
|
|
|
98
101
|
if (plugin.processTextNode) {
|
|
99
102
|
const result = plugin.processTextNode(textNode, state);
|
|
100
103
|
if (result) {
|
|
101
|
-
if (result.skip)
|
|
104
|
+
if (result.skip) {
|
|
105
|
+
textNode.excludedFromMarkdown = true;
|
|
106
|
+
return true;
|
|
107
|
+
}
|
|
102
108
|
textNode.value = result.content;
|
|
103
109
|
}
|
|
104
110
|
}
|
|
@@ -595,8 +595,9 @@ function tailwindPlugin() {
|
|
|
595
595
|
content: "",
|
|
596
596
|
skip: true
|
|
597
597
|
};
|
|
598
|
-
const
|
|
599
|
-
const
|
|
598
|
+
const formatsText = state.outputFormat === "markdown" && !state.codeText;
|
|
599
|
+
const prefix = formatsText ? tailwindData?.prefix || "" : "";
|
|
600
|
+
const suffix = formatsText ? tailwindData?.suffix || "" : "";
|
|
600
601
|
if (prefix || suffix) node.generatedMarkdown = {
|
|
601
602
|
prefix,
|
|
602
603
|
suffix
|