@mdream/js 2.0.0-beta.1 → 2.0.1
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/README.md +402 -126
- package/dist/_chunks/clean.mjs +526 -0
- package/dist/_chunks/const.mjs +3 -7
- package/dist/_chunks/extraction.d.mts +14 -0
- package/dist/_chunks/html.mjs +260 -0
- package/dist/_chunks/markdown-processor.mjs +1077 -828
- package/dist/_chunks/minimal.mjs +29 -0
- package/dist/_chunks/output-runner.mjs +54 -0
- package/dist/_chunks/parse.mjs +134 -1501
- package/dist/_chunks/plugin.mjs +14 -0
- package/dist/_chunks/src.mjs +291 -0
- package/dist/_chunks/tag-overrides.mjs +138 -0
- package/dist/_chunks/{plugins.mjs → tailwind.mjs} +145 -292
- package/dist/_chunks/text.mjs +583 -0
- package/dist/_chunks/types.d.mts +136 -126
- package/dist/_chunks/url.mjs +82 -0
- package/dist/_chunks/utils.mjs +659 -0
- package/dist/clean.d.mts +10 -0
- package/dist/clean.mjs +2 -0
- package/dist/cli.mjs +47 -8
- package/dist/html.d.mts +6 -0
- package/dist/html.mjs +2 -0
- package/dist/index.d.mts +5 -4
- package/dist/index.mjs +4 -58
- package/dist/parse.d.mts +15 -10
- package/dist/plugins.d.mts +13 -28
- package/dist/plugins.mjs +52 -2
- package/dist/preset/minimal.d.mts +14 -2
- package/dist/preset/minimal.mjs +1 -32
- package/dist/splitter.d.mts +12 -4
- package/dist/splitter.mjs +386 -113
- package/dist/text.d.mts +6 -0
- package/dist/text.mjs +2 -0
- package/package.json +34 -8
- package/dist/_chunks/index.d.mts +0 -9
- package/dist/_chunks/minimal.d.mts +0 -6
- package/dist/_chunks/resolve-plugins.mjs +0 -44
- package/dist/_chunks/stream.mjs +0 -37
- package/dist/core.d.mts +0 -15
- package/dist/core.mjs +0 -28
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
const NO_PLUGINS = [];
|
|
2
|
+
function createPlugin(plugin) {
|
|
3
|
+
return plugin;
|
|
4
|
+
}
|
|
5
|
+
function resolvePlugins(plugins) {
|
|
6
|
+
if (!plugins || plugins.length === 0) return NO_PLUGINS;
|
|
7
|
+
const resolved = Array.from({ length: plugins.length });
|
|
8
|
+
for (let index = 0; index < plugins.length; index++) {
|
|
9
|
+
const plugin = plugins[index];
|
|
10
|
+
resolved[index] = typeof plugin === "function" ? plugin() : plugin;
|
|
11
|
+
}
|
|
12
|
+
return resolved;
|
|
13
|
+
}
|
|
14
|
+
export { createPlugin, resolvePlugins };
|
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
import "./const.mjs";
|
|
2
|
+
import { isInsideHeading, trimOutputStart } from "./utils.mjs";
|
|
3
|
+
import { createMarkdownProcessor, isAsciiWhitespace, lineOpensRawHtmlBlock, stripBlockquoteListIndent, tagHandlers, trimAsciiWhitespaceEnd } from "./markdown-processor.mjs";
|
|
4
|
+
import { assertEngineOptions, buildTagOverrideHandlers, checkClean, endPlugins, processPluginsForEvent } from "./tag-overrides.mjs";
|
|
5
|
+
import { finalizeParse, parseHtmlStream } from "./parse.mjs";
|
|
6
|
+
import { resolvePlugins } from "./plugin.mjs";
|
|
7
|
+
function resetBufferScanCursors(scan) {
|
|
8
|
+
scan[1] = 0;
|
|
9
|
+
scan[2] = 0;
|
|
10
|
+
scan[3] = 0;
|
|
11
|
+
scan[4] = 0;
|
|
12
|
+
}
|
|
13
|
+
function flushBlockquoteLines(state) {
|
|
14
|
+
const buffer = state.buffer;
|
|
15
|
+
const frames = state.blockquotes;
|
|
16
|
+
let start = buffer.length;
|
|
17
|
+
let previous = -1;
|
|
18
|
+
for (const frame of frames) {
|
|
19
|
+
if (frame.fragment < previous) return false;
|
|
20
|
+
start = Math.min(start, frame.fragment);
|
|
21
|
+
previous = frame.fragment;
|
|
22
|
+
}
|
|
23
|
+
let length = 0;
|
|
24
|
+
for (let index = start; index < buffer.length; index++) length += buffer[index].length;
|
|
25
|
+
if (length < 8192) return false;
|
|
26
|
+
const content = buffer.slice(start).join("");
|
|
27
|
+
const stableEnd = trimAsciiWhitespaceEnd(content).length;
|
|
28
|
+
let end = content.lastIndexOf("\n", stableEnd - 1) + 1;
|
|
29
|
+
while (end >= 2 && content.charCodeAt(end - 2) === 10) end--;
|
|
30
|
+
if (end === 0) return false;
|
|
31
|
+
const starts = [];
|
|
32
|
+
for (const frame of frames) {
|
|
33
|
+
let offset = 0;
|
|
34
|
+
for (let index = start; index < frame.fragment; index++) offset += buffer[index].length;
|
|
35
|
+
if (offset >= end || starts.length > 0 && offset < starts[starts.length - 1]) return false;
|
|
36
|
+
starts.push(offset);
|
|
37
|
+
}
|
|
38
|
+
let quoted = content.slice(0, end);
|
|
39
|
+
for (let index = frames.length - 1; index >= 0; index--) {
|
|
40
|
+
const frame = frames[index];
|
|
41
|
+
const offset = starts[index];
|
|
42
|
+
const prefix = `${frame.listIndent}>`;
|
|
43
|
+
const body = quoted.slice(offset, -1);
|
|
44
|
+
quoted = `${quoted.slice(0, offset) + body.split("\n").map((line) => {
|
|
45
|
+
const unindented = stripBlockquoteListIndent(line, frame.listIndent);
|
|
46
|
+
return unindented ? `${prefix} ${unindented}` : prefix;
|
|
47
|
+
}).join("\n")}\n`;
|
|
48
|
+
}
|
|
49
|
+
let cut = start;
|
|
50
|
+
let remaining = end;
|
|
51
|
+
while (remaining >= buffer[cut].length) {
|
|
52
|
+
remaining -= buffer[cut].length;
|
|
53
|
+
cut++;
|
|
54
|
+
}
|
|
55
|
+
const tail = buffer.slice(cut);
|
|
56
|
+
if (remaining !== 0) {
|
|
57
|
+
const original = tail[0];
|
|
58
|
+
tail[0] = original.slice(remaining);
|
|
59
|
+
if (cut === buffer.length - 1 && state.lastContentCache === original) state.lastContentCache = tail[0];
|
|
60
|
+
}
|
|
61
|
+
buffer.length = start;
|
|
62
|
+
buffer.push(quoted);
|
|
63
|
+
for (const fragment of tail) buffer.push(fragment);
|
|
64
|
+
for (const frame of frames) {
|
|
65
|
+
frame.fragment = start + 1;
|
|
66
|
+
frame.followsWhitespace = true;
|
|
67
|
+
}
|
|
68
|
+
return true;
|
|
69
|
+
}
|
|
70
|
+
function prepareQuoteOutput(state, bufferScan, quoteScan) {
|
|
71
|
+
const owner = state.blockquotes[0];
|
|
72
|
+
let start = owner.fragment;
|
|
73
|
+
for (let index = 1; index < state.blockquotes.length; index++) start = Math.min(start, state.blockquotes[index].fragment);
|
|
74
|
+
if (!quoteScan || quoteScan.owner !== owner || quoteScan.depth !== state.blockquotes.length || quoteScan.start !== start || state.buffer[quoteScan.fragment - 1] !== quoteScan.lastFragment) quoteScan = {
|
|
75
|
+
owner,
|
|
76
|
+
depth: state.blockquotes.length,
|
|
77
|
+
start,
|
|
78
|
+
fragment: start,
|
|
79
|
+
lastFragment: void 0,
|
|
80
|
+
length: 0,
|
|
81
|
+
newline: -1,
|
|
82
|
+
settledEnd: -1,
|
|
83
|
+
attemptedEnd: -1
|
|
84
|
+
};
|
|
85
|
+
for (; quoteScan.fragment < state.buffer.length; quoteScan.fragment++) {
|
|
86
|
+
const fragment = state.buffer[quoteScan.fragment];
|
|
87
|
+
const lastNewline = fragment.lastIndexOf("\n");
|
|
88
|
+
const contentEnd = trimAsciiWhitespaceEnd(fragment).length;
|
|
89
|
+
if (contentEnd > 0) {
|
|
90
|
+
const settled = lastNewline < contentEnd ? lastNewline : fragment.lastIndexOf("\n", contentEnd - 1);
|
|
91
|
+
quoteScan.settledEnd = settled === -1 ? quoteScan.newline : quoteScan.length + settled;
|
|
92
|
+
}
|
|
93
|
+
if (lastNewline !== -1) quoteScan.newline = quoteScan.length + lastNewline;
|
|
94
|
+
quoteScan.length += fragment.length;
|
|
95
|
+
quoteScan.lastFragment = fragment;
|
|
96
|
+
}
|
|
97
|
+
if (quoteScan.length >= 8192 && quoteScan.settledEnd > quoteScan.attemptedEnd) {
|
|
98
|
+
quoteScan.attemptedEnd = quoteScan.settledEnd;
|
|
99
|
+
const lineLead = lineOpensRawHtmlBlock(state.buffer, bufferScan);
|
|
100
|
+
if (flushBlockquoteLines(state)) {
|
|
101
|
+
quoteScan = void 0;
|
|
102
|
+
bufferScan[5] = lineLead;
|
|
103
|
+
bufferScan[1] = 0;
|
|
104
|
+
bufferScan[2] = 0;
|
|
105
|
+
bufferScan[3] = 0;
|
|
106
|
+
bufferScan[4] = 0;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
return quoteScan;
|
|
110
|
+
}
|
|
111
|
+
function createMarkdownDrain(context, _hasPlugins) {
|
|
112
|
+
const { state, options, bufferScan } = context;
|
|
113
|
+
let lastYieldedLength = 0;
|
|
114
|
+
let hasYieldedContent = false;
|
|
115
|
+
let quoteScan;
|
|
116
|
+
function prepareDrain(final) {
|
|
117
|
+
context.settleItemMarker();
|
|
118
|
+
if (!final && state.blockquotes.length > 0 && !state.outputPositions && context.getNonQuoteHeldOutputFragment() === Infinity) quoteScan = prepareQuoteOutput(state, bufferScan, quoteScan);
|
|
119
|
+
else quoteScan = void 0;
|
|
120
|
+
return context.getHeldOutputFragment(final);
|
|
121
|
+
}
|
|
122
|
+
function observeStableOutput(content, end) {
|
|
123
|
+
bufferScan[5] = lineOpensRawHtmlBlock(state.buffer, bufferScan);
|
|
124
|
+
if (!context.inRawHtmlRegion() || bufferScan[0]) return;
|
|
125
|
+
let start = 0;
|
|
126
|
+
const scannedTo = bufferScan[1] > state.buffer.length ? 0 : bufferScan[1];
|
|
127
|
+
for (let index = 0; index < scannedTo; index++) start += state.buffer[index].length;
|
|
128
|
+
const blankLine = content.indexOf("\n\n", Math.max(0, start - 1));
|
|
129
|
+
if (blankLine !== -1 && blankLine + 2 <= end) bufferScan[0] = true;
|
|
130
|
+
}
|
|
131
|
+
function compactQuotePrefix(fragment, content, start) {
|
|
132
|
+
if (state.blockquotes.length === 0 || start <= 0 || state.outputPositions || context.getNonQuoteHeldOutputFragment() !== Infinity) return 0;
|
|
133
|
+
const retained = content.slice(start);
|
|
134
|
+
state.buffer.splice(0, fragment, retained);
|
|
135
|
+
for (const frame of state.blockquotes) frame.fragment -= fragment - 1;
|
|
136
|
+
quoteScan = void 0;
|
|
137
|
+
bufferScan[1] = 0;
|
|
138
|
+
bufferScan[2] = 1;
|
|
139
|
+
bufferScan[3] = bufferScan[5] === void 0 ? 1 : 0;
|
|
140
|
+
bufferScan[4] = 0;
|
|
141
|
+
return start;
|
|
142
|
+
}
|
|
143
|
+
function getMarkdownChunk(final = false) {
|
|
144
|
+
if (context.holdsOutput) return final ? context.getMarkdown() : "";
|
|
145
|
+
const heldFragment = prepareDrain(final);
|
|
146
|
+
const fragmentHeld = heldFragment !== Infinity;
|
|
147
|
+
const content = fragmentHeld ? state.buffer.slice(0, heldFragment).join("") : state.buffer.join("");
|
|
148
|
+
const currentContent = hasYieldedContent ? content : trimOutputStart(content);
|
|
149
|
+
const leadingTrimmed = content.length - currentContent.length;
|
|
150
|
+
const yieldedLength = Math.max(0, lastYieldedLength - leadingTrimmed);
|
|
151
|
+
const inPre = state.depthMap[34] !== 0 && state.preFenceOwnerDepth !== 0;
|
|
152
|
+
let stableLength = currentContent.length;
|
|
153
|
+
let retainMutableFragments = false;
|
|
154
|
+
if (inPre && !fragmentHeld) {
|
|
155
|
+
const trailingCode = currentContent.charCodeAt(stableLength - 1);
|
|
156
|
+
while (stableLength > 0 && currentContent.charCodeAt(stableLength - 1) === 32) stableLength--;
|
|
157
|
+
retainMutableFragments = stableLength < currentContent.length;
|
|
158
|
+
if (state.lastTextNode?.containsWhitespace && isAsciiWhitespace(trailingCode)) {
|
|
159
|
+
stableLength = trimAsciiWhitespaceEnd(currentContent).length;
|
|
160
|
+
retainMutableFragments = stableLength < currentContent.length;
|
|
161
|
+
}
|
|
162
|
+
} else {
|
|
163
|
+
while (stableLength > 0) {
|
|
164
|
+
const code = currentContent.charCodeAt(stableLength - 1);
|
|
165
|
+
if (code !== 32 && code !== 10) break;
|
|
166
|
+
stableLength--;
|
|
167
|
+
}
|
|
168
|
+
retainMutableFragments = stableLength < currentContent.length;
|
|
169
|
+
}
|
|
170
|
+
const headingHeld = !final && isInsideHeading(state.depthMap);
|
|
171
|
+
if (headingHeld) {
|
|
172
|
+
let headingPos = stableLength;
|
|
173
|
+
while (headingPos > 0) {
|
|
174
|
+
const code = currentContent.charCodeAt(headingPos - 1);
|
|
175
|
+
if (code !== 35 && code !== 32 && code !== 9) break;
|
|
176
|
+
headingPos--;
|
|
177
|
+
}
|
|
178
|
+
if (headingPos < stableLength) stableLength = headingPos;
|
|
179
|
+
}
|
|
180
|
+
if (stableLength < yieldedLength) stableLength = yieldedLength;
|
|
181
|
+
const newContent = currentContent.slice(yieldedLength, stableLength);
|
|
182
|
+
if (newContent || hasYieldedContent) lastYieldedLength = stableLength + leadingTrimmed;
|
|
183
|
+
if (newContent && !hasYieldedContent) {
|
|
184
|
+
hasYieldedContent = true;
|
|
185
|
+
context.markYielded();
|
|
186
|
+
}
|
|
187
|
+
const wrapping = !!options.wrapWidth;
|
|
188
|
+
if (hasYieldedContent && fragmentHeld && state.blockquotes.length > 0 && !headingHeld && !inPre && (!wrapping || leadingTrimmed === 0)) {
|
|
189
|
+
observeStableOutput(content, stableLength + leadingTrimmed);
|
|
190
|
+
let retainedStart = stableLength + leadingTrimmed - 17;
|
|
191
|
+
if (wrapping) retainedStart = Math.min(retainedStart, content.lastIndexOf("\n", stableLength + leadingTrimmed - 1));
|
|
192
|
+
const removed = compactQuotePrefix(heldFragment, content, Math.max(0, retainedStart));
|
|
193
|
+
lastYieldedLength -= removed;
|
|
194
|
+
}
|
|
195
|
+
if (hasYieldedContent && !fragmentHeld && !headingHeld && (!retainMutableFragments || !inPre) && (!wrapping || leadingTrimmed === 0)) {
|
|
196
|
+
observeStableOutput(content, stableLength + leadingTrimmed);
|
|
197
|
+
let contextStart = stableLength - 17;
|
|
198
|
+
if (wrapping) {
|
|
199
|
+
const lineStart = currentContent.lastIndexOf("\n", stableLength - 1);
|
|
200
|
+
if (lineStart < contextStart) contextStart = lineStart;
|
|
201
|
+
}
|
|
202
|
+
if (retainMutableFragments && leadingTrimmed === 0) {
|
|
203
|
+
const lastFragment = state.buffer.at(-1);
|
|
204
|
+
const fragmentStart = currentContent.length - lastFragment.length;
|
|
205
|
+
const tailStart = Math.max(0, Math.min(contextStart, fragmentStart));
|
|
206
|
+
const emittedTail = currentContent.slice(tailStart, fragmentStart);
|
|
207
|
+
state.buffer.length = 0;
|
|
208
|
+
resetBufferScanCursors(bufferScan);
|
|
209
|
+
if (emittedTail) state.buffer.push(emittedTail);
|
|
210
|
+
state.buffer.push(lastFragment);
|
|
211
|
+
lastYieldedLength = stableLength - tailStart;
|
|
212
|
+
} else if (!retainMutableFragments) {
|
|
213
|
+
const tailStart = Math.max(0, contextStart);
|
|
214
|
+
const emittedTail = currentContent.slice(tailStart, stableLength);
|
|
215
|
+
state.buffer.length = 0;
|
|
216
|
+
resetBufferScanCursors(bufferScan);
|
|
217
|
+
if (emittedTail) state.buffer.push(emittedTail);
|
|
218
|
+
lastYieldedLength = emittedTail.length;
|
|
219
|
+
}
|
|
220
|
+
bufferScan[2] = state.buffer.length;
|
|
221
|
+
if (bufferScan[5] === void 0) bufferScan[3] = state.buffer.length;
|
|
222
|
+
}
|
|
223
|
+
return newContent;
|
|
224
|
+
}
|
|
225
|
+
return { getMarkdownChunk };
|
|
226
|
+
}
|
|
227
|
+
async function* streamHtmlToMarkdown$1(htmlStream, options = {}, resolvedPlugins = [], tagOverrideHandlers) {
|
|
228
|
+
if (!htmlStream) throw new Error("Invalid HTML stream provided");
|
|
229
|
+
const decoder = new TextDecoder("utf-8", { ignoreBOM: true });
|
|
230
|
+
const reader = htmlStream.getReader();
|
|
231
|
+
const processor = createMarkdownProcessor(options, (context) => createMarkdownDrain(context, resolvedPlugins.length > 0));
|
|
232
|
+
const parseState = {
|
|
233
|
+
depthMap: processor.state.depthMap,
|
|
234
|
+
depth: 0,
|
|
235
|
+
resolvedPlugins,
|
|
236
|
+
tagHandlers,
|
|
237
|
+
tagOverrideHandlers,
|
|
238
|
+
plainText: false,
|
|
239
|
+
discardExcludedText: resolvedPlugins.length === 0
|
|
240
|
+
};
|
|
241
|
+
const handleEvent = resolvedPlugins.length ? (event) => processPluginsForEvent(event, resolvedPlugins, processor.state, processor.processEvent) : processor.processEvent;
|
|
242
|
+
let remainingHtml = "";
|
|
243
|
+
try {
|
|
244
|
+
while (true) {
|
|
245
|
+
const { done, value } = await reader.read();
|
|
246
|
+
if (done) break;
|
|
247
|
+
if (value === "") continue;
|
|
248
|
+
const decoded = typeof value === "string" ? decoder.decode() + value : decoder.decode(value, { stream: true });
|
|
249
|
+
const htmlContent = `${remainingHtml}${decoded}`;
|
|
250
|
+
remainingHtml = parseHtmlStream(htmlContent, parseState, handleEvent);
|
|
251
|
+
const chunk = processor.getMarkdownChunk();
|
|
252
|
+
if (chunk) yield chunk;
|
|
253
|
+
}
|
|
254
|
+
const decoderTail = decoder.decode();
|
|
255
|
+
const finalHtml = remainingHtml + decoderTail;
|
|
256
|
+
const leftover = finalHtml ? parseHtmlStream(finalHtml, parseState, handleEvent) : "";
|
|
257
|
+
finalizeParse(leftover, parseState, handleEvent);
|
|
258
|
+
endPlugins(resolvedPlugins, processor.state);
|
|
259
|
+
const finalChunk = processor.getMarkdownChunk(true);
|
|
260
|
+
if (finalChunk) yield finalChunk;
|
|
261
|
+
} finally {
|
|
262
|
+
reader.releaseLock();
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
function htmlToMarkdown(html, options = {}) {
|
|
266
|
+
assertEngineOptions(options);
|
|
267
|
+
checkClean(options);
|
|
268
|
+
const tagOverrideHandlers = options.tagOverrides ? buildTagOverrideHandlers(options.tagOverrides, tagHandlers) : void 0;
|
|
269
|
+
const plugins = resolvePlugins(options.plugins);
|
|
270
|
+
const processor = createMarkdownProcessor(options);
|
|
271
|
+
const parseState = {
|
|
272
|
+
depthMap: processor.state.depthMap,
|
|
273
|
+
depth: 0,
|
|
274
|
+
resolvedPlugins: plugins,
|
|
275
|
+
tagHandlers,
|
|
276
|
+
tagOverrideHandlers,
|
|
277
|
+
plainText: false
|
|
278
|
+
};
|
|
279
|
+
const handleEvent = plugins.length ? (event) => processPluginsForEvent(event, plugins, processor.state, processor.processEvent) : processor.processEvent;
|
|
280
|
+
const leftover = parseHtmlStream(html, parseState, handleEvent);
|
|
281
|
+
finalizeParse(leftover, parseState, handleEvent);
|
|
282
|
+
endPlugins(plugins, processor.state);
|
|
283
|
+
return processor.getMarkdown();
|
|
284
|
+
}
|
|
285
|
+
function streamHtmlToMarkdown(htmlStream, options = {}) {
|
|
286
|
+
assertEngineOptions(options);
|
|
287
|
+
checkClean(options);
|
|
288
|
+
const tagOverrideHandlers = options.tagOverrides ? buildTagOverrideHandlers(options.tagOverrides, tagHandlers) : void 0;
|
|
289
|
+
return streamHtmlToMarkdown$1(htmlStream, options, resolvePlugins(options.plugins), tagOverrideHandlers);
|
|
290
|
+
}
|
|
291
|
+
export { htmlToMarkdown, streamHtmlToMarkdown };
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import { TagIdMap } from "./const.mjs";
|
|
2
|
+
function checkClean(options) {
|
|
3
|
+
const clean = options.clean;
|
|
4
|
+
if (clean && typeof clean.apply !== "function") throw new TypeError("The clean option needs cleanup rules from clean(). Import it from '@mdream/js/clean'.");
|
|
5
|
+
}
|
|
6
|
+
const CONVERTER_OPTIONS = "origin, tagOverrides, clean, wrapWidth, plugins";
|
|
7
|
+
const SPLITTER_OPTIONS = `${CONVERTER_OPTIONS}, headersToSplitOn, returnEachLine, stripHeaders, chunkSize, chunkOverlap, lengthFunction, keepSeparator`;
|
|
8
|
+
function isConverterOption(key) {
|
|
9
|
+
switch (key) {
|
|
10
|
+
case "origin":
|
|
11
|
+
case "tagOverrides":
|
|
12
|
+
case "clean":
|
|
13
|
+
case "wrapWidth":
|
|
14
|
+
case "plugins": return true;
|
|
15
|
+
}
|
|
16
|
+
return false;
|
|
17
|
+
}
|
|
18
|
+
function isSplitterOption(key) {
|
|
19
|
+
switch (key) {
|
|
20
|
+
case "headersToSplitOn":
|
|
21
|
+
case "returnEachLine":
|
|
22
|
+
case "stripHeaders":
|
|
23
|
+
case "chunkSize":
|
|
24
|
+
case "chunkOverlap":
|
|
25
|
+
case "lengthFunction":
|
|
26
|
+
case "keepSeparator": return true;
|
|
27
|
+
}
|
|
28
|
+
return false;
|
|
29
|
+
}
|
|
30
|
+
function unknownOptionMessage(key, validOptions) {
|
|
31
|
+
switch (key) {
|
|
32
|
+
case "minimal":
|
|
33
|
+
case "preset": return `@mdream/js has no \`${key}\` option. Use withMinimalPreset() from '@mdream/js/preset/minimal': htmlToMarkdown(html, withMinimalPreset({ origin })).`;
|
|
34
|
+
case "format": return "@mdream/js has no `format` option. Use htmlToText() from '@mdream/js/text' or htmlToSafeHtml() from '@mdream/js/html'.";
|
|
35
|
+
case "hooks": return "@mdream/js has no `hooks` option. Pass the hooks in `plugins`.";
|
|
36
|
+
case "cleanUrls": return "@mdream/js has no `cleanUrls` option. Pass { clean: clean({ urls: true }) } with clean from '@mdream/js/clean'.";
|
|
37
|
+
case "frontmatter":
|
|
38
|
+
case "isolateMain":
|
|
39
|
+
case "tailwind":
|
|
40
|
+
case "filter":
|
|
41
|
+
case "extraction": return `@mdream/js has no \`${key}\` option. Pass { plugins: [${key}Plugin()] } with ${key}Plugin from '@mdream/js/plugins'.`;
|
|
42
|
+
}
|
|
43
|
+
return `@mdream/js has no \`${key}\` option. Valid options: ${validOptions}.`;
|
|
44
|
+
}
|
|
45
|
+
function assertEngineOptions(options, entry = "converter") {
|
|
46
|
+
const splitter = entry === "splitter";
|
|
47
|
+
for (const key in options) {
|
|
48
|
+
if (isConverterOption(key) || splitter && isSplitterOption(key)) continue;
|
|
49
|
+
if (options[key] !== void 0) throw new TypeError(unknownOptionMessage(key, splitter ? SPLITTER_OPTIONS : CONVERTER_OPTIONS));
|
|
50
|
+
}
|
|
51
|
+
const plugins = options.plugins;
|
|
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
|
+
}
|
|
54
|
+
function processPluginsForEvent(event, plugins, state, processEvent) {
|
|
55
|
+
state.depth = event.node.depth;
|
|
56
|
+
if (plugins?.length) {
|
|
57
|
+
if (event.node.type === 1 && event.type === 0) {
|
|
58
|
+
const element = event.node;
|
|
59
|
+
for (let index = 0; index < plugins.length; index++) {
|
|
60
|
+
const plugin = plugins[index];
|
|
61
|
+
if (plugin.processAttributes) plugin.processAttributes(element, state);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
let shouldSkip = false;
|
|
65
|
+
for (let index = 0; index < plugins.length; index++) {
|
|
66
|
+
const res = plugins[index].beforeNodeProcess?.(event, state);
|
|
67
|
+
if (typeof res === "object") shouldSkip = res.skip;
|
|
68
|
+
}
|
|
69
|
+
if (shouldSkip) return true;
|
|
70
|
+
if (event.node.type === 1) {
|
|
71
|
+
const element = event.node;
|
|
72
|
+
const fn = event.type === 0 ? "onNodeEnter" : "onNodeExit";
|
|
73
|
+
let pluginOutputs;
|
|
74
|
+
for (let index = 0; index < plugins.length; index++) {
|
|
75
|
+
const plugin = plugins[index];
|
|
76
|
+
if (plugin[fn]) {
|
|
77
|
+
const result = plugin[fn](element, state);
|
|
78
|
+
if (result) (pluginOutputs ??= []).push(result);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
if (pluginOutputs) element.pluginOutput = element.pluginOutput ? element.pluginOutput.concat(pluginOutputs) : pluginOutputs;
|
|
82
|
+
} else if (event.node.type === 2 && event.type === 0) {
|
|
83
|
+
const textNode = event.node;
|
|
84
|
+
for (let index = 0; index < plugins.length; index++) {
|
|
85
|
+
const plugin = plugins[index];
|
|
86
|
+
if (plugin.processTextNode) {
|
|
87
|
+
const result = plugin.processTextNode(textNode, state);
|
|
88
|
+
if (result) {
|
|
89
|
+
if (result.skip) return true;
|
|
90
|
+
textNode.value = result.content;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
processEvent(event);
|
|
97
|
+
return false;
|
|
98
|
+
}
|
|
99
|
+
function endPlugins(plugins, state) {
|
|
100
|
+
if (!plugins) return;
|
|
101
|
+
for (let index = 0; index < plugins.length; index++) plugins[index].onDocumentEnd?.(state);
|
|
102
|
+
}
|
|
103
|
+
function buildTagOverrideHandlers(overrides, tagHandlers) {
|
|
104
|
+
const result = /* @__PURE__ */ new Map();
|
|
105
|
+
for (const tagName in overrides) {
|
|
106
|
+
const override = overrides[tagName];
|
|
107
|
+
if (!override) continue;
|
|
108
|
+
if (typeof override === "string") {
|
|
109
|
+
const targetId = TagIdMap[override];
|
|
110
|
+
if (targetId !== void 0) result.set(tagName, {
|
|
111
|
+
...tagHandlers[targetId],
|
|
112
|
+
aliasTagId: targetId
|
|
113
|
+
});
|
|
114
|
+
continue;
|
|
115
|
+
}
|
|
116
|
+
const baseId = TagIdMap[tagName];
|
|
117
|
+
const baseHandler = baseId === void 0 ? void 0 : tagHandlers[baseId];
|
|
118
|
+
const handler = baseHandler ? { ...baseHandler } : {};
|
|
119
|
+
if (override.enter !== void 0) {
|
|
120
|
+
const output = override.enter;
|
|
121
|
+
handler.enter = () => output;
|
|
122
|
+
handler.literalEnter = true;
|
|
123
|
+
}
|
|
124
|
+
if (override.exit !== void 0) {
|
|
125
|
+
const output = override.exit;
|
|
126
|
+
handler.exit = () => output;
|
|
127
|
+
handler.literalExit = true;
|
|
128
|
+
handler.literalExitHasOutput = output.length > 0;
|
|
129
|
+
}
|
|
130
|
+
if (override.spacing !== void 0) handler.spacing = override.spacing;
|
|
131
|
+
if (override.isInline !== void 0) handler.isInline = override.isInline;
|
|
132
|
+
if (override.isSelfClosing !== void 0) handler.isSelfClosing = override.isSelfClosing;
|
|
133
|
+
if (override.collapsesInnerWhiteSpace !== void 0) handler.collapsesInnerWhiteSpace = override.collapsesInnerWhiteSpace;
|
|
134
|
+
result.set(tagName, handler);
|
|
135
|
+
}
|
|
136
|
+
return result;
|
|
137
|
+
}
|
|
138
|
+
export { assertEngineOptions, buildTagOverrideHandlers, checkClean, endPlugins, processPluginsForEvent };
|