@mdream/js 1.7.1 → 1.7.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/_chunks/index.d.mts +3 -4
- package/dist/_chunks/markdown-processor.mjs +905 -131
- package/dist/_chunks/minimal.d.mts +1 -2
- package/dist/_chunks/parse.mjs +146 -66
- package/dist/_chunks/stream.mjs +4 -2
- package/dist/_chunks/types.d.mts +27 -27
- package/dist/cli.d.mts +1 -1
- package/dist/cli.mjs +3 -2
- package/dist/core.d.mts +4 -5
- package/dist/core.mjs +4 -2
- package/dist/index.mjs +30 -1
- package/dist/llms-txt.d.mts +8 -9
- package/dist/negotiate.d.mts +4 -5
- package/dist/parse.d.mts +7 -8
- package/dist/plugins.d.mts +7 -7
- package/dist/splitter.d.mts +3 -3
- package/package.json +1 -1
- package/dist/_chunks/src.mjs +0 -316
|
@@ -1,13 +1,420 @@
|
|
|
1
1
|
import { DEFAULT_BLOCK_SPACING, NO_SPACING, TagIdMap, isInsideRawHtmlBlock } from "./const.mjs";
|
|
2
|
-
import { blockOpenPrefix, breakHandler, continuationPrefix, finalizeParse, getLanguageFromClass, isCharacterReferenceTail, isInsideHeading, isInsideTableCell, isSafeHtmlUrl, listMarkerLineStart, orderedItemNumber, parseHtmlStream, parseUnsignedInteger, renderBreak, resolveUrl } from "./parse.mjs";
|
|
2
|
+
import { blockOpenPrefix, breakHandler, continuationPrefix, escapeHtml, figcaptionOwnsBlockSpacing, finalizeParse, getLanguageFromClass, isCharacterReferenceTail, isEmptyLinkHref, isInsideHeading, isInsideTableCell, isSafeHtmlUrl, lastOutputChar, listMarkerLineStart, markRenderedChildContent, orderedItemNumber, parseHtmlStream, parseUnsignedInteger, renderBreak, resolveUrl, safeAnchorOutput } from "./parse.mjs";
|
|
3
|
+
function resolveClean(clean) {
|
|
4
|
+
if (clean === true) return {
|
|
5
|
+
urls: true,
|
|
6
|
+
fragments: true,
|
|
7
|
+
emptyLinks: true,
|
|
8
|
+
redundantLinks: true,
|
|
9
|
+
selfLinkHeadings: true,
|
|
10
|
+
emptyImages: true,
|
|
11
|
+
emptyLinkText: true
|
|
12
|
+
};
|
|
13
|
+
if (clean === false) return {};
|
|
14
|
+
return clean;
|
|
15
|
+
}
|
|
16
|
+
function parseLink(md, start) {
|
|
17
|
+
const len = md.length;
|
|
18
|
+
let j = start + 1;
|
|
19
|
+
let depth = 1;
|
|
20
|
+
while (j < len && depth > 0) {
|
|
21
|
+
const c = md.charCodeAt(j);
|
|
22
|
+
if (c === 91) depth++;
|
|
23
|
+
else if (c === 93) depth--;
|
|
24
|
+
j++;
|
|
25
|
+
}
|
|
26
|
+
if (depth !== 0) return null;
|
|
27
|
+
const textEnd = j - 1;
|
|
28
|
+
if (j >= len || md.charCodeAt(j) !== 40) return null;
|
|
29
|
+
j++;
|
|
30
|
+
const urlStart = j;
|
|
31
|
+
let parenDepth = 1;
|
|
32
|
+
while (j < len && parenDepth > 0) {
|
|
33
|
+
const c = md.charCodeAt(j);
|
|
34
|
+
if (c === 40) parenDepth++;
|
|
35
|
+
else if (c === 41) parenDepth--;
|
|
36
|
+
j++;
|
|
37
|
+
}
|
|
38
|
+
if (parenDepth !== 0) return null;
|
|
39
|
+
const urlEnd = j - 1;
|
|
40
|
+
return {
|
|
41
|
+
text: md.slice(start + 1, textEnd),
|
|
42
|
+
url: md.slice(urlStart, urlEnd),
|
|
43
|
+
end: j
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
function isEscaped(md, index) {
|
|
47
|
+
const end = index;
|
|
48
|
+
while (index > 0 && md.charCodeAt(index - 1) === 92) index--;
|
|
49
|
+
return (end - index) % 2 !== 0;
|
|
50
|
+
}
|
|
51
|
+
function htmlTagEnd(md, start, lastGt) {
|
|
52
|
+
if (start >= lastGt || isEscaped(md, start)) return -1;
|
|
53
|
+
let i = start + 1;
|
|
54
|
+
if (md.charCodeAt(i) === 47) i++;
|
|
55
|
+
let code = md.charCodeAt(i);
|
|
56
|
+
if (!(code >= 65 && code <= 90 || code >= 97 && code <= 122)) return -1;
|
|
57
|
+
do {
|
|
58
|
+
i++;
|
|
59
|
+
code = md.charCodeAt(i);
|
|
60
|
+
} while (code >= 65 && code <= 90 || code >= 97 && code <= 122 || code >= 48 && code <= 57 || code === 45);
|
|
61
|
+
if (code !== 32 && code !== 9 && code !== 10 && code !== 13 && code !== 47 && code !== 62) return -1;
|
|
62
|
+
let quote = 0;
|
|
63
|
+
while (i < md.length) {
|
|
64
|
+
code = md.charCodeAt(i);
|
|
65
|
+
if (quote) {
|
|
66
|
+
if (code === quote) quote = 0;
|
|
67
|
+
} else if (code === 34 || code === 39) quote = code;
|
|
68
|
+
else if (code === 62) return i + 1;
|
|
69
|
+
else if (code === 60) return -1;
|
|
70
|
+
i++;
|
|
71
|
+
}
|
|
72
|
+
return -1;
|
|
73
|
+
}
|
|
74
|
+
function slugify(text) {
|
|
75
|
+
let slug = "";
|
|
76
|
+
let lastWasDash = false;
|
|
77
|
+
for (let i = 0; i < text.length; i++) {
|
|
78
|
+
const c = text.charCodeAt(i);
|
|
79
|
+
if (c >= 97 && c <= 122) {
|
|
80
|
+
slug += text[i];
|
|
81
|
+
lastWasDash = false;
|
|
82
|
+
} else if (c >= 65 && c <= 90) {
|
|
83
|
+
slug += String.fromCharCode(c + 32);
|
|
84
|
+
lastWasDash = false;
|
|
85
|
+
} else if (c >= 48 && c <= 57) {
|
|
86
|
+
slug += text[i];
|
|
87
|
+
lastWasDash = false;
|
|
88
|
+
} else if (c === 95) {
|
|
89
|
+
slug += "_";
|
|
90
|
+
lastWasDash = false;
|
|
91
|
+
} else if (c === 32 || c === 9 || c === 45) {
|
|
92
|
+
if (!lastWasDash && slug.length > 0) {
|
|
93
|
+
slug += "-";
|
|
94
|
+
lastWasDash = true;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
if (lastWasDash) slug = slug.slice(0, -1);
|
|
99
|
+
return slug;
|
|
100
|
+
}
|
|
101
|
+
function stripHeadingFormatting(text) {
|
|
102
|
+
let result = "";
|
|
103
|
+
const len = text.length;
|
|
104
|
+
const lastGt = text.lastIndexOf(">");
|
|
105
|
+
let firstTickLength = 0;
|
|
106
|
+
let firstTickLast = 0;
|
|
107
|
+
let otherTickLasts;
|
|
108
|
+
if (text.includes("`")) {
|
|
109
|
+
let scan = 0;
|
|
110
|
+
while (scan < len) {
|
|
111
|
+
let code = text.charCodeAt(scan);
|
|
112
|
+
if (code === 96) {
|
|
113
|
+
const start = scan;
|
|
114
|
+
do {
|
|
115
|
+
scan++;
|
|
116
|
+
code = text.charCodeAt(scan);
|
|
117
|
+
} while (code === 96);
|
|
118
|
+
const ticks = scan - start;
|
|
119
|
+
if (firstTickLength === 0) {
|
|
120
|
+
firstTickLength = ticks;
|
|
121
|
+
firstTickLast = start;
|
|
122
|
+
} else if (ticks === firstTickLength) firstTickLast = start;
|
|
123
|
+
else {
|
|
124
|
+
otherTickLasts ||= /* @__PURE__ */ new Map();
|
|
125
|
+
otherTickLasts.set(ticks, start);
|
|
126
|
+
}
|
|
127
|
+
} else if (code === 60) {
|
|
128
|
+
const tagEnd = htmlTagEnd(text, scan, lastGt);
|
|
129
|
+
scan = tagEnd === -1 ? scan + 1 : tagEnd;
|
|
130
|
+
} else scan++;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
let i = 0;
|
|
134
|
+
let codeTicks = 0;
|
|
135
|
+
while (i < len) {
|
|
136
|
+
const c = text.charCodeAt(i);
|
|
137
|
+
if (c === 96) {
|
|
138
|
+
let end = i + 1;
|
|
139
|
+
while (text.charCodeAt(end) === 96) end++;
|
|
140
|
+
const ticks = end - i;
|
|
141
|
+
if (codeTicks === 0) {
|
|
142
|
+
if ((ticks === firstTickLength ? firstTickLast : otherTickLasts?.get(ticks) ?? i) > i) codeTicks = ticks;
|
|
143
|
+
} else if (codeTicks === ticks) codeTicks = 0;
|
|
144
|
+
i = end;
|
|
145
|
+
continue;
|
|
146
|
+
}
|
|
147
|
+
if (codeTicks !== 0) {
|
|
148
|
+
result += text[i];
|
|
149
|
+
i++;
|
|
150
|
+
continue;
|
|
151
|
+
}
|
|
152
|
+
if (c === 92 && i + 1 < len) {
|
|
153
|
+
result += text[i + 1];
|
|
154
|
+
i += 2;
|
|
155
|
+
continue;
|
|
156
|
+
}
|
|
157
|
+
const tagEnd = c === 60 ? htmlTagEnd(text, i, lastGt) : -1;
|
|
158
|
+
if (tagEnd !== -1) {
|
|
159
|
+
i = tagEnd;
|
|
160
|
+
continue;
|
|
161
|
+
}
|
|
162
|
+
if (c === 91) {
|
|
163
|
+
const link = parseLink(text, i);
|
|
164
|
+
if (link) {
|
|
165
|
+
result += link.text;
|
|
166
|
+
i = link.end;
|
|
167
|
+
continue;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
if (c === 42 || c === 95 || c === 126) {
|
|
171
|
+
i++;
|
|
172
|
+
continue;
|
|
173
|
+
}
|
|
174
|
+
result += text[i];
|
|
175
|
+
i++;
|
|
176
|
+
}
|
|
177
|
+
return result.trim();
|
|
178
|
+
}
|
|
179
|
+
function collectHeadingSlugs(md) {
|
|
180
|
+
const slugs = /* @__PURE__ */ new Set();
|
|
181
|
+
const len = md.length;
|
|
182
|
+
let i = 0;
|
|
183
|
+
while (i < len) {
|
|
184
|
+
if (i === 0 || md.charCodeAt(i - 1) === 10) {
|
|
185
|
+
let hashes = 0;
|
|
186
|
+
let j = i;
|
|
187
|
+
while (j < len && md.charCodeAt(j) === 35) {
|
|
188
|
+
hashes++;
|
|
189
|
+
j++;
|
|
190
|
+
}
|
|
191
|
+
if (hashes >= 1 && hashes <= 6 && j < len && md.charCodeAt(j) === 32) {
|
|
192
|
+
j++;
|
|
193
|
+
const lineEnd = md.indexOf("\n", j);
|
|
194
|
+
const cleaned = stripHeadingFormatting(lineEnd === -1 ? md.slice(j) : md.slice(j, lineEnd));
|
|
195
|
+
if (cleaned) slugs.add(slugify(cleaned));
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
const nl = md.indexOf("\n", i);
|
|
199
|
+
if (nl === -1) break;
|
|
200
|
+
i = nl + 1;
|
|
201
|
+
}
|
|
202
|
+
return slugs;
|
|
203
|
+
}
|
|
204
|
+
function cleanFragments(md) {
|
|
205
|
+
const slugs = collectHeadingSlugs(md);
|
|
206
|
+
const len = md.length;
|
|
207
|
+
const lastGt = md.lastIndexOf(">");
|
|
208
|
+
let result = "";
|
|
209
|
+
let i = 0;
|
|
210
|
+
while (i < len) {
|
|
211
|
+
const tagEnd = md.charCodeAt(i) === 60 ? htmlTagEnd(md, i, lastGt) : -1;
|
|
212
|
+
if (tagEnd !== -1) {
|
|
213
|
+
result += md.slice(i, tagEnd);
|
|
214
|
+
i = tagEnd;
|
|
215
|
+
continue;
|
|
216
|
+
}
|
|
217
|
+
if (md.charCodeAt(i) === 91) {
|
|
218
|
+
const link = parseLink(md, i);
|
|
219
|
+
if (link && link.url.charCodeAt(0) === 35 && link.url.length > 1) {
|
|
220
|
+
const fragment = link.url.slice(1).split(" ")[0];
|
|
221
|
+
if (slugs.size > 0 ? !slugs.has(fragment) : true) {
|
|
222
|
+
result += link.text;
|
|
223
|
+
i = link.end;
|
|
224
|
+
continue;
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
result += md[i];
|
|
229
|
+
i++;
|
|
230
|
+
}
|
|
231
|
+
return result;
|
|
232
|
+
}
|
|
233
|
+
function cleanEmptyLinks(md) {
|
|
234
|
+
const len = md.length;
|
|
235
|
+
const lastGt = md.lastIndexOf(">");
|
|
236
|
+
let result = "";
|
|
237
|
+
let i = 0;
|
|
238
|
+
while (i < len) {
|
|
239
|
+
const tagEnd = md.charCodeAt(i) === 60 ? htmlTagEnd(md, i, lastGt) : -1;
|
|
240
|
+
if (tagEnd !== -1) {
|
|
241
|
+
result += md.slice(i, tagEnd);
|
|
242
|
+
i = tagEnd;
|
|
243
|
+
continue;
|
|
244
|
+
}
|
|
245
|
+
if (md.charCodeAt(i) === 91) {
|
|
246
|
+
const link = parseLink(md, i);
|
|
247
|
+
if (link) {
|
|
248
|
+
const url = link.url;
|
|
249
|
+
if (isEmptyLinkHref(url)) {
|
|
250
|
+
result += link.text;
|
|
251
|
+
i = link.end;
|
|
252
|
+
continue;
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
result += md[i];
|
|
257
|
+
i++;
|
|
258
|
+
}
|
|
259
|
+
return result;
|
|
260
|
+
}
|
|
261
|
+
function isAutolinkUri(s) {
|
|
262
|
+
if (!(s.startsWith("http://") || s.startsWith("https://") || s.startsWith("ftp://") || s.startsWith("mailto:"))) return false;
|
|
263
|
+
for (let i = 0; i < s.length; i++) {
|
|
264
|
+
const c = s.charCodeAt(i);
|
|
265
|
+
if (c === 32 || c === 60 || c === 62 || c === 10 || c === 13 || c === 9) return false;
|
|
266
|
+
}
|
|
267
|
+
return true;
|
|
268
|
+
}
|
|
269
|
+
function cleanRedundantLinks(md) {
|
|
270
|
+
const len = md.length;
|
|
271
|
+
const lastGt = md.lastIndexOf(">");
|
|
272
|
+
let result = "";
|
|
273
|
+
let i = 0;
|
|
274
|
+
while (i < len) {
|
|
275
|
+
const code = md.charCodeAt(i);
|
|
276
|
+
const tagEnd = code === 60 ? htmlTagEnd(md, i, lastGt) : -1;
|
|
277
|
+
if (tagEnd !== -1) {
|
|
278
|
+
result += md.slice(i, tagEnd);
|
|
279
|
+
i = tagEnd;
|
|
280
|
+
continue;
|
|
281
|
+
}
|
|
282
|
+
if (code === 91) {
|
|
283
|
+
const link = parseLink(md, i);
|
|
284
|
+
if (link && link.text === link.url) {
|
|
285
|
+
result += link.text;
|
|
286
|
+
i = link.end;
|
|
287
|
+
continue;
|
|
288
|
+
}
|
|
289
|
+
} else if (code === 60 && !isEscaped(md, i)) {
|
|
290
|
+
const close = md.indexOf(">", i + 1);
|
|
291
|
+
if (close !== -1) {
|
|
292
|
+
const inner = md.slice(i + 1, close);
|
|
293
|
+
if (isAutolinkUri(inner)) {
|
|
294
|
+
result += inner;
|
|
295
|
+
i = close + 1;
|
|
296
|
+
continue;
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
result += md[i];
|
|
301
|
+
i++;
|
|
302
|
+
}
|
|
303
|
+
return result;
|
|
304
|
+
}
|
|
305
|
+
function cleanSelfLinkHeadings(md) {
|
|
306
|
+
const len = md.length;
|
|
307
|
+
const lastGt = md.lastIndexOf(">");
|
|
308
|
+
let result = "";
|
|
309
|
+
let i = 0;
|
|
310
|
+
while (i < len) {
|
|
311
|
+
const tagEnd = md.charCodeAt(i) === 60 ? htmlTagEnd(md, i, lastGt) : -1;
|
|
312
|
+
if (tagEnd !== -1) {
|
|
313
|
+
result += md.slice(i, tagEnd);
|
|
314
|
+
i = tagEnd;
|
|
315
|
+
continue;
|
|
316
|
+
}
|
|
317
|
+
if (i === 0 || md.charCodeAt(i - 1) === 10) {
|
|
318
|
+
let hashes = 0;
|
|
319
|
+
let j = i;
|
|
320
|
+
while (j < len && md.charCodeAt(j) === 35) {
|
|
321
|
+
hashes++;
|
|
322
|
+
j++;
|
|
323
|
+
}
|
|
324
|
+
if (hashes >= 1 && hashes <= 6 && j < len && md.charCodeAt(j) === 32) {
|
|
325
|
+
j++;
|
|
326
|
+
if (j < len && md.charCodeAt(j) === 91) {
|
|
327
|
+
const link = parseLink(md, j);
|
|
328
|
+
if (link && link.url.charCodeAt(0) === 35) {
|
|
329
|
+
result += md.slice(i, j);
|
|
330
|
+
result += link.text;
|
|
331
|
+
i = link.end;
|
|
332
|
+
continue;
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
result += md[i];
|
|
338
|
+
i++;
|
|
339
|
+
}
|
|
340
|
+
return result;
|
|
341
|
+
}
|
|
342
|
+
function cleanEmptyImages(md) {
|
|
343
|
+
const len = md.length;
|
|
344
|
+
const lastGt = md.lastIndexOf(">");
|
|
345
|
+
let result = "";
|
|
346
|
+
let i = 0;
|
|
347
|
+
while (i < len) {
|
|
348
|
+
const tagEnd = md.charCodeAt(i) === 60 ? htmlTagEnd(md, i, lastGt) : -1;
|
|
349
|
+
if (tagEnd !== -1) {
|
|
350
|
+
result += md.slice(i, tagEnd);
|
|
351
|
+
i = tagEnd;
|
|
352
|
+
continue;
|
|
353
|
+
}
|
|
354
|
+
if (md.charCodeAt(i) === 33 && i + 1 < len && md.charCodeAt(i + 1) === 91) {
|
|
355
|
+
let j = i + 2;
|
|
356
|
+
const altStart = j;
|
|
357
|
+
while (j < len && md.charCodeAt(j) !== 93) j++;
|
|
358
|
+
if (j < len) {
|
|
359
|
+
if (md.slice(altStart, j).trim().length === 0) {
|
|
360
|
+
j++;
|
|
361
|
+
if (j < len && md.charCodeAt(j) === 40) {
|
|
362
|
+
let parenDepth = 1;
|
|
363
|
+
j++;
|
|
364
|
+
while (j < len && parenDepth > 0) {
|
|
365
|
+
if (md.charCodeAt(j) === 40) parenDepth++;
|
|
366
|
+
else if (md.charCodeAt(j) === 41) parenDepth--;
|
|
367
|
+
j++;
|
|
368
|
+
}
|
|
369
|
+
i = j;
|
|
370
|
+
continue;
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
result += md[i];
|
|
376
|
+
i++;
|
|
377
|
+
}
|
|
378
|
+
return result;
|
|
379
|
+
}
|
|
380
|
+
function cleanEmptyLinkText(md) {
|
|
381
|
+
const len = md.length;
|
|
382
|
+
const lastGt = md.lastIndexOf(">");
|
|
383
|
+
let result = "";
|
|
384
|
+
let i = 0;
|
|
385
|
+
while (i < len) {
|
|
386
|
+
const tagEnd = md.charCodeAt(i) === 60 ? htmlTagEnd(md, i, lastGt) : -1;
|
|
387
|
+
if (tagEnd !== -1) {
|
|
388
|
+
result += md.slice(i, tagEnd);
|
|
389
|
+
i = tagEnd;
|
|
390
|
+
continue;
|
|
391
|
+
}
|
|
392
|
+
if (md.charCodeAt(i) === 91) {
|
|
393
|
+
const link = parseLink(md, i);
|
|
394
|
+
if (link && link.text.trim().length === 0) {
|
|
395
|
+
i = link.end;
|
|
396
|
+
continue;
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
result += md[i];
|
|
400
|
+
i++;
|
|
401
|
+
}
|
|
402
|
+
return result;
|
|
403
|
+
}
|
|
404
|
+
function applyClean(md, opts) {
|
|
405
|
+
if (opts.emptyImages) md = cleanEmptyImages(md);
|
|
406
|
+
if (opts.emptyLinks) md = cleanEmptyLinks(md);
|
|
407
|
+
if (opts.emptyLinkText) md = cleanEmptyLinkText(md);
|
|
408
|
+
if (opts.redundantLinks) md = cleanRedundantLinks(md);
|
|
409
|
+
if (opts.selfLinkHeadings) md = cleanSelfLinkHeadings(md);
|
|
410
|
+
if (opts.fragments) md = cleanFragments(md);
|
|
411
|
+
return md;
|
|
412
|
+
}
|
|
3
413
|
function createHtmlOutputState() {
|
|
4
414
|
return { frames: [] };
|
|
5
415
|
}
|
|
6
|
-
function escapeHtml(value, attribute = false) {
|
|
7
|
-
return value.replace(attribute ? /[&<>"]/g : /[&<>]/g, (character) => character === "&" ? "&" : character === "<" ? "<" : character === ">" ? ">" : """);
|
|
8
|
-
}
|
|
9
416
|
function slugifyHeading(text) {
|
|
10
|
-
return
|
|
417
|
+
return slugify(stripHeadingFormatting(text));
|
|
11
418
|
}
|
|
12
419
|
function tagName(node, tagId) {
|
|
13
420
|
switch (tagId) {
|
|
@@ -31,14 +438,8 @@ function resolveSafeUrl(url, options, image = false) {
|
|
|
31
438
|
const resolved = resolveUrl(url, options.origin, options.clean);
|
|
32
439
|
return isSafeHtmlUrl(resolved, image) ? resolved : void 0;
|
|
33
440
|
}
|
|
34
|
-
function safeAttributes(node, tagId
|
|
441
|
+
function safeAttributes(node, tagId) {
|
|
35
442
|
const attributes = node.attributes;
|
|
36
|
-
if (tagId === 26) {
|
|
37
|
-
const href = attributes.href && resolveSafeUrl(attributes.href, options);
|
|
38
|
-
if (!href) return void 0;
|
|
39
|
-
const title = attributes.title === void 0 ? "" : ` title="${escapeHtml(attributes.title, true)}"`;
|
|
40
|
-
return ` href="${escapeHtml(href, true)}"${title}`;
|
|
41
|
-
}
|
|
42
443
|
if (tagId === 33) {
|
|
43
444
|
const start = parseUnsignedInteger(attributes.start);
|
|
44
445
|
return start === void 0 ? "" : ` start="${start}"`;
|
|
@@ -134,14 +535,22 @@ function processHtmlOutputEvent(event, state, output, options) {
|
|
|
134
535
|
if (tagId === 27) {
|
|
135
536
|
if (type === 0) {
|
|
136
537
|
const src = element.attributes.src && resolveSafeUrl(element.attributes.src, options, true);
|
|
137
|
-
if (src)
|
|
538
|
+
if (src) {
|
|
539
|
+
append(state, output, `<img src="${escapeHtml(src, true)}" alt="${escapeHtml(element.attributes.alt || "", true)}"${element.attributes.title === void 0 ? "" : ` title="${escapeHtml(element.attributes.title, true)}"`}>`);
|
|
540
|
+
markRenderedChildContent(element);
|
|
541
|
+
}
|
|
138
542
|
}
|
|
139
543
|
return;
|
|
140
544
|
}
|
|
545
|
+
if (tagId === 26) {
|
|
546
|
+
const rendered = safeAnchorOutput(element, options, type === 0);
|
|
547
|
+
if (rendered) append(state, output, rendered);
|
|
548
|
+
return;
|
|
549
|
+
}
|
|
141
550
|
if (tagId === void 0) return;
|
|
142
551
|
const name = tagName(element, tagId);
|
|
143
552
|
if (!name) return;
|
|
144
|
-
const attributes = safeAttributes(element, tagId
|
|
553
|
+
const attributes = safeAttributes(element, tagId);
|
|
145
554
|
if (attributes === void 0) return;
|
|
146
555
|
append(state, output, type === 0 ? `<${name}${attributes}>` : `</${name}>`);
|
|
147
556
|
}
|
|
@@ -181,12 +590,17 @@ function processPluginsForEvent(event, plugins, state, processEvent) {
|
|
|
181
590
|
return false;
|
|
182
591
|
}
|
|
183
592
|
const INLINE_MARKER_TYPE = /* @__PURE__ */ new Uint8Array(113);
|
|
593
|
+
const MARKER_TYPE_MASK = 7;
|
|
594
|
+
const CAPTION_FRAME_SIZE = 4;
|
|
595
|
+
const CAPTION_OPEN = 1;
|
|
596
|
+
const CAPTION_INTERNAL_BREAK = 2;
|
|
597
|
+
const CAPTION_DEFAULT_SPACING = -1;
|
|
598
|
+
const CAPTION_NO_ANCHOR = -1;
|
|
184
599
|
INLINE_MARKER_TYPE[14] = 1;
|
|
185
600
|
INLINE_MARKER_TYPE[15] = 1;
|
|
186
601
|
INLINE_MARKER_TYPE[91] = 1;
|
|
187
602
|
INLINE_MARKER_TYPE[16] = 2;
|
|
188
603
|
INLINE_MARKER_TYPE[17] = 2;
|
|
189
|
-
INLINE_MARKER_TYPE[106] = 2;
|
|
190
604
|
INLINE_MARKER_TYPE[18] = 3;
|
|
191
605
|
INLINE_MARKER_TYPE[110] = 3;
|
|
192
606
|
INLINE_MARKER_TYPE[111] = 3;
|
|
@@ -197,7 +611,7 @@ INLINE_MARKER_TYPE[82] = 5;
|
|
|
197
611
|
INLINE_MARKER_TYPE[92] = 5;
|
|
198
612
|
INLINE_MARKER_TYPE[81] = 6;
|
|
199
613
|
function suppressesFormattingInPre(tagId) {
|
|
200
|
-
return tagId === 26 || tagId === 46 || tagId === 110 || tagId === 111 || tagId >= 14 && tagId <= 21 || tagId >= 79 && tagId <= 83 || tagId >= 89 && tagId <= 94;
|
|
614
|
+
return tagId === 26 || tagId === 46 || tagId === 110 || tagId === 111 || tagId === 106 || tagId >= 14 && tagId <= 21 || tagId >= 79 && tagId <= 83 || tagId >= 89 && tagId <= 94;
|
|
201
615
|
}
|
|
202
616
|
function newlineRunBefore(buffer, index) {
|
|
203
617
|
let run = 0;
|
|
@@ -210,6 +624,26 @@ function newlineRunBefore(buffer, index) {
|
|
|
210
624
|
}
|
|
211
625
|
return run;
|
|
212
626
|
}
|
|
627
|
+
function newlineRunBeforePrefix(buffer, index, prefix) {
|
|
628
|
+
let fragment = index - 1;
|
|
629
|
+
let offset = fragment >= 0 ? buffer[fragment].length : 0;
|
|
630
|
+
for (let prefixIndex = prefix.length - 1; prefixIndex >= 0; prefixIndex--) {
|
|
631
|
+
while (offset === 0) {
|
|
632
|
+
if (--fragment < 0) return newlineRunBefore(buffer, index);
|
|
633
|
+
offset = buffer[fragment].length;
|
|
634
|
+
}
|
|
635
|
+
if (buffer[fragment].charCodeAt(--offset) !== prefix.charCodeAt(prefixIndex)) return newlineRunBefore(buffer, index);
|
|
636
|
+
}
|
|
637
|
+
let run = 0;
|
|
638
|
+
for (;;) {
|
|
639
|
+
while (offset === 0) {
|
|
640
|
+
if (--fragment < 0) return run;
|
|
641
|
+
offset = buffer[fragment].length;
|
|
642
|
+
}
|
|
643
|
+
if (buffer[fragment].charCodeAt(--offset) !== 10) return run;
|
|
644
|
+
run++;
|
|
645
|
+
}
|
|
646
|
+
}
|
|
213
647
|
function recordItemMarker(state, index) {
|
|
214
648
|
if (index !== 0 || newlineRunBefore(state.buffer, state.buffer.length - 1) !== 1) {
|
|
215
649
|
state.emptyItemFragment = void 0;
|
|
@@ -217,7 +651,7 @@ function recordItemMarker(state, index) {
|
|
|
217
651
|
}
|
|
218
652
|
state.emptyItemFragment = state.buffer.length - 1;
|
|
219
653
|
}
|
|
220
|
-
function resolveItemMarker(state, atExit) {
|
|
654
|
+
function resolveItemMarker(state, atExit, unresolvedCaptionFragment = -1) {
|
|
221
655
|
const fragment = state.emptyItemFragment;
|
|
222
656
|
if (fragment === void 0) return;
|
|
223
657
|
const buffer = state.buffer;
|
|
@@ -233,6 +667,14 @@ function resolveItemMarker(state, atExit) {
|
|
|
233
667
|
}
|
|
234
668
|
}
|
|
235
669
|
if (first === -1 && !atExit) return;
|
|
670
|
+
if (!atExit && unresolvedCaptionFragment !== -1) {
|
|
671
|
+
let captionOpensItem = true;
|
|
672
|
+
for (let index = fragment + 1; index < unresolvedCaptionFragment; index++) if (hasNonWhitespace(buffer[index])) {
|
|
673
|
+
captionOpensItem = false;
|
|
674
|
+
break;
|
|
675
|
+
}
|
|
676
|
+
if (captionOpensItem) return;
|
|
677
|
+
}
|
|
236
678
|
state.emptyItemFragment = void 0;
|
|
237
679
|
if (first !== -1 && first !== 10) return;
|
|
238
680
|
buffer[fragment] = `\n${buffer[fragment]}`;
|
|
@@ -295,9 +737,8 @@ function foldPreLinesToBr(value) {
|
|
|
295
737
|
}
|
|
296
738
|
return out;
|
|
297
739
|
}
|
|
298
|
-
function escapeRawHtmlText(value, depthMap) {
|
|
740
|
+
function escapeRawHtmlText(value, depthMap, protectLinkText) {
|
|
299
741
|
const inTable = Boolean(depthMap[28]);
|
|
300
|
-
const inLink = Boolean(depthMap[26]);
|
|
301
742
|
let escaped = "";
|
|
302
743
|
let copiedUntil = 0;
|
|
303
744
|
let index = 0;
|
|
@@ -310,8 +751,8 @@ function escapeRawHtmlText(value, depthMap) {
|
|
|
310
751
|
else if (code === 10) replacement = " ";
|
|
311
752
|
else if (code === 13) replacement = " ";
|
|
312
753
|
else if (inTable && code === 124) replacement = "|";
|
|
313
|
-
else if (
|
|
314
|
-
else if (
|
|
754
|
+
else if (protectLinkText && code === 91) replacement = "[";
|
|
755
|
+
else if (protectLinkText && code === 93) replacement = "]";
|
|
315
756
|
if (replacement) {
|
|
316
757
|
escaped += value.slice(copiedUntil, index) + replacement;
|
|
317
758
|
copiedUntil = index + 1;
|
|
@@ -459,13 +900,16 @@ function escapeGfmText(value, buffer, depthMap) {
|
|
|
459
900
|
if (code === 10) {
|
|
460
901
|
lineIndent = 0;
|
|
461
902
|
orderedDigits = 0;
|
|
462
|
-
} else if (lineIndent >= 0)
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
903
|
+
} else if (lineIndent >= 0) {
|
|
904
|
+
if (code === 32 && lineIndent < 3) lineIndent++;
|
|
905
|
+
else {
|
|
906
|
+
orderedDigits = code >= 48 && code <= 57 ? 1 : 0;
|
|
907
|
+
lineIndent = -1;
|
|
908
|
+
}
|
|
909
|
+
} else if (orderedDigits > 0) {
|
|
910
|
+
if (code >= 48 && code <= 57 && orderedDigits < 9) orderedDigits++;
|
|
911
|
+
else orderedDigits = 0;
|
|
466
912
|
}
|
|
467
|
-
else if (orderedDigits > 0) if (code >= 48 && code <= 57 && orderedDigits < 9) orderedDigits++;
|
|
468
|
-
else orderedDigits = 0;
|
|
469
913
|
}
|
|
470
914
|
return copiedUntil === 0 ? value : escaped + value.slice(copiedUntil);
|
|
471
915
|
}
|
|
@@ -513,11 +957,12 @@ function wrapText(value, col, width, prefix) {
|
|
|
513
957
|
}
|
|
514
958
|
function calculateNewLineConfig(node, depthMap, plainText) {
|
|
515
959
|
const tagId = node.tagId;
|
|
960
|
+
if (plainText && tagId === 106) return NO_SPACING;
|
|
516
961
|
if (tagId !== 25 && (depthMap[25] || 0) > 0 || plainText && tagId !== 22 && (depthMap[22] || 0) > 0) {
|
|
517
962
|
if (plainText && tagId === 34) return [1, 1];
|
|
518
963
|
return NO_SPACING;
|
|
519
964
|
}
|
|
520
|
-
const isBlockElement = tagId !== void 0 && (tagId >= 7 && tagId <= 12 || tagId === 35 || tagId === 36 || tagId === 25);
|
|
965
|
+
const isBlockElement = tagId !== void 0 && (tagId >= 7 && tagId <= 12 || tagId === 35 || tagId === 36 || tagId === 25 || tagId === 106);
|
|
521
966
|
let currParent = node.parent;
|
|
522
967
|
while (currParent) {
|
|
523
968
|
if (currParent.tagHandler?.collapsesInnerWhiteSpace) {
|
|
@@ -536,7 +981,7 @@ function calculateNewLineConfig(node, depthMap, plainText) {
|
|
|
536
981
|
function hasNonWhitespace(value) {
|
|
537
982
|
for (let i = 0; i < value.length; i++) {
|
|
538
983
|
const c = value.charCodeAt(i);
|
|
539
|
-
if (c !== 32 && c !== 9 && c !== 10 && c !== 13) return true;
|
|
984
|
+
if (c !== 32 && c !== 9 && c !== 10 && c !== 12 && c !== 13) return true;
|
|
540
985
|
}
|
|
541
986
|
return false;
|
|
542
987
|
}
|
|
@@ -564,15 +1009,31 @@ function trimBufferedWhitespacePosition(content, position) {
|
|
|
564
1009
|
}
|
|
565
1010
|
function dropEmptyMarker(buffer, packed, markerType) {
|
|
566
1011
|
const idx = packed >> 3;
|
|
567
|
-
if ((packed &
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
1012
|
+
if ((packed & MARKER_TYPE_MASK) === markerType && idx < buffer.length) return dropEmptyLinkText(buffer, idx) ? idx : -1;
|
|
1013
|
+
return -1;
|
|
1014
|
+
}
|
|
1015
|
+
function resetRetractedCaptionFrames(frames, count, fragment) {
|
|
1016
|
+
if (!frames) return;
|
|
1017
|
+
for (let index = 0; index < count; index++) {
|
|
1018
|
+
const offset = index * CAPTION_FRAME_SIZE;
|
|
1019
|
+
if (frames[offset + 3] >= fragment) {
|
|
1020
|
+
frames[offset + 2] = frames[offset + 2] & -2;
|
|
1021
|
+
frames[offset + 3] = CAPTION_NO_ANCHOR;
|
|
571
1022
|
}
|
|
572
|
-
buffer.length = idx;
|
|
573
|
-
return idx;
|
|
574
1023
|
}
|
|
575
|
-
|
|
1024
|
+
}
|
|
1025
|
+
function dropEmptyLinkText(buffer, openFragment) {
|
|
1026
|
+
for (let index = openFragment + 1; index < buffer.length; index++) if (hasNonWhitespace(buffer[index])) return false;
|
|
1027
|
+
buffer.length = openFragment;
|
|
1028
|
+
return true;
|
|
1029
|
+
}
|
|
1030
|
+
function restoreCaptionBreakFlags(frames, snapshot, count) {
|
|
1031
|
+
if (!frames || !snapshot) return;
|
|
1032
|
+
const end = Math.min(count, snapshot.length);
|
|
1033
|
+
for (let index = 0; index < end; index++) {
|
|
1034
|
+
const offset = index * CAPTION_FRAME_SIZE + 2;
|
|
1035
|
+
frames[offset] = frames[offset] & -3 | snapshot[index];
|
|
1036
|
+
}
|
|
576
1037
|
}
|
|
577
1038
|
function maxBacktickRun(value) {
|
|
578
1039
|
let max = 0;
|
|
@@ -671,15 +1132,19 @@ function flushPreFence(state) {
|
|
|
671
1132
|
startCodeFence(state, state.buffer.length - 1, lang, state.listIndent);
|
|
672
1133
|
state.lastContentCache = fence;
|
|
673
1134
|
}
|
|
674
|
-
function consumePendingPreChild(state, node, eventType) {
|
|
1135
|
+
function consumePendingPreChild(state, node, eventType, beforeFlush) {
|
|
675
1136
|
if (eventType !== 0) return false;
|
|
676
1137
|
if (node.type === 1) {
|
|
677
1138
|
const element = node;
|
|
678
1139
|
if (element.tagId === 23 && element.parent?.tagId === 34) state.preFencePending = false;
|
|
679
|
-
else if (element.tagId !== 34
|
|
1140
|
+
else if (element.tagId !== 34 && element.tagId !== 106 && !suppressesFormattingInPre(element.tagId)) {
|
|
1141
|
+
beforeFlush();
|
|
1142
|
+
flushPreFence(state);
|
|
1143
|
+
}
|
|
680
1144
|
return false;
|
|
681
1145
|
}
|
|
682
1146
|
if (hasNonWhitespace(node.value)) {
|
|
1147
|
+
beforeFlush();
|
|
683
1148
|
flushPreFence(state);
|
|
684
1149
|
return false;
|
|
685
1150
|
}
|
|
@@ -759,8 +1224,11 @@ function getPlainTextOutput(node, eventType, state) {
|
|
|
759
1224
|
if (tagId === 32 || tagId === 31) return (depthMap[28] || 0) > 1 || node.index === 0 ? "" : " ";
|
|
760
1225
|
if (tagId === 27) {
|
|
761
1226
|
const alt = node.attributes?.alt;
|
|
762
|
-
|
|
763
|
-
|
|
1227
|
+
const clean = state.options?.clean;
|
|
1228
|
+
if ((clean === true || clean != null && clean !== false && clean.emptyImages === true) && !(alt !== void 0 && alt.trim().length > 0)) return void 0;
|
|
1229
|
+
const output = alt !== void 0 ? alt || void 0 : node.attributes?.title || resolveUrl(node.attributes?.src || "", state.options?.origin, state.options?.clean) || void 0;
|
|
1230
|
+
if (output && hasNonWhitespace(output)) markRenderedChildContent(node);
|
|
1231
|
+
return output;
|
|
764
1232
|
}
|
|
765
1233
|
if (tagId === 81) return "\"";
|
|
766
1234
|
return;
|
|
@@ -769,6 +1237,7 @@ function getPlainTextOutput(node, eventType, state) {
|
|
|
769
1237
|
}
|
|
770
1238
|
function createMarkdownProcessor(options = {}, resolvedPlugins = [], tagOverrideHandlers) {
|
|
771
1239
|
const outputFormat = options.format === "html" || options.format === "text" ? options.format : "markdown";
|
|
1240
|
+
const plainText = outputFormat === "text";
|
|
772
1241
|
const htmlState = outputFormat === "html" ? createHtmlOutputState() : void 0;
|
|
773
1242
|
const state = {
|
|
774
1243
|
options,
|
|
@@ -778,7 +1247,7 @@ function createMarkdownProcessor(options = {}, resolvedPlugins = [], tagOverride
|
|
|
778
1247
|
listIndentWidths: [],
|
|
779
1248
|
blockquotes: [],
|
|
780
1249
|
bufferedBlockquoteDepth: 0,
|
|
781
|
-
plainText
|
|
1250
|
+
plainText,
|
|
782
1251
|
emptyItemFragment: void 0
|
|
783
1252
|
};
|
|
784
1253
|
const bufferScan = [
|
|
@@ -791,22 +1260,182 @@ function createMarkdownProcessor(options = {}, resolvedPlugins = [], tagOverride
|
|
|
791
1260
|
let inRawHtmlRegion = false;
|
|
792
1261
|
const openMarkers = [];
|
|
793
1262
|
let openMarkerCount = 0;
|
|
1263
|
+
let captionFrames;
|
|
1264
|
+
let captionFrameCount = 0;
|
|
1265
|
+
let captionBoundary = 0;
|
|
1266
|
+
let captionBoundarySpacing = 0;
|
|
1267
|
+
let captionBreakRun = 0;
|
|
794
1268
|
const gfmLifecycle = { openCodeSpans: [] };
|
|
795
1269
|
let openLinkFragment = -1;
|
|
1270
|
+
let openLinkCaptionBreakRun = 0;
|
|
1271
|
+
let openLinkCaptionFlags;
|
|
1272
|
+
const clean = options.clean;
|
|
1273
|
+
const cleanEmptyLinkText = clean === true || clean !== null && typeof clean === "object" && clean.emptyLinkText === true;
|
|
1274
|
+
let rawHtmlLink;
|
|
796
1275
|
let lastYieldedLength = 0;
|
|
797
1276
|
let hasYieldedContent = false;
|
|
798
1277
|
let preserveLeadingWhitespace = false;
|
|
1278
|
+
function captionNeedsPreparation() {
|
|
1279
|
+
return captionBoundary !== 0 || captionFrameCount !== 0 && (captionFrames[(captionFrameCount - 1) * CAPTION_FRAME_SIZE + 2] & CAPTION_OPEN) === 0;
|
|
1280
|
+
}
|
|
1281
|
+
function pushCaptionFrame(element, handler) {
|
|
1282
|
+
let enterSpacing = 0;
|
|
1283
|
+
let exitSpacing = 0;
|
|
1284
|
+
if (!state.depthMap[34] && figcaptionOwnsBlockSpacing(element, plainText)) {
|
|
1285
|
+
const spacing = handler?.spacing;
|
|
1286
|
+
if (spacing) {
|
|
1287
|
+
enterSpacing = spacing[0];
|
|
1288
|
+
exitSpacing = spacing[1];
|
|
1289
|
+
} else {
|
|
1290
|
+
enterSpacing = CAPTION_DEFAULT_SPACING;
|
|
1291
|
+
exitSpacing = CAPTION_DEFAULT_SPACING;
|
|
1292
|
+
}
|
|
1293
|
+
}
|
|
1294
|
+
const frames = captionFrames || (captionFrames = []);
|
|
1295
|
+
const offset = captionFrameCount * CAPTION_FRAME_SIZE;
|
|
1296
|
+
frames[offset] = enterSpacing;
|
|
1297
|
+
frames[offset + 1] = exitSpacing;
|
|
1298
|
+
frames[offset + 2] = 0;
|
|
1299
|
+
frames[offset + 3] = CAPTION_NO_ANCHOR;
|
|
1300
|
+
captionFrameCount++;
|
|
1301
|
+
}
|
|
1302
|
+
function captionSpacingPrefix(spacing, includeListIndent, beforeFragment) {
|
|
1303
|
+
if (spacing === 0) return "";
|
|
1304
|
+
if (spacing === CAPTION_DEFAULT_SPACING) spacing = DEFAULT_BLOCK_SPACING[0];
|
|
1305
|
+
const indent = includeListIndent ? state.listIndent : "";
|
|
1306
|
+
const blockPrefix = blockOpenPrefix(state.buffer, indent, beforeFragment);
|
|
1307
|
+
const trailingNewLines = newlineRunBeforePrefix(state.buffer, beforeFragment, indent);
|
|
1308
|
+
if (blockPrefix === void 0 && trailingNewLines === 0) return "";
|
|
1309
|
+
const missingNewLines = Math.max(0, spacing - trailingNewLines);
|
|
1310
|
+
const prefix = missingNewLines === 0 ? blockPrefix || "" : `${"\n".repeat(missingNewLines)}${indent}`;
|
|
1311
|
+
if (!prefix) return "";
|
|
1312
|
+
const lastIndex = beforeFragment - 1;
|
|
1313
|
+
const last = state.buffer[lastIndex];
|
|
1314
|
+
if (prefix.charCodeAt(0) === 10 && last?.endsWith(" ")) {
|
|
1315
|
+
const trimmed = last.slice(0, -1);
|
|
1316
|
+
state.buffer[lastIndex] = trimmed;
|
|
1317
|
+
if (state.lastContentCache === last) state.lastContentCache = trimmed;
|
|
1318
|
+
state.lastTextNode = void 0;
|
|
1319
|
+
}
|
|
1320
|
+
return prefix;
|
|
1321
|
+
}
|
|
1322
|
+
function appendCaptionSpacing(spacing, includeListIndent) {
|
|
1323
|
+
const prefix = captionSpacingPrefix(spacing, includeListIndent, state.buffer.length);
|
|
1324
|
+
if (!prefix) return false;
|
|
1325
|
+
state.buffer.push(prefix);
|
|
1326
|
+
state.lastContentCache = prefix;
|
|
1327
|
+
state.pendingInlineWhitespace = false;
|
|
1328
|
+
return true;
|
|
1329
|
+
}
|
|
1330
|
+
function prepareCaptionOutput(explicitTop, includeBoundaryListIndent = true) {
|
|
1331
|
+
let changed = false;
|
|
1332
|
+
if (captionBoundary === 1) changed = appendCaptionSpacing(captionBoundarySpacing, includeBoundaryListIndent);
|
|
1333
|
+
const frames = captionFrames;
|
|
1334
|
+
if (frames) for (let index = 0; index < captionFrameCount; index++) {
|
|
1335
|
+
const offset = index * CAPTION_FRAME_SIZE;
|
|
1336
|
+
let flags = frames[offset + 2];
|
|
1337
|
+
if (flags & CAPTION_OPEN) continue;
|
|
1338
|
+
const anchor = frames[offset + 3];
|
|
1339
|
+
const beforeFragment = anchor === CAPTION_NO_ANCHOR ? state.buffer.length : anchor;
|
|
1340
|
+
let opening = flags & CAPTION_INTERNAL_BREAK ? "" : captionSpacingPrefix(frames[offset], true, beforeFragment);
|
|
1341
|
+
flags |= CAPTION_OPEN;
|
|
1342
|
+
if (!plainText && (!explicitTop || index !== captionFrameCount - 1)) opening += "*";
|
|
1343
|
+
if (opening) {
|
|
1344
|
+
if (beforeFragment === state.buffer.length) {
|
|
1345
|
+
state.buffer.push(opening);
|
|
1346
|
+
state.lastContentCache = opening;
|
|
1347
|
+
} else {
|
|
1348
|
+
const anchored = state.buffer[beforeFragment];
|
|
1349
|
+
const replacement = opening + anchored;
|
|
1350
|
+
state.buffer[beforeFragment] = replacement;
|
|
1351
|
+
const codeSpan = gfmLifecycle.openCodeSpans.at(-1);
|
|
1352
|
+
if (codeSpan?.fragment === beforeFragment) codeSpan.prefix = opening + codeSpan.prefix;
|
|
1353
|
+
if (state.lastContentCache === anchored) state.lastContentCache = replacement;
|
|
1354
|
+
}
|
|
1355
|
+
changed = true;
|
|
1356
|
+
}
|
|
1357
|
+
frames[offset + 2] = flags;
|
|
1358
|
+
}
|
|
1359
|
+
return changed;
|
|
1360
|
+
}
|
|
1361
|
+
function consumeCaptionTransition(isInlineElement, tagId) {
|
|
1362
|
+
captionBoundary = isInlineElement && tagId !== 27 && tagId !== 6 ? 2 : 0;
|
|
1363
|
+
}
|
|
1364
|
+
function anchorPendingCaptions(fragment) {
|
|
1365
|
+
const frames = captionFrames;
|
|
1366
|
+
if (!frames) return;
|
|
1367
|
+
for (let index = 0; index < captionFrameCount; index++) {
|
|
1368
|
+
const offset = index * CAPTION_FRAME_SIZE;
|
|
1369
|
+
if ((frames[offset + 2] & CAPTION_OPEN) === 0 && frames[offset + 3] === CAPTION_NO_ANCHOR) frames[offset + 3] = fragment;
|
|
1370
|
+
}
|
|
1371
|
+
}
|
|
1372
|
+
function commitCaptionFrames() {
|
|
1373
|
+
const frames = captionFrames;
|
|
1374
|
+
if (!frames) return;
|
|
1375
|
+
for (let index = 0; index < captionFrameCount; index++) {
|
|
1376
|
+
const offset = index * CAPTION_FRAME_SIZE;
|
|
1377
|
+
if (frames[offset + 2] & CAPTION_OPEN) frames[offset + 3] = CAPTION_NO_ANCHOR;
|
|
1378
|
+
}
|
|
1379
|
+
}
|
|
1380
|
+
function flushCaptionBreakRun() {
|
|
1381
|
+
if (captionBreakRun === 0) return false;
|
|
1382
|
+
let index = state.buffer.length;
|
|
1383
|
+
while (index !== 0) {
|
|
1384
|
+
const value = state.buffer[--index];
|
|
1385
|
+
let end = value.length;
|
|
1386
|
+
while (end !== 0 && value.charCodeAt(end - 1) === 32) end--;
|
|
1387
|
+
if (end === value.length) break;
|
|
1388
|
+
if (end !== 0) {
|
|
1389
|
+
state.buffer[index] = value.slice(0, end);
|
|
1390
|
+
state.buffer.length = index + 1;
|
|
1391
|
+
break;
|
|
1392
|
+
}
|
|
1393
|
+
state.buffer.length = index;
|
|
1394
|
+
}
|
|
1395
|
+
const output = plainText ? "\n".repeat(captionBreakRun) : ` \n${state.listIndent}`.repeat(captionBreakRun);
|
|
1396
|
+
captionBreakRun = 0;
|
|
1397
|
+
state.buffer.push(output);
|
|
1398
|
+
state.lastContentCache = output;
|
|
1399
|
+
return true;
|
|
1400
|
+
}
|
|
1401
|
+
function finishCaptionFrame() {
|
|
1402
|
+
if (captionFrameCount === 0) return;
|
|
1403
|
+
const offset = (captionFrameCount - 1) * CAPTION_FRAME_SIZE;
|
|
1404
|
+
const frames = captionFrames;
|
|
1405
|
+
const exitSpacing = frames[offset + 1];
|
|
1406
|
+
const opened = (frames[offset + 2] & CAPTION_OPEN) !== 0;
|
|
1407
|
+
const anchor = frames[offset + 3];
|
|
1408
|
+
frames.length = offset;
|
|
1409
|
+
captionFrameCount--;
|
|
1410
|
+
if (opened) {
|
|
1411
|
+
captionBoundary = exitSpacing === 0 ? 0 : 1;
|
|
1412
|
+
captionBoundarySpacing = exitSpacing;
|
|
1413
|
+
} else if (anchor !== CAPTION_NO_ANCHOR) {
|
|
1414
|
+
state.buffer.length = anchor;
|
|
1415
|
+
state.lastContentCache = state.buffer.at(-1);
|
|
1416
|
+
state.lastTextNode = void 0;
|
|
1417
|
+
} else if (captionBreakRun !== 0 && captionFrameCount === 0) captionBoundary = 2;
|
|
1418
|
+
}
|
|
799
1419
|
function processTextNode(textNode, lastNode, lastChar) {
|
|
800
1420
|
if (textNode.value) {
|
|
801
1421
|
if (textNode.excludedFromMarkdown) return;
|
|
1422
|
+
const ownsCaptionSpace = captionNeedsPreparation();
|
|
1423
|
+
if (ownsCaptionSpace) {
|
|
1424
|
+
if (textNode.value.charCodeAt(0) === 32) textNode.value = textNode.value.slice(1);
|
|
1425
|
+
if (!hasNonWhitespace(textNode.value)) return;
|
|
1426
|
+
flushCaptionBreakRun();
|
|
1427
|
+
prepareCaptionOutput(false);
|
|
1428
|
+
commitCaptionFrames();
|
|
1429
|
+
captionBoundary = 0;
|
|
1430
|
+
}
|
|
802
1431
|
if (state.pendingInlineWhitespace) {
|
|
803
1432
|
if (!textNode.value.trim()) return;
|
|
804
1433
|
if (lastChar && !" \n \r".includes(lastChar) && !" \n \r".includes(textNode.value[0] || "")) textNode.value = ` ${textNode.value}`;
|
|
805
1434
|
state.pendingInlineWhitespace = false;
|
|
806
1435
|
}
|
|
807
|
-
if (
|
|
1436
|
+
if (plainText && state.depthMap[34] && state.buffer.length === 0) preserveLeadingWhitespace = true;
|
|
808
1437
|
if (textNode.value === " " && (lastChar === " " || lastChar === "\n" || lastChar === " " || lastChar === "\r")) return;
|
|
809
|
-
if (!(
|
|
1438
|
+
if (!ownsCaptionSpace && !(plainText && state.depthMap[34]) && shouldAddSpacingBeforeText(lastChar, lastNode, textNode)) textNode.value = ` ${textNode.value}`;
|
|
810
1439
|
if ((state.depthMap[34] || 0) > 0 && (state.depthMap[25] || 0) > 0) {
|
|
811
1440
|
const indent = state.listIndent;
|
|
812
1441
|
let value = textNode.value.replace(/\n(?!\n|$)/g, `\n${indent}`);
|
|
@@ -817,8 +1446,12 @@ function createMarkdownProcessor(options = {}, resolvedPlugins = [], tagOverride
|
|
|
817
1446
|
const insideRawHtmlBlock = isInsideRawHtmlBlock(state.depthMap);
|
|
818
1447
|
let rawHtmlMarkdown = false;
|
|
819
1448
|
if (insideRawHtmlBlock) rawHtmlMarkdown = trackRawHtmlMarkdownContext(state.buffer, bufferScan);
|
|
820
|
-
if (!
|
|
821
|
-
|
|
1449
|
+
if (!plainText && !state.depthMap[34] && insideRawHtmlBlock) {
|
|
1450
|
+
let parent = rawHtmlLink ? textNode.parent : void 0;
|
|
1451
|
+
while (parent && parent !== rawHtmlLink) parent = parent.parent;
|
|
1452
|
+
textNode.value = escapeRawHtmlText(textNode.value, state.depthMap, rawHtmlLink !== void 0 && parent === rawHtmlLink);
|
|
1453
|
+
}
|
|
1454
|
+
if (!plainText && !state.depthMap[34] && !state.depthMap[23] && (!insideRawHtmlBlock || rawHtmlMarkdown) && mayNeedGfmTextEscape(textNode.value, state.buffer, state.depthMap) && (!insideRawHtmlBlock || !lineOpensRawHtmlBlock(state.buffer, bufferScan))) textNode.value = escapeGfmText(textNode.value, state.buffer, state.depthMap);
|
|
822
1455
|
if (textNode.generatedMarkdown && textNode.value) textNode.value = `${textNode.generatedMarkdown.prefix}${textNode.value}${textNode.generatedMarkdown.suffix}`;
|
|
823
1456
|
const wrapWidth = state.options?.wrapWidth;
|
|
824
1457
|
if (wrapWidth && canWrapHere(state.depthMap)) {
|
|
@@ -829,7 +1462,14 @@ function createMarkdownProcessor(options = {}, resolvedPlugins = [], tagOverride
|
|
|
829
1462
|
state.buffer.push(textNode.value);
|
|
830
1463
|
state.lastContentCache = textNode.value;
|
|
831
1464
|
}
|
|
832
|
-
if (openMarkerCount
|
|
1465
|
+
if (openMarkerCount || openLinkFragment >= 0) {
|
|
1466
|
+
const hasVisibleText = textNode.containsWhitespace === false || hasNonWhitespace(textNode.value);
|
|
1467
|
+
if (openLinkFragment >= 0 && hasVisibleText) openLinkFragment = -openLinkFragment - 2;
|
|
1468
|
+
if (openMarkerCount && hasVisibleText) {
|
|
1469
|
+
openMarkerCount = 0;
|
|
1470
|
+
commitCaptionFrames();
|
|
1471
|
+
}
|
|
1472
|
+
}
|
|
833
1473
|
}
|
|
834
1474
|
state.lastTextNode = textNode;
|
|
835
1475
|
}
|
|
@@ -855,9 +1495,31 @@ function createMarkdownProcessor(options = {}, resolvedPlugins = [], tagOverride
|
|
|
855
1495
|
const lastNode = state.lastNode;
|
|
856
1496
|
state.lastNode = event.node;
|
|
857
1497
|
const buff = state.buffer;
|
|
858
|
-
if (!
|
|
1498
|
+
if (!plainText && state.preFencePending) {
|
|
1499
|
+
const preparePendingPreFence = () => {
|
|
1500
|
+
if (captionNeedsPreparation()) {
|
|
1501
|
+
flushCaptionBreakRun();
|
|
1502
|
+
prepareCaptionOutput(false);
|
|
1503
|
+
captionBoundary = 0;
|
|
1504
|
+
}
|
|
1505
|
+
};
|
|
1506
|
+
if (node.type === 1) {
|
|
1507
|
+
const pendingElement = node;
|
|
1508
|
+
const pendingHandler = pendingElement.tagHandler;
|
|
1509
|
+
const literalOverride = eventType === 0 ? pendingHandler?.literalEnter : pendingHandler?.literalExit;
|
|
1510
|
+
if (literalOverride && pendingElement.tagId === 23 && pendingElement.parent?.tagId === 34) state.preFenceLang = getLanguageFromClass(pendingElement.attributes?.class);
|
|
1511
|
+
if (pendingElement.pluginOutput?.length || literalOverride) {
|
|
1512
|
+
preparePendingPreFence();
|
|
1513
|
+
flushPreFence(state);
|
|
1514
|
+
} else if (consumePendingPreChild(state, node, eventType, preparePendingPreFence)) return;
|
|
1515
|
+
} else if (consumePendingPreChild(state, node, eventType, preparePendingPreFence)) return;
|
|
1516
|
+
}
|
|
859
1517
|
let lastBuffEntry = buff.at(-1);
|
|
860
1518
|
let lastChar = lastBuffEntry?.charAt(lastBuffEntry.length - 1) || "";
|
|
1519
|
+
if (!lastChar && state.pendingInlineWhitespace && eventType === 0) {
|
|
1520
|
+
const code = lastOutputChar(buff);
|
|
1521
|
+
if (code !== -1) lastChar = String.fromCharCode(code);
|
|
1522
|
+
}
|
|
861
1523
|
if (node.type === 2 && eventType === 0) {
|
|
862
1524
|
processTextNode(node, lastNode, lastChar);
|
|
863
1525
|
return;
|
|
@@ -865,6 +1527,7 @@ function createMarkdownProcessor(options = {}, resolvedPlugins = [], tagOverride
|
|
|
865
1527
|
if (node.type !== 1) return;
|
|
866
1528
|
const element = node;
|
|
867
1529
|
const handler = node.tagHandler;
|
|
1530
|
+
const tagId = element.tagId;
|
|
868
1531
|
const insideRawHtmlRegion = isInsideRawHtmlBlock(state.depthMap);
|
|
869
1532
|
if (insideRawHtmlRegion && !inRawHtmlRegion) {
|
|
870
1533
|
bufferScan[0] = false;
|
|
@@ -873,9 +1536,26 @@ function createMarkdownProcessor(options = {}, resolvedPlugins = [], tagOverride
|
|
|
873
1536
|
}
|
|
874
1537
|
if (eventType === 0 && handler === breakHandler && !element.pluginOutput?.length) {
|
|
875
1538
|
const inPre = state.depthMap[34] !== 0;
|
|
876
|
-
let breakOutput =
|
|
877
|
-
if (
|
|
878
|
-
|
|
1539
|
+
let breakOutput = plainText ? "\n" : renderBreak(element, state);
|
|
1540
|
+
if (captionBoundary !== 0 || captionFrameCount !== 0) {
|
|
1541
|
+
const pendingCaption = captionFrameCount !== 0 && (captionFrames[(captionFrameCount - 1) * CAPTION_FRAME_SIZE + 2] & CAPTION_OPEN) === 0;
|
|
1542
|
+
if (!inPre && breakOutput && !hasNonWhitespace(breakOutput) && buff.length === 0 && !hasYieldedContent && state.blockquotes.length === 0 && pendingCaption) breakOutput = void 0;
|
|
1543
|
+
if (breakOutput && hasNonWhitespace(breakOutput) && captionNeedsPreparation()) {
|
|
1544
|
+
flushCaptionBreakRun();
|
|
1545
|
+
prepareCaptionOutput(false);
|
|
1546
|
+
commitCaptionFrames();
|
|
1547
|
+
consumeCaptionTransition(false, 6);
|
|
1548
|
+
lastBuffEntry = buff.at(-1);
|
|
1549
|
+
lastChar = lastBuffEntry?.charAt(lastBuffEntry.length - 1) || "";
|
|
1550
|
+
}
|
|
1551
|
+
}
|
|
1552
|
+
if (plainText && !inPre) {
|
|
1553
|
+
let trailingNewLines = captionBreakRun;
|
|
1554
|
+
if (lastChar === "\n") {
|
|
1555
|
+
trailingNewLines++;
|
|
1556
|
+
if ((lastBuffEntry.length > 1 ? lastBuffEntry.charAt(lastBuffEntry.length - 2) : buff.length > 1 ? buff[buff.length - 2]?.at(-1) : void 0) === "\n") trailingNewLines++;
|
|
1557
|
+
}
|
|
1558
|
+
if (trailingNewLines >= 2) breakOutput = void 0;
|
|
879
1559
|
}
|
|
880
1560
|
if (state.pendingInlineWhitespace) state.pendingInlineWhitespace = false;
|
|
881
1561
|
const lastFragment = state.lastContentCache;
|
|
@@ -898,32 +1578,46 @@ function createMarkdownProcessor(options = {}, resolvedPlugins = [], tagOverride
|
|
|
898
1578
|
state.lastTextNode = void 0;
|
|
899
1579
|
}
|
|
900
1580
|
}
|
|
901
|
-
|
|
1581
|
+
const pendingCaption = captionFrameCount !== 0 && (captionFrames[(captionFrameCount - 1) * CAPTION_FRAME_SIZE + 2] & CAPTION_OPEN) === 0;
|
|
1582
|
+
if (breakOutput && pendingCaption && !hasNonWhitespace(breakOutput)) {
|
|
1583
|
+
for (let index = 0; index < captionFrameCount; index++) {
|
|
1584
|
+
const offset = index * CAPTION_FRAME_SIZE + 2;
|
|
1585
|
+
if ((captionFrames[offset] & CAPTION_OPEN) === 0) captionFrames[offset] = captionFrames[offset] | CAPTION_INTERNAL_BREAK;
|
|
1586
|
+
}
|
|
1587
|
+
captionBreakRun++;
|
|
1588
|
+
} else if (breakOutput) {
|
|
902
1589
|
buff.push(breakOutput);
|
|
903
1590
|
state.lastContentCache = breakOutput;
|
|
904
1591
|
if (openMarkerCount && hasNonWhitespace(breakOutput)) openMarkerCount = 0;
|
|
905
1592
|
}
|
|
906
1593
|
return;
|
|
907
1594
|
}
|
|
908
|
-
let secondLastChar;
|
|
909
|
-
if (lastBuffEntry && lastBuffEntry.length > 1) secondLastChar = lastBuffEntry.charAt(lastBuffEntry.length - 2);
|
|
910
|
-
else if (buff.length > 1) {
|
|
911
|
-
const prevBuff = buff[buff.length - 2];
|
|
912
|
-
if (prevBuff) secondLastChar = prevBuff.charAt(prevBuff.length - 1);
|
|
913
|
-
}
|
|
914
1595
|
let output;
|
|
915
1596
|
if (element.pluginOutput?.length) {
|
|
916
1597
|
output = element.pluginOutput;
|
|
917
1598
|
element.pluginOutput = void 0;
|
|
918
1599
|
}
|
|
919
|
-
let
|
|
1600
|
+
let captionBufferChanged = false;
|
|
920
1601
|
const eventFn = eventType === 0 ? "enter" : "exit";
|
|
921
1602
|
const isInlineElement = handler?.isInline === true;
|
|
922
1603
|
let gfmAction;
|
|
923
1604
|
let handlerOutput;
|
|
924
|
-
const suppressedInPre = !
|
|
1605
|
+
const suppressedInPre = !plainText && (state.depthMap[34] || 0) > 0 && suppressesFormattingInPre(tagId) && !(eventType === 0 ? handler?.literalEnter : handler?.literalExit);
|
|
925
1606
|
if (!output && !suppressedInPre && handler?.[eventFn]) {
|
|
926
|
-
|
|
1607
|
+
if (openLinkFragment >= 0 && eventType === 1 && tagId === 26) {
|
|
1608
|
+
if (dropEmptyLinkText(buff, openLinkFragment)) {
|
|
1609
|
+
if (captionFrameCount !== 0) {
|
|
1610
|
+
captionBreakRun = openLinkCaptionBreakRun;
|
|
1611
|
+
restoreCaptionBreakFlags(captionFrames, openLinkCaptionFlags, captionFrameCount);
|
|
1612
|
+
resetRetractedCaptionFrames(captionFrames, captionFrameCount, openLinkFragment);
|
|
1613
|
+
}
|
|
1614
|
+
state.lastContentCache = buff.at(-1);
|
|
1615
|
+
state.lastTextNode = void 0;
|
|
1616
|
+
openLinkFragment = -1;
|
|
1617
|
+
return;
|
|
1618
|
+
}
|
|
1619
|
+
}
|
|
1620
|
+
const res = plainText ? getPlainTextOutput(element, eventType, state) : handler[eventFn]({
|
|
927
1621
|
node: element,
|
|
928
1622
|
state
|
|
929
1623
|
});
|
|
@@ -936,32 +1630,98 @@ function createMarkdownProcessor(options = {}, resolvedPlugins = [], tagOverride
|
|
|
936
1630
|
gfmAction = res;
|
|
937
1631
|
handlerOutput = consumeGfmAction(res, state, gfmLifecycle);
|
|
938
1632
|
if (handlerOutput) output = [handlerOutput];
|
|
939
|
-
if (res._tag === "BlockquoteExit")
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
1633
|
+
if (res._tag === "BlockquoteExit") captionBufferChanged = true;
|
|
1634
|
+
}
|
|
1635
|
+
}
|
|
1636
|
+
if (captionBreakRun && !(tagId === 106 && eventType === 1) && output) {
|
|
1637
|
+
flushCaptionBreakRun();
|
|
1638
|
+
if (gfmAction?._tag !== "BlockquoteEnter") anchorPendingCaptions(state.buffer.length);
|
|
1639
|
+
captionBufferChanged = true;
|
|
1640
|
+
}
|
|
1641
|
+
const captionEvent = tagId === 106;
|
|
1642
|
+
let captionTransition = false;
|
|
1643
|
+
if (captionEvent || captionBoundary !== 0 || captionFrameCount !== 0) {
|
|
1644
|
+
const captionEnter = captionEvent && eventType === 0 && !state.depthMap[34];
|
|
1645
|
+
const captionExit = captionEvent && eventType === 1 && !state.depthMap[34];
|
|
1646
|
+
if (captionEnter) pushCaptionFrame(element, handler);
|
|
1647
|
+
let dropBlockquoteOutput = false;
|
|
1648
|
+
if (captionBoundary === 1 && eventType === 0 && tagId === 22) {
|
|
1649
|
+
captionBufferChanged = appendCaptionSpacing(captionBoundarySpacing, false) || captionBufferChanged;
|
|
1650
|
+
captionTransition = true;
|
|
1651
|
+
captionBoundary = 0;
|
|
1652
|
+
dropBlockquoteOutput = true;
|
|
1653
|
+
output = void 0;
|
|
1654
|
+
handlerOutput = void 0;
|
|
1655
|
+
}
|
|
1656
|
+
let meaningfulOutput = !dropBlockquoteOutput && output?.some(hasNonWhitespace) === true;
|
|
1657
|
+
if (meaningfulOutput && handlerOutput !== void 0) {
|
|
1658
|
+
const markerType = INLINE_MARKER_TYPE[tagId];
|
|
1659
|
+
const literal = eventType === 0 ? handler?.literalEnter : handler?.literalExit;
|
|
1660
|
+
if ((markerType || tagId === 26 && eventType === 0) && !literal) {
|
|
1661
|
+
if (captionBoundary === 1) {
|
|
1662
|
+
captionBufferChanged = appendCaptionSpacing(captionBoundarySpacing, true) || captionBufferChanged;
|
|
1663
|
+
captionTransition = true;
|
|
1664
|
+
captionBoundary = 2;
|
|
1665
|
+
}
|
|
1666
|
+
if (eventType === 0) {
|
|
1667
|
+
anchorPendingCaptions(state.buffer.length);
|
|
1668
|
+
captionTransition = true;
|
|
1669
|
+
}
|
|
1670
|
+
meaningfulOutput = false;
|
|
944
1671
|
}
|
|
945
1672
|
}
|
|
1673
|
+
const defaultCaptionExit = captionExit && handlerOutput !== void 0 && !handler?.literalExit;
|
|
1674
|
+
if (defaultCaptionExit) meaningfulOutput = false;
|
|
1675
|
+
if (captionExit && captionFrameCount !== 0) {
|
|
1676
|
+
const offset = (captionFrameCount - 1) * CAPTION_FRAME_SIZE;
|
|
1677
|
+
const opened = (captionFrames[offset + 2] & CAPTION_OPEN) !== 0;
|
|
1678
|
+
if (!opened && defaultCaptionExit) {
|
|
1679
|
+
output = void 0;
|
|
1680
|
+
handlerOutput = void 0;
|
|
1681
|
+
} else if (meaningfulOutput && captionNeedsPreparation()) {
|
|
1682
|
+
if (flushCaptionBreakRun()) captionBufferChanged = true;
|
|
1683
|
+
captionBufferChanged = prepareCaptionOutput(false) || captionBufferChanged;
|
|
1684
|
+
captionTransition = true;
|
|
1685
|
+
consumeCaptionTransition(isInlineElement, tagId);
|
|
1686
|
+
}
|
|
1687
|
+
finishCaptionFrame();
|
|
1688
|
+
if (!opened && flushCaptionBreakRun()) captionBufferChanged = true;
|
|
1689
|
+
} else if (meaningfulOutput && captionNeedsPreparation()) {
|
|
1690
|
+
captionBufferChanged = prepareCaptionOutput(captionEnter, tagId !== 25) || captionBufferChanged;
|
|
1691
|
+
captionTransition = true;
|
|
1692
|
+
consumeCaptionTransition(isInlineElement, tagId);
|
|
1693
|
+
}
|
|
1694
|
+
if (meaningfulOutput) commitCaptionFrames();
|
|
1695
|
+
}
|
|
1696
|
+
const lastFragment = state.lastContentCache;
|
|
1697
|
+
if (captionBufferChanged) {
|
|
1698
|
+
lastBuffEntry = buff.at(-1);
|
|
1699
|
+
lastChar = lastBuffEntry?.charAt(lastBuffEntry.length - 1) || "";
|
|
1700
|
+
}
|
|
1701
|
+
const secondLastChar = lastBuffEntry && lastBuffEntry.length > 1 ? lastBuffEntry.charAt(lastBuffEntry.length - 2) : buff.length > 1 ? buff[buff.length - 2]?.at(-1) : void 0;
|
|
1702
|
+
if (element.tagId === 26) {
|
|
1703
|
+
if (eventType === 0) rawHtmlLink = insideRawHtmlRegion && handlerOutput !== void 0 && !handler?.literalExit ? element : void 0;
|
|
1704
|
+
else rawHtmlLink = void 0;
|
|
946
1705
|
}
|
|
947
1706
|
let lastNewLines = 0;
|
|
948
1707
|
if (lastChar === "\n") lastNewLines++;
|
|
949
1708
|
if (secondLastChar === "\n") lastNewLines++;
|
|
950
1709
|
if (eventType === 1 && openMarkerCount) {
|
|
951
|
-
const markerType = handlerOutput === void 0 ? 0 : INLINE_MARKER_TYPE[
|
|
952
|
-
if (markerType && (
|
|
1710
|
+
const markerType = handlerOutput === void 0 ? 0 : INLINE_MARKER_TYPE[tagId];
|
|
1711
|
+
if (markerType && (tagId !== 23 || !state.depthMap[34] && !isInsideRawHtmlBlock(state.depthMap)) && !handler?.literalExit) {
|
|
953
1712
|
const idx = dropEmptyMarker(buff, openMarkers[--openMarkerCount], markerType);
|
|
954
1713
|
if (idx >= 0) {
|
|
1714
|
+
resetRetractedCaptionFrames(captionFrames, captionFrameCount, idx);
|
|
955
1715
|
state.lastContentCache = idx > 0 ? buff[idx - 1] : void 0;
|
|
956
1716
|
return;
|
|
957
1717
|
}
|
|
958
1718
|
openMarkerCount = 0;
|
|
959
1719
|
}
|
|
960
1720
|
}
|
|
961
|
-
if (
|
|
962
|
-
const newLineConfig = calculateNewLineConfig(node, state.depthMap,
|
|
1721
|
+
if (tagId === 6 && !state.depthMap[34] && output?.length === 1 && output[0] === "\n" && lastChar === "\n" && secondLastChar === "\n") output = void 0;
|
|
1722
|
+
const newLineConfig = suppressedInPre ? NO_SPACING : calculateNewLineConfig(node, state.depthMap, plainText);
|
|
963
1723
|
const quoteAtStart = eventType === 0 && state.blockquotes.at(-1)?.fragment === state.buffer.length;
|
|
964
|
-
const configuredNewLines = quoteAtStart ? 0 : eventType === 1 &&
|
|
1724
|
+
const configuredNewLines = quoteAtStart || captionEvent || captionTransition ? 0 : eventType === 1 && tagId === 13 && state.blockquotes.length > 0 ? Math.min(1, newLineConfig[eventType] || 0) : newLineConfig[eventType] || 0;
|
|
965
1725
|
let effectiveLastNewLines = lastNewLines;
|
|
966
1726
|
if (eventType === 1 && output) for (let i = output.length - 1; i >= 0; i--) {
|
|
967
1727
|
const frag = output[i];
|
|
@@ -970,19 +1730,24 @@ function createMarkdownProcessor(options = {}, resolvedPlugins = [], tagOverride
|
|
|
970
1730
|
break;
|
|
971
1731
|
}
|
|
972
1732
|
}
|
|
973
|
-
|
|
1733
|
+
let newLines = Math.max(0, configuredNewLines - effectiveLastNewLines);
|
|
1734
|
+
if (newLines > 0 && buff.length === 0 && !hasYieldedContent) newLines = 0;
|
|
974
1735
|
if (state.pendingInlineWhitespace) {
|
|
975
1736
|
const firstOutput = output?.[0]?.[0] || "";
|
|
976
1737
|
if (eventType === 0) {
|
|
977
|
-
if (!isInlineElement ||
|
|
1738
|
+
if (!isInlineElement || tagId === 6 || newLines > 0 || firstOutput === "\n" || firstOutput === "\r") state.pendingInlineWhitespace = false;
|
|
978
1739
|
else if (firstOutput) {
|
|
979
|
-
if (lastChar && !" \n \r".includes(lastChar) && !" \n \r".includes(firstOutput))
|
|
1740
|
+
if (lastChar && !" \n \r".includes(lastChar) && !" \n \r".includes(firstOutput)) {
|
|
1741
|
+
state.buffer.push(" ");
|
|
1742
|
+
state.lastContentCache = " ";
|
|
1743
|
+
lastChar = " ";
|
|
1744
|
+
}
|
|
980
1745
|
state.pendingInlineWhitespace = false;
|
|
981
1746
|
}
|
|
982
1747
|
} else if (!isInlineElement || newLines > 0) state.pendingInlineWhitespace = false;
|
|
983
1748
|
}
|
|
984
1749
|
if (newLines > 0) {
|
|
985
|
-
if (!buff.length) {
|
|
1750
|
+
if (!buff.length && !captionEvent && !captionTransition) {
|
|
986
1751
|
if (output) {
|
|
987
1752
|
for (const fragment of output) if (fragment) {
|
|
988
1753
|
state.buffer.push(fragment);
|
|
@@ -998,9 +1763,10 @@ function createMarkdownProcessor(options = {}, resolvedPlugins = [], tagOverride
|
|
|
998
1763
|
buff[buff.length - 1] = buff.at(-1).substring(0, buff.at(-1).length - 1);
|
|
999
1764
|
state.lastTextNode = void 0;
|
|
1000
1765
|
}
|
|
1001
|
-
if (eventType === 0)
|
|
1002
|
-
|
|
1003
|
-
|
|
1766
|
+
if (eventType === 0) {
|
|
1767
|
+
if (output) output.unshift(newlinesStr);
|
|
1768
|
+
else output = [newlinesStr];
|
|
1769
|
+
} else if (output) output.push(newlinesStr);
|
|
1004
1770
|
else output = [newlinesStr];
|
|
1005
1771
|
} else if (lastFragment && state.lastTextNode?.containsWhitespace && (!!node.parent || isInlineElement) && "value" in state.lastTextNode && typeof state.lastTextNode.value === "string") {
|
|
1006
1772
|
let parent = node.parent;
|
|
@@ -1016,7 +1782,7 @@ function createMarkdownProcessor(options = {}, resolvedPlugins = [], tagOverride
|
|
|
1016
1782
|
const collapsesWhiteSpace = node.tagHandler?.collapsesInnerWhiteSpace;
|
|
1017
1783
|
const hasSpacing = node.tagHandler?.spacing && Array.isArray(node.tagHandler.spacing);
|
|
1018
1784
|
const isBlockElement = !isInlineElement && !collapsesWhiteSpace && configuredNewLines > 0;
|
|
1019
|
-
if (!quoteAtStart && (
|
|
1785
|
+
if (!quoteAtStart && (tagId === 6 && output?.[0]?.endsWith("\n") && !parentInPre || (!isInlineElement || eventType === 1) && !isBlockElement && !(collapsesWhiteSpace && eventType === 0) && !(hasSpacing && eventType === 0))) {
|
|
1020
1786
|
const originalLength = lastFragment.length;
|
|
1021
1787
|
const trimmed = trimAsciiWhitespaceEnd(lastFragment);
|
|
1022
1788
|
if (originalLength - trimmed.length > 0) {
|
|
@@ -1027,7 +1793,7 @@ function createMarkdownProcessor(options = {}, resolvedPlugins = [], tagOverride
|
|
|
1027
1793
|
state.lastTextNode = void 0;
|
|
1028
1794
|
}
|
|
1029
1795
|
}
|
|
1030
|
-
if (
|
|
1796
|
+
if (!captionTransition && tagId !== 6 && output?.[0]?.[0] && eventType === 0 && !handler?.literalEnter && lastChar && needsSpacing(lastChar, output[0][0], state)) {
|
|
1031
1797
|
state.buffer.push(" ");
|
|
1032
1798
|
state.lastContentCache = " ";
|
|
1033
1799
|
}
|
|
@@ -1040,15 +1806,30 @@ function createMarkdownProcessor(options = {}, resolvedPlugins = [], tagOverride
|
|
|
1040
1806
|
}
|
|
1041
1807
|
}
|
|
1042
1808
|
if (gfmAction) commitGfmAction(gfmAction, state, gfmLifecycle, outputStart);
|
|
1043
|
-
if (
|
|
1044
|
-
|
|
1045
|
-
|
|
1809
|
+
if (tagId === 25 && !plainText && !isInsideTableCell(state)) {
|
|
1810
|
+
if (eventType === 0) recordItemMarker(state, element.index);
|
|
1811
|
+
else resolveItemMarker(state, true);
|
|
1812
|
+
}
|
|
1813
|
+
if (eventType === 0 && tagId === 26 && outputStart < buff.length && buff.at(-1) === "[") {
|
|
1814
|
+
const fragment = buff.length - 1;
|
|
1815
|
+
const cleanCaptionLink = captionFrameCount !== 0 && cleanEmptyLinkText;
|
|
1816
|
+
openLinkFragment = cleanCaptionLink ? fragment : -fragment - 2;
|
|
1817
|
+
if (cleanCaptionLink) {
|
|
1818
|
+
openLinkCaptionBreakRun = captionBreakRun;
|
|
1819
|
+
const snapshot = openLinkCaptionFlags?.length === captionFrameCount ? openLinkCaptionFlags : new Uint8Array(captionFrameCount);
|
|
1820
|
+
for (let index = 0; index < captionFrameCount; index++) snapshot[index] = captionFrames[index * CAPTION_FRAME_SIZE + 2] & CAPTION_INTERNAL_BREAK;
|
|
1821
|
+
openLinkCaptionFlags = snapshot;
|
|
1822
|
+
}
|
|
1823
|
+
}
|
|
1046
1824
|
if (eventType === 0 && handlerOutput !== void 0 && isInlineElement && !handler?.literalEnter) {
|
|
1047
|
-
const markerType = INLINE_MARKER_TYPE[
|
|
1048
|
-
if (markerType && (
|
|
1049
|
-
else if (openMarkerCount && hasNonWhitespace(handlerOutput))
|
|
1050
|
-
|
|
1051
|
-
|
|
1825
|
+
const markerType = INLINE_MARKER_TYPE[tagId];
|
|
1826
|
+
if (markerType && (tagId !== 23 || !state.depthMap[34] && !isInsideRawHtmlBlock(state.depthMap)) && buff[buff.length - 1] === handlerOutput) openMarkers[openMarkerCount++] = buff.length - 1 << 3 | markerType;
|
|
1827
|
+
else if (openMarkerCount && !(tagId === 26 && cleanEmptyLinkText) && hasNonWhitespace(handlerOutput)) {
|
|
1828
|
+
openMarkerCount = 0;
|
|
1829
|
+
commitCaptionFrames();
|
|
1830
|
+
}
|
|
1831
|
+
} else if (openMarkerCount && (eventType === 1 && handler?.literalExit || tagId !== -1 && !isInlineElement || output?.some(hasNonWhitespace))) openMarkerCount = 0;
|
|
1832
|
+
if (eventType === 1 && tagId === 26) openLinkFragment = -1;
|
|
1052
1833
|
updateListIndent(state, element, eventType);
|
|
1053
1834
|
if (!insideRawHtmlRegion && inRawHtmlRegion) {
|
|
1054
1835
|
bufferScan[0] = false;
|
|
@@ -1065,7 +1846,8 @@ function createMarkdownProcessor(options = {}, resolvedPlugins = [], tagOverride
|
|
|
1065
1846
|
plainText: outputFormat !== "markdown"
|
|
1066
1847
|
};
|
|
1067
1848
|
const handleEvent = resolvedPlugins.length ? (event) => processPluginsForEvent(event, resolvedPlugins, state, processEvent) : processEvent;
|
|
1068
|
-
|
|
1849
|
+
const leftover = parseHtmlStream(html, parseState, handleEvent);
|
|
1850
|
+
finalizeParse(leftover, parseState, handleEvent);
|
|
1069
1851
|
}
|
|
1070
1852
|
function getMarkdown() {
|
|
1071
1853
|
const content = state.buffer.join("");
|
|
@@ -1073,7 +1855,7 @@ function createMarkdownProcessor(options = {}, resolvedPlugins = [], tagOverride
|
|
|
1073
1855
|
state.buffer.length = 0;
|
|
1074
1856
|
return content;
|
|
1075
1857
|
}
|
|
1076
|
-
const result =
|
|
1858
|
+
const result = plainText && preserveLeadingWhitespace ? content : content.trimStart();
|
|
1077
1859
|
state.buffer.length = 0;
|
|
1078
1860
|
preserveLeadingWhitespace = false;
|
|
1079
1861
|
return result.trimEnd();
|
|
@@ -1084,16 +1866,25 @@ function createMarkdownProcessor(options = {}, resolvedPlugins = [], tagOverride
|
|
|
1084
1866
|
state.buffer.length = 0;
|
|
1085
1867
|
return chunk;
|
|
1086
1868
|
}
|
|
1087
|
-
|
|
1869
|
+
let unresolvedCaptionFragment = -1;
|
|
1870
|
+
if (state.emptyItemFragment !== void 0 && captionFrames) for (let index = 0; index < captionFrameCount; index++) {
|
|
1871
|
+
const offset = index * CAPTION_FRAME_SIZE;
|
|
1872
|
+
const anchor = captionFrames[offset + 3];
|
|
1873
|
+
if (anchor !== CAPTION_NO_ANCHOR) {
|
|
1874
|
+
unresolvedCaptionFragment = anchor;
|
|
1875
|
+
break;
|
|
1876
|
+
}
|
|
1877
|
+
}
|
|
1878
|
+
resolveItemMarker(state, false, unresolvedCaptionFragment);
|
|
1088
1879
|
const content = state.buffer.join("");
|
|
1089
|
-
const currentContent = hasYieldedContent ||
|
|
1880
|
+
const currentContent = hasYieldedContent || plainText && preserveLeadingWhitespace ? content : content.trimStart();
|
|
1090
1881
|
const inPre = state.depthMap[34] !== 0;
|
|
1091
|
-
|
|
1092
|
-
let
|
|
1093
|
-
while (trailingSpaceEnd > 0 && currentContent.charCodeAt(trailingSpaceEnd - 1) === 32) trailingSpaceEnd--;
|
|
1094
|
-
let stableLength = trailingSpaceEnd;
|
|
1095
|
-
let retainMutableFragments = stableLength < currentContent.length;
|
|
1882
|
+
let stableLength = currentContent.length;
|
|
1883
|
+
let retainMutableFragments = false;
|
|
1096
1884
|
if (inPre) {
|
|
1885
|
+
const trailingCode = currentContent.charCodeAt(stableLength - 1);
|
|
1886
|
+
while (stableLength > 0 && currentContent.charCodeAt(stableLength - 1) === 32) stableLength--;
|
|
1887
|
+
retainMutableFragments = stableLength < currentContent.length;
|
|
1097
1888
|
if (state.lastTextNode?.containsWhitespace && isAsciiWhitespace(trailingCode)) {
|
|
1098
1889
|
stableLength = trimAsciiWhitespaceEnd(currentContent).length;
|
|
1099
1890
|
retainMutableFragments = stableLength < currentContent.length;
|
|
@@ -1104,42 +1895,25 @@ function createMarkdownProcessor(options = {}, resolvedPlugins = [], tagOverride
|
|
|
1104
1895
|
}
|
|
1105
1896
|
}
|
|
1106
1897
|
} else {
|
|
1107
|
-
while (stableLength > 0
|
|
1898
|
+
while (stableLength > 0) {
|
|
1899
|
+
const code = currentContent.charCodeAt(stableLength - 1);
|
|
1900
|
+
if (code !== 32 && code !== 10) break;
|
|
1901
|
+
stableLength--;
|
|
1902
|
+
}
|
|
1108
1903
|
retainMutableFragments = stableLength < currentContent.length;
|
|
1109
1904
|
}
|
|
1110
1905
|
const leadingTrimmed = content.length - currentContent.length;
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
const
|
|
1114
|
-
const
|
|
1115
|
-
if (
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
const spanPos = Math.max(lastYieldedLength, trimBufferedWhitespacePosition(currentContent, fragmentPosition(state.buffer, gfmLifecycle.openCodeSpans[0].fragment) - leadingTrimmed));
|
|
1120
|
-
if (spanPos < stableLength) stableLength = spanPos;
|
|
1121
|
-
}
|
|
1122
|
-
const emptyItemFragment = state.emptyItemFragment;
|
|
1123
|
-
const emptyItemHeld = emptyItemFragment !== void 0;
|
|
1124
|
-
if (emptyItemHeld) {
|
|
1125
|
-
const itemPos = Math.max(lastYieldedLength, trimBufferedWhitespacePosition(currentContent, fragmentPosition(state.buffer, emptyItemFragment) - leadingTrimmed));
|
|
1126
|
-
if (itemPos < stableLength) stableLength = itemPos;
|
|
1127
|
-
}
|
|
1128
|
-
const codeFenceHeld = state.codeFence !== void 0;
|
|
1129
|
-
if (codeFenceHeld) {
|
|
1130
|
-
const fencePos = Math.max(lastYieldedLength, trimBufferedWhitespacePosition(currentContent, fragmentPosition(state.buffer, state.codeFence.fragment) - leadingTrimmed));
|
|
1131
|
-
if (fencePos < stableLength) stableLength = fencePos;
|
|
1132
|
-
}
|
|
1133
|
-
const blockquoteHeld = state.blockquotes.length > 0;
|
|
1134
|
-
if (blockquoteHeld) {
|
|
1135
|
-
const blockquotePos = Math.max(lastYieldedLength, trimBufferedWhitespacePosition(currentContent, fragmentPosition(state.buffer, state.blockquotes[0].fragment) - leadingTrimmed));
|
|
1136
|
-
if (blockquotePos < stableLength) stableLength = blockquotePos;
|
|
1137
|
-
}
|
|
1138
|
-
const linkHeld = openLinkFragment >= 0;
|
|
1139
|
-
if (linkHeld) {
|
|
1140
|
-
const linkPos = Math.max(lastYieldedLength, trimBufferedWhitespacePosition(currentContent, fragmentPosition(state.buffer, openLinkFragment) - leadingTrimmed));
|
|
1141
|
-
if (linkPos < stableLength) stableLength = linkPos;
|
|
1906
|
+
let heldFragment = Math.min(openMarkerCount ? openMarkers[0] >> 3 : Infinity, gfmLifecycle.openCodeSpans[0]?.fragment ?? Infinity, state.emptyItemFragment ?? Infinity, state.codeFence?.fragment ?? Infinity, state.blockquotes[0]?.fragment ?? Infinity, openLinkFragment === -1 ? Infinity : openLinkFragment < -1 ? -openLinkFragment - 2 : openLinkFragment);
|
|
1907
|
+
for (let index = 0; index < captionFrameCount; index++) {
|
|
1908
|
+
const offset = index * CAPTION_FRAME_SIZE;
|
|
1909
|
+
const anchor = captionFrames[offset + 3];
|
|
1910
|
+
if ((captionFrames[offset + 2] & CAPTION_OPEN) === 0 && anchor !== CAPTION_NO_ANCHOR) {
|
|
1911
|
+
heldFragment = Math.min(heldFragment, anchor);
|
|
1912
|
+
break;
|
|
1913
|
+
}
|
|
1142
1914
|
}
|
|
1915
|
+
const fragmentHeld = heldFragment !== Infinity;
|
|
1916
|
+
if (fragmentHeld) stableLength = Math.min(stableLength, trimBufferedWhitespacePosition(currentContent, fragmentPosition(state.buffer, heldFragment) - leadingTrimmed));
|
|
1143
1917
|
const headingHeld = isInsideHeading(state.depthMap);
|
|
1144
1918
|
if (headingHeld) {
|
|
1145
1919
|
let headingPos = currentContent.length;
|
|
@@ -1154,8 +1928,8 @@ function createMarkdownProcessor(options = {}, resolvedPlugins = [], tagOverride
|
|
|
1154
1928
|
const newContent = currentContent.slice(lastYieldedLength, stableLength);
|
|
1155
1929
|
lastYieldedLength = stableLength;
|
|
1156
1930
|
if (newContent) hasYieldedContent = true;
|
|
1157
|
-
if (!
|
|
1158
|
-
if (!resolvedPlugins.length && !options.wrapWidth
|
|
1931
|
+
if (!fragmentHeld && !headingHeld && (!retainMutableFragments || !inPre)) {
|
|
1932
|
+
if (!resolvedPlugins.length && !options.wrapWidth) {
|
|
1159
1933
|
if (retainMutableFragments && leadingTrimmed === 0) {
|
|
1160
1934
|
const lastFragment = state.buffer.at(-1);
|
|
1161
1935
|
const fragmentStart = currentContent.length - lastFragment.length;
|
|
@@ -1190,4 +1964,4 @@ function createMarkdownProcessor(options = {}, resolvedPlugins = [], tagOverride
|
|
|
1190
1964
|
state
|
|
1191
1965
|
};
|
|
1192
1966
|
}
|
|
1193
|
-
export { createMarkdownProcessor, processPluginsForEvent };
|
|
1967
|
+
export { applyClean, createMarkdownProcessor, processPluginsForEvent, resolveClean };
|