@mdream/js 1.5.5 → 1.5.6
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 +2 -0
- package/dist/_chunks/markdown-processor.mjs +402 -64
- package/dist/_chunks/parse.mjs +174 -90
- package/dist/_chunks/plugins.mjs +3 -17
- package/dist/_chunks/src.mjs +2 -2
- package/dist/_chunks/types.d.mts +34 -2
- package/dist/index.mjs +1 -1
- package/package.json +1 -1
package/dist/_chunks/const.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { DEFAULT_BLOCK_SPACING, NO_SPACING } from "./const.mjs";
|
|
2
|
-
import { continuationPrefix, finalizeParse, parseHtmlStream, resolveUrl } from "./parse.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;
|
|
@@ -131,6 +133,118 @@ function foldPreLinesToBr(value) {
|
|
|
131
133
|
}
|
|
132
134
|
return out;
|
|
133
135
|
}
|
|
136
|
+
function isInsideRawHtmlBlock(depthMap) {
|
|
137
|
+
return Boolean(depthMap[2] || depthMap[3] || depthMap[100] || depthMap[101] || depthMap[99] || depthMap[98]);
|
|
138
|
+
}
|
|
139
|
+
function markdownLineIndent(buffer) {
|
|
140
|
+
let spaces = 0;
|
|
141
|
+
for (let fragmentIndex = buffer.length - 1; fragmentIndex >= 0; fragmentIndex--) {
|
|
142
|
+
const fragment = buffer[fragmentIndex];
|
|
143
|
+
for (let index = fragment.length - 1; index >= 0; index--) {
|
|
144
|
+
const code = fragment.charCodeAt(index);
|
|
145
|
+
if (code === 10) return spaces <= 3 ? spaces : -1;
|
|
146
|
+
if (code !== 32) return -1;
|
|
147
|
+
if (++spaces > 3) return -1;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
return spaces <= 3 ? spaces : -1;
|
|
151
|
+
}
|
|
152
|
+
function isMarkdownMarkerWhitespace(code) {
|
|
153
|
+
return code === 0 || code === 9 || code === 10 || code === 13 || code === 32;
|
|
154
|
+
}
|
|
155
|
+
function canStartGfmAngleConstruct(code) {
|
|
156
|
+
return code >= 65 && code <= 90 || code >= 97 && code <= 122 || code === 33 || code === 47 || code === 63;
|
|
157
|
+
}
|
|
158
|
+
function isThematicBreak(value, start, marker) {
|
|
159
|
+
let count = 0;
|
|
160
|
+
for (let index = start; index < value.length; index++) {
|
|
161
|
+
const code = value.charCodeAt(index);
|
|
162
|
+
if (code === marker) count++;
|
|
163
|
+
else if (code === 10 || code === 13) break;
|
|
164
|
+
else if (code !== 32 && code !== 9) return false;
|
|
165
|
+
}
|
|
166
|
+
return count >= 3;
|
|
167
|
+
}
|
|
168
|
+
function isParserProtectedEscape(depthMap, next) {
|
|
169
|
+
return next === 124 && Boolean(depthMap[28]) || (next === 91 || next === 93) && Boolean(depthMap[26]) || next === 62 && Boolean(depthMap[22]);
|
|
170
|
+
}
|
|
171
|
+
function isEntityReferenceAfterAmpersand(value, ampersand) {
|
|
172
|
+
let index = ampersand + 1;
|
|
173
|
+
if (value.charCodeAt(index) === 35) {
|
|
174
|
+
index++;
|
|
175
|
+
const hex = value.charCodeAt(index) === 120 || value.charCodeAt(index) === 88;
|
|
176
|
+
if (hex) index++;
|
|
177
|
+
const start = index;
|
|
178
|
+
while (index < value.length) {
|
|
179
|
+
const code = value.charCodeAt(index);
|
|
180
|
+
if (!(code >= 48 && code <= 57 || hex && (code >= 65 && code <= 70 || code >= 97 && code <= 102))) break;
|
|
181
|
+
index++;
|
|
182
|
+
}
|
|
183
|
+
return index > start && value.charCodeAt(index) === 59;
|
|
184
|
+
}
|
|
185
|
+
const start = index;
|
|
186
|
+
while (index < value.length) {
|
|
187
|
+
const code = value.charCodeAt(index);
|
|
188
|
+
if (!(code >= 48 && code <= 57 || code >= 65 && code <= 90 || code >= 97 && code <= 122)) break;
|
|
189
|
+
index++;
|
|
190
|
+
}
|
|
191
|
+
return index > start && value.charCodeAt(index) === 59;
|
|
192
|
+
}
|
|
193
|
+
const GFM_TEXT_NATIVE_TRIGGER = /[\\*_~`[<\r\n]/;
|
|
194
|
+
function mayNeedGfmTextEscape(value, buffer) {
|
|
195
|
+
if (value.search(GFM_TEXT_NATIVE_TRIGGER) !== -1) return true;
|
|
196
|
+
let lineIndent = markdownLineIndent(buffer);
|
|
197
|
+
if (lineIndent < 0) return false;
|
|
198
|
+
let index = 0;
|
|
199
|
+
while (index < value.length && lineIndent < 3 && value.charCodeAt(index) === 32) {
|
|
200
|
+
index++;
|
|
201
|
+
lineIndent++;
|
|
202
|
+
}
|
|
203
|
+
if (index >= value.length) return false;
|
|
204
|
+
const code = value.charCodeAt(index);
|
|
205
|
+
return code === 35 || code === 43 || code === 45 || code === 62 || code >= 48 && code <= 57;
|
|
206
|
+
}
|
|
207
|
+
function escapeGfmText(value, buffer, depthMap) {
|
|
208
|
+
let lineIndent = markdownLineIndent(buffer);
|
|
209
|
+
let orderedDigits = 0;
|
|
210
|
+
let copiedUntil = 0;
|
|
211
|
+
let escaped = "";
|
|
212
|
+
for (let index = 0; index < value.length; index++) {
|
|
213
|
+
const code = value.charCodeAt(index);
|
|
214
|
+
const next = index + 1 < value.length ? value.charCodeAt(index + 1) : 0;
|
|
215
|
+
if (code === 92 && (isParserProtectedEscape(depthMap, next) || next === 38 && isEntityReferenceAfterAmpersand(value, index + 1))) {
|
|
216
|
+
lineIndent = -1;
|
|
217
|
+
orderedDigits = 0;
|
|
218
|
+
index++;
|
|
219
|
+
continue;
|
|
220
|
+
}
|
|
221
|
+
let shouldEscape = code === 92 || code === 42 || code === 95 || code === 126 || code === 96 || code === 91 || code === 60 && canStartGfmAngleConstruct(value.charCodeAt(index + 1));
|
|
222
|
+
if (!shouldEscape && lineIndent >= 0) {
|
|
223
|
+
if (code === 35) {
|
|
224
|
+
let end = index + 1;
|
|
225
|
+
while (end < value.length && value.charCodeAt(end) === 35) end++;
|
|
226
|
+
shouldEscape = end - index <= 6 && (end === value.length || isMarkdownMarkerWhitespace(value.charCodeAt(end)));
|
|
227
|
+
} else if (code === 45 || code === 43) shouldEscape = index + 1 === value.length || isMarkdownMarkerWhitespace(value.charCodeAt(index + 1)) || code === 45 && isThematicBreak(value, index, code);
|
|
228
|
+
else if (code === 62) shouldEscape = true;
|
|
229
|
+
} else if (!shouldEscape && orderedDigits > 0 && (code === 46 || code === 41)) shouldEscape = index + 1 === value.length || isMarkdownMarkerWhitespace(value.charCodeAt(index + 1));
|
|
230
|
+
if (shouldEscape) {
|
|
231
|
+
escaped += value.slice(copiedUntil, index);
|
|
232
|
+
escaped += `\\${value[index]}`;
|
|
233
|
+
copiedUntil = index + 1;
|
|
234
|
+
}
|
|
235
|
+
if (code === 10) {
|
|
236
|
+
lineIndent = 0;
|
|
237
|
+
orderedDigits = 0;
|
|
238
|
+
} else if (lineIndent >= 0) if (code === 32 && lineIndent < 3) lineIndent++;
|
|
239
|
+
else {
|
|
240
|
+
orderedDigits = code >= 48 && code <= 57 ? 1 : 0;
|
|
241
|
+
lineIndent = -1;
|
|
242
|
+
}
|
|
243
|
+
else if (orderedDigits > 0) if (code >= 48 && code <= 57 && orderedDigits < 9) orderedDigits++;
|
|
244
|
+
else orderedDigits = 0;
|
|
245
|
+
}
|
|
246
|
+
return copiedUntil === 0 ? value : escaped + value.slice(copiedUntil);
|
|
247
|
+
}
|
|
134
248
|
function currentColumn(buffer) {
|
|
135
249
|
let col = 0;
|
|
136
250
|
for (let i = buffer.length - 1; i >= 0; i--) {
|
|
@@ -175,7 +289,7 @@ function wrapText(value, col, width, prefix) {
|
|
|
175
289
|
}
|
|
176
290
|
function calculateNewLineConfig(node, depthMap, plainText) {
|
|
177
291
|
const tagId = node.tagId;
|
|
178
|
-
if (tagId !== 25 && (depthMap[25] || 0) > 0 || tagId !== 22 && (depthMap[22] || 0) > 0) {
|
|
292
|
+
if (tagId !== 25 && (depthMap[25] || 0) > 0 || plainText && tagId !== 22 && (depthMap[22] || 0) > 0) {
|
|
179
293
|
if (plainText && tagId === 34) return [1, 1];
|
|
180
294
|
return NO_SPACING;
|
|
181
295
|
}
|
|
@@ -236,6 +350,89 @@ function dropEmptyMarker(buffer, packed, markerType) {
|
|
|
236
350
|
}
|
|
237
351
|
return -1;
|
|
238
352
|
}
|
|
353
|
+
function maxBacktickRun(value) {
|
|
354
|
+
let max = 0;
|
|
355
|
+
let run = 0;
|
|
356
|
+
for (let index = 0; index < value.length; index++) if (value.charCodeAt(index) === 96) {
|
|
357
|
+
run++;
|
|
358
|
+
if (run > max) max = run;
|
|
359
|
+
} else run = 0;
|
|
360
|
+
return max;
|
|
361
|
+
}
|
|
362
|
+
function maxLineLeadingRun(value, marker, indent) {
|
|
363
|
+
let max = 0;
|
|
364
|
+
let lineStart = 0;
|
|
365
|
+
while (lineStart <= value.length) {
|
|
366
|
+
let index = lineStart;
|
|
367
|
+
if (indent && value.startsWith(indent, index)) index += indent.length;
|
|
368
|
+
let spaces = 0;
|
|
369
|
+
while (spaces < 3 && value.charCodeAt(index) === 32) {
|
|
370
|
+
spaces++;
|
|
371
|
+
index++;
|
|
372
|
+
}
|
|
373
|
+
let run = 0;
|
|
374
|
+
while (value.charCodeAt(index + run) === marker) run++;
|
|
375
|
+
if (run > max) max = run;
|
|
376
|
+
const newline = value.indexOf("\n", lineStart);
|
|
377
|
+
if (newline === -1) break;
|
|
378
|
+
lineStart = newline + 1;
|
|
379
|
+
}
|
|
380
|
+
return max;
|
|
381
|
+
}
|
|
382
|
+
function finalizeCodeSpan(state, span) {
|
|
383
|
+
const content = state.buffer.slice(span.fragment + 1).join("");
|
|
384
|
+
const delimiter = "`".repeat(Math.max(1, maxBacktickRun(content) + 1));
|
|
385
|
+
const padded = content.startsWith("`") || content.endsWith("`");
|
|
386
|
+
state.buffer[span.fragment] = `${span.prefix}${delimiter}${padded ? " " : ""}`;
|
|
387
|
+
return `${padded ? " " : ""}${delimiter}`;
|
|
388
|
+
}
|
|
389
|
+
function startCodeFence(state, fragment, language, indent) {
|
|
390
|
+
state.codeFence = {
|
|
391
|
+
fragment,
|
|
392
|
+
markerOffset: state.buffer[fragment].lastIndexOf("```"),
|
|
393
|
+
indent,
|
|
394
|
+
language
|
|
395
|
+
};
|
|
396
|
+
}
|
|
397
|
+
function finalizeCodeFence(state) {
|
|
398
|
+
const fence = state.codeFence;
|
|
399
|
+
if (!fence) return;
|
|
400
|
+
state.codeFence = void 0;
|
|
401
|
+
const marker = fence.language.includes("`") ? "~" : "`";
|
|
402
|
+
const markerCode = marker.charCodeAt(0);
|
|
403
|
+
const content = state.buffer.slice(fence.fragment + 1).join("");
|
|
404
|
+
const delimiter = marker.repeat(Math.max(3, maxLineLeadingRun(content, markerCode, fence.indent) + 1));
|
|
405
|
+
const opening = state.buffer[fence.fragment];
|
|
406
|
+
state.buffer[fence.fragment] = `${opening.slice(0, fence.markerOffset)}${delimiter}${opening.slice(fence.markerOffset + 3)}`;
|
|
407
|
+
return delimiter;
|
|
408
|
+
}
|
|
409
|
+
function stripBlockquoteListIndent(line, listIndent) {
|
|
410
|
+
return listIndent && line.startsWith(listIndent) ? line.slice(listIndent.length) : line;
|
|
411
|
+
}
|
|
412
|
+
function finalizeBlockquote(state) {
|
|
413
|
+
const frame = state.blockquotes.pop();
|
|
414
|
+
if (!frame) return;
|
|
415
|
+
state.bufferedBlockquoteDepth = state.blockquotes.length;
|
|
416
|
+
const content = state.buffer.slice(frame.fragment).join("").replace(/[ \t\r\n]+$/, "");
|
|
417
|
+
const prefix = `${frame.listIndent}>`;
|
|
418
|
+
const quoted = content.split("\n").map((line) => {
|
|
419
|
+
const unindented = stripBlockquoteListIndent(line, frame.listIndent);
|
|
420
|
+
return unindented ? `${prefix} ${unindented}` : prefix;
|
|
421
|
+
}).join("\n");
|
|
422
|
+
state.buffer.splice(frame.fragment, state.buffer.length - frame.fragment, quoted);
|
|
423
|
+
state.lastContentCache = quoted;
|
|
424
|
+
}
|
|
425
|
+
function collapseNestedBlockquoteSeparator(buffer) {
|
|
426
|
+
let trailingNewlines = 0;
|
|
427
|
+
for (let fragmentIndex = buffer.length - 1; fragmentIndex >= 0 && trailingNewlines < 2; fragmentIndex--) {
|
|
428
|
+
const fragment = buffer[fragmentIndex];
|
|
429
|
+
for (let index = fragment.length - 1; index >= 0 && fragment.charCodeAt(index) === 10; index--) trailingNewlines++;
|
|
430
|
+
}
|
|
431
|
+
if (trailingNewlines < 2) return;
|
|
432
|
+
const last = buffer.at(-1);
|
|
433
|
+
if (last.length === 1) buffer.pop();
|
|
434
|
+
else buffer[buffer.length - 1] = last.slice(0, -1);
|
|
435
|
+
}
|
|
239
436
|
function flushPreFence(state) {
|
|
240
437
|
if (state.plainText) {
|
|
241
438
|
state.preFencePending = false;
|
|
@@ -247,8 +444,82 @@ function flushPreFence(state) {
|
|
|
247
444
|
const lang = state.preFenceLang || "";
|
|
248
445
|
const fence = (state.depthMap[25] || 0) > 0 ? `\n\n${state.listIndent}\`\`\`${lang}\n${state.listIndent}` : `\`\`\`${lang}\n`;
|
|
249
446
|
state.buffer.push(fence);
|
|
447
|
+
startCodeFence(state, state.buffer.length - 1, lang, state.listIndent);
|
|
250
448
|
state.lastContentCache = fence;
|
|
251
449
|
}
|
|
450
|
+
function consumePendingPreChild(state, node, eventType) {
|
|
451
|
+
if (eventType !== 0) return false;
|
|
452
|
+
if (node.type === 1) {
|
|
453
|
+
const element = node;
|
|
454
|
+
if (element.tagId === 23 && element.parent?.tagId === 34) state.preFencePending = false;
|
|
455
|
+
else if (element.tagId !== 34) flushPreFence(state);
|
|
456
|
+
return false;
|
|
457
|
+
}
|
|
458
|
+
if (hasNonWhitespace(node.value)) {
|
|
459
|
+
flushPreFence(state);
|
|
460
|
+
return false;
|
|
461
|
+
}
|
|
462
|
+
return true;
|
|
463
|
+
}
|
|
464
|
+
function consumeGfmAction(action, state, lifecycle) {
|
|
465
|
+
switch (action._tag) {
|
|
466
|
+
case "BlockquoteEnter": return action.output;
|
|
467
|
+
case "BlockquoteExit":
|
|
468
|
+
finalizeBlockquote(state);
|
|
469
|
+
return;
|
|
470
|
+
case "PreEnter":
|
|
471
|
+
state.preFencePending = true;
|
|
472
|
+
state.preOwnFence = false;
|
|
473
|
+
state.preFenceLang = action.language;
|
|
474
|
+
return;
|
|
475
|
+
case "PreExit": {
|
|
476
|
+
const ownFence = state.preOwnFence;
|
|
477
|
+
state.preFencePending = false;
|
|
478
|
+
state.preOwnFence = false;
|
|
479
|
+
if (!ownFence) return void 0;
|
|
480
|
+
const indent = state.listIndent;
|
|
481
|
+
const output = (state.depthMap[25] || 0) > 0 ? `\n${indent}\`\`\`\n\n${indent}` : `\n\`\`\``;
|
|
482
|
+
const delimiter = finalizeCodeFence(state);
|
|
483
|
+
return delimiter ? output.replace("```", delimiter) : output;
|
|
484
|
+
}
|
|
485
|
+
case "CodeSpanEnter": return action.output;
|
|
486
|
+
case "CodeSpanExit": {
|
|
487
|
+
const span = lifecycle.openCodeSpans.pop();
|
|
488
|
+
return span ? finalizeCodeSpan(state, span) : "`";
|
|
489
|
+
}
|
|
490
|
+
case "CodeFenceEnter":
|
|
491
|
+
state.preFencePending = false;
|
|
492
|
+
return action.output;
|
|
493
|
+
case "CodeFenceExit": {
|
|
494
|
+
const delimiter = finalizeCodeFence(state);
|
|
495
|
+
return delimiter ? action.output.replace("```", delimiter) : action.output;
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
function commitGfmAction(action, state, lifecycle, outputStart) {
|
|
500
|
+
switch (action._tag) {
|
|
501
|
+
case "BlockquoteEnter":
|
|
502
|
+
if (state.blockquotes.length > 0) collapseNestedBlockquoteSeparator(state.buffer);
|
|
503
|
+
state.blockquotes.push({
|
|
504
|
+
fragment: state.buffer.length,
|
|
505
|
+
listIndent: state.listIndent
|
|
506
|
+
});
|
|
507
|
+
state.bufferedBlockquoteDepth = state.blockquotes.length;
|
|
508
|
+
break;
|
|
509
|
+
case "CodeSpanEnter":
|
|
510
|
+
lifecycle.openCodeSpans.push({
|
|
511
|
+
fragment: outputStart,
|
|
512
|
+
prefix: action.output.slice(0, -1)
|
|
513
|
+
});
|
|
514
|
+
break;
|
|
515
|
+
case "CodeFenceEnter": {
|
|
516
|
+
const fenceFragment = state.buffer.findIndex((fragment, index) => index >= outputStart && fragment.includes("```"));
|
|
517
|
+
if (fenceFragment < 0) throw new Error("code fence opener missing from output");
|
|
518
|
+
startCodeFence(state, fenceFragment, action.language, state.listIndent);
|
|
519
|
+
break;
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
}
|
|
252
523
|
function getPlainTextOutput(node, eventType, state) {
|
|
253
524
|
const override = state.options?.plugins?.tagOverrides?.[node.name];
|
|
254
525
|
if (override && typeof override !== "string") {
|
|
@@ -268,7 +539,7 @@ function getPlainTextOutput(node, eventType, state) {
|
|
|
268
539
|
if (tagId === 27) {
|
|
269
540
|
const alt = node.attributes?.alt;
|
|
270
541
|
if (alt !== void 0) return alt || void 0;
|
|
271
|
-
return node.attributes?.title || resolveUrl(node.attributes?.src || "", state.options?.origin) || void 0;
|
|
542
|
+
return node.attributes?.title || resolveUrl(node.attributes?.src || "", state.options?.origin, state.options?.clean) || void 0;
|
|
272
543
|
}
|
|
273
544
|
if (tagId === 81) return "\"";
|
|
274
545
|
return;
|
|
@@ -279,17 +550,53 @@ function createMarkdownProcessor(options = {}, resolvedPlugins = [], tagOverride
|
|
|
279
550
|
const state = {
|
|
280
551
|
options,
|
|
281
552
|
buffer: [],
|
|
282
|
-
depthMap: /* @__PURE__ */ new Uint8Array(
|
|
553
|
+
depthMap: /* @__PURE__ */ new Uint8Array(112),
|
|
283
554
|
listIndent: "",
|
|
284
555
|
listIndentWidths: [],
|
|
556
|
+
blockquotes: [],
|
|
557
|
+
bufferedBlockquoteDepth: 0,
|
|
285
558
|
plainText: options.format === "text"
|
|
286
559
|
};
|
|
287
560
|
const openMarkers = [];
|
|
288
561
|
let openMarkerCount = 0;
|
|
562
|
+
const gfmLifecycle = { openCodeSpans: [] };
|
|
289
563
|
let openLinkFragment = -1;
|
|
290
564
|
let lastYieldedLength = 0;
|
|
291
565
|
let hasYieldedContent = false;
|
|
292
566
|
let preserveLeadingWhitespace = false;
|
|
567
|
+
function processTextNode(textNode, lastNode, lastChar) {
|
|
568
|
+
if (textNode.value) {
|
|
569
|
+
if (textNode.excludedFromMarkdown) return;
|
|
570
|
+
if (state.pendingInlineWhitespace) {
|
|
571
|
+
if (!textNode.value.trim()) return;
|
|
572
|
+
if (lastChar && !" \n \r".includes(lastChar) && !" \n \r".includes(textNode.value[0] || "")) textNode.value = ` ${textNode.value}`;
|
|
573
|
+
state.pendingInlineWhitespace = false;
|
|
574
|
+
}
|
|
575
|
+
if (state.plainText && state.depthMap[34] && state.buffer.length === 0) preserveLeadingWhitespace = true;
|
|
576
|
+
if (textNode.value === " " && (lastChar === " " || lastChar === "\n" || lastChar === " " || lastChar === "\r")) return;
|
|
577
|
+
if (!(state.plainText && state.depthMap[34]) && shouldAddSpacingBeforeText(lastChar, lastNode, textNode)) textNode.value = ` ${textNode.value}`;
|
|
578
|
+
if ((state.depthMap[34] || 0) > 0 && (state.depthMap[25] || 0) > 0) {
|
|
579
|
+
const indent = state.listIndent;
|
|
580
|
+
let value = textNode.value.replace(/\n(?!\n|$)/g, `\n${indent}`);
|
|
581
|
+
if (lastChar === "\n" && value[0] && value[0] !== "\n") value = indent + value;
|
|
582
|
+
textNode.value = value;
|
|
583
|
+
}
|
|
584
|
+
if (state.depthMap[34] > 0 && (state.depthMap[32] > 0 || state.depthMap[31] > 0)) textNode.value = foldPreLinesToBr(textNode.value);
|
|
585
|
+
if (!state.plainText && !state.depthMap[34] && !state.depthMap[23] && !isInsideRawHtmlBlock(state.depthMap) && mayNeedGfmTextEscape(textNode.value, state.buffer)) textNode.value = escapeGfmText(textNode.value, state.buffer, state.depthMap);
|
|
586
|
+
if (textNode.generatedMarkdown && textNode.value) textNode.value = `${textNode.generatedMarkdown.prefix}${textNode.value}${textNode.generatedMarkdown.suffix}`;
|
|
587
|
+
const wrapWidth = state.options?.wrapWidth;
|
|
588
|
+
if (wrapWidth && canWrapHere(state.depthMap)) {
|
|
589
|
+
const wrapped = wrapText(textNode.value, currentColumn(state.buffer), wrapWidth, continuationPrefix(textNode, state.listIndentWidths, state.blockquotes.length === 0));
|
|
590
|
+
state.buffer.push(wrapped);
|
|
591
|
+
state.lastContentCache = wrapped;
|
|
592
|
+
} else {
|
|
593
|
+
state.buffer.push(textNode.value);
|
|
594
|
+
state.lastContentCache = textNode.value;
|
|
595
|
+
}
|
|
596
|
+
if (openMarkerCount && hasNonWhitespace(textNode.value)) openMarkerCount = 0;
|
|
597
|
+
}
|
|
598
|
+
state.lastTextNode = textNode;
|
|
599
|
+
}
|
|
293
600
|
function processEvent(event) {
|
|
294
601
|
const { type: eventType, node } = event;
|
|
295
602
|
state.depth = node.depth;
|
|
@@ -297,80 +604,91 @@ function createMarkdownProcessor(options = {}, resolvedPlugins = [], tagOverride
|
|
|
297
604
|
const lastNode = state.lastNode;
|
|
298
605
|
state.lastNode = event.node;
|
|
299
606
|
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
|
-
}
|
|
607
|
+
if (!state.plainText && state.preFencePending && consumePendingPreChild(state, node, eventType)) return;
|
|
608
|
+
let lastBuffEntry = buff.at(-1);
|
|
609
|
+
let lastChar = lastBuffEntry?.charAt(lastBuffEntry.length - 1) || "";
|
|
315
610
|
if (node.type === 2 && eventType === 0) {
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
if (
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
611
|
+
processTextNode(node, lastNode, lastChar);
|
|
612
|
+
return;
|
|
613
|
+
}
|
|
614
|
+
if (node.type !== 1) return;
|
|
615
|
+
const element = node;
|
|
616
|
+
const handler = node.tagHandler;
|
|
617
|
+
if (eventType === 0 && handler === breakHandler && !element.pluginOutput?.length) {
|
|
618
|
+
const inPre = state.depthMap[34] !== 0;
|
|
619
|
+
let breakOutput = state.plainText ? "\n" : renderBreak(element, state);
|
|
620
|
+
if (state.plainText && !inPre && lastChar === "\n") {
|
|
621
|
+
if ((lastBuffEntry.length > 1 ? lastBuffEntry.charAt(lastBuffEntry.length - 2) : buff.length > 1 ? buff[buff.length - 2]?.at(-1) : void 0) === "\n") breakOutput = void 0;
|
|
622
|
+
}
|
|
623
|
+
if (state.pendingInlineWhitespace) state.pendingInlineWhitespace = false;
|
|
624
|
+
const lastFragment = state.lastContentCache;
|
|
625
|
+
const lastTextNode = state.lastTextNode;
|
|
626
|
+
if (lastFragment && lastTextNode?.containsWhitespace && typeof lastTextNode.value === "string") {
|
|
627
|
+
let parent = element.parent;
|
|
628
|
+
let parentInPre = false;
|
|
629
|
+
while (parent) {
|
|
630
|
+
if (parent.tagId === 34) {
|
|
631
|
+
parentInPre = true;
|
|
632
|
+
break;
|
|
633
|
+
}
|
|
634
|
+
parent = parent.parent;
|
|
333
635
|
}
|
|
334
|
-
if (
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
state.
|
|
340
|
-
} else {
|
|
341
|
-
state.buffer.push(textNode.value);
|
|
342
|
-
state.lastContentCache = textNode.value;
|
|
636
|
+
if (!parentInPre || element.parent?.tagId === 34) {
|
|
637
|
+
if (breakOutput?.endsWith("\n") && !parentInPre) {
|
|
638
|
+
const trimmed = trimAsciiWhitespaceEnd(lastFragment);
|
|
639
|
+
if (trimmed.length !== lastFragment.length && buff.at(-1) === lastFragment) buff[buff.length - 1] = trimmed;
|
|
640
|
+
}
|
|
641
|
+
state.lastTextNode = void 0;
|
|
343
642
|
}
|
|
344
|
-
if (openMarkerCount && hasNonWhitespace(textNode.value)) openMarkerCount = 0;
|
|
345
643
|
}
|
|
346
|
-
|
|
644
|
+
if (breakOutput) {
|
|
645
|
+
buff.push(breakOutput);
|
|
646
|
+
state.lastContentCache = breakOutput;
|
|
647
|
+
if (openMarkerCount && hasNonWhitespace(breakOutput)) openMarkerCount = 0;
|
|
648
|
+
}
|
|
347
649
|
return;
|
|
348
650
|
}
|
|
349
|
-
|
|
651
|
+
let secondLastChar;
|
|
652
|
+
if (lastBuffEntry && lastBuffEntry.length > 1) secondLastChar = lastBuffEntry.charAt(lastBuffEntry.length - 2);
|
|
653
|
+
else if (buff.length > 1) {
|
|
654
|
+
const prevBuff = buff[buff.length - 2];
|
|
655
|
+
if (prevBuff) secondLastChar = prevBuff.charAt(prevBuff.length - 1);
|
|
656
|
+
}
|
|
350
657
|
let output;
|
|
351
|
-
const element = node;
|
|
352
658
|
if (element.pluginOutput?.length) {
|
|
353
659
|
output = element.pluginOutput;
|
|
354
660
|
element.pluginOutput = void 0;
|
|
355
661
|
}
|
|
356
|
-
|
|
357
|
-
let lastNewLines = 0;
|
|
358
|
-
if (lastChar === "\n") lastNewLines++;
|
|
359
|
-
if (secondLastChar === "\n") lastNewLines++;
|
|
662
|
+
let lastFragment = state.lastContentCache;
|
|
360
663
|
const eventFn = eventType === 0 ? "enter" : "exit";
|
|
361
|
-
const handler = node.tagHandler;
|
|
362
664
|
const isInlineElement = handler?.isInline === true;
|
|
665
|
+
let gfmAction;
|
|
363
666
|
let handlerOutput;
|
|
364
667
|
if (!output && handler?.[eventFn]) {
|
|
365
668
|
const res = state.plainText ? getPlainTextOutput(element, eventType, state) : handler[eventFn]({
|
|
366
669
|
node: element,
|
|
367
670
|
state
|
|
368
671
|
});
|
|
369
|
-
if (res) {
|
|
370
|
-
|
|
371
|
-
|
|
672
|
+
if (typeof res === "string") {
|
|
673
|
+
if (res) {
|
|
674
|
+
output = [res];
|
|
675
|
+
handlerOutput = res;
|
|
676
|
+
}
|
|
677
|
+
} else if (res) {
|
|
678
|
+
gfmAction = res;
|
|
679
|
+
handlerOutput = consumeGfmAction(res, state, gfmLifecycle);
|
|
680
|
+
if (handlerOutput) output = [handlerOutput];
|
|
681
|
+
if (res._tag === "BlockquoteExit") {
|
|
682
|
+
lastFragment = state.lastContentCache;
|
|
683
|
+
lastBuffEntry = buff.at(-1);
|
|
684
|
+
lastChar = lastBuffEntry?.charAt(lastBuffEntry.length - 1) || "";
|
|
685
|
+
secondLastChar = lastBuffEntry && lastBuffEntry.length > 1 ? lastBuffEntry.charAt(lastBuffEntry.length - 2) : buff.length > 1 ? buff[buff.length - 2]?.at(-1) : void 0;
|
|
686
|
+
}
|
|
372
687
|
}
|
|
373
688
|
}
|
|
689
|
+
let lastNewLines = 0;
|
|
690
|
+
if (lastChar === "\n") lastNewLines++;
|
|
691
|
+
if (secondLastChar === "\n") lastNewLines++;
|
|
374
692
|
if (eventType === 1 && openMarkerCount) {
|
|
375
693
|
const markerType = handlerOutput === void 0 ? 0 : INLINE_MARKER_TYPE[element.tagId];
|
|
376
694
|
if (markerType && (element.tagId !== 23 || !state.depthMap[34]) && !handler?.literalExit) {
|
|
@@ -383,12 +701,13 @@ function createMarkdownProcessor(options = {}, resolvedPlugins = [], tagOverride
|
|
|
383
701
|
}
|
|
384
702
|
}
|
|
385
703
|
if (element.tagId === 6 && !state.depthMap[34] && output?.length === 1 && output[0] === "\n" && lastChar === "\n" && secondLastChar === "\n") output = void 0;
|
|
386
|
-
const
|
|
704
|
+
const newLineConfig = calculateNewLineConfig(node, state.depthMap, state.plainText === true);
|
|
705
|
+
const configuredNewLines = eventType === 0 && state.blockquotes.at(-1)?.fragment === state.buffer.length ? 0 : eventType === 1 && element.tagId === 13 && state.blockquotes.length > 0 ? Math.min(1, newLineConfig[eventType] || 0) : newLineConfig[eventType] || 0;
|
|
387
706
|
let effectiveLastNewLines = lastNewLines;
|
|
388
707
|
if (eventType === 1 && output) for (let i = output.length - 1; i >= 0; i--) {
|
|
389
708
|
const frag = output[i];
|
|
390
709
|
if (frag) {
|
|
391
|
-
if (frag.
|
|
710
|
+
if (frag.endsWith("```") || frag.endsWith("~~~")) effectiveLastNewLines = 0;
|
|
392
711
|
break;
|
|
393
712
|
}
|
|
394
713
|
}
|
|
@@ -396,7 +715,7 @@ function createMarkdownProcessor(options = {}, resolvedPlugins = [], tagOverride
|
|
|
396
715
|
if (state.pendingInlineWhitespace) {
|
|
397
716
|
const firstOutput = output?.[0]?.[0] || "";
|
|
398
717
|
if (eventType === 0) {
|
|
399
|
-
if (!isInlineElement || newLines > 0 || firstOutput === "\n" || firstOutput === "\r") state.pendingInlineWhitespace = false;
|
|
718
|
+
if (!isInlineElement || element.tagId === 6 || newLines > 0 || firstOutput === "\n" || firstOutput === "\r") state.pendingInlineWhitespace = false;
|
|
400
719
|
else if (firstOutput) {
|
|
401
720
|
if (lastChar && !" \n \r".includes(lastChar) && !" \n \r".includes(firstOutput)) state.buffer.push(" ");
|
|
402
721
|
state.pendingInlineWhitespace = false;
|
|
@@ -411,6 +730,7 @@ function createMarkdownProcessor(options = {}, resolvedPlugins = [], tagOverride
|
|
|
411
730
|
state.lastContentCache = fragment;
|
|
412
731
|
}
|
|
413
732
|
}
|
|
733
|
+
if (gfmAction) commitGfmAction(gfmAction, state, gfmLifecycle, state.buffer.length);
|
|
414
734
|
updateListIndent(state, element, eventType);
|
|
415
735
|
return;
|
|
416
736
|
}
|
|
@@ -437,7 +757,7 @@ function createMarkdownProcessor(options = {}, resolvedPlugins = [], tagOverride
|
|
|
437
757
|
const collapsesWhiteSpace = node.tagHandler?.collapsesInnerWhiteSpace;
|
|
438
758
|
const hasSpacing = node.tagHandler?.spacing && Array.isArray(node.tagHandler.spacing);
|
|
439
759
|
const isBlockElement = !isInlineElement && !collapsesWhiteSpace && configuredNewLines > 0;
|
|
440
|
-
if (element.tagId === 6 && output?.[0]?.
|
|
760
|
+
if (element.tagId === 6 && output?.[0]?.endsWith("\n") && !parentInPre || (!isInlineElement || eventType === 1) && !isBlockElement && !(collapsesWhiteSpace && eventType === 0) && !(hasSpacing && eventType === 0)) {
|
|
441
761
|
const originalLength = lastFragment.length;
|
|
442
762
|
const trimmed = trimAsciiWhitespaceEnd(lastFragment);
|
|
443
763
|
if (originalLength - trimmed.length > 0) {
|
|
@@ -448,16 +768,19 @@ function createMarkdownProcessor(options = {}, resolvedPlugins = [], tagOverride
|
|
|
448
768
|
state.lastTextNode = void 0;
|
|
449
769
|
}
|
|
450
770
|
}
|
|
451
|
-
if (output?.[0]?.[0] && eventType === 0 && !node.tagHandler?.literalEnter && lastChar && needsSpacing(lastChar, output[0][0], state)) {
|
|
771
|
+
if (element.tagId !== 6 && output?.[0]?.[0] && eventType === 0 && !node.tagHandler?.literalEnter && lastChar && needsSpacing(lastChar, output[0][0], state)) {
|
|
452
772
|
state.buffer.push(" ");
|
|
453
773
|
state.lastContentCache = " ";
|
|
454
774
|
}
|
|
775
|
+
let outputStart = state.buffer.length;
|
|
455
776
|
if (output) {
|
|
777
|
+
outputStart = state.buffer.length;
|
|
456
778
|
for (const fragment of output) if (fragment) {
|
|
457
779
|
state.buffer.push(fragment);
|
|
458
780
|
state.lastContentCache = fragment;
|
|
459
781
|
}
|
|
460
782
|
}
|
|
783
|
+
if (gfmAction) commitGfmAction(gfmAction, state, gfmLifecycle, outputStart);
|
|
461
784
|
if (eventType === 0 && element.tagId === 26 && handlerOutput === "[" && buff.at(-1) === "[") openLinkFragment = buff.length - 1;
|
|
462
785
|
if (eventType === 0 && handlerOutput !== void 0 && isInlineElement && !handler?.literalEnter) {
|
|
463
786
|
const markerType = INLINE_MARKER_TYPE[element.tagId];
|
|
@@ -515,6 +838,21 @@ function createMarkdownProcessor(options = {}, resolvedPlugins = [], tagOverride
|
|
|
515
838
|
const markerPos = Math.max(lastYieldedLength, trimBufferedWhitespacePosition(currentContent, fragmentPosition(state.buffer, openFragment) - leadingTrimmed));
|
|
516
839
|
if (markerPos < stableLength) stableLength = markerPos;
|
|
517
840
|
}
|
|
841
|
+
const codeSpanHeld = gfmLifecycle.openCodeSpans.length > 0;
|
|
842
|
+
if (codeSpanHeld) {
|
|
843
|
+
const spanPos = Math.max(lastYieldedLength, trimBufferedWhitespacePosition(currentContent, fragmentPosition(state.buffer, gfmLifecycle.openCodeSpans[0].fragment) - leadingTrimmed));
|
|
844
|
+
if (spanPos < stableLength) stableLength = spanPos;
|
|
845
|
+
}
|
|
846
|
+
const codeFenceHeld = state.codeFence !== void 0;
|
|
847
|
+
if (codeFenceHeld) {
|
|
848
|
+
const fencePos = Math.max(lastYieldedLength, trimBufferedWhitespacePosition(currentContent, fragmentPosition(state.buffer, state.codeFence.fragment) - leadingTrimmed));
|
|
849
|
+
if (fencePos < stableLength) stableLength = fencePos;
|
|
850
|
+
}
|
|
851
|
+
const blockquoteHeld = state.blockquotes.length > 0;
|
|
852
|
+
if (blockquoteHeld) {
|
|
853
|
+
const blockquotePos = Math.max(lastYieldedLength, trimBufferedWhitespacePosition(currentContent, fragmentPosition(state.buffer, state.blockquotes[0].fragment) - leadingTrimmed));
|
|
854
|
+
if (blockquotePos < stableLength) stableLength = blockquotePos;
|
|
855
|
+
}
|
|
518
856
|
const linkHeld = openLinkFragment >= 0;
|
|
519
857
|
if (linkHeld) {
|
|
520
858
|
const linkPos = Math.max(lastYieldedLength, trimBufferedWhitespacePosition(currentContent, fragmentPosition(state.buffer, openLinkFragment) - leadingTrimmed));
|
|
@@ -524,7 +862,7 @@ function createMarkdownProcessor(options = {}, resolvedPlugins = [], tagOverride
|
|
|
524
862
|
const newContent = currentContent.slice(lastYieldedLength, stableLength);
|
|
525
863
|
lastYieldedLength = stableLength;
|
|
526
864
|
if (newContent) hasYieldedContent = true;
|
|
527
|
-
if (!markerHeld && !linkHeld && (!retainMutableFragments || !inPre)) {
|
|
865
|
+
if (!markerHeld && !codeSpanHeld && !codeFenceHeld && !blockquoteHeld && !linkHeld && (!retainMutableFragments || !inPre)) {
|
|
528
866
|
if (!resolvedPlugins.length && !options.wrapWidth && !state.depthMap[26]) {
|
|
529
867
|
if (retainMutableFragments && leadingTrimmed === 0) {
|
|
530
868
|
const lastFragment = state.buffer.at(-1);
|
package/dist/_chunks/parse.mjs
CHANGED
|
@@ -253,7 +253,20 @@ const HTML_ENTITIES = {
|
|
|
253
253
|
yuml: "ÿ",
|
|
254
254
|
$zeta: "ζ"
|
|
255
255
|
};
|
|
256
|
-
function
|
|
256
|
+
function equalsAsciiCaseInsensitive(value, expected) {
|
|
257
|
+
for (let index = 0; index < expected.length; index++) if ((value.charCodeAt(index) | 32) !== expected.charCodeAt(index)) return false;
|
|
258
|
+
return true;
|
|
259
|
+
}
|
|
260
|
+
function isEmptyLinkHref(href) {
|
|
261
|
+
if (href === "#") return true;
|
|
262
|
+
switch (href.indexOf(":")) {
|
|
263
|
+
case 4: return equalsAsciiCaseInsensitive(href, "data");
|
|
264
|
+
case 8: return equalsAsciiCaseInsensitive(href, "vbscript");
|
|
265
|
+
case 10: return equalsAsciiCaseInsensitive(href, "javascript");
|
|
266
|
+
default: return false;
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
function continuationPrefix(node, listIndentWidths, includeBlockquotes = true) {
|
|
257
270
|
const chain = [];
|
|
258
271
|
let current = node.parent;
|
|
259
272
|
while (current) {
|
|
@@ -264,7 +277,7 @@ function continuationPrefix(node, listIndentWidths) {
|
|
|
264
277
|
let listIndex = 0;
|
|
265
278
|
for (let i = chain.length - 1; i >= 0; i--) {
|
|
266
279
|
const tagId = chain[i].tagId;
|
|
267
|
-
if (tagId === 22) prefix += "> ";
|
|
280
|
+
if (tagId === 22 && includeBlockquotes) prefix += "> ";
|
|
268
281
|
else if (tagId === 25) {
|
|
269
282
|
prefix += " ".repeat(listIndentWidths[listIndex] ?? 2);
|
|
270
283
|
listIndex++;
|
|
@@ -281,7 +294,28 @@ function numericReplacement(codePoint) {
|
|
|
281
294
|
if (codePoint >= 128 && codePoint <= 159) return C1_REPLACEMENTS[codePoint - 128];
|
|
282
295
|
return String.fromCodePoint(codePoint);
|
|
283
296
|
}
|
|
284
|
-
function
|
|
297
|
+
function isCharacterReferenceTail(text, start) {
|
|
298
|
+
let index = start;
|
|
299
|
+
if (text.charCodeAt(index) === 35) {
|
|
300
|
+
index++;
|
|
301
|
+
const hex = text.charCodeAt(index) === 120 || text.charCodeAt(index) === 88;
|
|
302
|
+
if (hex) index++;
|
|
303
|
+
const digitStart = index;
|
|
304
|
+
while (index < text.length) {
|
|
305
|
+
const code = text.charCodeAt(index);
|
|
306
|
+
if (!(code >= 48 && code <= 57) && !(hex && (code >= 65 && code <= 70 || code >= 97 && code <= 102))) break;
|
|
307
|
+
index++;
|
|
308
|
+
}
|
|
309
|
+
return index > digitStart && text.charCodeAt(index) === 59;
|
|
310
|
+
}
|
|
311
|
+
const nameStart = index;
|
|
312
|
+
while (index < text.length && isAsciiAlphaNumeric(text.charCodeAt(index))) index++;
|
|
313
|
+
return index > nameStart && text.charCodeAt(index) === 59;
|
|
314
|
+
}
|
|
315
|
+
function decodedReference(replacement, text, next, protectDecodedEntityReferences) {
|
|
316
|
+
return replacement === "&" && protectDecodedEntityReferences && isCharacterReferenceTail(text, next) ? "\\&" : replacement;
|
|
317
|
+
}
|
|
318
|
+
function decodeHTMLEntities(text, inAttribute = false, protectDecodedEntityReferences = false) {
|
|
285
319
|
let result = "";
|
|
286
320
|
let i = 0;
|
|
287
321
|
const len = text.length;
|
|
@@ -309,7 +343,7 @@ function decodeHTMLEntities(text, inAttribute = false) {
|
|
|
309
343
|
end++;
|
|
310
344
|
}
|
|
311
345
|
if (end > digitStart) {
|
|
312
|
-
result += numericReplacement(codePoint);
|
|
346
|
+
result += decodedReference(numericReplacement(codePoint), text, text.charCodeAt(end) === 59 ? end + 1 : end, protectDecodedEntityReferences);
|
|
313
347
|
if (text.charCodeAt(end) === 59) end++;
|
|
314
348
|
i = end;
|
|
315
349
|
continue;
|
|
@@ -324,7 +358,7 @@ function decodeHTMLEntities(text, inAttribute = false) {
|
|
|
324
358
|
const direct = HTML_ENTITIES[name];
|
|
325
359
|
const replacement = typeof direct === "string" ? direct : HTML_ENTITIES[`$${name}`];
|
|
326
360
|
if (replacement !== void 0) {
|
|
327
|
-
result += replacement;
|
|
361
|
+
result += decodedReference(replacement, text, nameEnd + 1, protectDecodedEntityReferences);
|
|
328
362
|
i = nameEnd + 1;
|
|
329
363
|
continue;
|
|
330
364
|
}
|
|
@@ -336,7 +370,7 @@ function decodeHTMLEntities(text, inAttribute = false) {
|
|
|
336
370
|
if (typeof replacement === "string") {
|
|
337
371
|
const next = text.charCodeAt(legacyEnd);
|
|
338
372
|
if (replacement !== void 0 && !(inAttribute && (next === 61 || isAsciiAlphaNumeric(next)))) {
|
|
339
|
-
result += replacement;
|
|
373
|
+
result += decodedReference(replacement, text, legacyEnd, protectDecodedEntityReferences);
|
|
340
374
|
i = legacyEnd;
|
|
341
375
|
decodedLegacy = true;
|
|
342
376
|
}
|
|
@@ -360,16 +394,45 @@ function traverseUpToFirstBlockNode(node) {
|
|
|
360
394
|
}
|
|
361
395
|
return parentsToIncrement;
|
|
362
396
|
}
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
397
|
+
const TRACKING_PARAM_RE = /^(?:utm_|fbclid|gclid|mc_eid|msclkid|oly_)/;
|
|
398
|
+
const URL_SCHEME_RE = /^[\dA-Z+.-]+:/i;
|
|
399
|
+
function stripTrackingParams(url) {
|
|
400
|
+
const queryStart = url.indexOf("?");
|
|
401
|
+
const fragmentStart = url.indexOf("#");
|
|
402
|
+
if (queryStart === -1 || fragmentStart !== -1 && fragmentStart < queryStart) return url;
|
|
403
|
+
const queryEnd = fragmentStart === -1 ? url.length : fragmentStart;
|
|
404
|
+
const query = url.slice(queryStart + 1, queryEnd).split("&").filter((parameter) => !TRACKING_PARAM_RE.test(parameter)).join("&");
|
|
405
|
+
return `${url.slice(0, queryStart)}${query ? `?${query}` : ""}${url.slice(queryEnd)}`;
|
|
406
|
+
}
|
|
407
|
+
function resolveUrl(url, origin, clean) {
|
|
408
|
+
if (!url || url[0] === "#") return url;
|
|
409
|
+
let resolved = url;
|
|
410
|
+
if (url.startsWith("//")) resolved = `https:${url}`;
|
|
411
|
+
else if (origin && !URL_SCHEME_RE.test(url)) {
|
|
412
|
+
const path = url.startsWith("./") ? url.slice(2) : url;
|
|
413
|
+
while (origin.endsWith("/")) origin = origin.slice(0, -1);
|
|
414
|
+
resolved = `${origin}${path[0] === "/" ? "" : "/"}${path}`;
|
|
371
415
|
}
|
|
372
|
-
return
|
|
416
|
+
return (clean === true || !!clean && clean.urls === true) && resolved.includes("?") ? stripTrackingParams(resolved) : resolved;
|
|
417
|
+
}
|
|
418
|
+
function serializeMarkdownDestination(destination) {
|
|
419
|
+
if (!/[\t\n\f\r ()\\<>]/.test(destination)) return destination;
|
|
420
|
+
return `<${/[\\<>]/.test(destination) ? destination.replace(/[\\<>]/g, "\\$&") : destination}>`;
|
|
421
|
+
}
|
|
422
|
+
function stripsEmptyLink(state, href) {
|
|
423
|
+
const clean = state.options?.clean;
|
|
424
|
+
if (!(clean === true || typeof clean === "object" && clean.emptyLinks)) return false;
|
|
425
|
+
return isEmptyLinkHref(href);
|
|
426
|
+
}
|
|
427
|
+
function serializeMarkdownTitle(title) {
|
|
428
|
+
return /[\\"]/.test(title) ? title.replace(/[\\"]/g, "\\$&") : title;
|
|
429
|
+
}
|
|
430
|
+
function serializeMarkdownResource(destination, title) {
|
|
431
|
+
const serializedTitle = title ? ` "${serializeMarkdownTitle(title)}"` : "";
|
|
432
|
+
return `(${serializeMarkdownDestination(destination)}${serializedTitle})`;
|
|
433
|
+
}
|
|
434
|
+
function serializeImageDescription(alt) {
|
|
435
|
+
return /[\\[\]*_`~<&]/.test(alt) ? alt.replace(/[\\[\]*_`~<&]/g, "\\$&") : alt;
|
|
373
436
|
}
|
|
374
437
|
function isAutolinkUri(s) {
|
|
375
438
|
if (!(s.startsWith("http://") || s.startsWith("https://") || s.startsWith("ftp://") || s.startsWith("mailto:"))) return false;
|
|
@@ -387,6 +450,19 @@ function isInsideRawHtmlBlock(state) {
|
|
|
387
450
|
const depthMap = state.depthMap;
|
|
388
451
|
return Boolean(depthMap[2] || depthMap[3] || depthMap[100] || depthMap[101] || depthMap[99] || depthMap[98]);
|
|
389
452
|
}
|
|
453
|
+
function renderBreak(node, state) {
|
|
454
|
+
const depthMap = state.depthMap;
|
|
455
|
+
if (isInsideTableCell(state) || isInsideRawHtmlBlock(state) || depthMap[7] || depthMap[8] || depthMap[9] || depthMap[10] || depthMap[11] || depthMap[12]) return "<br>";
|
|
456
|
+
if (depthMap[34] || depthMap[23]) return "\n";
|
|
457
|
+
return `\\\n${continuationPrefix(node, state.listIndentWidths || [], !state.bufferedBlockquoteDepth)}`;
|
|
458
|
+
}
|
|
459
|
+
const breakHandler = {
|
|
460
|
+
enter: ({ node, state }) => renderBreak(node, state),
|
|
461
|
+
isSelfClosing: true,
|
|
462
|
+
spacing: NO_SPACING,
|
|
463
|
+
collapsesInnerWhiteSpace: true,
|
|
464
|
+
isInline: true
|
|
465
|
+
};
|
|
390
466
|
function getLanguageFromClass(className) {
|
|
391
467
|
if (!className) return "";
|
|
392
468
|
const langParts = className.split(" ").map((c) => c.split("language-")[1]).filter(Boolean);
|
|
@@ -420,16 +496,23 @@ const Strong = {
|
|
|
420
496
|
const Emphasis = {
|
|
421
497
|
enter: ({ state }) => {
|
|
422
498
|
if ((state.depthMap?.[17] || 0) > 1) return "";
|
|
423
|
-
return "
|
|
499
|
+
return "*";
|
|
424
500
|
},
|
|
425
501
|
exit: ({ node, state }) => {
|
|
426
502
|
if ((state.depthMap?.[17] || 0) + (node.tagId === 17 ? 1 : 0) > 1) return "";
|
|
427
|
-
return "
|
|
503
|
+
return "*";
|
|
428
504
|
},
|
|
429
505
|
collapsesInnerWhiteSpace: true,
|
|
430
506
|
spacing: NO_SPACING,
|
|
431
507
|
isInline: true
|
|
432
508
|
};
|
|
509
|
+
const Strikethrough = {
|
|
510
|
+
enter: () => "~~",
|
|
511
|
+
exit: () => "~~",
|
|
512
|
+
collapsesInnerWhiteSpace: true,
|
|
513
|
+
spacing: NO_SPACING,
|
|
514
|
+
isInline: true
|
|
515
|
+
};
|
|
433
516
|
const tagHandlers = {
|
|
434
517
|
[1]: {
|
|
435
518
|
spacing: NO_SPACING,
|
|
@@ -461,17 +544,7 @@ const tagHandlers = {
|
|
|
461
544
|
isSelfClosing: true,
|
|
462
545
|
spacing: NO_SPACING
|
|
463
546
|
},
|
|
464
|
-
[6]:
|
|
465
|
-
enter: ({ node, state }) => {
|
|
466
|
-
const depthMap = state.depthMap;
|
|
467
|
-
if (isInsideTableCell(state) || isInsideRawHtmlBlock(state) || depthMap[7] || depthMap[8] || depthMap[9] || depthMap[10] || depthMap[11] || depthMap[12]) return "<br>";
|
|
468
|
-
return `\n${continuationPrefix(node, state.listIndentWidths || [])}`;
|
|
469
|
-
},
|
|
470
|
-
isSelfClosing: true,
|
|
471
|
-
spacing: NO_SPACING,
|
|
472
|
-
collapsesInnerWhiteSpace: true,
|
|
473
|
-
isInline: true
|
|
474
|
-
},
|
|
547
|
+
[6]: breakHandler,
|
|
475
548
|
[7]: handleHeading(1),
|
|
476
549
|
[8]: handleHeading(2),
|
|
477
550
|
[9]: handleHeading(3),
|
|
@@ -486,13 +559,9 @@ const tagHandlers = {
|
|
|
486
559
|
[15]: Strong,
|
|
487
560
|
[16]: Emphasis,
|
|
488
561
|
[17]: Emphasis,
|
|
489
|
-
[18]:
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
collapsesInnerWhiteSpace: true,
|
|
493
|
-
spacing: NO_SPACING,
|
|
494
|
-
isInline: true
|
|
495
|
-
},
|
|
562
|
+
[18]: Strikethrough,
|
|
563
|
+
[110]: Strikethrough,
|
|
564
|
+
[111]: Strikethrough,
|
|
496
565
|
[19]: {
|
|
497
566
|
enter: () => "<sub>",
|
|
498
567
|
exit: () => "</sub>",
|
|
@@ -516,31 +585,25 @@ const tagHandlers = {
|
|
|
516
585
|
},
|
|
517
586
|
[22]: {
|
|
518
587
|
enter: ({ state }) => {
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
588
|
+
return {
|
|
589
|
+
_tag: "BlockquoteEnter",
|
|
590
|
+
output: (state.depthMap?.[25] || 0) > 0 ? "\n" : void 0
|
|
591
|
+
};
|
|
523
592
|
},
|
|
593
|
+
exit: () => ({ _tag: "BlockquoteExit" }),
|
|
524
594
|
spacing: BLOCKQUOTE_SPACING
|
|
525
595
|
},
|
|
526
596
|
[34]: {
|
|
527
597
|
enter: ({ node, state }) => {
|
|
528
598
|
if (isInsideTableCell(state)) return "<pre>";
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
599
|
+
return {
|
|
600
|
+
_tag: "PreEnter",
|
|
601
|
+
language: getLanguageFromClass(node.attributes?.class)
|
|
602
|
+
};
|
|
532
603
|
},
|
|
533
604
|
exit: ({ state }) => {
|
|
534
605
|
if (isInsideTableCell(state)) return "</pre>";
|
|
535
|
-
|
|
536
|
-
state.preFencePending = false;
|
|
537
|
-
state.preOwnFence = false;
|
|
538
|
-
if (!ownFence) return;
|
|
539
|
-
if ((state.depthMap?.[25] || 0) > 0) {
|
|
540
|
-
const indent = state.listIndent;
|
|
541
|
-
return `\n${indent}\`\`\`\n\n${indent}`;
|
|
542
|
-
}
|
|
543
|
-
return `\n\`\`\``;
|
|
606
|
+
return { _tag: "PreExit" };
|
|
544
607
|
}
|
|
545
608
|
},
|
|
546
609
|
[23]: {
|
|
@@ -548,17 +611,30 @@ const tagHandlers = {
|
|
|
548
611
|
if ((state.depthMap?.[34] || 0) > 0) {
|
|
549
612
|
if (isInsideTableCell(state)) return "<code>";
|
|
550
613
|
if (state.preOwnFence) return;
|
|
551
|
-
state.preFencePending = false;
|
|
552
614
|
const language = getLanguageFromClass(node.attributes?.class);
|
|
553
|
-
if ((state.depthMap?.[25] || 0) > 0) return
|
|
554
|
-
|
|
615
|
+
if ((state.depthMap?.[25] || 0) > 0) return {
|
|
616
|
+
_tag: "CodeFenceEnter",
|
|
617
|
+
language,
|
|
618
|
+
output: `\n\n${state.listIndent}\`\`\`${language}\n`
|
|
619
|
+
};
|
|
620
|
+
return {
|
|
621
|
+
_tag: "CodeFenceEnter",
|
|
622
|
+
language,
|
|
623
|
+
output: `\`\`\`${language}\n`
|
|
624
|
+
};
|
|
555
625
|
}
|
|
556
626
|
if ((state.depthMap?.[25] || 0) > 0) {
|
|
557
627
|
const lastEntry = state.buffer.at(-1);
|
|
558
628
|
const lastChar = lastEntry?.charAt(lastEntry.length - 1) || "";
|
|
559
|
-
if (lastChar && lastChar !== " " && lastChar !== "\n" && lastChar !== " " && lastChar !== "*" && lastChar !== "_" && lastChar !== "~" && lastChar !== "[" && lastChar !== ">") return
|
|
629
|
+
if (lastChar && lastChar !== " " && lastChar !== "\n" && lastChar !== " " && lastChar !== "*" && lastChar !== "_" && lastChar !== "~" && lastChar !== "[" && lastChar !== ">") return {
|
|
630
|
+
_tag: "CodeSpanEnter",
|
|
631
|
+
output: ` \``
|
|
632
|
+
};
|
|
560
633
|
}
|
|
561
|
-
return
|
|
634
|
+
return {
|
|
635
|
+
_tag: "CodeSpanEnter",
|
|
636
|
+
output: "`"
|
|
637
|
+
};
|
|
562
638
|
},
|
|
563
639
|
exit: ({ state }) => {
|
|
564
640
|
if ((state.depthMap?.[34] || 0) > 0) {
|
|
@@ -566,11 +642,17 @@ const tagHandlers = {
|
|
|
566
642
|
if (state.preOwnFence) return;
|
|
567
643
|
if ((state.depthMap?.[25] || 0) > 0) {
|
|
568
644
|
const indent = state.listIndent;
|
|
569
|
-
return
|
|
645
|
+
return {
|
|
646
|
+
_tag: "CodeFenceExit",
|
|
647
|
+
output: `\n${indent}\`\`\`\n\n${indent}`
|
|
648
|
+
};
|
|
570
649
|
}
|
|
571
|
-
return
|
|
650
|
+
return {
|
|
651
|
+
_tag: "CodeFenceExit",
|
|
652
|
+
output: `\n\`\`\``
|
|
653
|
+
};
|
|
572
654
|
}
|
|
573
|
-
return "
|
|
655
|
+
return { _tag: "CodeSpanExit" };
|
|
574
656
|
},
|
|
575
657
|
collapsesInnerWhiteSpace: true,
|
|
576
658
|
spacing: NO_SPACING,
|
|
@@ -594,12 +676,16 @@ const tagHandlers = {
|
|
|
594
676
|
spacing: LIST_ITEM_SPACING
|
|
595
677
|
},
|
|
596
678
|
[26]: {
|
|
597
|
-
enter: ({ node }) => {
|
|
598
|
-
if (node.attributes?.href)
|
|
679
|
+
enter: ({ node, state }) => {
|
|
680
|
+
if (node.attributes?.href !== void 0) {
|
|
681
|
+
if (stripsEmptyLink(state, node.attributes.href)) return;
|
|
682
|
+
return "[";
|
|
683
|
+
}
|
|
599
684
|
},
|
|
600
685
|
exit: ({ node, state }) => {
|
|
601
|
-
if (
|
|
602
|
-
|
|
686
|
+
if (node.attributes?.href === void 0) return "";
|
|
687
|
+
if (stripsEmptyLink(state, node.attributes.href)) return "";
|
|
688
|
+
const href = resolveUrl(node.attributes.href, state.options?.origin, state.options?.clean);
|
|
603
689
|
let title = node.attributes?.title;
|
|
604
690
|
if (state.lastContentCache === title) title = "";
|
|
605
691
|
if (!title && isAutolinkUri(href)) {
|
|
@@ -620,7 +706,7 @@ const tagHandlers = {
|
|
|
620
706
|
return "";
|
|
621
707
|
}
|
|
622
708
|
}
|
|
623
|
-
return
|
|
709
|
+
return `]${serializeMarkdownResource(href, title)}`;
|
|
624
710
|
},
|
|
625
711
|
collapsesInnerWhiteSpace: true,
|
|
626
712
|
spacing: NO_SPACING,
|
|
@@ -628,7 +714,9 @@ const tagHandlers = {
|
|
|
628
714
|
},
|
|
629
715
|
[27]: {
|
|
630
716
|
enter: ({ node, state }) => {
|
|
631
|
-
|
|
717
|
+
const alt = node.attributes?.alt || "";
|
|
718
|
+
const src = resolveUrl(node.attributes?.src || "", state.options?.origin, state.options?.clean);
|
|
719
|
+
return `![${serializeImageDescription(alt)}]${serializeMarkdownResource(src, node.attributes?.title)}`;
|
|
632
720
|
},
|
|
633
721
|
collapsesInnerWhiteSpace: true,
|
|
634
722
|
isSelfClosing: true,
|
|
@@ -705,15 +793,6 @@ const tagHandlers = {
|
|
|
705
793
|
spacing: NO_SPACING
|
|
706
794
|
},
|
|
707
795
|
[35]: { enter: ({ state }) => {
|
|
708
|
-
const bqDepth = state.depthMap?.[22] || 0;
|
|
709
|
-
if (bqDepth > 0) {
|
|
710
|
-
const lastEntry = state.buffer.at(-1);
|
|
711
|
-
const lastChar = lastEntry?.charAt(lastEntry.length - 1) || "";
|
|
712
|
-
if (lastChar && lastChar !== "\n" && lastChar !== " " && lastChar !== ">") {
|
|
713
|
-
const prefix = "> ".repeat(bqDepth);
|
|
714
|
-
return `\n${prefix.trimEnd()}\n${prefix}`;
|
|
715
|
-
}
|
|
716
|
-
}
|
|
717
796
|
if ((state.depthMap?.[25] || 0) > 0 && !isInsideTableCell(state)) {
|
|
718
797
|
const lastEntry = state.buffer.at(-1);
|
|
719
798
|
const lastChar = lastEntry?.charAt(lastEntry.length - 1) || "";
|
|
@@ -997,8 +1076,8 @@ const tagHandlers = {
|
|
|
997
1076
|
[104]: {},
|
|
998
1077
|
[102]: {},
|
|
999
1078
|
[106]: {
|
|
1000
|
-
enter: () => "
|
|
1001
|
-
exit: () => "
|
|
1079
|
+
enter: () => "*",
|
|
1080
|
+
exit: () => "*",
|
|
1002
1081
|
collapsesInnerWhiteSpace: true,
|
|
1003
1082
|
spacing: NO_SPACING,
|
|
1004
1083
|
isInline: true
|
|
@@ -1056,10 +1135,13 @@ const TAB_CHAR = 9;
|
|
|
1056
1135
|
const NEWLINE_CHAR = 10;
|
|
1057
1136
|
const FORM_FEED_CHAR = 12;
|
|
1058
1137
|
const CARRIAGE_RETURN_CHAR = 13;
|
|
1059
|
-
const BACKTICK_CHAR = 96;
|
|
1060
1138
|
const PIPE_CHAR = 124;
|
|
1061
1139
|
const OPEN_BRACKET_CHAR = 91;
|
|
1062
1140
|
const CLOSE_BRACKET_CHAR = 93;
|
|
1141
|
+
function shouldProtectDecodedEntityReferences(state) {
|
|
1142
|
+
const depthMap = state.depthMap;
|
|
1143
|
+
return !state.plainText && !depthMap[34] && !depthMap[23] && !depthMap[2] && !depthMap[3] && !depthMap[100] && !depthMap[101] && !depthMap[99] && !depthMap[98];
|
|
1144
|
+
}
|
|
1063
1145
|
const SCRIPT_DATA = 0;
|
|
1064
1146
|
const SCRIPT_DATA_ESCAPED = 1;
|
|
1065
1147
|
const SCRIPT_DATA_ESCAPED_DASH = 2;
|
|
@@ -1082,7 +1164,7 @@ const HEAD_CONTENT_TAGS = /* @__PURE__ */ new Set([
|
|
|
1082
1164
|
78
|
|
1083
1165
|
]);
|
|
1084
1166
|
const CLOSES_P = (() => {
|
|
1085
|
-
const t = /* @__PURE__ */ new Uint8Array(
|
|
1167
|
+
const t = /* @__PURE__ */ new Uint8Array(112);
|
|
1086
1168
|
for (const id of [
|
|
1087
1169
|
36,
|
|
1088
1170
|
35,
|
|
@@ -1318,12 +1400,12 @@ var ParsedElementNode = class {
|
|
|
1318
1400
|
}
|
|
1319
1401
|
get depthMap() {
|
|
1320
1402
|
if (this.cachedDepthMap) return this.cachedDepthMap;
|
|
1321
|
-
const depthMap = /* @__PURE__ */ new Uint8Array(
|
|
1322
|
-
if (this.tagId >= 0 && this.tagId <
|
|
1403
|
+
const depthMap = /* @__PURE__ */ new Uint8Array(112);
|
|
1404
|
+
if (this.tagId >= 0 && this.tagId < 112) depthMap[this.tagId] = 1;
|
|
1323
1405
|
let node = this.parent;
|
|
1324
1406
|
while (node) {
|
|
1325
1407
|
const tagId = node.tagId;
|
|
1326
|
-
if (tagId !== void 0 && tagId >= 0 && tagId <
|
|
1408
|
+
if (tagId !== void 0 && tagId >= 0 && tagId < 112) depthMap[tagId] = (depthMap[tagId] || 0) + 1;
|
|
1327
1409
|
node = node.parent;
|
|
1328
1410
|
}
|
|
1329
1411
|
this.cachedDepthMap = depthMap;
|
|
@@ -1441,7 +1523,7 @@ function matchesClosingTag(node, tagName, tagId, closingIsAlias) {
|
|
|
1441
1523
|
function parseHtml(html, options = {}) {
|
|
1442
1524
|
const events = [];
|
|
1443
1525
|
const state = {
|
|
1444
|
-
depthMap: /* @__PURE__ */ new Uint8Array(
|
|
1526
|
+
depthMap: /* @__PURE__ */ new Uint8Array(112),
|
|
1445
1527
|
depth: 0,
|
|
1446
1528
|
resolvedPlugins: options.resolvedPlugins || []
|
|
1447
1529
|
};
|
|
@@ -1461,7 +1543,7 @@ function parseHtmlInternal(htmlChunk, state, handleEvent) {
|
|
|
1461
1543
|
if (state.depthLimitReached) return "";
|
|
1462
1544
|
let textBuffer = "";
|
|
1463
1545
|
let runStart = 0;
|
|
1464
|
-
state.depthMap ??= /* @__PURE__ */ new Uint8Array(
|
|
1546
|
+
state.depthMap ??= /* @__PURE__ */ new Uint8Array(112);
|
|
1465
1547
|
state.depth ??= 0;
|
|
1466
1548
|
state.lastCharWasWhitespace ??= true;
|
|
1467
1549
|
state.justClosedTag ??= false;
|
|
@@ -1499,7 +1581,6 @@ function parseHtmlInternal(htmlChunk, state, handleEvent) {
|
|
|
1499
1581
|
state.lastCharWasWhitespace = false;
|
|
1500
1582
|
state.justClosedTag = false;
|
|
1501
1583
|
if (!state.plainText && currentCharCode === PIPE_CHAR && state.depthMap[28]) textBuffer += "\\|";
|
|
1502
|
-
else if (!state.plainText && currentCharCode === BACKTICK_CHAR && (state.depthMap[23] || state.depthMap[34])) textBuffer += "\\`";
|
|
1503
1584
|
else if (!state.plainText && currentCharCode === OPEN_BRACKET_CHAR && state.depthMap[26]) textBuffer += "\\[";
|
|
1504
1585
|
else if (!state.plainText && currentCharCode === CLOSE_BRACKET_CHAR && state.depthMap[26]) textBuffer += "\\]";
|
|
1505
1586
|
else if (!state.plainText && currentCharCode === GT_CHAR && state.depthMap[22]) textBuffer += "\\>";
|
|
@@ -1605,6 +1686,9 @@ function parseHtmlInternal(htmlChunk, state, handleEvent) {
|
|
|
1605
1686
|
continue;
|
|
1606
1687
|
}
|
|
1607
1688
|
if (!tagName) {
|
|
1689
|
+
state.textBufferContainsNonWhitespace = true;
|
|
1690
|
+
state.lastCharWasWhitespace = false;
|
|
1691
|
+
state.justClosedTag = false;
|
|
1608
1692
|
textBuffer += htmlChunk[i++];
|
|
1609
1693
|
continue;
|
|
1610
1694
|
}
|
|
@@ -1654,7 +1738,7 @@ function processTextBuffer(textBuffer, state, handleEvent) {
|
|
|
1654
1738
|
let rootText = textBuffer;
|
|
1655
1739
|
if (rootText.length === 0) return;
|
|
1656
1740
|
if (state.hasEncodedHtmlEntity) {
|
|
1657
|
-
rootText = decodeHTMLEntities(String(rootText));
|
|
1741
|
+
rootText = decodeHTMLEntities(String(rootText), false, shouldProtectDecodedEntityReferences(state));
|
|
1658
1742
|
state.hasEncodedHtmlEntity = false;
|
|
1659
1743
|
}
|
|
1660
1744
|
const rootTextNode = {
|
|
@@ -1686,7 +1770,7 @@ function processTextBuffer(textBuffer, state, handleEvent) {
|
|
|
1686
1770
|
if (start > 0) text = text.substring(start);
|
|
1687
1771
|
}
|
|
1688
1772
|
if (state.hasEncodedHtmlEntity) {
|
|
1689
|
-
text = decodeHTMLEntities(String(text));
|
|
1773
|
+
text = decodeHTMLEntities(String(text), false, shouldProtectDecodedEntityReferences(state));
|
|
1690
1774
|
state.hasEncodedHtmlEntity = false;
|
|
1691
1775
|
}
|
|
1692
1776
|
const textNode = {
|
|
@@ -1790,7 +1874,7 @@ function closeNode(node, state, handleEvent) {
|
|
|
1790
1874
|
}
|
|
1791
1875
|
}
|
|
1792
1876
|
const tagId = node.tagId;
|
|
1793
|
-
if (tagId !== void 0 && tagId >= 0 && tagId <
|
|
1877
|
+
if (tagId !== void 0 && tagId >= 0 && tagId < 112) state.depthMap[tagId] = Math.max(0, (state.depthMap[tagId] || 0) - 1);
|
|
1794
1878
|
state.depth--;
|
|
1795
1879
|
handleEvent({
|
|
1796
1880
|
type: 1,
|
|
@@ -1880,7 +1964,7 @@ function processOpeningTag(tagName, tagId, htmlChunk, i, state, handleEvent) {
|
|
|
1880
1964
|
const headNode = state.currentNode;
|
|
1881
1965
|
if (headNode && headNode.tagId === 1) closeNode(headNode, state, handleEvent);
|
|
1882
1966
|
}
|
|
1883
|
-
if (tagId >= 0 && tagId <
|
|
1967
|
+
if (tagId >= 0 && tagId < 112 && NEEDS_IMPLIED_END_RECOVERY[tagId] === 1) if (tagId === 65 && (state.depthMap[65] || 0) > 0) {
|
|
1884
1968
|
if (closeSelectTo(state, 65, handleEvent)) return {
|
|
1885
1969
|
complete: true,
|
|
1886
1970
|
newPosition: result.newPosition,
|
|
@@ -1923,7 +2007,7 @@ function processOpeningTag(tagName, tagId, htmlChunk, i, state, handleEvent) {
|
|
|
1923
2007
|
skip: true
|
|
1924
2008
|
};
|
|
1925
2009
|
}
|
|
1926
|
-
if (tagId >= 0 && tagId <
|
|
2010
|
+
if (tagId >= 0 && tagId < 112) state.depthMap[tagId] = (state.depthMap[tagId] || 0) + 1;
|
|
1927
2011
|
state.depth++;
|
|
1928
2012
|
i = result.newPosition;
|
|
1929
2013
|
if (state.currentNode) state.currentNode.currentWalkIndex = state.currentNode.currentWalkIndex || 0;
|
|
@@ -2083,4 +2167,4 @@ function parseAttributes(attrStr) {
|
|
|
2083
2167
|
}
|
|
2084
2168
|
return result;
|
|
2085
2169
|
}
|
|
2086
|
-
export { buildTagOverrideHandlers, continuationPrefix, finalizeParse, parseAttributes, parseHtml, parseHtmlStream, resolveUrl };
|
|
2170
|
+
export { breakHandler, buildTagOverrideHandlers, continuationPrefix, finalizeParse, isEmptyLinkHref, parseAttributes, parseHtml, parseHtmlStream, renderBreak, resolveUrl };
|
package/dist/_chunks/plugins.mjs
CHANGED
|
@@ -546,15 +546,6 @@ function groupByFormattingType(classes) {
|
|
|
546
546
|
}
|
|
547
547
|
return groups;
|
|
548
548
|
}
|
|
549
|
-
function fixRedundantDelimiters(content) {
|
|
550
|
-
content = content.replaceAll("****", "**");
|
|
551
|
-
content = content.replaceAll("~~~~", "~~");
|
|
552
|
-
if (content.includes("***") && content.split("***").length > 3) {
|
|
553
|
-
const parts = content.split("***");
|
|
554
|
-
if (parts.length >= 4) content = `${parts[0]}***${parts[1]} ${parts[2]}***${parts.slice(3).join("***")}`;
|
|
555
|
-
}
|
|
556
|
-
return content;
|
|
557
|
-
}
|
|
558
549
|
function normalizeClasses(classes) {
|
|
559
550
|
const result = [];
|
|
560
551
|
const mobileClasses = classes.filter((cls) => !hasBreakpoint(cls));
|
|
@@ -648,16 +639,11 @@ function tailwindPlugin() {
|
|
|
648
639
|
content: "",
|
|
649
640
|
skip: true
|
|
650
641
|
};
|
|
651
|
-
let content = node.value;
|
|
652
642
|
const prefix = state.options?.format === "text" ? "" : tailwindData?.prefix || "";
|
|
653
643
|
const suffix = state.options?.format === "text" ? "" : tailwindData?.suffix || "";
|
|
654
|
-
if (prefix || suffix) {
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
}
|
|
658
|
-
return {
|
|
659
|
-
content,
|
|
660
|
-
skip: false
|
|
644
|
+
if (prefix || suffix) node.generatedMarkdown = {
|
|
645
|
+
prefix,
|
|
646
|
+
suffix
|
|
661
647
|
};
|
|
662
648
|
},
|
|
663
649
|
beforeNodeProcess({ node }) {
|
package/dist/_chunks/src.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import "./const.mjs";
|
|
2
|
-
import { buildTagOverrideHandlers } from "./parse.mjs";
|
|
2
|
+
import { buildTagOverrideHandlers, isEmptyLinkHref } from "./parse.mjs";
|
|
3
3
|
import { createMarkdownProcessor } from "./markdown-processor.mjs";
|
|
4
4
|
import "./plugins.mjs";
|
|
5
5
|
import { resolvePlugins } from "./resolve-plugins.mjs";
|
|
@@ -154,7 +154,7 @@ function cleanEmptyLinks(md) {
|
|
|
154
154
|
const link = parseLink(md, i);
|
|
155
155
|
if (link) {
|
|
156
156
|
const url = link.url;
|
|
157
|
-
if (
|
|
157
|
+
if (isEmptyLinkHref(url)) {
|
|
158
158
|
result += link.text;
|
|
159
159
|
i = link.end;
|
|
160
160
|
continue;
|
package/dist/_chunks/types.d.mts
CHANGED
|
@@ -167,6 +167,11 @@ interface ElementNode extends Node {
|
|
|
167
167
|
interface TextNode extends Node {
|
|
168
168
|
/** Text content (for TEXT_NODE) */
|
|
169
169
|
value: string;
|
|
170
|
+
/** Markdown delimiters generated by a plugin around this literal text. */
|
|
171
|
+
generatedMarkdown?: {
|
|
172
|
+
prefix: string;
|
|
173
|
+
suffix: string;
|
|
174
|
+
};
|
|
170
175
|
/** Custom data added by plugins */
|
|
171
176
|
context?: PluginContext;
|
|
172
177
|
}
|
|
@@ -271,6 +276,8 @@ interface MdreamRuntimeState extends Partial<MdreamProcessingState> {
|
|
|
271
276
|
preFencePending?: boolean;
|
|
272
277
|
preFenceLang?: string;
|
|
273
278
|
preOwnFence?: boolean;
|
|
279
|
+
/** Number of default blockquotes currently buffered for line prefixing. */
|
|
280
|
+
bufferedBlockquoteDepth?: number;
|
|
274
281
|
/** Whether output should omit Markdown/HTML markup */
|
|
275
282
|
plainText?: boolean;
|
|
276
283
|
}
|
|
@@ -298,13 +305,38 @@ interface HandlerContext {
|
|
|
298
305
|
/** Runtime state */
|
|
299
306
|
state: MdreamRuntimeState;
|
|
300
307
|
}
|
|
308
|
+
/** Internal serializer lifecycle emitted only by built-in GFM handlers. */
|
|
309
|
+
type GfmAction = {
|
|
310
|
+
_tag: 'BlockquoteEnter';
|
|
311
|
+
output?: string;
|
|
312
|
+
} | {
|
|
313
|
+
_tag: 'BlockquoteExit';
|
|
314
|
+
} | {
|
|
315
|
+
_tag: 'PreEnter';
|
|
316
|
+
language: string;
|
|
317
|
+
} | {
|
|
318
|
+
_tag: 'PreExit';
|
|
319
|
+
} | {
|
|
320
|
+
_tag: 'CodeSpanEnter';
|
|
321
|
+
output: string;
|
|
322
|
+
} | {
|
|
323
|
+
_tag: 'CodeSpanExit';
|
|
324
|
+
} | {
|
|
325
|
+
_tag: 'CodeFenceEnter';
|
|
326
|
+
language: string;
|
|
327
|
+
output: string;
|
|
328
|
+
} | {
|
|
329
|
+
_tag: 'CodeFenceExit';
|
|
330
|
+
output: string;
|
|
331
|
+
};
|
|
332
|
+
type TagHandlerResult = string | GfmAction | undefined | void;
|
|
301
333
|
/**
|
|
302
334
|
* Tag handler interface for HTML elements
|
|
303
335
|
* Used by plugins to extend or customize tag handling
|
|
304
336
|
*/
|
|
305
337
|
interface TagHandler {
|
|
306
|
-
enter?: (context: HandlerContext) =>
|
|
307
|
-
exit?: (context: HandlerContext) =>
|
|
338
|
+
enter?: (context: HandlerContext) => TagHandlerResult;
|
|
339
|
+
exit?: (context: HandlerContext) => TagHandlerResult;
|
|
308
340
|
isSelfClosing?: boolean;
|
|
309
341
|
isNonNesting?: boolean;
|
|
310
342
|
collapsesInnerWhiteSpace?: boolean;
|
package/dist/index.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { htmlToMarkdown, streamHtmlToMarkdown } from "./_chunks/src.mjs";
|
|
2
1
|
import { ELEMENT_NODE, NodeEventEnter, NodeEventExit, TAG_H1, TAG_H2, TAG_H3, TAG_H4, TAG_H5, TAG_H6, TEXT_NODE } from "./_chunks/const.mjs";
|
|
2
|
+
import { htmlToMarkdown, streamHtmlToMarkdown } from "./_chunks/src.mjs";
|
|
3
3
|
import { createPlugin } from "./_chunks/plugins.mjs";
|
|
4
4
|
import { withMinimalPreset } from "./preset/minimal.mjs";
|
|
5
5
|
export { ELEMENT_NODE, NodeEventEnter, NodeEventExit, TAG_H1, TAG_H2, TAG_H3, TAG_H4, TAG_H5, TAG_H6, TEXT_NODE, createPlugin, htmlToMarkdown, streamHtmlToMarkdown, withMinimalPreset };
|
package/package.json
CHANGED