@mdream/js 1.5.8 → 1.5.10
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/markdown-processor.mjs +189 -88
- package/dist/_chunks/parse.mjs +217 -49
- package/dist/_chunks/plugins.mjs +87 -26
- package/dist/_chunks/types.d.mts +5 -4
- package/dist/plugins.d.mts +2 -1
- package/package.json +1 -1
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { DEFAULT_BLOCK_SPACING, NO_SPACING, isInsideRawHtmlBlock } from "./const.mjs";
|
|
2
|
-
import { breakHandler, continuationPrefix, finalizeParse, parseHtmlStream, renderBreak, resolveUrl } from "./parse.mjs";
|
|
2
|
+
import { blockOpenPrefix, breakHandler, continuationPrefix, finalizeParse, isCharacterReferenceTail, isInsideHeading, isInsideTableCell, listMarkerLineStart, orderedItemNumber, 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) {
|
|
@@ -51,12 +51,53 @@ INLINE_MARKER_TYPE[23] = 5;
|
|
|
51
51
|
INLINE_MARKER_TYPE[82] = 5;
|
|
52
52
|
INLINE_MARKER_TYPE[92] = 5;
|
|
53
53
|
INLINE_MARKER_TYPE[81] = 6;
|
|
54
|
+
function suppressesFormattingInPre(tagId) {
|
|
55
|
+
return tagId === 26 || tagId === 46 || tagId === 110 || tagId === 111 || tagId >= 14 && tagId <= 21 || tagId >= 79 && tagId <= 83 || tagId >= 89 && tagId <= 94;
|
|
56
|
+
}
|
|
57
|
+
function newlineRunBefore(buffer, index) {
|
|
58
|
+
let run = 0;
|
|
59
|
+
for (let i = index - 1; i >= 0; i--) {
|
|
60
|
+
const fragment = buffer[i];
|
|
61
|
+
for (let j = fragment.length - 1; j >= 0; j--) {
|
|
62
|
+
if (fragment.charCodeAt(j) !== 10) return run;
|
|
63
|
+
run++;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return run;
|
|
67
|
+
}
|
|
68
|
+
function recordItemMarker(state, index) {
|
|
69
|
+
if (index !== 0 || newlineRunBefore(state.buffer, state.buffer.length - 1) !== 1) {
|
|
70
|
+
state.emptyItemFragment = void 0;
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
state.emptyItemFragment = state.buffer.length - 1;
|
|
74
|
+
}
|
|
75
|
+
function resolveItemMarker(state, atExit) {
|
|
76
|
+
const fragment = state.emptyItemFragment;
|
|
77
|
+
if (fragment === void 0) return;
|
|
78
|
+
const buffer = state.buffer;
|
|
79
|
+
let first = -1;
|
|
80
|
+
for (let i = fragment + 1; i < buffer.length && first === -1; i++) {
|
|
81
|
+
const fragment = buffer[i];
|
|
82
|
+
for (let j = 0; j < fragment.length; j++) {
|
|
83
|
+
const code = fragment.charCodeAt(j);
|
|
84
|
+
if (code !== 32) {
|
|
85
|
+
first = code;
|
|
86
|
+
break;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
if (first === -1 && !atExit) return;
|
|
91
|
+
state.emptyItemFragment = void 0;
|
|
92
|
+
if (first !== -1 && first !== 10) return;
|
|
93
|
+
buffer[fragment] = `\n${buffer[fragment]}`;
|
|
94
|
+
}
|
|
54
95
|
function updateListIndent(state, element, eventType) {
|
|
55
96
|
if (element.tagId !== 25) return;
|
|
56
|
-
if ((state
|
|
97
|
+
if (isInsideTableCell(state)) return;
|
|
57
98
|
if (eventType === 0) {
|
|
58
99
|
const isOrdered = element.parent?.tagId === 33;
|
|
59
|
-
const width = state.plainText ? 0 : isOrdered ? String(element.index
|
|
100
|
+
const width = state.plainText ? 0 : isOrdered ? String(orderedItemNumber(element.parent, element.index)).length + 2 : 2;
|
|
60
101
|
state.listIndentWidths.push(width);
|
|
61
102
|
state.listIndent += " ".repeat(width);
|
|
62
103
|
} else if (eventType === 1) {
|
|
@@ -65,46 +106,21 @@ function updateListIndent(state, element, eventType) {
|
|
|
65
106
|
}
|
|
66
107
|
}
|
|
67
108
|
function needsSpacing(lastChar, firstChar, state) {
|
|
68
|
-
if (
|
|
69
|
-
if (firstChar === " " || firstChar === "\n" || firstChar === " ") return false;
|
|
70
|
-
const noSpaceAfter = /* @__PURE__ */ new Set([
|
|
71
|
-
"[",
|
|
72
|
-
"(",
|
|
73
|
-
">",
|
|
74
|
-
"*",
|
|
75
|
-
"_",
|
|
76
|
-
"`"
|
|
77
|
-
]);
|
|
78
|
-
const noSpaceBefore = /* @__PURE__ */ new Set([
|
|
79
|
-
"]",
|
|
80
|
-
")",
|
|
81
|
-
"<",
|
|
82
|
-
".",
|
|
83
|
-
",",
|
|
84
|
-
"!",
|
|
85
|
-
"?",
|
|
86
|
-
":",
|
|
87
|
-
";",
|
|
88
|
-
"*",
|
|
89
|
-
"_",
|
|
90
|
-
"`"
|
|
91
|
-
]);
|
|
109
|
+
if (" \n ".includes(lastChar) || " \n ".includes(firstChar)) return false;
|
|
92
110
|
if (lastChar === "|" && firstChar === "<" && state && (state.depthMap[28] || 0) > 0) return true;
|
|
93
|
-
|
|
94
|
-
return true;
|
|
111
|
+
return !"[(>*_`".includes(lastChar) && !"])<.,!?:;*_`".includes(firstChar);
|
|
95
112
|
}
|
|
96
113
|
function shouldAddSpacingBeforeText(lastChar, lastNode, textNode) {
|
|
97
|
-
if (!lastChar ||
|
|
114
|
+
if (!lastChar || "\n [>".includes(lastChar)) return false;
|
|
98
115
|
if (lastNode?.tagHandler?.isInline) return false;
|
|
99
116
|
const firstChar = textNode.value[0];
|
|
100
117
|
if (firstChar === " ") return false;
|
|
101
|
-
if (firstChar
|
|
118
|
+
if (firstChar && ".,!?:;_*`)]".includes(firstChar)) return false;
|
|
102
119
|
return true;
|
|
103
120
|
}
|
|
104
121
|
function canWrapHere(depthMap) {
|
|
105
122
|
if (depthMap[34] || depthMap[23] || depthMap[32] || depthMap[31]) return false;
|
|
106
|
-
|
|
107
|
-
return true;
|
|
123
|
+
return !isInsideHeading(depthMap);
|
|
108
124
|
}
|
|
109
125
|
function foldPreLinesToBr(value) {
|
|
110
126
|
let start = 0;
|
|
@@ -159,18 +175,70 @@ function escapeRawHtmlText(value, depthMap) {
|
|
|
159
175
|
}
|
|
160
176
|
return copiedUntil === 0 ? value : escaped + value.slice(copiedUntil);
|
|
161
177
|
}
|
|
178
|
+
function resetBufferScanCursors(scan) {
|
|
179
|
+
scan[1] = 0;
|
|
180
|
+
scan[2] = 0;
|
|
181
|
+
scan[3] = 0;
|
|
182
|
+
scan[4] = 0;
|
|
183
|
+
}
|
|
184
|
+
function trackRawHtmlMarkdownContext(buffer, scan) {
|
|
185
|
+
if (scan[0]) return true;
|
|
186
|
+
let index = scan[1];
|
|
187
|
+
if (index > buffer.length) index = 0;
|
|
188
|
+
let previous = index > 0 ? buffer[index - 1].charCodeAt(buffer[index - 1].length - 1) : -1;
|
|
189
|
+
for (; index < buffer.length; index++) {
|
|
190
|
+
const fragment = buffer[index];
|
|
191
|
+
if (!fragment) continue;
|
|
192
|
+
if (fragment.includes("\n\n") || previous === 10 && fragment.charCodeAt(0) === 10) {
|
|
193
|
+
scan[0] = true;
|
|
194
|
+
break;
|
|
195
|
+
}
|
|
196
|
+
previous = fragment.charCodeAt(fragment.length - 1);
|
|
197
|
+
}
|
|
198
|
+
scan[1] = buffer.length;
|
|
199
|
+
return scan[0];
|
|
200
|
+
}
|
|
201
|
+
function lineOpensRawHtmlBlock(buffer, scan) {
|
|
202
|
+
let scanned = scan[2];
|
|
203
|
+
if (scanned > buffer.length) {
|
|
204
|
+
scanned = 0;
|
|
205
|
+
scan[3] = 0;
|
|
206
|
+
scan[4] = 0;
|
|
207
|
+
}
|
|
208
|
+
for (let index = scanned; index < buffer.length; index++) {
|
|
209
|
+
const newline = buffer[index].lastIndexOf("\n");
|
|
210
|
+
if (newline !== -1) {
|
|
211
|
+
scan[3] = index;
|
|
212
|
+
scan[4] = newline + 1;
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
scan[2] = buffer.length;
|
|
216
|
+
let spaces = 0;
|
|
217
|
+
let offset = scan[4];
|
|
218
|
+
for (let index = scan[3]; index < buffer.length; index++) {
|
|
219
|
+
const fragment = buffer[index];
|
|
220
|
+
while (offset < fragment.length) {
|
|
221
|
+
const code = fragment.charCodeAt(offset);
|
|
222
|
+
if (code !== 32) return code === 60;
|
|
223
|
+
if (++spaces > 3) return false;
|
|
224
|
+
offset++;
|
|
225
|
+
}
|
|
226
|
+
offset = 0;
|
|
227
|
+
}
|
|
228
|
+
return false;
|
|
229
|
+
}
|
|
162
230
|
function markdownLineIndent(buffer) {
|
|
163
231
|
let spaces = 0;
|
|
164
232
|
for (let fragmentIndex = buffer.length - 1; fragmentIndex >= 0; fragmentIndex--) {
|
|
165
233
|
const fragment = buffer[fragmentIndex];
|
|
166
234
|
for (let index = fragment.length - 1; index >= 0; index--) {
|
|
167
235
|
const code = fragment.charCodeAt(index);
|
|
168
|
-
if (code === 10) return spaces
|
|
169
|
-
if (code !== 32) return -1;
|
|
170
|
-
|
|
236
|
+
if (code === 10) return spaces;
|
|
237
|
+
if (code !== 32 || spaces === 3) return listMarkerLineStart(buffer, fragmentIndex, index) ? 0 : -1;
|
|
238
|
+
spaces++;
|
|
171
239
|
}
|
|
172
240
|
}
|
|
173
|
-
return spaces
|
|
241
|
+
return spaces;
|
|
174
242
|
}
|
|
175
243
|
function isMarkdownMarkerWhitespace(code) {
|
|
176
244
|
return code === 0 || code === 9 || code === 10 || code === 13 || code === 32;
|
|
@@ -188,32 +256,12 @@ function isThematicBreak(value, start, marker) {
|
|
|
188
256
|
}
|
|
189
257
|
return count >= 3;
|
|
190
258
|
}
|
|
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
259
|
const GFM_TEXT_NATIVE_TRIGGER = /[\\*_~`[<\r\n]/;
|
|
214
260
|
function mayNeedGfmTextEscape(value, buffer, depthMap) {
|
|
215
261
|
if (value.search(GFM_TEXT_NATIVE_TRIGGER) !== -1) return true;
|
|
216
262
|
if (depthMap[28] && value.includes("|") || depthMap[26] && value.includes("]") || depthMap[22] && value.includes(">")) return true;
|
|
263
|
+
const first = value.charCodeAt(0);
|
|
264
|
+
if (!" #+->".includes(value[0]) && !(first >= 48 && first <= 57)) return false;
|
|
217
265
|
let lineIndent = markdownLineIndent(buffer);
|
|
218
266
|
if (lineIndent < 0) return false;
|
|
219
267
|
let index = 0;
|
|
@@ -223,7 +271,7 @@ function mayNeedGfmTextEscape(value, buffer, depthMap) {
|
|
|
223
271
|
}
|
|
224
272
|
if (index >= value.length) return false;
|
|
225
273
|
const code = value.charCodeAt(index);
|
|
226
|
-
return
|
|
274
|
+
return "#+->".includes(value[index]) || code >= 48 && code <= 57;
|
|
227
275
|
}
|
|
228
276
|
function escapeGfmText(value, buffer, depthMap) {
|
|
229
277
|
const inTable = Boolean(depthMap[28]);
|
|
@@ -235,8 +283,9 @@ function escapeGfmText(value, buffer, depthMap) {
|
|
|
235
283
|
let escaped = "";
|
|
236
284
|
for (let index = 0; index < value.length; index++) {
|
|
237
285
|
const code = value.charCodeAt(index);
|
|
286
|
+
const character = value[index];
|
|
238
287
|
const next = index + 1 < value.length ? value.charCodeAt(index + 1) : 0;
|
|
239
|
-
if (code === 92 && next === 38 &&
|
|
288
|
+
if (code === 92 && next === 38 && isCharacterReferenceTail(value, index + 2)) {
|
|
240
289
|
lineIndent = -1;
|
|
241
290
|
orderedDigits = 0;
|
|
242
291
|
index++;
|
|
@@ -248,7 +297,7 @@ function escapeGfmText(value, buffer, depthMap) {
|
|
|
248
297
|
copiedUntil = index + 1;
|
|
249
298
|
continue;
|
|
250
299
|
}
|
|
251
|
-
let shouldEscape =
|
|
300
|
+
let shouldEscape = "\\*_~`[".includes(character) || code === 93 && inLink || code === 124 && inTable || code === 62 && inBlockquote || code === 60 && canStartGfmAngleConstruct(value.charCodeAt(index + 1));
|
|
252
301
|
if (!shouldEscape && lineIndent >= 0) {
|
|
253
302
|
if (code === 35) {
|
|
254
303
|
let end = index + 1;
|
|
@@ -259,7 +308,7 @@ function escapeGfmText(value, buffer, depthMap) {
|
|
|
259
308
|
} else if (!shouldEscape && orderedDigits > 0 && (code === 46 || code === 41)) shouldEscape = index + 1 === value.length || isMarkdownMarkerWhitespace(value.charCodeAt(index + 1));
|
|
260
309
|
if (shouldEscape) {
|
|
261
310
|
escaped += value.slice(copiedUntil, index);
|
|
262
|
-
escaped += `\\${
|
|
311
|
+
escaped += `\\${character}`;
|
|
263
312
|
copiedUntil = index + 1;
|
|
264
313
|
}
|
|
265
314
|
if (code === 10) {
|
|
@@ -323,7 +372,7 @@ function calculateNewLineConfig(node, depthMap, plainText) {
|
|
|
323
372
|
if (plainText && tagId === 34) return [1, 1];
|
|
324
373
|
return NO_SPACING;
|
|
325
374
|
}
|
|
326
|
-
const isBlockElement = tagId !== void 0 && (tagId >= 7 && tagId <= 12 || tagId === 35 || tagId === 36);
|
|
375
|
+
const isBlockElement = tagId !== void 0 && (tagId >= 7 && tagId <= 12 || tagId === 35 || tagId === 36 || tagId === 25);
|
|
327
376
|
let currParent = node.parent;
|
|
328
377
|
while (currParent) {
|
|
329
378
|
if (currParent.tagHandler?.collapsesInnerWhiteSpace) {
|
|
@@ -410,7 +459,12 @@ function maxLineLeadingRun(value, marker, indent) {
|
|
|
410
459
|
return max;
|
|
411
460
|
}
|
|
412
461
|
function finalizeCodeSpan(state, span) {
|
|
413
|
-
|
|
462
|
+
let content = state.buffer.slice(span.fragment + 1).join("");
|
|
463
|
+
if ((state.depthMap[28] || 0) > 0 && content.includes("|")) {
|
|
464
|
+
content = content.replaceAll("|", "\\|");
|
|
465
|
+
state.buffer.length = span.fragment + 1;
|
|
466
|
+
state.buffer.push(content);
|
|
467
|
+
}
|
|
414
468
|
const delimiter = "`".repeat(Math.max(1, maxBacktickRun(content) + 1));
|
|
415
469
|
const padded = content.startsWith("`") || content.endsWith("`");
|
|
416
470
|
state.buffer[span.fragment] = `${span.prefix}${delimiter}${padded ? " " : ""}`;
|
|
@@ -454,12 +508,7 @@ function finalizeBlockquote(state) {
|
|
|
454
508
|
state.lastContentCache = quoted;
|
|
455
509
|
}
|
|
456
510
|
function collapseNestedBlockquoteSeparator(buffer) {
|
|
457
|
-
|
|
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;
|
|
511
|
+
if (newlineRunBefore(buffer, buffer.length) < 2) return;
|
|
463
512
|
const last = buffer.at(-1);
|
|
464
513
|
if (last.length === 1) buffer.pop();
|
|
465
514
|
else buffer[buffer.length - 1] = last.slice(0, -1);
|
|
@@ -467,13 +516,12 @@ function collapseNestedBlockquoteSeparator(buffer) {
|
|
|
467
516
|
function flushPreFence(state) {
|
|
468
517
|
if (state.plainText) {
|
|
469
518
|
state.preFencePending = false;
|
|
470
|
-
state.preOwnFence = false;
|
|
471
519
|
return;
|
|
472
520
|
}
|
|
473
521
|
state.preFencePending = false;
|
|
474
|
-
state.
|
|
522
|
+
state.preFenceOpen = true;
|
|
475
523
|
const lang = state.preFenceLang || "";
|
|
476
|
-
const fence = (state.depthMap[25] || 0) > 0 ?
|
|
524
|
+
const fence = (state.depthMap[25] || 0) > 0 ? `${blockOpenPrefix(state.buffer, state.listIndent) ?? ""}\`\`\`${lang}\n${state.listIndent}` : `\`\`\`${lang}\n`;
|
|
477
525
|
state.buffer.push(fence);
|
|
478
526
|
startCodeFence(state, state.buffer.length - 1, lang, state.listIndent);
|
|
479
527
|
state.lastContentCache = fence;
|
|
@@ -500,14 +548,14 @@ function consumeGfmAction(action, state, lifecycle) {
|
|
|
500
548
|
return;
|
|
501
549
|
case "PreEnter":
|
|
502
550
|
state.preFencePending = true;
|
|
503
|
-
state.
|
|
551
|
+
state.preFenceOpen = false;
|
|
504
552
|
state.preFenceLang = action.language;
|
|
505
553
|
return;
|
|
506
554
|
case "PreExit": {
|
|
507
|
-
const
|
|
555
|
+
const fenceOpen = state.preFenceOpen;
|
|
508
556
|
state.preFencePending = false;
|
|
509
|
-
state.
|
|
510
|
-
if (!
|
|
557
|
+
state.preFenceOpen = false;
|
|
558
|
+
if (!fenceOpen) return void 0;
|
|
511
559
|
const indent = state.listIndent;
|
|
512
560
|
const output = (state.depthMap[25] || 0) > 0 ? `\n${indent}\`\`\`\n\n${indent}` : `\n\`\`\``;
|
|
513
561
|
const delimiter = finalizeCodeFence(state);
|
|
@@ -520,11 +568,8 @@ function consumeGfmAction(action, state, lifecycle) {
|
|
|
520
568
|
}
|
|
521
569
|
case "CodeFenceEnter":
|
|
522
570
|
state.preFencePending = false;
|
|
571
|
+
state.preFenceOpen = true;
|
|
523
572
|
return action.output;
|
|
524
|
-
case "CodeFenceExit": {
|
|
525
|
-
const delimiter = finalizeCodeFence(state);
|
|
526
|
-
return delimiter ? action.output.replace("```", delimiter) : action.output;
|
|
527
|
-
}
|
|
528
573
|
}
|
|
529
574
|
}
|
|
530
575
|
function commitGfmAction(action, state, lifecycle, outputStart) {
|
|
@@ -561,7 +606,7 @@ function getPlainTextOutput(node, eventType, state) {
|
|
|
561
606
|
const depthMap = state.depthMap;
|
|
562
607
|
if (eventType === 0) {
|
|
563
608
|
if (tagId === 6) return "\n";
|
|
564
|
-
if (tagId === 35 && ((depthMap[22] || 0) > 0 || (depthMap[25] || 0) > 0 && !(
|
|
609
|
+
if (tagId === 35 && ((depthMap[22] || 0) > 0 || (depthMap[25] || 0) > 0 && !isInsideTableCell(state))) {
|
|
565
610
|
const lastEntry = state.buffer.at(-1);
|
|
566
611
|
const lastChar = lastEntry?.charAt(lastEntry.length - 1) || "";
|
|
567
612
|
if (lastChar && lastChar !== " " && lastChar !== "\n") return "\n\n";
|
|
@@ -586,8 +631,17 @@ function createMarkdownProcessor(options = {}, resolvedPlugins = [], tagOverride
|
|
|
586
631
|
listIndentWidths: [],
|
|
587
632
|
blockquotes: [],
|
|
588
633
|
bufferedBlockquoteDepth: 0,
|
|
589
|
-
plainText: options.format === "text"
|
|
634
|
+
plainText: options.format === "text",
|
|
635
|
+
emptyItemFragment: void 0
|
|
590
636
|
};
|
|
637
|
+
const bufferScan = [
|
|
638
|
+
false,
|
|
639
|
+
0,
|
|
640
|
+
0,
|
|
641
|
+
0,
|
|
642
|
+
0
|
|
643
|
+
];
|
|
644
|
+
let inRawHtmlRegion = false;
|
|
591
645
|
const openMarkers = [];
|
|
592
646
|
let openMarkerCount = 0;
|
|
593
647
|
const gfmLifecycle = { openCodeSpans: [] };
|
|
@@ -612,10 +666,12 @@ function createMarkdownProcessor(options = {}, resolvedPlugins = [], tagOverride
|
|
|
612
666
|
if (lastChar === "\n" && value[0] && value[0] !== "\n") value = indent + value;
|
|
613
667
|
textNode.value = value;
|
|
614
668
|
}
|
|
615
|
-
if (state.depthMap[34] > 0 && (state
|
|
669
|
+
if (state.depthMap[34] > 0 && isInsideTableCell(state)) textNode.value = foldPreLinesToBr(textNode.value);
|
|
616
670
|
const insideRawHtmlBlock = isInsideRawHtmlBlock(state.depthMap);
|
|
671
|
+
let rawHtmlMarkdown = false;
|
|
672
|
+
if (insideRawHtmlBlock) rawHtmlMarkdown = trackRawHtmlMarkdownContext(state.buffer, bufferScan);
|
|
617
673
|
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);
|
|
674
|
+
if (!state.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);
|
|
619
675
|
if (textNode.generatedMarkdown && textNode.value) textNode.value = `${textNode.generatedMarkdown.prefix}${textNode.value}${textNode.generatedMarkdown.suffix}`;
|
|
620
676
|
const wrapWidth = state.options?.wrapWidth;
|
|
621
677
|
if (wrapWidth && canWrapHere(state.depthMap)) {
|
|
@@ -634,6 +690,17 @@ function createMarkdownProcessor(options = {}, resolvedPlugins = [], tagOverride
|
|
|
634
690
|
const { type: eventType, node } = event;
|
|
635
691
|
state.depth = node.depth;
|
|
636
692
|
if (node.type === 1 ? node.excludedFromMarkdown : node.parent?.excludedFromMarkdown) return;
|
|
693
|
+
if (state.listRulePending !== void 0) {
|
|
694
|
+
const isVisibleText = node.type === 2 && eventType === 0 && hasNonWhitespace(node.value);
|
|
695
|
+
const opensVisibleElement = node.type === 1 && eventType === 0 && !node.tagHandler?.excludesTextNodes;
|
|
696
|
+
if (isVisibleText || opensVisibleElement) {
|
|
697
|
+
const separator = blockOpenPrefix(state.buffer, state.listRulePending);
|
|
698
|
+
state.buffer.push(separator);
|
|
699
|
+
state.lastContentCache = separator;
|
|
700
|
+
state.listRulePending = void 0;
|
|
701
|
+
} else if (node.type === 1 && eventType === 1 && node.tagId === 25) state.listRulePending = void 0;
|
|
702
|
+
else if (node.type === 2) return;
|
|
703
|
+
}
|
|
637
704
|
const lastNode = state.lastNode;
|
|
638
705
|
state.lastNode = event.node;
|
|
639
706
|
const buff = state.buffer;
|
|
@@ -647,6 +714,12 @@ function createMarkdownProcessor(options = {}, resolvedPlugins = [], tagOverride
|
|
|
647
714
|
if (node.type !== 1) return;
|
|
648
715
|
const element = node;
|
|
649
716
|
const handler = node.tagHandler;
|
|
717
|
+
const insideRawHtmlRegion = isInsideRawHtmlBlock(state.depthMap);
|
|
718
|
+
if (insideRawHtmlRegion && !inRawHtmlRegion) {
|
|
719
|
+
bufferScan[0] = false;
|
|
720
|
+
bufferScan[1] = state.buffer.length;
|
|
721
|
+
inRawHtmlRegion = true;
|
|
722
|
+
}
|
|
650
723
|
if (eventType === 0 && handler === breakHandler && !element.pluginOutput?.length) {
|
|
651
724
|
const inPre = state.depthMap[34] !== 0;
|
|
652
725
|
let breakOutput = state.plainText ? "\n" : renderBreak(element, state);
|
|
@@ -697,7 +770,8 @@ function createMarkdownProcessor(options = {}, resolvedPlugins = [], tagOverride
|
|
|
697
770
|
const isInlineElement = handler?.isInline === true;
|
|
698
771
|
let gfmAction;
|
|
699
772
|
let handlerOutput;
|
|
700
|
-
|
|
773
|
+
const suppressedInPre = !state.plainText && (state.depthMap[34] || 0) > 0 && suppressesFormattingInPre(element.tagId);
|
|
774
|
+
if (!output && !suppressedInPre && handler?.[eventFn]) {
|
|
701
775
|
const res = state.plainText ? getPlainTextOutput(element, eventType, state) : handler[eventFn]({
|
|
702
776
|
node: element,
|
|
703
777
|
state
|
|
@@ -815,6 +889,8 @@ function createMarkdownProcessor(options = {}, resolvedPlugins = [], tagOverride
|
|
|
815
889
|
}
|
|
816
890
|
}
|
|
817
891
|
if (gfmAction) commitGfmAction(gfmAction, state, gfmLifecycle, outputStart);
|
|
892
|
+
if (element.tagId === 25 && !state.plainText && !isInsideTableCell(state)) if (eventType === 0) recordItemMarker(state, element.index);
|
|
893
|
+
else resolveItemMarker(state, true);
|
|
818
894
|
if (eventType === 0 && element.tagId === 26 && handlerOutput === "[" && buff.at(-1) === "[") openLinkFragment = buff.length - 1;
|
|
819
895
|
if (eventType === 0 && handlerOutput !== void 0 && isInlineElement && !handler?.literalEnter) {
|
|
820
896
|
const markerType = INLINE_MARKER_TYPE[element.tagId];
|
|
@@ -823,6 +899,11 @@ function createMarkdownProcessor(options = {}, resolvedPlugins = [], tagOverride
|
|
|
823
899
|
} else if (openMarkerCount && (handler?.literalExit || element.tagId !== -1 && !isInlineElement || output?.some(hasNonWhitespace))) openMarkerCount = 0;
|
|
824
900
|
if (eventType === 1 && element.tagId === 26) openLinkFragment = -1;
|
|
825
901
|
updateListIndent(state, element, eventType);
|
|
902
|
+
if (!insideRawHtmlRegion && inRawHtmlRegion) {
|
|
903
|
+
bufferScan[0] = false;
|
|
904
|
+
bufferScan[1] = state.buffer.length;
|
|
905
|
+
inRawHtmlRegion = false;
|
|
906
|
+
}
|
|
826
907
|
}
|
|
827
908
|
function processHtml(html) {
|
|
828
909
|
const parseState = {
|
|
@@ -843,6 +924,7 @@ function createMarkdownProcessor(options = {}, resolvedPlugins = [], tagOverride
|
|
|
843
924
|
return result.trimEnd();
|
|
844
925
|
}
|
|
845
926
|
function getMarkdownChunk() {
|
|
927
|
+
resolveItemMarker(state, false);
|
|
846
928
|
const content = state.buffer.join("");
|
|
847
929
|
const currentContent = hasYieldedContent || state.plainText && preserveLeadingWhitespace ? content : content.trimStart();
|
|
848
930
|
const inPre = state.depthMap[34] !== 0;
|
|
@@ -877,6 +959,12 @@ function createMarkdownProcessor(options = {}, resolvedPlugins = [], tagOverride
|
|
|
877
959
|
const spanPos = Math.max(lastYieldedLength, trimBufferedWhitespacePosition(currentContent, fragmentPosition(state.buffer, gfmLifecycle.openCodeSpans[0].fragment) - leadingTrimmed));
|
|
878
960
|
if (spanPos < stableLength) stableLength = spanPos;
|
|
879
961
|
}
|
|
962
|
+
const emptyItemFragment = state.emptyItemFragment;
|
|
963
|
+
const emptyItemHeld = emptyItemFragment !== void 0;
|
|
964
|
+
if (emptyItemHeld) {
|
|
965
|
+
const itemPos = Math.max(lastYieldedLength, trimBufferedWhitespacePosition(currentContent, fragmentPosition(state.buffer, emptyItemFragment) - leadingTrimmed));
|
|
966
|
+
if (itemPos < stableLength) stableLength = itemPos;
|
|
967
|
+
}
|
|
880
968
|
const codeFenceHeld = state.codeFence !== void 0;
|
|
881
969
|
if (codeFenceHeld) {
|
|
882
970
|
const fencePos = Math.max(lastYieldedLength, trimBufferedWhitespacePosition(currentContent, fragmentPosition(state.buffer, state.codeFence.fragment) - leadingTrimmed));
|
|
@@ -892,11 +980,21 @@ function createMarkdownProcessor(options = {}, resolvedPlugins = [], tagOverride
|
|
|
892
980
|
const linkPos = Math.max(lastYieldedLength, trimBufferedWhitespacePosition(currentContent, fragmentPosition(state.buffer, openLinkFragment) - leadingTrimmed));
|
|
893
981
|
if (linkPos < stableLength) stableLength = linkPos;
|
|
894
982
|
}
|
|
983
|
+
const headingHeld = isInsideHeading(state.depthMap);
|
|
984
|
+
if (headingHeld) {
|
|
985
|
+
let headingPos = currentContent.length;
|
|
986
|
+
while (headingPos > 0) {
|
|
987
|
+
const code = currentContent.charCodeAt(headingPos - 1);
|
|
988
|
+
if (code !== 35 && code !== 32 && code !== 9) break;
|
|
989
|
+
headingPos--;
|
|
990
|
+
}
|
|
991
|
+
if (headingPos < stableLength) stableLength = headingPos;
|
|
992
|
+
}
|
|
895
993
|
if (stableLength < lastYieldedLength) stableLength = lastYieldedLength;
|
|
896
994
|
const newContent = currentContent.slice(lastYieldedLength, stableLength);
|
|
897
995
|
lastYieldedLength = stableLength;
|
|
898
996
|
if (newContent) hasYieldedContent = true;
|
|
899
|
-
if (!markerHeld && !codeSpanHeld && !codeFenceHeld && !blockquoteHeld && !linkHeld && (!retainMutableFragments || !inPre)) {
|
|
997
|
+
if (!markerHeld && !codeSpanHeld && !codeFenceHeld && !blockquoteHeld && !linkHeld && !emptyItemHeld && !headingHeld && (!retainMutableFragments || !inPre)) {
|
|
900
998
|
if (!resolvedPlugins.length && !options.wrapWidth && !state.depthMap[26]) {
|
|
901
999
|
if (retainMutableFragments && leadingTrimmed === 0) {
|
|
902
1000
|
const lastFragment = state.buffer.at(-1);
|
|
@@ -904,6 +1002,7 @@ function createMarkdownProcessor(options = {}, resolvedPlugins = [], tagOverride
|
|
|
904
1002
|
const tailStart = Math.max(0, Math.min(stableLength - 2, fragmentStart));
|
|
905
1003
|
const emittedTail = currentContent.slice(tailStart, fragmentStart);
|
|
906
1004
|
state.buffer.length = 0;
|
|
1005
|
+
resetBufferScanCursors(bufferScan);
|
|
907
1006
|
if (emittedTail) state.buffer.push(emittedTail);
|
|
908
1007
|
state.buffer.push(lastFragment);
|
|
909
1008
|
lastYieldedLength = stableLength - tailStart;
|
|
@@ -911,11 +1010,13 @@ function createMarkdownProcessor(options = {}, resolvedPlugins = [], tagOverride
|
|
|
911
1010
|
const tailStart = Math.max(0, stableLength - 2);
|
|
912
1011
|
const emittedTail = currentContent.slice(tailStart, stableLength);
|
|
913
1012
|
state.buffer.length = 0;
|
|
1013
|
+
resetBufferScanCursors(bufferScan);
|
|
914
1014
|
if (emittedTail) state.buffer.push(emittedTail);
|
|
915
1015
|
lastYieldedLength = emittedTail.length;
|
|
916
1016
|
}
|
|
917
1017
|
} else if (!retainMutableFragments && state.buffer.length > 1) {
|
|
918
1018
|
state.buffer.length = 0;
|
|
1019
|
+
resetBufferScanCursors(bufferScan);
|
|
919
1020
|
state.buffer.push(currentContent);
|
|
920
1021
|
}
|
|
921
1022
|
}
|
package/dist/_chunks/parse.mjs
CHANGED
|
@@ -253,16 +253,37 @@ const HTML_ENTITIES = {
|
|
|
253
253
|
yuml: "ÿ",
|
|
254
254
|
$zeta: "ζ"
|
|
255
255
|
};
|
|
256
|
-
function
|
|
257
|
-
|
|
258
|
-
|
|
256
|
+
function lowerAscii(code) {
|
|
257
|
+
return code >= 65 && code <= 90 ? code + 32 : code;
|
|
258
|
+
}
|
|
259
|
+
function isInsideHeading(depthMap) {
|
|
260
|
+
for (let tagId = 7; tagId <= 12; tagId++) if (depthMap[tagId]) return true;
|
|
261
|
+
return false;
|
|
262
|
+
}
|
|
263
|
+
function isInsideTableCell(state) {
|
|
264
|
+
const depthMap = state.depthMap;
|
|
265
|
+
return depthMap[32] > 0 || depthMap[31] > 0;
|
|
266
|
+
}
|
|
267
|
+
function schemeMatches(href, start, end, scheme) {
|
|
268
|
+
let matched = 0;
|
|
269
|
+
for (let index = start; index < end; index++) {
|
|
270
|
+
const code = href.charCodeAt(index);
|
|
271
|
+
if (code === 9 || code === 10 || code === 13) continue;
|
|
272
|
+
if (lowerAscii(code) !== scheme.charCodeAt(matched)) return false;
|
|
273
|
+
if (++matched === scheme.length) return true;
|
|
274
|
+
}
|
|
275
|
+
return false;
|
|
259
276
|
}
|
|
260
277
|
function isEmptyLinkHref(href) {
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
278
|
+
let start = 0;
|
|
279
|
+
while (start < href.length && href.charCodeAt(start) <= 32) start++;
|
|
280
|
+
let end = href.length;
|
|
281
|
+
while (end > start && href.charCodeAt(end - 1) <= 32) end--;
|
|
282
|
+
switch (lowerAscii(href.charCodeAt(start))) {
|
|
283
|
+
case 35: return end - start === 1;
|
|
284
|
+
case 106: return schemeMatches(href, start, end, "javascript:");
|
|
285
|
+
case 100: return schemeMatches(href, start, end, "data:");
|
|
286
|
+
case 118: return schemeMatches(href, start, end, "vbscript:");
|
|
266
287
|
default: return false;
|
|
267
288
|
}
|
|
268
289
|
}
|
|
@@ -336,6 +357,71 @@ function getLanguageFromClass(className) {
|
|
|
336
357
|
}
|
|
337
358
|
return "";
|
|
338
359
|
}
|
|
360
|
+
function parseUnsignedInteger(raw) {
|
|
361
|
+
return raw !== void 0 && /^[\t\n\f\r ]*\+?\d+[\t\n\f\r ]*$/.test(raw) ? Number(raw) : void 0;
|
|
362
|
+
}
|
|
363
|
+
function lastOutputChar(buffer) {
|
|
364
|
+
for (let index = buffer.length - 1; index >= 0; index--) {
|
|
365
|
+
const fragment = buffer[index];
|
|
366
|
+
if (fragment.length > 0) return fragment.charCodeAt(fragment.length - 1);
|
|
367
|
+
}
|
|
368
|
+
return -1;
|
|
369
|
+
}
|
|
370
|
+
function blockOpenPrefix(buffer, prefix) {
|
|
371
|
+
switch (lastOutputChar(buffer)) {
|
|
372
|
+
case -1: return;
|
|
373
|
+
case 32: {
|
|
374
|
+
let fragment = buffer.length - 1;
|
|
375
|
+
let index = buffer[fragment].length;
|
|
376
|
+
for (;;) {
|
|
377
|
+
if (index === 0) {
|
|
378
|
+
if (--fragment < 0) return void 0;
|
|
379
|
+
index = buffer[fragment].length;
|
|
380
|
+
continue;
|
|
381
|
+
}
|
|
382
|
+
const code = buffer[fragment].charCodeAt(--index);
|
|
383
|
+
if (code !== 32) return code === 10 || listMarkerLineStart(buffer, fragment, index) ? void 0 : `\n\n${prefix ?? ""}`;
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
case 10: return prefix || void 0;
|
|
387
|
+
default: return `\n\n${prefix ?? ""}`;
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
function listMarkerLineStart(buffer, fragmentIndex, markerIndex) {
|
|
391
|
+
const marker = buffer[fragmentIndex].charCodeAt(markerIndex);
|
|
392
|
+
const ordered = marker === 46 || marker === 41;
|
|
393
|
+
if (!ordered && marker !== 45 && marker !== 42 && marker !== 43) return false;
|
|
394
|
+
let digits = 0;
|
|
395
|
+
let spaces = 0;
|
|
396
|
+
let fragment = fragmentIndex;
|
|
397
|
+
let cursor = markerIndex;
|
|
398
|
+
for (;;) {
|
|
399
|
+
if (cursor === 0) {
|
|
400
|
+
if (--fragment < 0) return !ordered || digits > 0;
|
|
401
|
+
cursor = buffer[fragment].length;
|
|
402
|
+
continue;
|
|
403
|
+
}
|
|
404
|
+
const code = buffer[fragment].charCodeAt(--cursor);
|
|
405
|
+
if (ordered && spaces === 0 && code >= 48 && code <= 57) {
|
|
406
|
+
if (++digits > 9) return false;
|
|
407
|
+
continue;
|
|
408
|
+
}
|
|
409
|
+
if (ordered && digits === 0) return false;
|
|
410
|
+
if (code === 32) {
|
|
411
|
+
if (++spaces > 3) return false;
|
|
412
|
+
continue;
|
|
413
|
+
}
|
|
414
|
+
return code === 10;
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
const MAX_ORDERED_START = 999999999;
|
|
418
|
+
function orderedItemNumber(list, index) {
|
|
419
|
+
const raw = list?.attributes?.start;
|
|
420
|
+
if (raw === void 0) return index + 1;
|
|
421
|
+
const start = parseUnsignedInteger(raw);
|
|
422
|
+
if (start === void 0 || start > MAX_ORDERED_START) return Math.min(1 + index, MAX_ORDERED_START);
|
|
423
|
+
return Math.min(start + index, MAX_ORDERED_START);
|
|
424
|
+
}
|
|
339
425
|
function numericReplacement(codePoint) {
|
|
340
426
|
if (codePoint === 0 || codePoint > 1114111 || codePoint >= 55296 && codePoint <= 57343) return "�";
|
|
341
427
|
if (codePoint >= 128 && codePoint <= 159) return C1_REPLACEMENTS[codePoint - 128];
|
|
@@ -489,13 +575,9 @@ function isAutolinkUri(s) {
|
|
|
489
575
|
}
|
|
490
576
|
return true;
|
|
491
577
|
}
|
|
492
|
-
function isInsideTableCell(state) {
|
|
493
|
-
const depthMap = state.depthMap;
|
|
494
|
-
return depthMap[32] > 0 || depthMap[31] > 0;
|
|
495
|
-
}
|
|
496
578
|
function renderBreak(node, state) {
|
|
497
579
|
const depthMap = state.depthMap;
|
|
498
|
-
if (isInsideTableCell(state) || isInsideRawHtmlBlock(depthMap) || depthMap
|
|
580
|
+
if (isInsideTableCell(state) || isInsideRawHtmlBlock(depthMap) || isInsideHeading(depthMap)) return "<br>";
|
|
499
581
|
if (depthMap[34] || depthMap[23]) return "\n";
|
|
500
582
|
return ` \n${continuationPrefix(node, state.listIndentWidths || [], !state.bufferedBlockquoteDepth)}`;
|
|
501
583
|
}
|
|
@@ -506,14 +588,87 @@ const breakHandler = {
|
|
|
506
588
|
collapsesInnerWhiteSpace: true,
|
|
507
589
|
isInline: true
|
|
508
590
|
};
|
|
591
|
+
function escapeTrailingHeadingHashes(buffer) {
|
|
592
|
+
let index = buffer.length - 1;
|
|
593
|
+
while (index >= 0 && buffer[index] === "") index--;
|
|
594
|
+
if (index < 0) return;
|
|
595
|
+
const entry = buffer[index];
|
|
596
|
+
let end = entry.length;
|
|
597
|
+
while (end > 0 && (entry[end - 1] === " " || entry[end - 1] === " ")) end--;
|
|
598
|
+
let run = end;
|
|
599
|
+
while (run > 0 && entry[run - 1] === "#") run--;
|
|
600
|
+
if (run === end) return;
|
|
601
|
+
const previous = buffer[index - 1];
|
|
602
|
+
const before = run > 0 ? entry[run - 1] : previous?.charAt(previous.length - 1);
|
|
603
|
+
if (before !== " " && before !== " ") return;
|
|
604
|
+
buffer[index] = `${entry.slice(0, run)}\\${entry.slice(run)}`;
|
|
605
|
+
}
|
|
606
|
+
const LINE_FRESH = -1;
|
|
607
|
+
const LINE_OPEN = 0;
|
|
608
|
+
const LINE_ROW = 1;
|
|
609
|
+
const LINE_CONTENT = 2;
|
|
610
|
+
function lineStateBeforeRow(buffer) {
|
|
611
|
+
let fragment = buffer.length - 1;
|
|
612
|
+
while (fragment >= 0 && buffer[fragment].length === 0) fragment--;
|
|
613
|
+
if (fragment < 0) return LINE_OPEN;
|
|
614
|
+
const tail = buffer[fragment];
|
|
615
|
+
if (tail.charCodeAt(tail.length - 1) === 10) return LINE_FRESH;
|
|
616
|
+
let firstNonSpace = -1;
|
|
617
|
+
let markerFragment = -1;
|
|
618
|
+
let markerIndex = -1;
|
|
619
|
+
let cursor = tail.length;
|
|
620
|
+
for (;;) {
|
|
621
|
+
if (cursor === 0) {
|
|
622
|
+
if (--fragment < 0) break;
|
|
623
|
+
cursor = buffer[fragment].length;
|
|
624
|
+
continue;
|
|
625
|
+
}
|
|
626
|
+
const code = buffer[fragment].charCodeAt(--cursor);
|
|
627
|
+
if (code === 10) break;
|
|
628
|
+
if (code !== 32 && code !== 9) {
|
|
629
|
+
firstNonSpace = code;
|
|
630
|
+
if (markerFragment < 0) {
|
|
631
|
+
markerFragment = fragment;
|
|
632
|
+
markerIndex = cursor;
|
|
633
|
+
}
|
|
634
|
+
}
|
|
635
|
+
}
|
|
636
|
+
if (firstNonSpace === -1) return LINE_OPEN;
|
|
637
|
+
if (firstNonSpace === 124) return LINE_ROW;
|
|
638
|
+
return listMarkerLineStart(buffer, markerFragment, markerIndex) ? LINE_OPEN : LINE_CONTENT;
|
|
639
|
+
}
|
|
640
|
+
function rowMarker(state) {
|
|
641
|
+
const indent = state.listIndent;
|
|
642
|
+
const lineState = lineStateBeforeRow(state.buffer);
|
|
643
|
+
if (!indent) return lineState === LINE_ROW ? "\n| " : lineState === LINE_CONTENT ? "\n\n| " : "| ";
|
|
644
|
+
switch (lineState) {
|
|
645
|
+
case LINE_ROW: return `\n${indent}| `;
|
|
646
|
+
case LINE_CONTENT: return `\n\n${indent}| `;
|
|
647
|
+
case LINE_FRESH: return `${indent}| `;
|
|
648
|
+
default: return "| ";
|
|
649
|
+
}
|
|
650
|
+
}
|
|
651
|
+
const MAX_CELL_SPAN = 64;
|
|
652
|
+
function cellEnter(node, state) {
|
|
653
|
+
if (node.index === 0) return "";
|
|
654
|
+
const header = state.tableHeaderCells || 0;
|
|
655
|
+
return header > 0 && (state.tableCurrentRowCells || 0) >= header ? " " : " | ";
|
|
656
|
+
}
|
|
657
|
+
function cellExit(node, state) {
|
|
658
|
+
const parsedSpan = parseUnsignedInteger(node.attributes?.colspan);
|
|
659
|
+
const span = parsedSpan && parsedSpan < 256 ? Math.min(parsedSpan, MAX_CELL_SPAN) : 1;
|
|
660
|
+
state.tableCurrentRowCells += span;
|
|
661
|
+
return span > 1 ? " |".repeat(span - 1) : void 0;
|
|
662
|
+
}
|
|
509
663
|
function handleHeading(depth) {
|
|
510
664
|
return {
|
|
511
665
|
enter: ({ state }) => {
|
|
512
|
-
if ((state.depthMap?.[26] || 0) > 0) return `<h${depth}>`;
|
|
666
|
+
if ((state.depthMap?.[26] || 0) > 0 || isInsideTableCell(state)) return `<h${depth}>`;
|
|
513
667
|
return `${"#".repeat(depth)} `;
|
|
514
668
|
},
|
|
515
669
|
exit: ({ state }) => {
|
|
516
|
-
if ((state.depthMap?.[26] || 0) > 0) return `</h${depth}>`;
|
|
670
|
+
if ((state.depthMap?.[26] || 0) > 0 || isInsideTableCell(state)) return `</h${depth}>`;
|
|
671
|
+
escapeTrailingHeadingHashes(state.buffer);
|
|
517
672
|
},
|
|
518
673
|
collapsesInnerWhiteSpace: true
|
|
519
674
|
};
|
|
@@ -595,7 +750,15 @@ const tagHandlers = {
|
|
|
595
750
|
[11]: handleHeading(5),
|
|
596
751
|
[12]: handleHeading(6),
|
|
597
752
|
[13]: {
|
|
598
|
-
enter: () =>
|
|
753
|
+
enter: ({ node, state }) => {
|
|
754
|
+
if (isInsideTableCell(state)) return "<hr>";
|
|
755
|
+
if (!(state.depthMap?.[25] || state.bufferedBlockquoteDepth)) return "---";
|
|
756
|
+
const prefix = continuationPrefix(node, state.listIndentWidths || [], !state.bufferedBlockquoteDepth);
|
|
757
|
+
if (!prefix) return "---";
|
|
758
|
+
if (state.depthMap?.[25]) state.listRulePending = prefix;
|
|
759
|
+
const open = blockOpenPrefix(state.buffer, prefix);
|
|
760
|
+
return open === void 0 ? "***" : `${open}---`;
|
|
761
|
+
},
|
|
599
762
|
isSelfClosing: true
|
|
600
763
|
},
|
|
601
764
|
[14]: Strong,
|
|
@@ -653,13 +816,16 @@ const tagHandlers = {
|
|
|
653
816
|
enter: ({ node, state }) => {
|
|
654
817
|
if ((state.depthMap?.[34] || 0) > 0) {
|
|
655
818
|
if (isInsideTableCell(state)) return "<code>";
|
|
656
|
-
if (state.
|
|
819
|
+
if (state.preFenceOpen) return;
|
|
657
820
|
const language = getLanguageFromClass(node.attributes?.class);
|
|
658
|
-
if ((state.depthMap?.[25] || 0) > 0)
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
821
|
+
if ((state.depthMap?.[25] || 0) > 0) {
|
|
822
|
+
const indent = state.listIndent;
|
|
823
|
+
return {
|
|
824
|
+
_tag: "CodeFenceEnter",
|
|
825
|
+
language,
|
|
826
|
+
output: `${blockOpenPrefix(state.buffer, indent) ?? ""}\`\`\`${language}\n`
|
|
827
|
+
};
|
|
828
|
+
}
|
|
663
829
|
return {
|
|
664
830
|
_tag: "CodeFenceEnter",
|
|
665
831
|
language,
|
|
@@ -683,18 +849,7 @@ const tagHandlers = {
|
|
|
683
849
|
exit: ({ state }) => {
|
|
684
850
|
if ((state.depthMap?.[34] || 0) > 0) {
|
|
685
851
|
if (isInsideTableCell(state)) return "</code>";
|
|
686
|
-
|
|
687
|
-
if ((state.depthMap?.[25] || 0) > 0) {
|
|
688
|
-
const indent = state.listIndent;
|
|
689
|
-
return {
|
|
690
|
-
_tag: "CodeFenceExit",
|
|
691
|
-
output: `\n${indent}\`\`\`\n\n${indent}`
|
|
692
|
-
};
|
|
693
|
-
}
|
|
694
|
-
return {
|
|
695
|
-
_tag: "CodeFenceExit",
|
|
696
|
-
output: `\n\`\`\``
|
|
697
|
-
};
|
|
852
|
+
return;
|
|
698
853
|
}
|
|
699
854
|
if (isInsideRawHtmlBlock(state.depthMap)) return "</code>";
|
|
700
855
|
return { _tag: "CodeSpanExit" };
|
|
@@ -714,7 +869,7 @@ const tagHandlers = {
|
|
|
714
869
|
[25]: {
|
|
715
870
|
enter: ({ node, state }) => {
|
|
716
871
|
if (isInsideTableCell(state)) return "<li>";
|
|
717
|
-
const marker = node.parent?.tagId === 33 ? `${node.index
|
|
872
|
+
const marker = node.parent?.tagId === 33 ? `${orderedItemNumber(node.parent, node.index)}. ` : "- ";
|
|
718
873
|
return `${state.listIndent}${marker}`;
|
|
719
874
|
},
|
|
720
875
|
exit: ({ state }) => isInsideTableCell(state) ? "</li>" : void 0,
|
|
@@ -773,9 +928,14 @@ const tagHandlers = {
|
|
|
773
928
|
if (isInsideTableCell(state)) return "<table>";
|
|
774
929
|
if ((state.depthMap?.[28] || 0) <= 1) state.tableRenderedTable = false;
|
|
775
930
|
state.tableColumnAlignments = [];
|
|
931
|
+
state.tableHeaderCells = 0;
|
|
776
932
|
},
|
|
777
933
|
exit: ({ state }) => isInsideTableCell(state) ? "</table>" : void 0
|
|
778
934
|
},
|
|
935
|
+
[107]: { enter: ({ state }) => {
|
|
936
|
+
if ((state.depthMap?.[25] || 0) === 0 || isInsideTableCell(state)) return void 0;
|
|
937
|
+
return blockOpenPrefix(state.buffer, state.listIndent);
|
|
938
|
+
} },
|
|
779
939
|
[29]: {
|
|
780
940
|
enter: ({ state }) => {
|
|
781
941
|
if (isInsideTableCell(state)) return "<thead>";
|
|
@@ -788,7 +948,7 @@ const tagHandlers = {
|
|
|
788
948
|
enter: ({ state }) => {
|
|
789
949
|
if (isInsideTableCell(state)) return "<tr>";
|
|
790
950
|
state.tableCurrentRowCells = 0;
|
|
791
|
-
return
|
|
951
|
+
return rowMarker(state);
|
|
792
952
|
},
|
|
793
953
|
exit: ({ state }) => {
|
|
794
954
|
if (isInsideTableCell(state) || (state.depthMap?.[28] || 0) > 1) return "</tr>";
|
|
@@ -796,14 +956,16 @@ const tagHandlers = {
|
|
|
796
956
|
state.tableRenderedTable = true;
|
|
797
957
|
const alignments = state.tableColumnAlignments;
|
|
798
958
|
while (alignments.length < state.tableCurrentRowCells) alignments.push("");
|
|
799
|
-
|
|
959
|
+
const alignmentMarkers = alignments.map((align) => {
|
|
800
960
|
switch (align) {
|
|
801
961
|
case "left": return ":---";
|
|
802
962
|
case "center": return ":---:";
|
|
803
963
|
case "right": return "---:";
|
|
804
964
|
default: return "---";
|
|
805
965
|
}
|
|
806
|
-
})
|
|
966
|
+
});
|
|
967
|
+
state.tableHeaderCells = alignments.length;
|
|
968
|
+
return ` |\n${(state.depthMap?.[25] || 0) > 0 ? state.listIndent : ""}| ${alignmentMarkers.join(" | ")} |`;
|
|
807
969
|
}
|
|
808
970
|
return " |";
|
|
809
971
|
},
|
|
@@ -816,11 +978,11 @@ const tagHandlers = {
|
|
|
816
978
|
const align = node.attributes?.align?.toLowerCase();
|
|
817
979
|
if (align) state.tableColumnAlignments.push(align);
|
|
818
980
|
else if (state.tableColumnAlignments.length <= state.tableCurrentRowCells) state.tableColumnAlignments.push("");
|
|
819
|
-
return node
|
|
981
|
+
return cellEnter(node, state);
|
|
820
982
|
},
|
|
821
|
-
exit: ({ state }) => {
|
|
983
|
+
exit: ({ node, state }) => {
|
|
822
984
|
if ((state.depthMap?.[28] || 0) > 1) return "</th>";
|
|
823
|
-
state
|
|
985
|
+
return cellExit(node, state);
|
|
824
986
|
},
|
|
825
987
|
collapsesInnerWhiteSpace: true,
|
|
826
988
|
spacing: NO_SPACING
|
|
@@ -828,11 +990,11 @@ const tagHandlers = {
|
|
|
828
990
|
[32]: {
|
|
829
991
|
enter: ({ node, state }) => {
|
|
830
992
|
if ((state.depthMap?.[28] || 0) > 1) return "<td>";
|
|
831
|
-
return node
|
|
993
|
+
return cellEnter(node, state);
|
|
832
994
|
},
|
|
833
|
-
exit: ({ state }) => {
|
|
995
|
+
exit: ({ node, state }) => {
|
|
834
996
|
if ((state.depthMap?.[28] || 0) > 1) return "</td>";
|
|
835
|
-
state
|
|
997
|
+
return cellExit(node, state);
|
|
836
998
|
},
|
|
837
999
|
collapsesInnerWhiteSpace: true,
|
|
838
1000
|
spacing: NO_SPACING
|
|
@@ -1403,11 +1565,17 @@ function closeTableContext(state, closeable, handleEvent) {
|
|
|
1403
1565
|
if (id === void 0 || !closeable.has(id)) return;
|
|
1404
1566
|
clearFlattenedElements(state);
|
|
1405
1567
|
}
|
|
1406
|
-
|
|
1407
|
-
|
|
1408
|
-
|
|
1409
|
-
|
|
1568
|
+
let closeCount = 0;
|
|
1569
|
+
let walked = 0;
|
|
1570
|
+
for (let node = state.currentNode; node; node = node.parent) {
|
|
1571
|
+
const id = node.tagId;
|
|
1572
|
+
if (id !== void 0 && closeable.has(id)) {
|
|
1573
|
+
walked++;
|
|
1574
|
+
closeCount = walked;
|
|
1575
|
+
} else if (id === 28 || id === 78 || id === 107) break;
|
|
1576
|
+
else walked++;
|
|
1410
1577
|
}
|
|
1578
|
+
for (let i = 0; i < closeCount && state.currentNode; i++) closeNode(state.currentNode, state, handleEvent);
|
|
1411
1579
|
}
|
|
1412
1580
|
function closeSelectTo(state, targetId, handleEvent) {
|
|
1413
1581
|
if (state.flattenedTagName) {
|
|
@@ -2409,4 +2577,4 @@ function parseAttributes(attrStr) {
|
|
|
2409
2577
|
if (result._tag === "incomplete") throw new Error("Isolated attribute scans always complete at EOF");
|
|
2410
2578
|
return result.attributes === EMPTY_ATTRIBUTES ? {} : result.attributes;
|
|
2411
2579
|
}
|
|
2412
|
-
export { breakHandler, buildTagOverrideHandlers, continuationPrefix, finalizeParse, isEmptyLinkHref, parseAttributes, parseHtml, parseHtmlStream, renderBreak, resolveUrl };
|
|
2580
|
+
export { blockOpenPrefix, breakHandler, buildTagOverrideHandlers, continuationPrefix, finalizeParse, isCharacterReferenceTail, isEmptyLinkHref, isInsideHeading, isInsideTableCell, listMarkerLineStart, orderedItemNumber, parseAttributes, parseHtml, parseHtmlStream, renderBreak, resolveUrl };
|
package/dist/_chunks/plugins.mjs
CHANGED
|
@@ -84,9 +84,16 @@ function createCompoundSelector(selectors) {
|
|
|
84
84
|
toString: () => selectors.map((s) => s.toString()).join("")
|
|
85
85
|
};
|
|
86
86
|
}
|
|
87
|
-
function
|
|
88
|
-
|
|
89
|
-
|
|
87
|
+
function createSelectorList(selectors) {
|
|
88
|
+
return {
|
|
89
|
+
matches(element) {
|
|
90
|
+
for (let i = 0; i < selectors.length; i++) if (selectors[i].matches(element)) return true;
|
|
91
|
+
return false;
|
|
92
|
+
},
|
|
93
|
+
toString: () => selectors.map((s) => s.toString()).join(", ")
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
function parseCompoundSelector(selector) {
|
|
90
97
|
const selectorParts = [];
|
|
91
98
|
let current = "";
|
|
92
99
|
let inAttribute = false;
|
|
@@ -112,6 +119,41 @@ function parseSelector(selector) {
|
|
|
112
119
|
if (selectorParts.length === 1) return selectorParts[0];
|
|
113
120
|
return createCompoundSelector(selectorParts);
|
|
114
121
|
}
|
|
122
|
+
function parseSelector(selector) {
|
|
123
|
+
selector = selector.trim();
|
|
124
|
+
if (!selector) throw new Error("Empty selector");
|
|
125
|
+
const selectors = [];
|
|
126
|
+
let start = 0;
|
|
127
|
+
let inAttribute = false;
|
|
128
|
+
let quote = 0;
|
|
129
|
+
let escaped = false;
|
|
130
|
+
for (let i = 0; i < selector.length; i++) {
|
|
131
|
+
const char = selector.charCodeAt(i);
|
|
132
|
+
if (quote) {
|
|
133
|
+
if (escaped) escaped = false;
|
|
134
|
+
else if (char === 92) escaped = true;
|
|
135
|
+
else if (char === quote) quote = 0;
|
|
136
|
+
continue;
|
|
137
|
+
}
|
|
138
|
+
if (inAttribute) {
|
|
139
|
+
if (char === 34 || char === 39) quote = char;
|
|
140
|
+
else if (char === 93) inAttribute = false;
|
|
141
|
+
continue;
|
|
142
|
+
}
|
|
143
|
+
if (char === 91) inAttribute = true;
|
|
144
|
+
else if (char === 44) {
|
|
145
|
+
const part = selector.slice(start, i).trim();
|
|
146
|
+
if (!part) return createTagSelector(selector);
|
|
147
|
+
selectors.push(parseCompoundSelector(part));
|
|
148
|
+
start = i + 1;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
if (!selectors.length) return parseCompoundSelector(selector);
|
|
152
|
+
const last = selector.slice(start).trim();
|
|
153
|
+
if (!last) return createTagSelector(selector);
|
|
154
|
+
selectors.push(parseCompoundSelector(last));
|
|
155
|
+
return createSelectorList(selectors);
|
|
156
|
+
}
|
|
115
157
|
function extractionPlugin(selectors) {
|
|
116
158
|
const matcherCallbacks = Object.entries(selectors).map(([selector, callback]) => ({
|
|
117
159
|
matcher: parseSelector(selector),
|
|
@@ -120,11 +162,17 @@ function extractionPlugin(selectors) {
|
|
|
120
162
|
const trackedElements = /* @__PURE__ */ new Map();
|
|
121
163
|
return createPlugin({
|
|
122
164
|
onNodeEnter(element) {
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
165
|
+
let callbacks;
|
|
166
|
+
for (let i = 0; i < matcherCallbacks.length; i++) {
|
|
167
|
+
const { matcher, callback } = matcherCallbacks[i];
|
|
168
|
+
if (matcher.matches(element)) {
|
|
169
|
+
callbacks ||= [];
|
|
170
|
+
callbacks.push(callback);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
if (callbacks) trackedElements.set(element, {
|
|
174
|
+
textContent: "",
|
|
175
|
+
callbacks
|
|
128
176
|
});
|
|
129
177
|
},
|
|
130
178
|
processTextNode(textNode) {
|
|
@@ -138,11 +186,14 @@ function extractionPlugin(selectors) {
|
|
|
138
186
|
onNodeExit(element, state) {
|
|
139
187
|
const tracked = trackedElements.get(element);
|
|
140
188
|
if (tracked) {
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
189
|
+
for (let i = 0; i < tracked.callbacks.length; i++) {
|
|
190
|
+
const extractedElement = {
|
|
191
|
+
...element,
|
|
192
|
+
attributes: { ...element.attributes },
|
|
193
|
+
textContent: tracked.textContent.trim()
|
|
194
|
+
};
|
|
195
|
+
tracked.callbacks[i](extractedElement, state);
|
|
196
|
+
}
|
|
146
197
|
trackedElements.delete(element);
|
|
147
198
|
}
|
|
148
199
|
}
|
|
@@ -158,14 +209,18 @@ function extractionCollectorPlugin(config) {
|
|
|
158
209
|
const trackedElements = /* @__PURE__ */ new Map();
|
|
159
210
|
const plugin = createPlugin({
|
|
160
211
|
onNodeEnter(element) {
|
|
212
|
+
let selectors;
|
|
161
213
|
for (let i = 0; i < matchers.length; i++) {
|
|
162
214
|
const m = matchers[i];
|
|
163
|
-
if (m.matcher.matches(element))
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
});
|
|
215
|
+
if (m.matcher.matches(element)) {
|
|
216
|
+
selectors ||= [];
|
|
217
|
+
selectors.push(m.selector);
|
|
218
|
+
}
|
|
168
219
|
}
|
|
220
|
+
if (selectors) trackedElements.set(element, {
|
|
221
|
+
textContent: "",
|
|
222
|
+
selectors
|
|
223
|
+
});
|
|
169
224
|
},
|
|
170
225
|
processTextNode(textNode) {
|
|
171
226
|
let currentParent = textNode.parent;
|
|
@@ -178,13 +233,15 @@ function extractionCollectorPlugin(config) {
|
|
|
178
233
|
onNodeExit(element) {
|
|
179
234
|
const tracked = trackedElements.get(element);
|
|
180
235
|
if (tracked) {
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
236
|
+
for (let i = 0; i < tracked.selectors.length; i++) {
|
|
237
|
+
const extracted = {
|
|
238
|
+
selector: tracked.selectors[i],
|
|
239
|
+
tagName: element.name,
|
|
240
|
+
textContent: tracked.textContent.trim(),
|
|
241
|
+
attributes: { ...element.attributes }
|
|
242
|
+
};
|
|
243
|
+
results.push(extracted);
|
|
244
|
+
}
|
|
188
245
|
trackedElements.delete(element);
|
|
189
246
|
}
|
|
190
247
|
}
|
|
@@ -227,6 +284,7 @@ function filterPlugin(options = {}) {
|
|
|
227
284
|
const excludeSelectors = options.exclude?.map((selector) => compileSelector(selector)) || [];
|
|
228
285
|
const processChildren = options.processChildren !== false;
|
|
229
286
|
const skippedSubtrees = /* @__PURE__ */ new WeakSet();
|
|
287
|
+
const skippedByInclude = /* @__PURE__ */ new WeakSet();
|
|
230
288
|
function excludesSubtree(element) {
|
|
231
289
|
if (skippedSubtrees.has(element)) return true;
|
|
232
290
|
if ((element.parent ? skippedSubtrees.has(element.parent) : false) || isHidden(element) || excludeSelectors.some((selector) => selector.matches(element))) {
|
|
@@ -240,8 +298,10 @@ function filterPlugin(options = {}) {
|
|
|
240
298
|
beforeNodeProcess(event) {
|
|
241
299
|
const { node } = event;
|
|
242
300
|
if (node.type === 2) {
|
|
243
|
-
const
|
|
301
|
+
const textNode = node;
|
|
302
|
+
const parent = textNode.parent;
|
|
244
303
|
if (parent && skippedSubtrees.has(parent)) return { skip: true };
|
|
304
|
+
if (parent && skippedByInclude.has(parent)) textNode.excludedFromMarkdown = true;
|
|
245
305
|
return;
|
|
246
306
|
}
|
|
247
307
|
if (node.type !== 1) return;
|
|
@@ -254,6 +314,7 @@ function filterPlugin(options = {}) {
|
|
|
254
314
|
if (!processChildren) break;
|
|
255
315
|
currentElement = currentElement.parent;
|
|
256
316
|
}
|
|
317
|
+
skippedByInclude.add(element);
|
|
257
318
|
return { skip: true };
|
|
258
319
|
}
|
|
259
320
|
}
|
package/dist/_chunks/types.d.mts
CHANGED
|
@@ -259,6 +259,8 @@ interface MdreamRuntimeState extends Partial<MdreamProcessingState> {
|
|
|
259
259
|
tableRenderedTable?: boolean;
|
|
260
260
|
tableCurrentRowCells?: number;
|
|
261
261
|
tableColumnAlignments?: string[];
|
|
262
|
+
/** See MarkdownState for semantics. */
|
|
263
|
+
tableHeaderCells?: number;
|
|
262
264
|
/** Resolved plugin instances for efficient iteration */
|
|
263
265
|
resolvedPlugins?: TransformPlugin[];
|
|
264
266
|
/** Content buffer for markdown output */
|
|
@@ -280,9 +282,11 @@ interface MdreamRuntimeState extends Partial<MdreamProcessingState> {
|
|
|
280
282
|
*/
|
|
281
283
|
preFencePending?: boolean;
|
|
282
284
|
preFenceLang?: string;
|
|
283
|
-
|
|
285
|
+
preFenceOpen?: boolean;
|
|
284
286
|
/** Number of default blockquotes currently buffered for line prefixing. */
|
|
285
287
|
bufferedBlockquoteDepth?: number;
|
|
288
|
+
/** Content-column prefix deferred after a list item rule. */
|
|
289
|
+
listRulePending?: string;
|
|
286
290
|
/** Whether output should omit Markdown/HTML markup */
|
|
287
291
|
plainText?: boolean;
|
|
288
292
|
}
|
|
@@ -330,9 +334,6 @@ type GfmAction = {
|
|
|
330
334
|
_tag: 'CodeFenceEnter';
|
|
331
335
|
language: string;
|
|
332
336
|
output: string;
|
|
333
|
-
} | {
|
|
334
|
-
_tag: 'CodeFenceExit';
|
|
335
|
-
output: string;
|
|
336
337
|
};
|
|
337
338
|
type TagHandlerResult = string | GfmAction | undefined | void;
|
|
338
339
|
/**
|
package/dist/plugins.d.mts
CHANGED
|
@@ -3,8 +3,9 @@ import { createPlugin } from "./_chunks/index.mjs";
|
|
|
3
3
|
interface ExtractedElement extends ElementNode {
|
|
4
4
|
textContent: string;
|
|
5
5
|
}
|
|
6
|
+
type ExtractionCallback = (element: ExtractedElement, state: MdreamRuntimeState) => void;
|
|
6
7
|
/** Extract matching elements through the composable JavaScript plugin interface. */
|
|
7
|
-
declare function extractionPlugin(selectors: Record<string,
|
|
8
|
+
declare function extractionPlugin(selectors: Record<string, ExtractionCallback>): TransformPlugin;
|
|
8
9
|
/**
|
|
9
10
|
* Extraction collector for `plugins.extraction` config.
|
|
10
11
|
* Collects results during processing; callbacks are called post-conversion
|
package/package.json
CHANGED