@mdream/js 1.5.5 → 1.5.7
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/const.mjs +7 -2
- package/dist/_chunks/markdown-processor.mjs +439 -67
- package/dist/_chunks/parse.mjs +430 -129
- package/dist/_chunks/plugins.mjs +35 -41
- package/dist/_chunks/src.mjs +2 -2
- package/dist/_chunks/types.d.mts +42 -5
- package/dist/index.mjs +1 -1
- package/dist/parse.d.mts +14 -3
- package/dist/splitter.mjs +5 -15
- package/package.json +1 -1
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { DEFAULT_BLOCK_SPACING, NO_SPACING } from "./const.mjs";
|
|
2
|
-
import { continuationPrefix, finalizeParse, parseHtmlStream, resolveUrl } from "./parse.mjs";
|
|
1
|
+
import { DEFAULT_BLOCK_SPACING, NO_SPACING, isInsideRawHtmlBlock } from "./const.mjs";
|
|
2
|
+
import { breakHandler, continuationPrefix, finalizeParse, parseHtmlStream, renderBreak, resolveUrl } from "./parse.mjs";
|
|
3
3
|
function processPluginsForEvent(event, plugins, state, processEvent) {
|
|
4
4
|
if (plugins?.length) {
|
|
5
5
|
if (event.node.type === 1 && event.type === 0) {
|
|
@@ -35,7 +35,7 @@ function processPluginsForEvent(event, plugins, state, processEvent) {
|
|
|
35
35
|
processEvent(event);
|
|
36
36
|
return false;
|
|
37
37
|
}
|
|
38
|
-
const INLINE_MARKER_TYPE = /* @__PURE__ */ new Uint8Array(
|
|
38
|
+
const INLINE_MARKER_TYPE = /* @__PURE__ */ new Uint8Array(112);
|
|
39
39
|
INLINE_MARKER_TYPE[14] = 1;
|
|
40
40
|
INLINE_MARKER_TYPE[15] = 1;
|
|
41
41
|
INLINE_MARKER_TYPE[91] = 1;
|
|
@@ -43,6 +43,8 @@ INLINE_MARKER_TYPE[16] = 2;
|
|
|
43
43
|
INLINE_MARKER_TYPE[17] = 2;
|
|
44
44
|
INLINE_MARKER_TYPE[106] = 2;
|
|
45
45
|
INLINE_MARKER_TYPE[18] = 3;
|
|
46
|
+
INLINE_MARKER_TYPE[110] = 3;
|
|
47
|
+
INLINE_MARKER_TYPE[111] = 3;
|
|
46
48
|
INLINE_MARKER_TYPE[90] = 4;
|
|
47
49
|
INLINE_MARKER_TYPE[46] = 5;
|
|
48
50
|
INLINE_MARKER_TYPE[23] = 5;
|
|
@@ -127,10 +129,152 @@ function foldPreLinesToBr(value) {
|
|
|
127
129
|
else if (c === 38) out += "&";
|
|
128
130
|
else if (c === 60) out += "<";
|
|
129
131
|
else if (c === 62) out += ">";
|
|
132
|
+
else if (c === 124) out += "|";
|
|
130
133
|
else out += value[i];
|
|
131
134
|
}
|
|
132
135
|
return out;
|
|
133
136
|
}
|
|
137
|
+
function escapeRawHtmlText(value, depthMap) {
|
|
138
|
+
const inTable = Boolean(depthMap[28]);
|
|
139
|
+
const inLink = Boolean(depthMap[26]);
|
|
140
|
+
let escaped = "";
|
|
141
|
+
let copiedUntil = 0;
|
|
142
|
+
let index = 0;
|
|
143
|
+
while (index < value.length) {
|
|
144
|
+
const code = value.charCodeAt(index);
|
|
145
|
+
let replacement;
|
|
146
|
+
if (code === 38) replacement = "&";
|
|
147
|
+
else if (code === 60) replacement = "<";
|
|
148
|
+
else if (code === 62) replacement = ">";
|
|
149
|
+
else if (code === 10) replacement = " ";
|
|
150
|
+
else if (code === 13) replacement = " ";
|
|
151
|
+
else if (inTable && code === 124) replacement = "|";
|
|
152
|
+
else if (inLink && code === 91) replacement = "[";
|
|
153
|
+
else if (inLink && code === 93) replacement = "]";
|
|
154
|
+
if (replacement) {
|
|
155
|
+
escaped += value.slice(copiedUntil, index) + replacement;
|
|
156
|
+
copiedUntil = index + 1;
|
|
157
|
+
}
|
|
158
|
+
index++;
|
|
159
|
+
}
|
|
160
|
+
return copiedUntil === 0 ? value : escaped + value.slice(copiedUntil);
|
|
161
|
+
}
|
|
162
|
+
function markdownLineIndent(buffer) {
|
|
163
|
+
let spaces = 0;
|
|
164
|
+
for (let fragmentIndex = buffer.length - 1; fragmentIndex >= 0; fragmentIndex--) {
|
|
165
|
+
const fragment = buffer[fragmentIndex];
|
|
166
|
+
for (let index = fragment.length - 1; index >= 0; index--) {
|
|
167
|
+
const code = fragment.charCodeAt(index);
|
|
168
|
+
if (code === 10) return spaces <= 3 ? spaces : -1;
|
|
169
|
+
if (code !== 32) return -1;
|
|
170
|
+
if (++spaces > 3) return -1;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
return spaces <= 3 ? spaces : -1;
|
|
174
|
+
}
|
|
175
|
+
function isMarkdownMarkerWhitespace(code) {
|
|
176
|
+
return code === 0 || code === 9 || code === 10 || code === 13 || code === 32;
|
|
177
|
+
}
|
|
178
|
+
function canStartGfmAngleConstruct(code) {
|
|
179
|
+
return code >= 65 && code <= 90 || code >= 97 && code <= 122 || code === 33 || code === 47 || code === 63;
|
|
180
|
+
}
|
|
181
|
+
function isThematicBreak(value, start, marker) {
|
|
182
|
+
let count = 0;
|
|
183
|
+
for (let index = start; index < value.length; index++) {
|
|
184
|
+
const code = value.charCodeAt(index);
|
|
185
|
+
if (code === marker) count++;
|
|
186
|
+
else if (code === 10 || code === 13) break;
|
|
187
|
+
else if (code !== 32 && code !== 9) return false;
|
|
188
|
+
}
|
|
189
|
+
return count >= 3;
|
|
190
|
+
}
|
|
191
|
+
function isEntityReferenceAfterAmpersand(value, ampersand) {
|
|
192
|
+
let index = ampersand + 1;
|
|
193
|
+
if (value.charCodeAt(index) === 35) {
|
|
194
|
+
index++;
|
|
195
|
+
const hex = value.charCodeAt(index) === 120 || value.charCodeAt(index) === 88;
|
|
196
|
+
if (hex) index++;
|
|
197
|
+
const start = index;
|
|
198
|
+
while (index < value.length) {
|
|
199
|
+
const code = value.charCodeAt(index);
|
|
200
|
+
if (!(code >= 48 && code <= 57 || hex && (code >= 65 && code <= 70 || code >= 97 && code <= 102))) break;
|
|
201
|
+
index++;
|
|
202
|
+
}
|
|
203
|
+
return index > start && value.charCodeAt(index) === 59;
|
|
204
|
+
}
|
|
205
|
+
const start = index;
|
|
206
|
+
while (index < value.length) {
|
|
207
|
+
const code = value.charCodeAt(index);
|
|
208
|
+
if (!(code >= 48 && code <= 57 || code >= 65 && code <= 90 || code >= 97 && code <= 122)) break;
|
|
209
|
+
index++;
|
|
210
|
+
}
|
|
211
|
+
return index > start && value.charCodeAt(index) === 59;
|
|
212
|
+
}
|
|
213
|
+
const GFM_TEXT_NATIVE_TRIGGER = /[\\*_~`[<\r\n]/;
|
|
214
|
+
function mayNeedGfmTextEscape(value, buffer, depthMap) {
|
|
215
|
+
if (value.search(GFM_TEXT_NATIVE_TRIGGER) !== -1) return true;
|
|
216
|
+
if (depthMap[28] && value.includes("|") || depthMap[26] && value.includes("]") || depthMap[22] && value.includes(">")) return true;
|
|
217
|
+
let lineIndent = markdownLineIndent(buffer);
|
|
218
|
+
if (lineIndent < 0) return false;
|
|
219
|
+
let index = 0;
|
|
220
|
+
while (index < value.length && lineIndent < 3 && value.charCodeAt(index) === 32) {
|
|
221
|
+
index++;
|
|
222
|
+
lineIndent++;
|
|
223
|
+
}
|
|
224
|
+
if (index >= value.length) return false;
|
|
225
|
+
const code = value.charCodeAt(index);
|
|
226
|
+
return code === 35 || code === 43 || code === 45 || code === 62 || code >= 48 && code <= 57;
|
|
227
|
+
}
|
|
228
|
+
function escapeGfmText(value, buffer, depthMap) {
|
|
229
|
+
const inTable = Boolean(depthMap[28]);
|
|
230
|
+
const inLink = Boolean(depthMap[26]);
|
|
231
|
+
const inBlockquote = Boolean(depthMap[22]);
|
|
232
|
+
let lineIndent = markdownLineIndent(buffer);
|
|
233
|
+
let orderedDigits = 0;
|
|
234
|
+
let copiedUntil = 0;
|
|
235
|
+
let escaped = "";
|
|
236
|
+
for (let index = 0; index < value.length; index++) {
|
|
237
|
+
const code = value.charCodeAt(index);
|
|
238
|
+
const next = index + 1 < value.length ? value.charCodeAt(index + 1) : 0;
|
|
239
|
+
if (code === 92 && next === 38 && isEntityReferenceAfterAmpersand(value, index + 1)) {
|
|
240
|
+
lineIndent = -1;
|
|
241
|
+
orderedDigits = 0;
|
|
242
|
+
index++;
|
|
243
|
+
continue;
|
|
244
|
+
}
|
|
245
|
+
if (inTable && (code === 10 || code === 13)) {
|
|
246
|
+
escaped += value.slice(copiedUntil, index);
|
|
247
|
+
escaped += code === 10 ? " " : " ";
|
|
248
|
+
copiedUntil = index + 1;
|
|
249
|
+
continue;
|
|
250
|
+
}
|
|
251
|
+
let shouldEscape = code === 92 || code === 42 || code === 95 || code === 126 || code === 96 || code === 91 || code === 93 && inLink || code === 124 && inTable || code === 62 && inBlockquote || code === 60 && canStartGfmAngleConstruct(value.charCodeAt(index + 1));
|
|
252
|
+
if (!shouldEscape && lineIndent >= 0) {
|
|
253
|
+
if (code === 35) {
|
|
254
|
+
let end = index + 1;
|
|
255
|
+
while (end < value.length && value.charCodeAt(end) === 35) end++;
|
|
256
|
+
shouldEscape = end - index <= 6 && (end === value.length || isMarkdownMarkerWhitespace(value.charCodeAt(end)));
|
|
257
|
+
} else if (code === 45 || code === 43) shouldEscape = index + 1 === value.length || isMarkdownMarkerWhitespace(value.charCodeAt(index + 1)) || code === 45 && isThematicBreak(value, index, code);
|
|
258
|
+
else if (code === 62) shouldEscape = true;
|
|
259
|
+
} else if (!shouldEscape && orderedDigits > 0 && (code === 46 || code === 41)) shouldEscape = index + 1 === value.length || isMarkdownMarkerWhitespace(value.charCodeAt(index + 1));
|
|
260
|
+
if (shouldEscape) {
|
|
261
|
+
escaped += value.slice(copiedUntil, index);
|
|
262
|
+
escaped += `\\${value[index]}`;
|
|
263
|
+
copiedUntil = index + 1;
|
|
264
|
+
}
|
|
265
|
+
if (code === 10) {
|
|
266
|
+
lineIndent = 0;
|
|
267
|
+
orderedDigits = 0;
|
|
268
|
+
} else if (lineIndent >= 0) if (code === 32 && lineIndent < 3) lineIndent++;
|
|
269
|
+
else {
|
|
270
|
+
orderedDigits = code >= 48 && code <= 57 ? 1 : 0;
|
|
271
|
+
lineIndent = -1;
|
|
272
|
+
}
|
|
273
|
+
else if (orderedDigits > 0) if (code >= 48 && code <= 57 && orderedDigits < 9) orderedDigits++;
|
|
274
|
+
else orderedDigits = 0;
|
|
275
|
+
}
|
|
276
|
+
return copiedUntil === 0 ? value : escaped + value.slice(copiedUntil);
|
|
277
|
+
}
|
|
134
278
|
function currentColumn(buffer) {
|
|
135
279
|
let col = 0;
|
|
136
280
|
for (let i = buffer.length - 1; i >= 0; i--) {
|
|
@@ -175,7 +319,7 @@ function wrapText(value, col, width, prefix) {
|
|
|
175
319
|
}
|
|
176
320
|
function calculateNewLineConfig(node, depthMap, plainText) {
|
|
177
321
|
const tagId = node.tagId;
|
|
178
|
-
if (tagId !== 25 && (depthMap[25] || 0) > 0 || tagId !== 22 && (depthMap[22] || 0) > 0) {
|
|
322
|
+
if (tagId !== 25 && (depthMap[25] || 0) > 0 || plainText && tagId !== 22 && (depthMap[22] || 0) > 0) {
|
|
179
323
|
if (plainText && tagId === 34) return [1, 1];
|
|
180
324
|
return NO_SPACING;
|
|
181
325
|
}
|
|
@@ -236,6 +380,90 @@ function dropEmptyMarker(buffer, packed, markerType) {
|
|
|
236
380
|
}
|
|
237
381
|
return -1;
|
|
238
382
|
}
|
|
383
|
+
function maxBacktickRun(value) {
|
|
384
|
+
let max = 0;
|
|
385
|
+
let run = 0;
|
|
386
|
+
for (let index = 0; index < value.length; index++) if (value.charCodeAt(index) === 96) {
|
|
387
|
+
run++;
|
|
388
|
+
if (run > max) max = run;
|
|
389
|
+
} else run = 0;
|
|
390
|
+
return max;
|
|
391
|
+
}
|
|
392
|
+
function maxLineLeadingRun(value, marker, indent) {
|
|
393
|
+
let max = 0;
|
|
394
|
+
let lineStart = 0;
|
|
395
|
+
while (lineStart <= value.length) {
|
|
396
|
+
let index = lineStart;
|
|
397
|
+
if (indent && value.startsWith(indent, index)) index += indent.length;
|
|
398
|
+
let spaces = 0;
|
|
399
|
+
while (spaces < 3 && value.charCodeAt(index) === 32) {
|
|
400
|
+
spaces++;
|
|
401
|
+
index++;
|
|
402
|
+
}
|
|
403
|
+
let run = 0;
|
|
404
|
+
while (value.charCodeAt(index + run) === marker) run++;
|
|
405
|
+
if (run > max) max = run;
|
|
406
|
+
const newline = value.indexOf("\n", lineStart);
|
|
407
|
+
if (newline === -1) break;
|
|
408
|
+
lineStart = newline + 1;
|
|
409
|
+
}
|
|
410
|
+
return max;
|
|
411
|
+
}
|
|
412
|
+
function finalizeCodeSpan(state, span) {
|
|
413
|
+
const content = state.buffer.slice(span.fragment + 1).join("");
|
|
414
|
+
const delimiter = "`".repeat(Math.max(1, maxBacktickRun(content) + 1));
|
|
415
|
+
const padded = content.startsWith("`") || content.endsWith("`");
|
|
416
|
+
state.buffer[span.fragment] = `${span.prefix}${delimiter}${padded ? " " : ""}`;
|
|
417
|
+
return `${padded ? " " : ""}${delimiter}`;
|
|
418
|
+
}
|
|
419
|
+
function startCodeFence(state, fragment, language, indent) {
|
|
420
|
+
state.codeFence = {
|
|
421
|
+
fragment,
|
|
422
|
+
markerOffset: state.buffer[fragment].lastIndexOf("```"),
|
|
423
|
+
indent,
|
|
424
|
+
language
|
|
425
|
+
};
|
|
426
|
+
state.onCodeFenceOpen?.(language);
|
|
427
|
+
}
|
|
428
|
+
function finalizeCodeFence(state) {
|
|
429
|
+
const fence = state.codeFence;
|
|
430
|
+
if (!fence) return;
|
|
431
|
+
state.codeFence = void 0;
|
|
432
|
+
const marker = fence.language.includes("`") ? "~" : "`";
|
|
433
|
+
const markerCode = marker.charCodeAt(0);
|
|
434
|
+
const content = state.buffer.slice(fence.fragment + 1).join("");
|
|
435
|
+
const delimiter = marker.repeat(Math.max(3, maxLineLeadingRun(content, markerCode, fence.indent) + 1));
|
|
436
|
+
const opening = state.buffer[fence.fragment];
|
|
437
|
+
state.buffer[fence.fragment] = `${opening.slice(0, fence.markerOffset)}${delimiter}${opening.slice(fence.markerOffset + 3)}`;
|
|
438
|
+
return delimiter;
|
|
439
|
+
}
|
|
440
|
+
function stripBlockquoteListIndent(line, listIndent) {
|
|
441
|
+
return listIndent && line.startsWith(listIndent) ? line.slice(listIndent.length) : line;
|
|
442
|
+
}
|
|
443
|
+
function finalizeBlockquote(state) {
|
|
444
|
+
const frame = state.blockquotes.pop();
|
|
445
|
+
if (!frame) return;
|
|
446
|
+
state.bufferedBlockquoteDepth = state.blockquotes.length;
|
|
447
|
+
const content = state.buffer.slice(frame.fragment).join("").replace(/[ \t\r\n]+$/, "");
|
|
448
|
+
const prefix = `${frame.listIndent}>`;
|
|
449
|
+
const quoted = content.split("\n").map((line) => {
|
|
450
|
+
const unindented = stripBlockquoteListIndent(line, frame.listIndent);
|
|
451
|
+
return unindented ? `${prefix} ${unindented}` : prefix;
|
|
452
|
+
}).join("\n");
|
|
453
|
+
state.buffer.splice(frame.fragment, state.buffer.length - frame.fragment, quoted);
|
|
454
|
+
state.lastContentCache = quoted;
|
|
455
|
+
}
|
|
456
|
+
function collapseNestedBlockquoteSeparator(buffer) {
|
|
457
|
+
let trailingNewlines = 0;
|
|
458
|
+
for (let fragmentIndex = buffer.length - 1; fragmentIndex >= 0 && trailingNewlines < 2; fragmentIndex--) {
|
|
459
|
+
const fragment = buffer[fragmentIndex];
|
|
460
|
+
for (let index = fragment.length - 1; index >= 0 && fragment.charCodeAt(index) === 10; index--) trailingNewlines++;
|
|
461
|
+
}
|
|
462
|
+
if (trailingNewlines < 2) return;
|
|
463
|
+
const last = buffer.at(-1);
|
|
464
|
+
if (last.length === 1) buffer.pop();
|
|
465
|
+
else buffer[buffer.length - 1] = last.slice(0, -1);
|
|
466
|
+
}
|
|
239
467
|
function flushPreFence(state) {
|
|
240
468
|
if (state.plainText) {
|
|
241
469
|
state.preFencePending = false;
|
|
@@ -247,8 +475,82 @@ function flushPreFence(state) {
|
|
|
247
475
|
const lang = state.preFenceLang || "";
|
|
248
476
|
const fence = (state.depthMap[25] || 0) > 0 ? `\n\n${state.listIndent}\`\`\`${lang}\n${state.listIndent}` : `\`\`\`${lang}\n`;
|
|
249
477
|
state.buffer.push(fence);
|
|
478
|
+
startCodeFence(state, state.buffer.length - 1, lang, state.listIndent);
|
|
250
479
|
state.lastContentCache = fence;
|
|
251
480
|
}
|
|
481
|
+
function consumePendingPreChild(state, node, eventType) {
|
|
482
|
+
if (eventType !== 0) return false;
|
|
483
|
+
if (node.type === 1) {
|
|
484
|
+
const element = node;
|
|
485
|
+
if (element.tagId === 23 && element.parent?.tagId === 34) state.preFencePending = false;
|
|
486
|
+
else if (element.tagId !== 34) flushPreFence(state);
|
|
487
|
+
return false;
|
|
488
|
+
}
|
|
489
|
+
if (hasNonWhitespace(node.value)) {
|
|
490
|
+
flushPreFence(state);
|
|
491
|
+
return false;
|
|
492
|
+
}
|
|
493
|
+
return true;
|
|
494
|
+
}
|
|
495
|
+
function consumeGfmAction(action, state, lifecycle) {
|
|
496
|
+
switch (action._tag) {
|
|
497
|
+
case "BlockquoteEnter": return action.output;
|
|
498
|
+
case "BlockquoteExit":
|
|
499
|
+
finalizeBlockquote(state);
|
|
500
|
+
return;
|
|
501
|
+
case "PreEnter":
|
|
502
|
+
state.preFencePending = true;
|
|
503
|
+
state.preOwnFence = false;
|
|
504
|
+
state.preFenceLang = action.language;
|
|
505
|
+
return;
|
|
506
|
+
case "PreExit": {
|
|
507
|
+
const ownFence = state.preOwnFence;
|
|
508
|
+
state.preFencePending = false;
|
|
509
|
+
state.preOwnFence = false;
|
|
510
|
+
if (!ownFence) return void 0;
|
|
511
|
+
const indent = state.listIndent;
|
|
512
|
+
const output = (state.depthMap[25] || 0) > 0 ? `\n${indent}\`\`\`\n\n${indent}` : `\n\`\`\``;
|
|
513
|
+
const delimiter = finalizeCodeFence(state);
|
|
514
|
+
return delimiter ? output.replace("```", delimiter) : output;
|
|
515
|
+
}
|
|
516
|
+
case "CodeSpanEnter": return action.output;
|
|
517
|
+
case "CodeSpanExit": {
|
|
518
|
+
const span = lifecycle.openCodeSpans.pop();
|
|
519
|
+
return span ? finalizeCodeSpan(state, span) : "`";
|
|
520
|
+
}
|
|
521
|
+
case "CodeFenceEnter":
|
|
522
|
+
state.preFencePending = false;
|
|
523
|
+
return action.output;
|
|
524
|
+
case "CodeFenceExit": {
|
|
525
|
+
const delimiter = finalizeCodeFence(state);
|
|
526
|
+
return delimiter ? action.output.replace("```", delimiter) : action.output;
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
}
|
|
530
|
+
function commitGfmAction(action, state, lifecycle, outputStart) {
|
|
531
|
+
switch (action._tag) {
|
|
532
|
+
case "BlockquoteEnter":
|
|
533
|
+
if (state.blockquotes.length > 0) collapseNestedBlockquoteSeparator(state.buffer);
|
|
534
|
+
state.blockquotes.push({
|
|
535
|
+
fragment: state.buffer.length,
|
|
536
|
+
listIndent: state.listIndent
|
|
537
|
+
});
|
|
538
|
+
state.bufferedBlockquoteDepth = state.blockquotes.length;
|
|
539
|
+
break;
|
|
540
|
+
case "CodeSpanEnter":
|
|
541
|
+
lifecycle.openCodeSpans.push({
|
|
542
|
+
fragment: outputStart,
|
|
543
|
+
prefix: action.output.slice(0, -1)
|
|
544
|
+
});
|
|
545
|
+
break;
|
|
546
|
+
case "CodeFenceEnter": {
|
|
547
|
+
const fenceFragment = state.buffer.findIndex((fragment, index) => index >= outputStart && fragment.includes("```"));
|
|
548
|
+
if (fenceFragment < 0) throw new Error("code fence opener missing from output");
|
|
549
|
+
startCodeFence(state, fenceFragment, action.language, state.listIndent);
|
|
550
|
+
break;
|
|
551
|
+
}
|
|
552
|
+
}
|
|
553
|
+
}
|
|
252
554
|
function getPlainTextOutput(node, eventType, state) {
|
|
253
555
|
const override = state.options?.plugins?.tagOverrides?.[node.name];
|
|
254
556
|
if (override && typeof override !== "string") {
|
|
@@ -268,7 +570,7 @@ function getPlainTextOutput(node, eventType, state) {
|
|
|
268
570
|
if (tagId === 27) {
|
|
269
571
|
const alt = node.attributes?.alt;
|
|
270
572
|
if (alt !== void 0) return alt || void 0;
|
|
271
|
-
return node.attributes?.title || resolveUrl(node.attributes?.src || "", state.options?.origin) || void 0;
|
|
573
|
+
return node.attributes?.title || resolveUrl(node.attributes?.src || "", state.options?.origin, state.options?.clean) || void 0;
|
|
272
574
|
}
|
|
273
575
|
if (tagId === 81) return "\"";
|
|
274
576
|
return;
|
|
@@ -279,17 +581,55 @@ function createMarkdownProcessor(options = {}, resolvedPlugins = [], tagOverride
|
|
|
279
581
|
const state = {
|
|
280
582
|
options,
|
|
281
583
|
buffer: [],
|
|
282
|
-
depthMap: /* @__PURE__ */ new
|
|
584
|
+
depthMap: /* @__PURE__ */ new Uint16Array(112),
|
|
283
585
|
listIndent: "",
|
|
284
586
|
listIndentWidths: [],
|
|
587
|
+
blockquotes: [],
|
|
588
|
+
bufferedBlockquoteDepth: 0,
|
|
285
589
|
plainText: options.format === "text"
|
|
286
590
|
};
|
|
287
591
|
const openMarkers = [];
|
|
288
592
|
let openMarkerCount = 0;
|
|
593
|
+
const gfmLifecycle = { openCodeSpans: [] };
|
|
289
594
|
let openLinkFragment = -1;
|
|
290
595
|
let lastYieldedLength = 0;
|
|
291
596
|
let hasYieldedContent = false;
|
|
292
597
|
let preserveLeadingWhitespace = false;
|
|
598
|
+
function processTextNode(textNode, lastNode, lastChar) {
|
|
599
|
+
if (textNode.value) {
|
|
600
|
+
if (textNode.excludedFromMarkdown) return;
|
|
601
|
+
if (state.pendingInlineWhitespace) {
|
|
602
|
+
if (!textNode.value.trim()) return;
|
|
603
|
+
if (lastChar && !" \n \r".includes(lastChar) && !" \n \r".includes(textNode.value[0] || "")) textNode.value = ` ${textNode.value}`;
|
|
604
|
+
state.pendingInlineWhitespace = false;
|
|
605
|
+
}
|
|
606
|
+
if (state.plainText && state.depthMap[34] && state.buffer.length === 0) preserveLeadingWhitespace = true;
|
|
607
|
+
if (textNode.value === " " && (lastChar === " " || lastChar === "\n" || lastChar === " " || lastChar === "\r")) return;
|
|
608
|
+
if (!(state.plainText && state.depthMap[34]) && shouldAddSpacingBeforeText(lastChar, lastNode, textNode)) textNode.value = ` ${textNode.value}`;
|
|
609
|
+
if ((state.depthMap[34] || 0) > 0 && (state.depthMap[25] || 0) > 0) {
|
|
610
|
+
const indent = state.listIndent;
|
|
611
|
+
let value = textNode.value.replace(/\n(?!\n|$)/g, `\n${indent}`);
|
|
612
|
+
if (lastChar === "\n" && value[0] && value[0] !== "\n") value = indent + value;
|
|
613
|
+
textNode.value = value;
|
|
614
|
+
}
|
|
615
|
+
if (state.depthMap[34] > 0 && (state.depthMap[32] > 0 || state.depthMap[31] > 0)) textNode.value = foldPreLinesToBr(textNode.value);
|
|
616
|
+
const insideRawHtmlBlock = isInsideRawHtmlBlock(state.depthMap);
|
|
617
|
+
if (!state.plainText && !state.depthMap[34] && insideRawHtmlBlock) textNode.value = escapeRawHtmlText(textNode.value, state.depthMap);
|
|
618
|
+
if (!state.plainText && !state.depthMap[34] && !state.depthMap[23] && !insideRawHtmlBlock && mayNeedGfmTextEscape(textNode.value, state.buffer, state.depthMap)) textNode.value = escapeGfmText(textNode.value, state.buffer, state.depthMap);
|
|
619
|
+
if (textNode.generatedMarkdown && textNode.value) textNode.value = `${textNode.generatedMarkdown.prefix}${textNode.value}${textNode.generatedMarkdown.suffix}`;
|
|
620
|
+
const wrapWidth = state.options?.wrapWidth;
|
|
621
|
+
if (wrapWidth && canWrapHere(state.depthMap)) {
|
|
622
|
+
const wrapped = wrapText(textNode.value, currentColumn(state.buffer), wrapWidth, continuationPrefix(textNode, state.listIndentWidths, state.blockquotes.length === 0));
|
|
623
|
+
state.buffer.push(wrapped);
|
|
624
|
+
state.lastContentCache = wrapped;
|
|
625
|
+
} else {
|
|
626
|
+
state.buffer.push(textNode.value);
|
|
627
|
+
state.lastContentCache = textNode.value;
|
|
628
|
+
}
|
|
629
|
+
if (openMarkerCount && hasNonWhitespace(textNode.value)) openMarkerCount = 0;
|
|
630
|
+
}
|
|
631
|
+
state.lastTextNode = textNode;
|
|
632
|
+
}
|
|
293
633
|
function processEvent(event) {
|
|
294
634
|
const { type: eventType, node } = event;
|
|
295
635
|
state.depth = node.depth;
|
|
@@ -297,83 +637,94 @@ function createMarkdownProcessor(options = {}, resolvedPlugins = [], tagOverride
|
|
|
297
637
|
const lastNode = state.lastNode;
|
|
298
638
|
state.lastNode = event.node;
|
|
299
639
|
const buff = state.buffer;
|
|
300
|
-
if (!state.plainText && state.preFencePending &&
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
if (el.tagId === 23 && el.parent?.tagId === 34) state.preFencePending = false;
|
|
304
|
-
else if (el.tagId !== 34) flushPreFence(state);
|
|
305
|
-
} else if (node.type === 2 && hasNonWhitespace(node.value)) flushPreFence(state);
|
|
306
|
-
}
|
|
307
|
-
const lastBuffEntry = buff.at(-1);
|
|
308
|
-
const lastChar = lastBuffEntry?.charAt(lastBuffEntry.length - 1) || "";
|
|
309
|
-
let secondLastChar;
|
|
310
|
-
if (lastBuffEntry && lastBuffEntry.length > 1) secondLastChar = lastBuffEntry.charAt(lastBuffEntry.length - 2);
|
|
311
|
-
else if (buff.length > 1) {
|
|
312
|
-
const prevBuff = buff[buff.length - 2];
|
|
313
|
-
if (prevBuff) secondLastChar = prevBuff.charAt(prevBuff.length - 1);
|
|
314
|
-
}
|
|
640
|
+
if (!state.plainText && state.preFencePending && consumePendingPreChild(state, node, eventType)) return;
|
|
641
|
+
let lastBuffEntry = buff.at(-1);
|
|
642
|
+
let lastChar = lastBuffEntry?.charAt(lastBuffEntry.length - 1) || "";
|
|
315
643
|
if (node.type === 2 && eventType === 0) {
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
if (
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
644
|
+
processTextNode(node, lastNode, lastChar);
|
|
645
|
+
return;
|
|
646
|
+
}
|
|
647
|
+
if (node.type !== 1) return;
|
|
648
|
+
const element = node;
|
|
649
|
+
const handler = node.tagHandler;
|
|
650
|
+
if (eventType === 0 && handler === breakHandler && !element.pluginOutput?.length) {
|
|
651
|
+
const inPre = state.depthMap[34] !== 0;
|
|
652
|
+
let breakOutput = state.plainText ? "\n" : renderBreak(element, state);
|
|
653
|
+
if (state.plainText && !inPre && lastChar === "\n") {
|
|
654
|
+
if ((lastBuffEntry.length > 1 ? lastBuffEntry.charAt(lastBuffEntry.length - 2) : buff.length > 1 ? buff[buff.length - 2]?.at(-1) : void 0) === "\n") breakOutput = void 0;
|
|
655
|
+
}
|
|
656
|
+
if (state.pendingInlineWhitespace) state.pendingInlineWhitespace = false;
|
|
657
|
+
const lastFragment = state.lastContentCache;
|
|
658
|
+
const lastTextNode = state.lastTextNode;
|
|
659
|
+
if (lastFragment && lastTextNode?.containsWhitespace && typeof lastTextNode.value === "string") {
|
|
660
|
+
let parent = element.parent;
|
|
661
|
+
let parentInPre = false;
|
|
662
|
+
while (parent) {
|
|
663
|
+
if (parent.tagId === 34) {
|
|
664
|
+
parentInPre = true;
|
|
665
|
+
break;
|
|
666
|
+
}
|
|
667
|
+
parent = parent.parent;
|
|
333
668
|
}
|
|
334
|
-
if (
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
state.
|
|
340
|
-
} else {
|
|
341
|
-
state.buffer.push(textNode.value);
|
|
342
|
-
state.lastContentCache = textNode.value;
|
|
669
|
+
if (!parentInPre || element.parent?.tagId === 34) {
|
|
670
|
+
if (breakOutput?.endsWith("\n") && !parentInPre) {
|
|
671
|
+
const trimmed = trimAsciiWhitespaceEnd(lastFragment);
|
|
672
|
+
if (trimmed.length !== lastFragment.length && buff.at(-1) === lastFragment) buff[buff.length - 1] = trimmed;
|
|
673
|
+
}
|
|
674
|
+
state.lastTextNode = void 0;
|
|
343
675
|
}
|
|
344
|
-
if (openMarkerCount && hasNonWhitespace(textNode.value)) openMarkerCount = 0;
|
|
345
676
|
}
|
|
346
|
-
|
|
677
|
+
if (breakOutput) {
|
|
678
|
+
buff.push(breakOutput);
|
|
679
|
+
state.lastContentCache = breakOutput;
|
|
680
|
+
if (openMarkerCount && hasNonWhitespace(breakOutput)) openMarkerCount = 0;
|
|
681
|
+
}
|
|
347
682
|
return;
|
|
348
683
|
}
|
|
349
|
-
|
|
684
|
+
let secondLastChar;
|
|
685
|
+
if (lastBuffEntry && lastBuffEntry.length > 1) secondLastChar = lastBuffEntry.charAt(lastBuffEntry.length - 2);
|
|
686
|
+
else if (buff.length > 1) {
|
|
687
|
+
const prevBuff = buff[buff.length - 2];
|
|
688
|
+
if (prevBuff) secondLastChar = prevBuff.charAt(prevBuff.length - 1);
|
|
689
|
+
}
|
|
350
690
|
let output;
|
|
351
|
-
const element = node;
|
|
352
691
|
if (element.pluginOutput?.length) {
|
|
353
692
|
output = element.pluginOutput;
|
|
354
693
|
element.pluginOutput = void 0;
|
|
355
694
|
}
|
|
356
|
-
|
|
357
|
-
let lastNewLines = 0;
|
|
358
|
-
if (lastChar === "\n") lastNewLines++;
|
|
359
|
-
if (secondLastChar === "\n") lastNewLines++;
|
|
695
|
+
let lastFragment = state.lastContentCache;
|
|
360
696
|
const eventFn = eventType === 0 ? "enter" : "exit";
|
|
361
|
-
const handler = node.tagHandler;
|
|
362
697
|
const isInlineElement = handler?.isInline === true;
|
|
698
|
+
let gfmAction;
|
|
363
699
|
let handlerOutput;
|
|
364
700
|
if (!output && handler?.[eventFn]) {
|
|
365
701
|
const res = state.plainText ? getPlainTextOutput(element, eventType, state) : handler[eventFn]({
|
|
366
702
|
node: element,
|
|
367
703
|
state
|
|
368
704
|
});
|
|
369
|
-
if (res) {
|
|
370
|
-
|
|
371
|
-
|
|
705
|
+
if (typeof res === "string") {
|
|
706
|
+
if (res) {
|
|
707
|
+
output = [res];
|
|
708
|
+
handlerOutput = res;
|
|
709
|
+
}
|
|
710
|
+
} else if (res) {
|
|
711
|
+
gfmAction = res;
|
|
712
|
+
handlerOutput = consumeGfmAction(res, state, gfmLifecycle);
|
|
713
|
+
if (handlerOutput) output = [handlerOutput];
|
|
714
|
+
if (res._tag === "BlockquoteExit") {
|
|
715
|
+
lastFragment = state.lastContentCache;
|
|
716
|
+
lastBuffEntry = buff.at(-1);
|
|
717
|
+
lastChar = lastBuffEntry?.charAt(lastBuffEntry.length - 1) || "";
|
|
718
|
+
secondLastChar = lastBuffEntry && lastBuffEntry.length > 1 ? lastBuffEntry.charAt(lastBuffEntry.length - 2) : buff.length > 1 ? buff[buff.length - 2]?.at(-1) : void 0;
|
|
719
|
+
}
|
|
372
720
|
}
|
|
373
721
|
}
|
|
722
|
+
let lastNewLines = 0;
|
|
723
|
+
if (lastChar === "\n") lastNewLines++;
|
|
724
|
+
if (secondLastChar === "\n") lastNewLines++;
|
|
374
725
|
if (eventType === 1 && openMarkerCount) {
|
|
375
726
|
const markerType = handlerOutput === void 0 ? 0 : INLINE_MARKER_TYPE[element.tagId];
|
|
376
|
-
if (markerType && (element.tagId !== 23 || !state.depthMap[34]) && !handler?.literalExit) {
|
|
727
|
+
if (markerType && (element.tagId !== 23 || !state.depthMap[34] && !isInsideRawHtmlBlock(state.depthMap)) && !handler?.literalExit) {
|
|
377
728
|
const idx = dropEmptyMarker(buff, openMarkers[--openMarkerCount], markerType);
|
|
378
729
|
if (idx >= 0) {
|
|
379
730
|
state.lastContentCache = idx > 0 ? buff[idx - 1] : void 0;
|
|
@@ -383,12 +734,14 @@ function createMarkdownProcessor(options = {}, resolvedPlugins = [], tagOverride
|
|
|
383
734
|
}
|
|
384
735
|
}
|
|
385
736
|
if (element.tagId === 6 && !state.depthMap[34] && output?.length === 1 && output[0] === "\n" && lastChar === "\n" && secondLastChar === "\n") output = void 0;
|
|
386
|
-
const
|
|
737
|
+
const newLineConfig = calculateNewLineConfig(node, state.depthMap, state.plainText === true);
|
|
738
|
+
const quoteAtStart = eventType === 0 && state.blockquotes.at(-1)?.fragment === state.buffer.length;
|
|
739
|
+
const configuredNewLines = quoteAtStart ? 0 : eventType === 1 && element.tagId === 13 && state.blockquotes.length > 0 ? Math.min(1, newLineConfig[eventType] || 0) : newLineConfig[eventType] || 0;
|
|
387
740
|
let effectiveLastNewLines = lastNewLines;
|
|
388
741
|
if (eventType === 1 && output) for (let i = output.length - 1; i >= 0; i--) {
|
|
389
742
|
const frag = output[i];
|
|
390
743
|
if (frag) {
|
|
391
|
-
if (frag.
|
|
744
|
+
if (frag.endsWith("```") || frag.endsWith("~~~")) effectiveLastNewLines = 0;
|
|
392
745
|
break;
|
|
393
746
|
}
|
|
394
747
|
}
|
|
@@ -396,7 +749,7 @@ function createMarkdownProcessor(options = {}, resolvedPlugins = [], tagOverride
|
|
|
396
749
|
if (state.pendingInlineWhitespace) {
|
|
397
750
|
const firstOutput = output?.[0]?.[0] || "";
|
|
398
751
|
if (eventType === 0) {
|
|
399
|
-
if (!isInlineElement || newLines > 0 || firstOutput === "\n" || firstOutput === "\r") state.pendingInlineWhitespace = false;
|
|
752
|
+
if (!isInlineElement || element.tagId === 6 || newLines > 0 || firstOutput === "\n" || firstOutput === "\r") state.pendingInlineWhitespace = false;
|
|
400
753
|
else if (firstOutput) {
|
|
401
754
|
if (lastChar && !" \n \r".includes(lastChar) && !" \n \r".includes(firstOutput)) state.buffer.push(" ");
|
|
402
755
|
state.pendingInlineWhitespace = false;
|
|
@@ -411,6 +764,7 @@ function createMarkdownProcessor(options = {}, resolvedPlugins = [], tagOverride
|
|
|
411
764
|
state.lastContentCache = fragment;
|
|
412
765
|
}
|
|
413
766
|
}
|
|
767
|
+
if (gfmAction) commitGfmAction(gfmAction, state, gfmLifecycle, state.buffer.length);
|
|
414
768
|
updateListIndent(state, element, eventType);
|
|
415
769
|
return;
|
|
416
770
|
}
|
|
@@ -437,7 +791,7 @@ function createMarkdownProcessor(options = {}, resolvedPlugins = [], tagOverride
|
|
|
437
791
|
const collapsesWhiteSpace = node.tagHandler?.collapsesInnerWhiteSpace;
|
|
438
792
|
const hasSpacing = node.tagHandler?.spacing && Array.isArray(node.tagHandler.spacing);
|
|
439
793
|
const isBlockElement = !isInlineElement && !collapsesWhiteSpace && configuredNewLines > 0;
|
|
440
|
-
if (element.tagId === 6 && output?.[0]?.
|
|
794
|
+
if (!quoteAtStart && (element.tagId === 6 && output?.[0]?.endsWith("\n") && !parentInPre || (!isInlineElement || eventType === 1) && !isBlockElement && !(collapsesWhiteSpace && eventType === 0) && !(hasSpacing && eventType === 0))) {
|
|
441
795
|
const originalLength = lastFragment.length;
|
|
442
796
|
const trimmed = trimAsciiWhitespaceEnd(lastFragment);
|
|
443
797
|
if (originalLength - trimmed.length > 0) {
|
|
@@ -448,20 +802,23 @@ function createMarkdownProcessor(options = {}, resolvedPlugins = [], tagOverride
|
|
|
448
802
|
state.lastTextNode = void 0;
|
|
449
803
|
}
|
|
450
804
|
}
|
|
451
|
-
if (output?.[0]?.[0] && eventType === 0 && !node.tagHandler?.literalEnter && lastChar && needsSpacing(lastChar, output[0][0], state)) {
|
|
805
|
+
if (element.tagId !== 6 && output?.[0]?.[0] && eventType === 0 && !node.tagHandler?.literalEnter && lastChar && needsSpacing(lastChar, output[0][0], state)) {
|
|
452
806
|
state.buffer.push(" ");
|
|
453
807
|
state.lastContentCache = " ";
|
|
454
808
|
}
|
|
809
|
+
let outputStart = state.buffer.length;
|
|
455
810
|
if (output) {
|
|
811
|
+
outputStart = state.buffer.length;
|
|
456
812
|
for (const fragment of output) if (fragment) {
|
|
457
813
|
state.buffer.push(fragment);
|
|
458
814
|
state.lastContentCache = fragment;
|
|
459
815
|
}
|
|
460
816
|
}
|
|
817
|
+
if (gfmAction) commitGfmAction(gfmAction, state, gfmLifecycle, outputStart);
|
|
461
818
|
if (eventType === 0 && element.tagId === 26 && handlerOutput === "[" && buff.at(-1) === "[") openLinkFragment = buff.length - 1;
|
|
462
819
|
if (eventType === 0 && handlerOutput !== void 0 && isInlineElement && !handler?.literalEnter) {
|
|
463
820
|
const markerType = INLINE_MARKER_TYPE[element.tagId];
|
|
464
|
-
if (markerType && (element.tagId !== 23 || !state.depthMap[34]) && buff[buff.length - 1] === handlerOutput) openMarkers[openMarkerCount++] = buff.length - 1 << 3 | markerType;
|
|
821
|
+
if (markerType && (element.tagId !== 23 || !state.depthMap[34] && !isInsideRawHtmlBlock(state.depthMap)) && buff[buff.length - 1] === handlerOutput) openMarkers[openMarkerCount++] = buff.length - 1 << 3 | markerType;
|
|
465
822
|
else if (openMarkerCount && hasNonWhitespace(handlerOutput)) openMarkerCount = 0;
|
|
466
823
|
} else if (openMarkerCount && (handler?.literalExit || element.tagId !== -1 && !isInlineElement || output?.some(hasNonWhitespace))) openMarkerCount = 0;
|
|
467
824
|
if (eventType === 1 && element.tagId === 26) openLinkFragment = -1;
|
|
@@ -515,6 +872,21 @@ function createMarkdownProcessor(options = {}, resolvedPlugins = [], tagOverride
|
|
|
515
872
|
const markerPos = Math.max(lastYieldedLength, trimBufferedWhitespacePosition(currentContent, fragmentPosition(state.buffer, openFragment) - leadingTrimmed));
|
|
516
873
|
if (markerPos < stableLength) stableLength = markerPos;
|
|
517
874
|
}
|
|
875
|
+
const codeSpanHeld = gfmLifecycle.openCodeSpans.length > 0;
|
|
876
|
+
if (codeSpanHeld) {
|
|
877
|
+
const spanPos = Math.max(lastYieldedLength, trimBufferedWhitespacePosition(currentContent, fragmentPosition(state.buffer, gfmLifecycle.openCodeSpans[0].fragment) - leadingTrimmed));
|
|
878
|
+
if (spanPos < stableLength) stableLength = spanPos;
|
|
879
|
+
}
|
|
880
|
+
const codeFenceHeld = state.codeFence !== void 0;
|
|
881
|
+
if (codeFenceHeld) {
|
|
882
|
+
const fencePos = Math.max(lastYieldedLength, trimBufferedWhitespacePosition(currentContent, fragmentPosition(state.buffer, state.codeFence.fragment) - leadingTrimmed));
|
|
883
|
+
if (fencePos < stableLength) stableLength = fencePos;
|
|
884
|
+
}
|
|
885
|
+
const blockquoteHeld = state.blockquotes.length > 0;
|
|
886
|
+
if (blockquoteHeld) {
|
|
887
|
+
const blockquotePos = Math.max(lastYieldedLength, trimBufferedWhitespacePosition(currentContent, fragmentPosition(state.buffer, state.blockquotes[0].fragment) - leadingTrimmed));
|
|
888
|
+
if (blockquotePos < stableLength) stableLength = blockquotePos;
|
|
889
|
+
}
|
|
518
890
|
const linkHeld = openLinkFragment >= 0;
|
|
519
891
|
if (linkHeld) {
|
|
520
892
|
const linkPos = Math.max(lastYieldedLength, trimBufferedWhitespacePosition(currentContent, fragmentPosition(state.buffer, openLinkFragment) - leadingTrimmed));
|
|
@@ -524,7 +896,7 @@ function createMarkdownProcessor(options = {}, resolvedPlugins = [], tagOverride
|
|
|
524
896
|
const newContent = currentContent.slice(lastYieldedLength, stableLength);
|
|
525
897
|
lastYieldedLength = stableLength;
|
|
526
898
|
if (newContent) hasYieldedContent = true;
|
|
527
|
-
if (!markerHeld && !linkHeld && (!retainMutableFragments || !inPre)) {
|
|
899
|
+
if (!markerHeld && !codeSpanHeld && !codeFenceHeld && !blockquoteHeld && !linkHeld && (!retainMutableFragments || !inPre)) {
|
|
528
900
|
if (!resolvedPlugins.length && !options.wrapWidth && !state.depthMap[26]) {
|
|
529
901
|
if (retainMutableFragments && leadingTrimmed === 0) {
|
|
530
902
|
const lastFragment = state.buffer.at(-1);
|