@mdream/js 2.0.0-beta.1 → 2.0.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/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 +133 -1500
- 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 +355 -113
- package/dist/text.d.mts +6 -0
- package/dist/text.mjs +2 -0
- package/package.json +33 -7
- 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,583 @@
|
|
|
1
|
+
import { DEFAULT_BLOCK_SPACING, LIST_ITEM_SPACING, NO_SPACING, TABLE_ROW_SPACING } from "./const.mjs";
|
|
2
|
+
import { figcaptionOwnsBlockSpacing, isDataUrl, isInsideHeading, isInsideTableCell, lastOutputChar, markRenderedChildContent, trimOutputStart, trimTextAtLineStart } from "./utils.mjs";
|
|
3
|
+
import { resolveUrl } from "./url.mjs";
|
|
4
|
+
import { assertEngineOptions, buildTagOverrideHandlers, checkClean } from "./tag-overrides.mjs";
|
|
5
|
+
import { parserTagHandlers } from "./parse.mjs";
|
|
6
|
+
import { resolvePlugins } from "./plugin.mjs";
|
|
7
|
+
import { processHtmlOutput, streamHtmlOutput } from "./output-runner.mjs";
|
|
8
|
+
function trimAsciiWhitespaceEnd(value) {
|
|
9
|
+
let end = value.length;
|
|
10
|
+
while (end > 0) {
|
|
11
|
+
const code = value.charCodeAt(end - 1);
|
|
12
|
+
if (code !== 32 && (code < 9 || code > 13)) break;
|
|
13
|
+
end--;
|
|
14
|
+
}
|
|
15
|
+
return end === value.length ? value : value.slice(0, end);
|
|
16
|
+
}
|
|
17
|
+
function shouldAddSpacingBeforeText(lastChar, lastNode, textNode) {
|
|
18
|
+
if (!lastChar || "\n [>".includes(lastChar) || textNode.value[0] === " ") return false;
|
|
19
|
+
if (textNode.joinsPrevious || lastNode && (lastNode.tagHandler ? lastNode.tagHandler.isInline : lastNode.tagId === -1)) return false;
|
|
20
|
+
const firstChar = textNode.value[0];
|
|
21
|
+
return Boolean(firstChar && !".,!?:;_*`)]".includes(firstChar));
|
|
22
|
+
}
|
|
23
|
+
function currentColumn(buffer, base, scan) {
|
|
24
|
+
if (scan[0] >= buffer.length) {
|
|
25
|
+
scan[0] = 0;
|
|
26
|
+
scan[1] = base;
|
|
27
|
+
}
|
|
28
|
+
for (; scan[0] < buffer.length - 1; scan[0]++) {
|
|
29
|
+
const value = buffer[scan[0]];
|
|
30
|
+
scan[1] = columnAt(value, value.length, scan[1]);
|
|
31
|
+
}
|
|
32
|
+
const tail = buffer.at(-1);
|
|
33
|
+
return tail ? columnAt(tail, tail.length, scan[1]) : base;
|
|
34
|
+
}
|
|
35
|
+
function columnAt(value, end, column) {
|
|
36
|
+
const newline = end > 0 ? value.lastIndexOf("\n", end - 1) : -1;
|
|
37
|
+
let index = 0;
|
|
38
|
+
if (newline >= 0) {
|
|
39
|
+
index = newline + 1;
|
|
40
|
+
column = 0;
|
|
41
|
+
}
|
|
42
|
+
for (; index < end; index++) {
|
|
43
|
+
const code = value.charCodeAt(index);
|
|
44
|
+
if (code >= 55296 && code <= 56319 && index + 1 < end) {
|
|
45
|
+
const next = value.charCodeAt(index + 1);
|
|
46
|
+
if (next >= 56320 && next <= 57343) index++;
|
|
47
|
+
}
|
|
48
|
+
column++;
|
|
49
|
+
}
|
|
50
|
+
return column;
|
|
51
|
+
}
|
|
52
|
+
const YIELDED_CONTEXT = 2;
|
|
53
|
+
function wrapText(value, column, width) {
|
|
54
|
+
const leading = value.charCodeAt(0) === 32;
|
|
55
|
+
const trailing = value.charCodeAt(value.length - 1) === 32;
|
|
56
|
+
let output = "";
|
|
57
|
+
let first = true;
|
|
58
|
+
let index = 0;
|
|
59
|
+
while (index < value.length) {
|
|
60
|
+
let next = value.indexOf(" ", index);
|
|
61
|
+
if (next === -1) next = value.length;
|
|
62
|
+
if (next > index) {
|
|
63
|
+
const word = value.slice(index, next);
|
|
64
|
+
const wordLength = columnAt(word, word.length, 0);
|
|
65
|
+
const needsSpace = first ? leading : true;
|
|
66
|
+
if (needsSpace && column > 0 && column + 1 + wordLength > width) {
|
|
67
|
+
output += "\n";
|
|
68
|
+
column = 0;
|
|
69
|
+
} else if (needsSpace) {
|
|
70
|
+
output += " ";
|
|
71
|
+
column++;
|
|
72
|
+
}
|
|
73
|
+
output += word;
|
|
74
|
+
column += wordLength;
|
|
75
|
+
first = false;
|
|
76
|
+
}
|
|
77
|
+
index = next + 1;
|
|
78
|
+
}
|
|
79
|
+
if (trailing && output && !output.endsWith(" ") && !output.endsWith("\n")) output += " ";
|
|
80
|
+
return output || (leading || trailing ? " " : "");
|
|
81
|
+
}
|
|
82
|
+
const CAPTION_SPACING = 2;
|
|
83
|
+
function trimSpacesEnd(value, keepTabs = false) {
|
|
84
|
+
let end = value.length;
|
|
85
|
+
while (end > 0) {
|
|
86
|
+
const code = value.charCodeAt(end - 1);
|
|
87
|
+
if (code !== 32 && (keepTabs || code !== 9)) break;
|
|
88
|
+
end--;
|
|
89
|
+
}
|
|
90
|
+
return end === value.length ? value : value.slice(0, end);
|
|
91
|
+
}
|
|
92
|
+
function trimAsciiWhitespaceStart(value) {
|
|
93
|
+
let start = 0;
|
|
94
|
+
while (start < value.length && value.charCodeAt(start) <= 32) start++;
|
|
95
|
+
return start === 0 ? value : value.slice(start);
|
|
96
|
+
}
|
|
97
|
+
function hasVisibleContent(value) {
|
|
98
|
+
for (let index = 0; index < value.length; index++) if (value.charCodeAt(index) > 32) return true;
|
|
99
|
+
return false;
|
|
100
|
+
}
|
|
101
|
+
function hasNonWhitespace(value) {
|
|
102
|
+
for (let index = 0; index < value.length; index++) {
|
|
103
|
+
const code = value.charCodeAt(index);
|
|
104
|
+
if (code !== 32 && code !== 10 && code !== 9 && code !== 13 && code !== 12) return true;
|
|
105
|
+
}
|
|
106
|
+
return false;
|
|
107
|
+
}
|
|
108
|
+
function charOf(code) {
|
|
109
|
+
return code === -1 ? "" : String.fromCharCode(code);
|
|
110
|
+
}
|
|
111
|
+
function canWrapHere(depthMap) {
|
|
112
|
+
if (depthMap[34] || depthMap[23] || depthMap[32] || depthMap[31]) return false;
|
|
113
|
+
return !isInsideHeading(depthMap);
|
|
114
|
+
}
|
|
115
|
+
function newlineConfig(node, depthMap) {
|
|
116
|
+
const tagId = node.tagId;
|
|
117
|
+
if (tagId === 106) return NO_SPACING;
|
|
118
|
+
if (tagId !== 25 && depthMap[25] || tagId !== 22 && depthMap[22]) return tagId === 34 ? [1, 1] : NO_SPACING;
|
|
119
|
+
const block = tagId !== void 0 && (tagId >= 7 && tagId <= 12 || tagId === 35 || tagId === 36 || tagId === 25);
|
|
120
|
+
let parent = node.parent;
|
|
121
|
+
while (parent) {
|
|
122
|
+
if (parent.tagHandler?.collapsesInnerWhiteSpace) {
|
|
123
|
+
if (block && parent.tagId === 37) {
|
|
124
|
+
parent = parent.parent;
|
|
125
|
+
continue;
|
|
126
|
+
}
|
|
127
|
+
return NO_SPACING;
|
|
128
|
+
}
|
|
129
|
+
parent = parent.parent;
|
|
130
|
+
}
|
|
131
|
+
if (node.tagHandler?.spacing) return node.tagHandler.spacing;
|
|
132
|
+
return tagId === -1 ? NO_SPACING : DEFAULT_BLOCK_SPACING;
|
|
133
|
+
}
|
|
134
|
+
function elementOutput(node, eventType, state) {
|
|
135
|
+
const override = state.options.tagOverrides?.[node.name];
|
|
136
|
+
if (override && typeof override !== "string") {
|
|
137
|
+
const output = eventType === 0 ? override.enter : override.exit;
|
|
138
|
+
if (output !== void 0) return output;
|
|
139
|
+
}
|
|
140
|
+
if (eventType === 0) {
|
|
141
|
+
if (node.tagId === 6) return "\n";
|
|
142
|
+
if (node.tagId === 35 && (state.depthMap[22] || state.depthMap[25] && !isInsideTableCell(state))) {
|
|
143
|
+
const lastCode = lastOutputChar(state.buffer);
|
|
144
|
+
if (lastCode !== -1 && lastCode !== 32 && lastCode !== 10) return "\n\n";
|
|
145
|
+
}
|
|
146
|
+
if (node.tagId === 32 || node.tagId === 31) return state.depthMap[28] > 1 || node.index === 0 ? void 0 : " ";
|
|
147
|
+
if (node.tagId === 27) {
|
|
148
|
+
const alt = node.attributes.alt;
|
|
149
|
+
const clean = state.options.clean;
|
|
150
|
+
if ((clean === true || clean != null && clean !== false && clean.emptyImages === true) && !(alt !== void 0 && alt.trim().length > 0)) return void 0;
|
|
151
|
+
const src = node.attributes.src || "";
|
|
152
|
+
const output = alt !== void 0 ? alt || void 0 : node.attributes.title || resolveUrl(isDataUrl(src) ? "" : src, state.options.origin, state.options.clean) || void 0;
|
|
153
|
+
if (output && hasVisibleContent(output)) {
|
|
154
|
+
markRenderedChildContent(node);
|
|
155
|
+
if (lastOutputChar(state.buffer) > 32) return ` ${output}`;
|
|
156
|
+
}
|
|
157
|
+
return output;
|
|
158
|
+
}
|
|
159
|
+
if (node.tagId === 81) return lastOutputChar(state.buffer) > 32 ? " \"" : "\"";
|
|
160
|
+
} else if (node.tagId === 81) return "\"";
|
|
161
|
+
}
|
|
162
|
+
function trailingNewlines(buffer) {
|
|
163
|
+
let count = 0;
|
|
164
|
+
for (let index = buffer.length - 1; index >= 0 && count < 2; index--) {
|
|
165
|
+
const value = buffer[index];
|
|
166
|
+
for (let offset = value.length - 1; offset >= 0 && count < 2; offset--) {
|
|
167
|
+
if (value.charCodeAt(offset) !== 10) return count;
|
|
168
|
+
count++;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
return count;
|
|
172
|
+
}
|
|
173
|
+
function appendOutput(state, element, eventType, output) {
|
|
174
|
+
const buffer = state.buffer;
|
|
175
|
+
const wantedNewlines = newlineConfig(element, state.depthMap)[eventType] || 0;
|
|
176
|
+
if (wantedNewlines > 0 && buffer.length > 0 && !(state.depthMap[34] > 0 && element.tagId !== 34)) {
|
|
177
|
+
const tail = buffer[buffer.length - 1];
|
|
178
|
+
const trimmed = trimSpacesEnd(tail, state.depthMap[28] > 0);
|
|
179
|
+
if (trimmed.length !== tail.length) buffer[buffer.length - 1] = trimmed;
|
|
180
|
+
}
|
|
181
|
+
const missingNewlines = Math.max(0, wantedNewlines - trailingNewlines(buffer));
|
|
182
|
+
const isInline = element.tagHandler ? element.tagHandler.isInline === true : element.tagId === -1;
|
|
183
|
+
if (buffer.length === 0) {
|
|
184
|
+
if (output) buffer.push(output);
|
|
185
|
+
return;
|
|
186
|
+
}
|
|
187
|
+
if (state.pendingInlineWhitespace) {
|
|
188
|
+
const firstOutput = output?.[0] || "";
|
|
189
|
+
if (eventType === 0) {
|
|
190
|
+
if (!isInline || element.tagId === 6 || missingNewlines > 0 || firstOutput === "\n" || firstOutput === "\r") state.pendingInlineWhitespace = false;
|
|
191
|
+
else if (firstOutput) {
|
|
192
|
+
const last = charOf(lastOutputChar(buffer));
|
|
193
|
+
if (last && !" \n \r".includes(last) && !" \n \r".includes(firstOutput)) buffer.push(" ");
|
|
194
|
+
state.pendingInlineWhitespace = false;
|
|
195
|
+
}
|
|
196
|
+
} else if (!isInline || missingNewlines > 0) state.pendingInlineWhitespace = false;
|
|
197
|
+
}
|
|
198
|
+
const closesPre = element.tagId === 34;
|
|
199
|
+
const inPre = state.depthMap[34] > 0 && !closesPre;
|
|
200
|
+
if (buffer.length && state.lastTextNode?.containsWhitespace && !inPre) {
|
|
201
|
+
const isBlock = !isInline && missingNewlines > 0;
|
|
202
|
+
const collapses = element.tagHandler?.collapsesInnerWhiteSpace;
|
|
203
|
+
const hasSpacing = Array.isArray(element.tagHandler?.spacing);
|
|
204
|
+
if (element.tagId === 6 && output?.endsWith("\n") || isBlock || (!isInline || eventType === 1) && !(collapses && eventType === 0) && !(hasSpacing && eventType === 0)) {
|
|
205
|
+
const last = buffer.at(-1);
|
|
206
|
+
const trimmed = closesPre ? trimSpacesEnd(last) : trimAsciiWhitespaceEnd(last);
|
|
207
|
+
if (trimmed.length !== last.length) {
|
|
208
|
+
buffer[buffer.length - 1] = trimmed;
|
|
209
|
+
if (eventType === 1 && isInline) state.pendingInlineWhitespace = true;
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
state.lastTextNode = void 0;
|
|
213
|
+
}
|
|
214
|
+
const newline = missingNewlines ? "\n".repeat(missingNewlines) : "";
|
|
215
|
+
if (eventType === 0 && newline) buffer.push(newline);
|
|
216
|
+
if (output) buffer.push(output);
|
|
217
|
+
if (eventType === 1 && newline) buffer.push(newline);
|
|
218
|
+
}
|
|
219
|
+
function createTextOutputProcessor(options, hasPlugins = false) {
|
|
220
|
+
const state = {
|
|
221
|
+
options,
|
|
222
|
+
outputFormat: "text",
|
|
223
|
+
buffer: [],
|
|
224
|
+
depthMap: /* @__PURE__ */ new Uint16Array(113),
|
|
225
|
+
depth: 0,
|
|
226
|
+
plainText: true
|
|
227
|
+
};
|
|
228
|
+
let preserveLeadingWhitespace = false;
|
|
229
|
+
let yieldedLength = 0;
|
|
230
|
+
let started = false;
|
|
231
|
+
let bufferColumn = 0;
|
|
232
|
+
const columnScan = [0, 0];
|
|
233
|
+
let captionOpen = 0;
|
|
234
|
+
let captionContent = false;
|
|
235
|
+
let captionBreakRun = 0;
|
|
236
|
+
let captionEnterSpacing = CAPTION_SPACING;
|
|
237
|
+
let captionExitSpacing = CAPTION_SPACING;
|
|
238
|
+
let captionClosedSpacing = 0;
|
|
239
|
+
let captionBreakOwnsLine = false;
|
|
240
|
+
const openQuotes = [];
|
|
241
|
+
const openQuoteNodes = [];
|
|
242
|
+
function pushCaptionBoundary(newlines) {
|
|
243
|
+
if (state.buffer.length === 0 || newlines === 0) return false;
|
|
244
|
+
const tail = state.buffer[state.buffer.length - 1];
|
|
245
|
+
const trimmed = trimSpacesEnd(tail);
|
|
246
|
+
if (trimmed.length !== tail.length) state.buffer[state.buffer.length - 1] = trimmed;
|
|
247
|
+
const missing = newlines - trailingNewlines(state.buffer);
|
|
248
|
+
if (missing > 0) state.buffer.push("\n".repeat(missing));
|
|
249
|
+
return true;
|
|
250
|
+
}
|
|
251
|
+
function openCaptionBoundary() {
|
|
252
|
+
let emitted = false;
|
|
253
|
+
if (captionClosedSpacing !== 0) {
|
|
254
|
+
emitted = pushCaptionBoundary(captionClosedSpacing);
|
|
255
|
+
captionClosedSpacing = 0;
|
|
256
|
+
}
|
|
257
|
+
if (captionOpen > 0 && !captionContent) {
|
|
258
|
+
captionContent = true;
|
|
259
|
+
if (captionBreakRun === 0) emitted = pushCaptionBoundary(captionEnterSpacing) || emitted;
|
|
260
|
+
}
|
|
261
|
+
return emitted;
|
|
262
|
+
}
|
|
263
|
+
function processTextNode(node, lastNode) {
|
|
264
|
+
if (node.trimsAtLineStart) trimTextAtLineStart(node, state.buffer);
|
|
265
|
+
if (node.excludedFromMarkdown || !node.value) return;
|
|
266
|
+
if (hasVisibleContent(node.value) && openCaptionBoundary()) {
|
|
267
|
+
node.value = trimAsciiWhitespaceStart(node.value);
|
|
268
|
+
state.pendingInlineWhitespace = false;
|
|
269
|
+
}
|
|
270
|
+
const last = charOf(lastOutputChar(state.buffer));
|
|
271
|
+
if (state.pendingInlineWhitespace) {
|
|
272
|
+
if (!node.value.trim()) return;
|
|
273
|
+
if (last && !" \n \r".includes(last) && !" \n \r".includes(node.value[0] || "")) node.value = ` ${node.value}`;
|
|
274
|
+
state.pendingInlineWhitespace = false;
|
|
275
|
+
}
|
|
276
|
+
if (state.depthMap[34] && state.buffer.length === 0) preserveLeadingWhitespace = true;
|
|
277
|
+
if (node.value === " " && last !== "" && " \n \r".includes(last)) return;
|
|
278
|
+
if (captionBreakOwnsLine) {
|
|
279
|
+
captionBreakOwnsLine = false;
|
|
280
|
+
if (last === "\n" && !state.depthMap[34]) node.value = trimAsciiWhitespaceStart(node.value);
|
|
281
|
+
}
|
|
282
|
+
if (!state.depthMap[34] && shouldAddSpacingBeforeText(last, lastNode, node)) node.value = ` ${node.value}`;
|
|
283
|
+
const width = state.options.wrapWidth;
|
|
284
|
+
if (hasPlugins) {
|
|
285
|
+
columnScan[0] = 0;
|
|
286
|
+
columnScan[1] = bufferColumn;
|
|
287
|
+
}
|
|
288
|
+
const value = width && canWrapHere(state.depthMap) ? wrapText(node.value, currentColumn(state.buffer, bufferColumn, columnScan), width) : node.value;
|
|
289
|
+
state.buffer.push(value);
|
|
290
|
+
state.lastContentCache = value;
|
|
291
|
+
state.lastTextNode = node;
|
|
292
|
+
if (openQuotes.length !== 0 && hasNonWhitespace(value)) {
|
|
293
|
+
openQuotes.length = 0;
|
|
294
|
+
openQuoteNodes.length = 0;
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
function processEvent(event) {
|
|
298
|
+
state.depth = event.node.depth;
|
|
299
|
+
if (event.node.type === 1 ? event.node.excludedFromMarkdown : event.node.parent?.excludedFromMarkdown) return;
|
|
300
|
+
const lastNode = state.lastNode;
|
|
301
|
+
state.lastNode = event.node;
|
|
302
|
+
if (event.node.type === 2 && event.type === 0) {
|
|
303
|
+
processTextNode(event.node, lastNode);
|
|
304
|
+
return;
|
|
305
|
+
}
|
|
306
|
+
if (event.node.type !== 1) return;
|
|
307
|
+
const element = event.node;
|
|
308
|
+
let output;
|
|
309
|
+
if (element.tagId === 81 && event.type === 1 && openQuotes.length !== 0) {
|
|
310
|
+
let record = openQuoteNodes.length - 1;
|
|
311
|
+
while (record >= 0 && openQuoteNodes[record] !== element) record--;
|
|
312
|
+
if (record >= 0) {
|
|
313
|
+
const opener = openQuotes[record];
|
|
314
|
+
openQuotes.length = record;
|
|
315
|
+
openQuoteNodes.length = record;
|
|
316
|
+
if (!element.pluginOutput?.length) {
|
|
317
|
+
const buffer = state.buffer;
|
|
318
|
+
let empty = true;
|
|
319
|
+
for (let index = opener + 1; index < buffer.length; index++) if (hasNonWhitespace(buffer[index])) {
|
|
320
|
+
empty = false;
|
|
321
|
+
break;
|
|
322
|
+
}
|
|
323
|
+
if (empty) {
|
|
324
|
+
const openerOutput = buffer[opener];
|
|
325
|
+
buffer.length = opener;
|
|
326
|
+
if (openerOutput === " \"") buffer.push(" ");
|
|
327
|
+
state.lastNode = element;
|
|
328
|
+
return;
|
|
329
|
+
}
|
|
330
|
+
openQuotes.length = 0;
|
|
331
|
+
openQuoteNodes.length = 0;
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
if (element.pluginOutput?.length) {
|
|
336
|
+
output = element.pluginOutput.join("");
|
|
337
|
+
element.pluginOutput = void 0;
|
|
338
|
+
} else output = elementOutput(element, event.type, state);
|
|
339
|
+
if (element.tagId === 6 && event.type === 0 && output) {
|
|
340
|
+
const currentNewlines = trailingNewlines(state.buffer);
|
|
341
|
+
if (!state.depthMap[34] && currentNewlines >= 2) output = void 0;
|
|
342
|
+
state.pendingInlineWhitespace = false;
|
|
343
|
+
if (output && !state.depthMap[34] && state.buffer.length > 0) {
|
|
344
|
+
const tail = state.buffer[state.buffer.length - 1];
|
|
345
|
+
const trimmed = trimSpacesEnd(tail);
|
|
346
|
+
if (trimmed.length !== tail.length) state.buffer[state.buffer.length - 1] = trimmed;
|
|
347
|
+
}
|
|
348
|
+
if (output && captionOpen > 0 && !captionContent) captionBreakRun++;
|
|
349
|
+
}
|
|
350
|
+
const ownsCaptionSpace = element.tagId === 106 && !state.depthMap[34] && figcaptionOwnsBlockSpacing(element, true);
|
|
351
|
+
if (ownsCaptionSpace && event.type === 0) {
|
|
352
|
+
const spacing = element.tagHandler?.spacing;
|
|
353
|
+
captionEnterSpacing = spacing ? spacing[0] : CAPTION_SPACING;
|
|
354
|
+
captionExitSpacing = spacing ? spacing[1] : CAPTION_SPACING;
|
|
355
|
+
captionOpen++;
|
|
356
|
+
captionContent = false;
|
|
357
|
+
captionBreakRun = 0;
|
|
358
|
+
}
|
|
359
|
+
if (output && hasVisibleContent(output) && openCaptionBoundary()) {
|
|
360
|
+
output = trimAsciiWhitespaceStart(output);
|
|
361
|
+
state.pendingInlineWhitespace = false;
|
|
362
|
+
}
|
|
363
|
+
appendOutput(state, element, event.type, output);
|
|
364
|
+
if (element.tagId === 81 && event.type === 0 && (output === "\"" || output === " \"") && state.buffer.at(-1) === output) {
|
|
365
|
+
openQuotes.push(state.buffer.length - 1);
|
|
366
|
+
openQuoteNodes.push(element);
|
|
367
|
+
} else if (openQuotes.length !== 0) {
|
|
368
|
+
const isInline = element.tagHandler ? element.tagHandler.isInline === true : element.tagId === -1;
|
|
369
|
+
if (output && hasNonWhitespace(output) || element.tagId !== -1 && !isInline) {
|
|
370
|
+
openQuotes.length = 0;
|
|
371
|
+
openQuoteNodes.length = 0;
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
if (ownsCaptionSpace && event.type === 1) {
|
|
375
|
+
captionOpen--;
|
|
376
|
+
if (captionContent) captionClosedSpacing = captionExitSpacing;
|
|
377
|
+
else if (captionBreakRun > 0) captionBreakOwnsLine = true;
|
|
378
|
+
captionContent = false;
|
|
379
|
+
captionBreakRun = 0;
|
|
380
|
+
}
|
|
381
|
+
state.lastNode = element;
|
|
382
|
+
}
|
|
383
|
+
function dropYielded(content, stableEnd) {
|
|
384
|
+
const buffer = state.buffer;
|
|
385
|
+
let keepFrom = buffer.length;
|
|
386
|
+
let keptStart = content.length;
|
|
387
|
+
while (keepFrom > 0 && keptStart > stableEnd) {
|
|
388
|
+
keepFrom--;
|
|
389
|
+
keptStart -= buffer[keepFrom].length;
|
|
390
|
+
}
|
|
391
|
+
let contextStart = keptStart > YIELDED_CONTEXT ? keptStart - YIELDED_CONTEXT : 0;
|
|
392
|
+
const first = content.charCodeAt(contextStart);
|
|
393
|
+
if (contextStart > 0 && first >= 56320 && first <= 57343) contextStart--;
|
|
394
|
+
if (contextStart === 0) return;
|
|
395
|
+
if (state.options.wrapWidth) bufferColumn = columnAt(content, contextStart, bufferColumn);
|
|
396
|
+
const kept = buffer.slice(keepFrom);
|
|
397
|
+
buffer.length = 0;
|
|
398
|
+
buffer.push(content.slice(contextStart, keptStart));
|
|
399
|
+
for (let index = 0; index < kept.length; index++) buffer.push(kept[index]);
|
|
400
|
+
const shift = keepFrom - 1;
|
|
401
|
+
for (let index = 0; index < openQuotes.length; index++) openQuotes[index] -= shift;
|
|
402
|
+
columnScan[0] = 0;
|
|
403
|
+
columnScan[1] = bufferColumn;
|
|
404
|
+
yieldedLength = stableEnd - contextStart;
|
|
405
|
+
}
|
|
406
|
+
return {
|
|
407
|
+
state,
|
|
408
|
+
processEvent,
|
|
409
|
+
takeOutput(final) {
|
|
410
|
+
const content = state.buffer.join("");
|
|
411
|
+
const leading = started || preserveLeadingWhitespace ? 0 : content.length - trimOutputStart(content).length;
|
|
412
|
+
let stableEnd = content.length;
|
|
413
|
+
if (openQuotes.length !== 0 && !final) {
|
|
414
|
+
stableEnd = 0;
|
|
415
|
+
for (let index = 0; index < openQuotes[0]; index++) stableEnd += state.buffer[index].length;
|
|
416
|
+
}
|
|
417
|
+
while (stableEnd > leading) {
|
|
418
|
+
const code = content.charCodeAt(stableEnd - 1);
|
|
419
|
+
if (code !== 32 && (code < 9 || code > 13)) break;
|
|
420
|
+
stableEnd--;
|
|
421
|
+
}
|
|
422
|
+
const yieldedEnd = started ? yieldedLength : leading;
|
|
423
|
+
if (stableEnd < yieldedEnd) stableEnd = yieldedEnd;
|
|
424
|
+
const output = content.slice(yieldedEnd, stableEnd);
|
|
425
|
+
if (output) {
|
|
426
|
+
started = true;
|
|
427
|
+
yieldedLength = stableEnd;
|
|
428
|
+
dropYielded(content, stableEnd);
|
|
429
|
+
}
|
|
430
|
+
return output;
|
|
431
|
+
}
|
|
432
|
+
};
|
|
433
|
+
}
|
|
434
|
+
const textTagHandlers = { ...parserTagHandlers };
|
|
435
|
+
function assign(tagIds, metadata) {
|
|
436
|
+
for (const tagId of tagIds) textTagHandlers[tagId] = {
|
|
437
|
+
...textTagHandlers[tagId],
|
|
438
|
+
...metadata
|
|
439
|
+
};
|
|
440
|
+
}
|
|
441
|
+
assign([
|
|
442
|
+
14,
|
|
443
|
+
15,
|
|
444
|
+
16,
|
|
445
|
+
17,
|
|
446
|
+
18,
|
|
447
|
+
110,
|
|
448
|
+
111,
|
|
449
|
+
19,
|
|
450
|
+
20,
|
|
451
|
+
21,
|
|
452
|
+
23,
|
|
453
|
+
26,
|
|
454
|
+
27,
|
|
455
|
+
37,
|
|
456
|
+
42,
|
|
457
|
+
43,
|
|
458
|
+
46,
|
|
459
|
+
55,
|
|
460
|
+
56,
|
|
461
|
+
59,
|
|
462
|
+
60,
|
|
463
|
+
64,
|
|
464
|
+
79,
|
|
465
|
+
80,
|
|
466
|
+
81,
|
|
467
|
+
82,
|
|
468
|
+
83,
|
|
469
|
+
89,
|
|
470
|
+
90,
|
|
471
|
+
91,
|
|
472
|
+
92,
|
|
473
|
+
93,
|
|
474
|
+
94,
|
|
475
|
+
95,
|
|
476
|
+
96,
|
|
477
|
+
97
|
|
478
|
+
], {
|
|
479
|
+
collapsesInnerWhiteSpace: true,
|
|
480
|
+
isInline: true,
|
|
481
|
+
spacing: NO_SPACING
|
|
482
|
+
});
|
|
483
|
+
assign([106], {
|
|
484
|
+
collapsesInnerWhiteSpace: true,
|
|
485
|
+
isInline: true
|
|
486
|
+
});
|
|
487
|
+
assign([
|
|
488
|
+
1,
|
|
489
|
+
5,
|
|
490
|
+
6,
|
|
491
|
+
31,
|
|
492
|
+
32,
|
|
493
|
+
38,
|
|
494
|
+
44,
|
|
495
|
+
45,
|
|
496
|
+
47,
|
|
497
|
+
40,
|
|
498
|
+
54,
|
|
499
|
+
57,
|
|
500
|
+
58,
|
|
501
|
+
61,
|
|
502
|
+
62,
|
|
503
|
+
63,
|
|
504
|
+
65,
|
|
505
|
+
4,
|
|
506
|
+
67,
|
|
507
|
+
109,
|
|
508
|
+
68,
|
|
509
|
+
69,
|
|
510
|
+
49,
|
|
511
|
+
84,
|
|
512
|
+
112,
|
|
513
|
+
108,
|
|
514
|
+
85
|
|
515
|
+
], { spacing: NO_SPACING });
|
|
516
|
+
assign([
|
|
517
|
+
29,
|
|
518
|
+
30,
|
|
519
|
+
39
|
|
520
|
+
], { spacing: TABLE_ROW_SPACING });
|
|
521
|
+
assign([25], { spacing: LIST_ITEM_SPACING });
|
|
522
|
+
assign([98], { spacing: [0, 1] });
|
|
523
|
+
assign([6, 54], {
|
|
524
|
+
collapsesInnerWhiteSpace: true,
|
|
525
|
+
isInline: true
|
|
526
|
+
});
|
|
527
|
+
assign([
|
|
528
|
+
88,
|
|
529
|
+
70,
|
|
530
|
+
72,
|
|
531
|
+
75,
|
|
532
|
+
101,
|
|
533
|
+
73,
|
|
534
|
+
74,
|
|
535
|
+
76,
|
|
536
|
+
87,
|
|
537
|
+
77,
|
|
538
|
+
78,
|
|
539
|
+
66,
|
|
540
|
+
71,
|
|
541
|
+
86
|
|
542
|
+
], { spacing: NO_SPACING });
|
|
543
|
+
assign([100], {
|
|
544
|
+
collapsesInnerWhiteSpace: true,
|
|
545
|
+
spacing: NO_SPACING
|
|
546
|
+
});
|
|
547
|
+
assign([
|
|
548
|
+
7,
|
|
549
|
+
8,
|
|
550
|
+
9,
|
|
551
|
+
10,
|
|
552
|
+
11,
|
|
553
|
+
12,
|
|
554
|
+
1,
|
|
555
|
+
5,
|
|
556
|
+
32,
|
|
557
|
+
31,
|
|
558
|
+
4
|
|
559
|
+
], { collapsesInnerWhiteSpace: true });
|
|
560
|
+
assign([22], { spacing: DEFAULT_BLOCK_SPACING });
|
|
561
|
+
assign([99], {
|
|
562
|
+
collapsesInnerWhiteSpace: true,
|
|
563
|
+
spacing: [0, 1]
|
|
564
|
+
});
|
|
565
|
+
function resolveOutputOptions(options) {
|
|
566
|
+
assertEngineOptions(options);
|
|
567
|
+
checkClean(options);
|
|
568
|
+
return {
|
|
569
|
+
plugins: resolvePlugins(options.plugins),
|
|
570
|
+
tagHandlers: textTagHandlers,
|
|
571
|
+
tagOverrideHandlers: options.tagOverrides ? buildTagOverrideHandlers(options.tagOverrides, textTagHandlers) : void 0,
|
|
572
|
+
plainText: true
|
|
573
|
+
};
|
|
574
|
+
}
|
|
575
|
+
function htmlToText(html, options = {}) {
|
|
576
|
+
const outputOptions = resolveOutputOptions(options);
|
|
577
|
+
return processHtmlOutput(html, createTextOutputProcessor(options, outputOptions.plugins.length !== 0), outputOptions);
|
|
578
|
+
}
|
|
579
|
+
function streamHtmlToText(htmlStream, options = {}) {
|
|
580
|
+
const outputOptions = resolveOutputOptions(options);
|
|
581
|
+
return streamHtmlOutput(htmlStream, createTextOutputProcessor(options, outputOptions.plugins.length !== 0), outputOptions);
|
|
582
|
+
}
|
|
583
|
+
export { htmlToText, streamHtmlToText };
|