@heikeson/md-engine 0.2.1 → 0.3.1
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/LICENSE +21 -21
- package/README.md +283 -271
- package/dist/{chunk-QDDS7MPU.js → chunk-4TDQECWJ.js} +2 -2
- package/dist/chunk-4TDQECWJ.js.map +1 -0
- package/dist/{chunk-3GDOWIMO.js → chunk-SUI5PVAV.js} +362 -24
- package/dist/chunk-SUI5PVAV.js.map +1 -0
- package/dist/components/index.js +1 -1
- package/dist/editor/index.js +3 -3
- package/dist/editor/index.js.map +1 -1
- package/dist/index.d.ts +17 -4
- package/dist/index.js +3 -3
- package/dist/index.js.map +1 -1
- package/dist/styles.css +1 -0
- package/package.json +7 -3
- package/styles/accordion.css +109 -0
- package/styles/callout.css +94 -0
- package/styles/cards.css +57 -0
- package/styles/editor.css +132 -0
- package/styles/engine.css +9 -373
- package/styles/files.css +108 -0
- package/styles/image.css +64 -0
- package/styles/index.css +22 -0
- package/styles/steps.css +69 -0
- package/styles/tabs.css +55 -0
- package/styles/tokens.css +32 -0
- package/styles/type-table.css +67 -0
- package//345/257/271/346/216/245/345/245/221/347/272/246.md +388 -352
- package/dist/chunk-3GDOWIMO.js.map +0 -1
- package/dist/chunk-QDDS7MPU.js.map +0 -1
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
import { mdxComponents, isSafeHref } from './chunk-
|
|
1
|
+
import { mdxComponents, isSafeHref } from './chunk-4TDQECWJ.js';
|
|
2
2
|
import { asciiAlpha, asciiAlphanumeric, markdownLineEnding } from 'micromark-util-character';
|
|
3
3
|
import { fromMarkdown } from 'mdast-util-from-markdown';
|
|
4
4
|
import { gfm } from 'micromark-extension-gfm';
|
|
5
5
|
import { gfmFromMarkdown } from 'mdast-util-gfm';
|
|
6
6
|
import { math } from 'micromark-extension-math';
|
|
7
7
|
import { mathFromMarkdown } from 'mdast-util-math';
|
|
8
|
+
import { visit } from 'unist-util-visit';
|
|
8
9
|
import { useMemo } from 'react';
|
|
9
10
|
import { jsxs, jsx, Fragment } from 'react/jsx-runtime';
|
|
10
11
|
import { unified } from 'unified';
|
|
@@ -15,7 +16,6 @@ import remarkRehype from 'remark-rehype';
|
|
|
15
16
|
import rehypeSlug from 'rehype-slug';
|
|
16
17
|
import rehypeKatex from 'rehype-katex';
|
|
17
18
|
import { toJsxRuntime } from 'hast-util-to-jsx-runtime';
|
|
18
|
-
import { visit } from 'unist-util-visit';
|
|
19
19
|
|
|
20
20
|
// src/registry.ts
|
|
21
21
|
var MAX_CONTAINER_DEPTH = 3;
|
|
@@ -56,7 +56,10 @@ var COMPONENTS = {
|
|
|
56
56
|
attrs: ["src", "alt", "align", "width", "caption"],
|
|
57
57
|
boolAttrs: ["zoom"],
|
|
58
58
|
defaultAttrs: { align: "center" }
|
|
59
|
-
}
|
|
59
|
+
},
|
|
60
|
+
// 通用分块容器:作者需要一层可挂 class / id 的 div 做局部样式或锚点时用它;
|
|
61
|
+
// 属性原样透传到 DOM(不套组件),内容照常按 Markdown 解析。
|
|
62
|
+
div: { name: "div", mode: "visual", attrs: ["class", "id"] }
|
|
60
63
|
};
|
|
61
64
|
function getComponent(name) {
|
|
62
65
|
return COMPONENTS[name];
|
|
@@ -76,6 +79,7 @@ var SLASH = 47;
|
|
|
76
79
|
var DQUOTE = 34;
|
|
77
80
|
var SQUOTE = 39;
|
|
78
81
|
var SPACE = 32;
|
|
82
|
+
var TAB = 9;
|
|
79
83
|
function isKnownName(name) {
|
|
80
84
|
return Boolean(COMPONENTS[name]) || Boolean(ITEMS[name]);
|
|
81
85
|
}
|
|
@@ -119,6 +123,7 @@ function tokenizeJsxBlock(effects, ok, nok) {
|
|
|
119
123
|
let nestedDepth = 0;
|
|
120
124
|
const closeConstruct = { tokenize: tokenizeCloseTag, partial: true };
|
|
121
125
|
const openConstruct = { tokenize: tokenizeOpenProbe, partial: true };
|
|
126
|
+
const indentConstruct = { tokenize: tokenizeIndentTag, partial: true };
|
|
122
127
|
return start;
|
|
123
128
|
function start(code) {
|
|
124
129
|
if (code !== LT) return nok(code);
|
|
@@ -204,6 +209,21 @@ function tokenizeJsxBlock(effects, ok, nok) {
|
|
|
204
209
|
return lineStart(code);
|
|
205
210
|
}
|
|
206
211
|
function lineStart(code) {
|
|
212
|
+
return effects.check(indentConstruct, indentDetected, plainLineStart)(code);
|
|
213
|
+
}
|
|
214
|
+
function indentDetected(code) {
|
|
215
|
+
effects.enter("jsxBlockIndent");
|
|
216
|
+
return consumeIndent(code);
|
|
217
|
+
}
|
|
218
|
+
function consumeIndent(code) {
|
|
219
|
+
if (code === SPACE || code === TAB) {
|
|
220
|
+
effects.consume(code);
|
|
221
|
+
return consumeIndent;
|
|
222
|
+
}
|
|
223
|
+
effects.exit("jsxBlockIndent");
|
|
224
|
+
return plainLineStart(code);
|
|
225
|
+
}
|
|
226
|
+
function plainLineStart(code) {
|
|
207
227
|
return effects.check(closeConstruct, closeDetected, notClose)(code);
|
|
208
228
|
}
|
|
209
229
|
function closeDetected(code) {
|
|
@@ -352,6 +372,50 @@ function tokenizeJsxBlock(effects, ok, nok) {
|
|
|
352
372
|
return probeAttrs;
|
|
353
373
|
}
|
|
354
374
|
}
|
|
375
|
+
function tokenizeIndentTag(effects2, ok2, nok2) {
|
|
376
|
+
let probe = "";
|
|
377
|
+
return indentStart;
|
|
378
|
+
function indentStart(code) {
|
|
379
|
+
if (code !== SPACE && code !== TAB) return nok2(code);
|
|
380
|
+
effects2.enter("jsxBlockIndent");
|
|
381
|
+
return indentSpace(code);
|
|
382
|
+
}
|
|
383
|
+
function indentSpace(code) {
|
|
384
|
+
if (code === SPACE || code === TAB) {
|
|
385
|
+
effects2.consume(code);
|
|
386
|
+
return indentSpace;
|
|
387
|
+
}
|
|
388
|
+
if (code === LT) {
|
|
389
|
+
effects2.consume(code);
|
|
390
|
+
return indentLt;
|
|
391
|
+
}
|
|
392
|
+
return nok2(code);
|
|
393
|
+
}
|
|
394
|
+
function indentLt(code) {
|
|
395
|
+
if (code === SLASH) {
|
|
396
|
+
effects2.consume(code);
|
|
397
|
+
return indentNameStart;
|
|
398
|
+
}
|
|
399
|
+
return indentNameStart(code);
|
|
400
|
+
}
|
|
401
|
+
function indentNameStart(code) {
|
|
402
|
+
if (code !== null && asciiAlpha(code)) {
|
|
403
|
+
probe = String.fromCharCode(code);
|
|
404
|
+
effects2.consume(code);
|
|
405
|
+
return indentNameRest;
|
|
406
|
+
}
|
|
407
|
+
return nok2(code);
|
|
408
|
+
}
|
|
409
|
+
function indentNameRest(code) {
|
|
410
|
+
if (code !== null && asciiAlphanumeric(code)) {
|
|
411
|
+
probe += String.fromCharCode(code);
|
|
412
|
+
effects2.consume(code);
|
|
413
|
+
return indentNameRest;
|
|
414
|
+
}
|
|
415
|
+
effects2.exit("jsxBlockIndent");
|
|
416
|
+
return isKnownName(probe) ? ok2(code) : nok2(code);
|
|
417
|
+
}
|
|
418
|
+
}
|
|
355
419
|
function tokenizeNonLazyLine(effects2, ok2, nok2) {
|
|
356
420
|
return nonLazyStart;
|
|
357
421
|
function nonLazyStart(code) {
|
|
@@ -429,18 +493,50 @@ function serializeAttributes(attributes) {
|
|
|
429
493
|
return ` ${key}="${String(value).replace(/"/g, """)}"`;
|
|
430
494
|
}).join("");
|
|
431
495
|
}
|
|
496
|
+
function isInlineLayout(node) {
|
|
497
|
+
return node.data?.inline === true;
|
|
498
|
+
}
|
|
499
|
+
function needsBlankLine(prevLine, nextLine) {
|
|
500
|
+
if (typeof prevLine !== "number" || typeof nextLine !== "number") return true;
|
|
501
|
+
return nextLine - prevLine > 1;
|
|
502
|
+
}
|
|
432
503
|
function handleJsxBlock(node, _parent, state, info) {
|
|
433
504
|
const attrs = serializeAttributes(node.attributes ?? {});
|
|
505
|
+
const open = `<${node.name}${attrs}>`;
|
|
506
|
+
if (node.data?.degraded && typeof node.data.degradedText === "string") return node.data.degradedText;
|
|
434
507
|
if (node.selfClosing) return `<${node.name}${attrs} />`;
|
|
435
|
-
const
|
|
436
|
-
if (
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
${inner}
|
|
442
|
-
|
|
508
|
+
const children = node.children ?? [];
|
|
509
|
+
if (children.length === 0) {
|
|
510
|
+
const position = node.position;
|
|
511
|
+
const sameLine = position ? position.start.line === position.end.line : false;
|
|
512
|
+
return sameLine ? `${open}</${node.name}>` : `${open}
|
|
443
513
|
</${node.name}>`;
|
|
514
|
+
}
|
|
515
|
+
if (isInlineLayout(node)) {
|
|
516
|
+
const flat = children.flatMap((child) => child.type === "paragraph" ? child.children : [child]);
|
|
517
|
+
if (flat.length === 0) return `${open}</${node.name}>`;
|
|
518
|
+
const body2 = state.containerPhrasing(
|
|
519
|
+
{ type: "paragraph", children: flat },
|
|
520
|
+
{ ...info, before: ">", after: "<" }
|
|
521
|
+
);
|
|
522
|
+
return `${open}${body2}</${node.name}>`;
|
|
523
|
+
}
|
|
524
|
+
const tracker = state.createTracker(info);
|
|
525
|
+
let body = "";
|
|
526
|
+
let prevEnd = node.position?.start.line;
|
|
527
|
+
for (const child of children) {
|
|
528
|
+
const gap = needsBlankLine(prevEnd, child.position?.start.line) ? "\n\n" : "\n";
|
|
529
|
+
const piece = state.handle(child, node, state, {
|
|
530
|
+
before: "\n",
|
|
531
|
+
after: "\n",
|
|
532
|
+
...tracker.current()
|
|
533
|
+
});
|
|
534
|
+
body += gap + tracker.move(piece);
|
|
535
|
+
prevEnd = child.position?.end.line;
|
|
536
|
+
}
|
|
537
|
+
if (node.data?.closed === false) return `${open}${body}`;
|
|
538
|
+
const closeGap = needsBlankLine(prevEnd, node.position?.end.line) ? "\n\n" : "\n";
|
|
539
|
+
return `${open}${body}${closeGap}</${node.name}>`;
|
|
444
540
|
}
|
|
445
541
|
|
|
446
542
|
// src/syntax/jsx-remark.ts
|
|
@@ -453,6 +549,7 @@ function remarkJsxBlocks() {
|
|
|
453
549
|
}
|
|
454
550
|
|
|
455
551
|
// src/blocks.ts
|
|
552
|
+
var CONTAINERISH = /* @__PURE__ */ new Set(["list", "listItem", "blockquote"]);
|
|
456
553
|
function isJsxBlock(node) {
|
|
457
554
|
return node.type === "jsxBlock";
|
|
458
555
|
}
|
|
@@ -467,6 +564,9 @@ function toBlocks(children, source, depth) {
|
|
|
467
564
|
function toBlock(node, source, depth) {
|
|
468
565
|
const [start, end] = rangeOf(node);
|
|
469
566
|
if (isJsxBlock(node)) {
|
|
567
|
+
if (node.data?.degraded) {
|
|
568
|
+
return { kind: "source", text: source.slice(start, end), reason: "depth-exceeded", start, end };
|
|
569
|
+
}
|
|
470
570
|
const spec = COMPONENTS[node.name];
|
|
471
571
|
const children = node.children ?? [];
|
|
472
572
|
const itemNames = spec?.items ? Array.isArray(spec.items) ? spec.items : [spec.items] : [];
|
|
@@ -510,12 +610,193 @@ function toBlock(node, source, depth) {
|
|
|
510
610
|
if (node.type === "definition") {
|
|
511
611
|
return { kind: "source", text: source.slice(start, end), reason: "definition", start, end };
|
|
512
612
|
}
|
|
613
|
+
if (CONTAINERISH.has(node.type)) {
|
|
614
|
+
const children = node.children ?? [];
|
|
615
|
+
return { kind: "visual", type: node.type, start, end, blocks: toBlocks(children, source, depth) };
|
|
616
|
+
}
|
|
513
617
|
return { kind: "visual", type: node.type, start, end };
|
|
514
618
|
}
|
|
515
619
|
function extractBlocks(tree, source) {
|
|
516
620
|
return toBlocks(tree.children, source, 1);
|
|
517
621
|
}
|
|
518
622
|
|
|
623
|
+
// src/syntax/jsx-inline.ts
|
|
624
|
+
var CLOSE_TAG_RE = /^<\/([A-Za-z][\w]*)\s*>$/;
|
|
625
|
+
function itemsOf(name) {
|
|
626
|
+
const items = COMPONENTS[name]?.items;
|
|
627
|
+
if (!items) return [];
|
|
628
|
+
return typeof items === "string" ? [items] : items;
|
|
629
|
+
}
|
|
630
|
+
function isBlank(node) {
|
|
631
|
+
return node.type === "text" && !String(node.value).trim();
|
|
632
|
+
}
|
|
633
|
+
function allBlank(nodes) {
|
|
634
|
+
return nodes.every((node) => isBlank(node));
|
|
635
|
+
}
|
|
636
|
+
function spanPosition(first, last) {
|
|
637
|
+
const start = first?.position?.start;
|
|
638
|
+
const end = last?.position?.end;
|
|
639
|
+
if (!start || !end) return void 0;
|
|
640
|
+
return { start, end };
|
|
641
|
+
}
|
|
642
|
+
function makeItem(name, raw, selfClosing, children, inline, open, close) {
|
|
643
|
+
const { attributes } = parseOpenTag(raw);
|
|
644
|
+
const node = {
|
|
645
|
+
type: "jsxBlock",
|
|
646
|
+
name,
|
|
647
|
+
attributes,
|
|
648
|
+
selfClosing,
|
|
649
|
+
children
|
|
650
|
+
};
|
|
651
|
+
node.data = inline ? { closed: true, inline: true } : { closed: true };
|
|
652
|
+
const position = spanPosition(open, close);
|
|
653
|
+
if (position) node.position = position;
|
|
654
|
+
return node;
|
|
655
|
+
}
|
|
656
|
+
function toParagraph(nodes, fallback) {
|
|
657
|
+
if (nodes.length === 0 || allBlank(nodes)) return null;
|
|
658
|
+
const position = spanPosition(nodes[0], nodes[nodes.length - 1]) ?? fallback;
|
|
659
|
+
const paragraph = { type: "paragraph", children: nodes };
|
|
660
|
+
if (position) paragraph.position = position;
|
|
661
|
+
return paragraph;
|
|
662
|
+
}
|
|
663
|
+
function firstMeaningful(nodes, from) {
|
|
664
|
+
for (let index = from; index < nodes.length; index += 1) {
|
|
665
|
+
if (!isBlank(nodes[index])) return index;
|
|
666
|
+
}
|
|
667
|
+
return -1;
|
|
668
|
+
}
|
|
669
|
+
function findClose(nodes, from, name) {
|
|
670
|
+
for (let index = from; index < nodes.length; index += 1) {
|
|
671
|
+
const candidate = nodes[index];
|
|
672
|
+
if (candidate.type !== "html") continue;
|
|
673
|
+
const match = CLOSE_TAG_RE.exec(String(candidate.value).trim());
|
|
674
|
+
if (match && match[1] === name) return index;
|
|
675
|
+
}
|
|
676
|
+
return -1;
|
|
677
|
+
}
|
|
678
|
+
function tryFoldAt(children, index, allowed) {
|
|
679
|
+
const paragraph = children[index];
|
|
680
|
+
if (paragraph.type !== "paragraph") return null;
|
|
681
|
+
const nodes = paragraph.children;
|
|
682
|
+
const openIndex = firstMeaningful(nodes, 0);
|
|
683
|
+
if (openIndex === -1) return null;
|
|
684
|
+
const openNode = nodes[openIndex];
|
|
685
|
+
if (openNode.type !== "html") return null;
|
|
686
|
+
const parsed = parseOpenTag(String(openNode.value).trim());
|
|
687
|
+
if (!parsed.name || !allowed.includes(parsed.name)) return null;
|
|
688
|
+
if (parsed.selfClosing) {
|
|
689
|
+
const head = toParagraph(nodes.slice(0, openIndex), paragraph.position);
|
|
690
|
+
const tail = toParagraph(nodes.slice(openIndex + 1), paragraph.position);
|
|
691
|
+
const item = makeItem(parsed.name, String(openNode.value).trim(), true, [], true, openNode, openNode);
|
|
692
|
+
return { nodes: [...head ? [head] : [], item, ...tail ? [tail] : []], consume: 1, itemOffset: head ? 1 : 0 };
|
|
693
|
+
}
|
|
694
|
+
const inlineClose = findClose(nodes, openIndex + 1, parsed.name);
|
|
695
|
+
if (inlineClose !== -1) {
|
|
696
|
+
const inner = toParagraph(nodes.slice(openIndex + 1, inlineClose), paragraph.position);
|
|
697
|
+
const head = toParagraph(nodes.slice(0, openIndex), paragraph.position);
|
|
698
|
+
const tail = toParagraph(nodes.slice(inlineClose + 1), paragraph.position);
|
|
699
|
+
const closeNode = nodes[inlineClose];
|
|
700
|
+
const item = makeItem(parsed.name, String(openNode.value).trim(), false, inner ? [inner] : [], true, openNode, closeNode);
|
|
701
|
+
return { nodes: [...head ? [head] : [], item, ...tail ? [tail] : []], consume: 1, itemOffset: head ? 1 : 0 };
|
|
702
|
+
}
|
|
703
|
+
for (let probe = index + 1; probe < children.length; probe += 1) {
|
|
704
|
+
const sibling = children[probe];
|
|
705
|
+
if (sibling.type !== "paragraph") return null;
|
|
706
|
+
const siblingNodes = sibling.children;
|
|
707
|
+
const closeIndex = findClose(siblingNodes, 0, parsed.name);
|
|
708
|
+
const inner = [];
|
|
709
|
+
const firstBody = toParagraph(nodes.slice(openIndex + 1), paragraph.position);
|
|
710
|
+
if (firstBody) inner.push(firstBody);
|
|
711
|
+
for (let middle = index + 1; middle < probe; middle += 1) inner.push(children[middle]);
|
|
712
|
+
const head = toParagraph(nodes.slice(0, openIndex), paragraph.position);
|
|
713
|
+
if (closeIndex !== -1) {
|
|
714
|
+
const lastBody = toParagraph(siblingNodes.slice(0, closeIndex), sibling.position);
|
|
715
|
+
if (lastBody) inner.push(lastBody);
|
|
716
|
+
const tail = toParagraph(siblingNodes.slice(closeIndex + 1), sibling.position);
|
|
717
|
+
const item = makeItem(
|
|
718
|
+
parsed.name,
|
|
719
|
+
String(openNode.value).trim(),
|
|
720
|
+
false,
|
|
721
|
+
inner,
|
|
722
|
+
false,
|
|
723
|
+
openNode,
|
|
724
|
+
siblingNodes[closeIndex]
|
|
725
|
+
);
|
|
726
|
+
return {
|
|
727
|
+
nodes: [...head ? [head] : [], item, ...tail ? [tail] : []],
|
|
728
|
+
consume: probe - index + 1,
|
|
729
|
+
itemOffset: head ? 1 : 0
|
|
730
|
+
};
|
|
731
|
+
}
|
|
732
|
+
}
|
|
733
|
+
return null;
|
|
734
|
+
}
|
|
735
|
+
function foldChildren(parent) {
|
|
736
|
+
const allowed = parent.type === "jsxBlock" ? itemsOf(parent.name) : [];
|
|
737
|
+
if (allowed.length === 0) return;
|
|
738
|
+
const children = parent.children;
|
|
739
|
+
let index = 0;
|
|
740
|
+
while (index < children.length) {
|
|
741
|
+
const folded = tryFoldAt(children, index, allowed);
|
|
742
|
+
if (!folded) {
|
|
743
|
+
index += 1;
|
|
744
|
+
continue;
|
|
745
|
+
}
|
|
746
|
+
children.splice(index, folded.consume, ...folded.nodes);
|
|
747
|
+
index += folded.itemOffset + 1;
|
|
748
|
+
}
|
|
749
|
+
}
|
|
750
|
+
function walk(parent) {
|
|
751
|
+
if (parent.type === "root" || parent.type === "jsxBlock") foldChildren(parent);
|
|
752
|
+
const children = parent.children;
|
|
753
|
+
if (!children) return;
|
|
754
|
+
for (const child of children) walk(child);
|
|
755
|
+
}
|
|
756
|
+
function foldInlineItems(tree) {
|
|
757
|
+
walk(tree);
|
|
758
|
+
}
|
|
759
|
+
function remarkInlineItems() {
|
|
760
|
+
return (tree) => {
|
|
761
|
+
foldInlineItems(tree);
|
|
762
|
+
return void 0;
|
|
763
|
+
};
|
|
764
|
+
}
|
|
765
|
+
function isSingleLineBlockMath(raw) {
|
|
766
|
+
if (!raw.startsWith("$$") || !raw.endsWith("$$")) return false;
|
|
767
|
+
if (raw.length <= 4) return false;
|
|
768
|
+
return !raw.slice(2, -2).includes("$$");
|
|
769
|
+
}
|
|
770
|
+
function promoteBlockMath(tree, source) {
|
|
771
|
+
visit(tree, "paragraph", (node, index, parent) => {
|
|
772
|
+
if (typeof index !== "number" || !parent) return;
|
|
773
|
+
if (node.children.length !== 1) return;
|
|
774
|
+
const only = node.children[0];
|
|
775
|
+
if (only.type !== "inlineMath") return;
|
|
776
|
+
const start = node.position?.start.offset;
|
|
777
|
+
const end = node.position?.end.offset;
|
|
778
|
+
if (typeof start !== "number" || typeof end !== "number") return;
|
|
779
|
+
if (!isSingleLineBlockMath(source.slice(start, end).trim())) return;
|
|
780
|
+
parent.children[index] = {
|
|
781
|
+
type: "math",
|
|
782
|
+
meta: null,
|
|
783
|
+
value: only.value,
|
|
784
|
+
data: {
|
|
785
|
+
hName: "pre",
|
|
786
|
+
hChildren: [
|
|
787
|
+
{
|
|
788
|
+
type: "element",
|
|
789
|
+
tagName: "code",
|
|
790
|
+
properties: { className: ["language-math", "math-display"] },
|
|
791
|
+
children: [{ type: "text", value: only.value }]
|
|
792
|
+
}
|
|
793
|
+
]
|
|
794
|
+
},
|
|
795
|
+
position: node.position
|
|
796
|
+
};
|
|
797
|
+
});
|
|
798
|
+
}
|
|
799
|
+
|
|
519
800
|
// src/policy.ts
|
|
520
801
|
function isJsxBlock2(node) {
|
|
521
802
|
return node.type === "jsxBlock";
|
|
@@ -526,13 +807,13 @@ function sliceNode(source, node) {
|
|
|
526
807
|
if (typeof start !== "number" || typeof end !== "number") return "";
|
|
527
808
|
return source.slice(start, end);
|
|
528
809
|
}
|
|
529
|
-
function
|
|
810
|
+
function walk2(parent, source, depth, maxDepth, out) {
|
|
530
811
|
const children = parent.children;
|
|
531
812
|
if (!children) return;
|
|
532
813
|
const next = [];
|
|
533
814
|
for (const child of children) {
|
|
534
815
|
if (!isJsxBlock2(child)) {
|
|
535
|
-
|
|
816
|
+
walk2(child, source, depth, maxDepth, out);
|
|
536
817
|
next.push(child);
|
|
537
818
|
continue;
|
|
538
819
|
}
|
|
@@ -548,18 +829,18 @@ function walk(parent, source, depth, maxDepth, out) {
|
|
|
548
829
|
message: `\u7B2C ${childDepth} \u5C42\u5BB9\u5668 ${child.name} \u8D85\u51FA\u4E0A\u9650 ${maxDepth}\uFF0C\u5DF2\u6309\u6E90\u7801\u663E\u793A`,
|
|
549
830
|
range: [child.position?.start.offset ?? 0, child.position?.end.offset ?? 0]
|
|
550
831
|
});
|
|
551
|
-
|
|
552
|
-
next.push(
|
|
832
|
+
child.data = { ...child.data ?? {}, degraded: true, degradedText: text };
|
|
833
|
+
next.push(child);
|
|
553
834
|
continue;
|
|
554
835
|
}
|
|
555
|
-
|
|
836
|
+
walk2(child, source, childDepth, maxDepth, out);
|
|
556
837
|
next.push(child);
|
|
557
838
|
}
|
|
558
839
|
parent.children = next;
|
|
559
840
|
}
|
|
560
841
|
function enforceMaxDepth(tree, source, maxDepth = MAX_CONTAINER_DEPTH) {
|
|
561
842
|
const diagnostics = [];
|
|
562
|
-
|
|
843
|
+
walk2(tree, source, 0, maxDepth, diagnostics);
|
|
563
844
|
return diagnostics;
|
|
564
845
|
}
|
|
565
846
|
function hasCloseTag(block, source) {
|
|
@@ -570,9 +851,36 @@ function hasCloseTag(block, source) {
|
|
|
570
851
|
if (body.test(source.slice(start, end))) return true;
|
|
571
852
|
return new RegExp(`^\\s*</${block.name}\\s*>`).test(source.slice(end, end + 64));
|
|
572
853
|
}
|
|
854
|
+
function collectIndentAsCode(tree, source) {
|
|
855
|
+
const diagnostics = [];
|
|
856
|
+
const visit7 = (node, inside) => {
|
|
857
|
+
if (node.type === "code") {
|
|
858
|
+
const start = node.position?.start.offset;
|
|
859
|
+
const end = node.position?.end.offset;
|
|
860
|
+
if (inside && typeof start === "number" && typeof end === "number") {
|
|
861
|
+
const raw = source.slice(start, end);
|
|
862
|
+
if (!/^ {0,3}(`{3,}|~{3,})/.test(raw)) {
|
|
863
|
+
diagnostics.push({
|
|
864
|
+
code: "INDENT_AS_CODE",
|
|
865
|
+
level: "warn",
|
|
866
|
+
line: node.position?.start.line ?? 1,
|
|
867
|
+
message: "\u5BB9\u5668\u5185\u8FD9\u6BB5\u5185\u5BB9\u56E0\u7F29\u8FDB\uFF08\u22654 \u7A7A\u683C\u6216 tab\uFF09\u88AB\u5F53\u6210\u4EE3\u7801\u5757\uFF1B\u82E5\u662F\u6392\u7248\u7F29\u8FDB\uFF0C\u8BF7\u9876\u683C\u4E66\u5199",
|
|
868
|
+
range: [start, end]
|
|
869
|
+
});
|
|
870
|
+
}
|
|
871
|
+
}
|
|
872
|
+
return;
|
|
873
|
+
}
|
|
874
|
+
const children = node.children;
|
|
875
|
+
if (!children) return;
|
|
876
|
+
for (const child of children) visit7(child, inside || node.type === "jsxBlock");
|
|
877
|
+
};
|
|
878
|
+
visit7(tree, false);
|
|
879
|
+
return diagnostics;
|
|
880
|
+
}
|
|
573
881
|
function collectUnclosed(tree, source) {
|
|
574
882
|
const diagnostics = [];
|
|
575
|
-
const
|
|
883
|
+
const visit7 = (node) => {
|
|
576
884
|
if (node.type === "jsxBlock") {
|
|
577
885
|
const block = node;
|
|
578
886
|
if (!block.selfClosing && !hasCloseTag(block, source)) {
|
|
@@ -587,9 +895,9 @@ function collectUnclosed(tree, source) {
|
|
|
587
895
|
}
|
|
588
896
|
}
|
|
589
897
|
const children = node.children;
|
|
590
|
-
if (children) for (const child of children)
|
|
898
|
+
if (children) for (const child of children) visit7(child);
|
|
591
899
|
};
|
|
592
|
-
|
|
900
|
+
visit7(tree);
|
|
593
901
|
return diagnostics;
|
|
594
902
|
}
|
|
595
903
|
|
|
@@ -660,11 +968,14 @@ function parse(md, options) {
|
|
|
660
968
|
extensions: [gfm(), math(), jsxBlocks()],
|
|
661
969
|
mdastExtensions: [gfmFromMarkdown(), mathFromMarkdown(), jsxFromMarkdown()]
|
|
662
970
|
});
|
|
971
|
+
foldInlineItems(tree);
|
|
972
|
+
promoteBlockMath(tree, source);
|
|
663
973
|
const diagnostics = [
|
|
664
974
|
...enforceMaxDepth(tree, source, options?.maxDepth),
|
|
665
975
|
...collectUnclosed(tree, source),
|
|
666
976
|
...collectUnsafeUrls(tree),
|
|
667
|
-
...collectUnknownTags(tree)
|
|
977
|
+
...collectUnknownTags(tree),
|
|
978
|
+
...collectIndentAsCode(tree, source)
|
|
668
979
|
];
|
|
669
980
|
const blocks = extractBlocks(tree, source);
|
|
670
981
|
return { tree, blocks, diagnostics };
|
|
@@ -711,6 +1022,17 @@ function normalizeMarkdown(md) {
|
|
|
711
1022
|
while (out.length > 0 && out[out.length - 1] === "") out.pop();
|
|
712
1023
|
return out.join("\n");
|
|
713
1024
|
}
|
|
1025
|
+
function remarkHtmlAsText() {
|
|
1026
|
+
return (tree) => {
|
|
1027
|
+
visit(tree, "html", (node, index, parent) => {
|
|
1028
|
+
if (typeof index !== "number" || !parent) return;
|
|
1029
|
+
const replacement = { type: "text", value: node.value };
|
|
1030
|
+
if (node.position) replacement.position = node.position;
|
|
1031
|
+
parent.children[index] = replacement;
|
|
1032
|
+
});
|
|
1033
|
+
return void 0;
|
|
1034
|
+
};
|
|
1035
|
+
}
|
|
714
1036
|
var URL_PROPS = ["href", "src"];
|
|
715
1037
|
function rehypeSafeUrls() {
|
|
716
1038
|
return (tree) => {
|
|
@@ -808,6 +1130,21 @@ function rehypeSourcePositions() {
|
|
|
808
1130
|
};
|
|
809
1131
|
}
|
|
810
1132
|
function jsxBlockToHast(state, node) {
|
|
1133
|
+
if (node.data?.degraded && typeof node.data.degradedText === "string") {
|
|
1134
|
+
return {
|
|
1135
|
+
type: "element",
|
|
1136
|
+
tagName: "pre",
|
|
1137
|
+
properties: { className: ["md-degraded"] },
|
|
1138
|
+
children: [
|
|
1139
|
+
{
|
|
1140
|
+
type: "element",
|
|
1141
|
+
tagName: "code",
|
|
1142
|
+
properties: {},
|
|
1143
|
+
children: [{ type: "text", value: node.data.degradedText }]
|
|
1144
|
+
}
|
|
1145
|
+
]
|
|
1146
|
+
};
|
|
1147
|
+
}
|
|
811
1148
|
const properties = {};
|
|
812
1149
|
for (const [key, value] of Object.entries(node.attributes ?? {})) {
|
|
813
1150
|
properties[key] = value;
|
|
@@ -820,7 +1157,7 @@ function jsxBlockToHast(state, node) {
|
|
|
820
1157
|
};
|
|
821
1158
|
}
|
|
822
1159
|
function createProcessor(extraRemark, extraRehype, sourcePositions) {
|
|
823
|
-
return unified().use(remarkParse).use(remarkJsxBlocks).use(remarkGfm).use(remarkMath).use(extraRemark ?? []).use(remarkRehype, { handlers: { jsxBlock: jsxBlockToHast } }).use(rehypeSafeUrls).use(rehypeSafeAnchors).use(sourcePositions ? [rehypeSourcePositions] : []).use(rehypeCodeMeta).use(rehypeSlug).use(extraRehype ?? []).use(rehypeKatex);
|
|
1160
|
+
return unified().use(remarkParse).use(remarkJsxBlocks).use(remarkGfm).use(remarkMath).use(remarkInlineItems).use(remarkHtmlAsText).use(extraRemark ?? []).use(remarkRehype, { handlers: { jsxBlock: jsxBlockToHast } }).use(rehypeSafeUrls).use(rehypeSafeAnchors).use(sourcePositions ? [rehypeSourcePositions] : []).use(rehypeCodeMeta).use(rehypeSlug).use(extraRehype ?? []).use(rehypeKatex);
|
|
824
1161
|
}
|
|
825
1162
|
function MarkdownRenderer({
|
|
826
1163
|
content,
|
|
@@ -838,6 +1175,7 @@ function MarkdownRenderer({
|
|
|
838
1175
|
const rendered = useMemo(() => {
|
|
839
1176
|
const source = content ?? "";
|
|
840
1177
|
const mdast = processor.parse(source);
|
|
1178
|
+
promoteBlockMath(mdast, source);
|
|
841
1179
|
enforceMaxDepth(mdast, source);
|
|
842
1180
|
const hast = processor.runSync(mdast);
|
|
843
1181
|
return toJsxRuntime(hast, {
|
|
@@ -851,5 +1189,5 @@ function MarkdownRenderer({
|
|
|
851
1189
|
}
|
|
852
1190
|
|
|
853
1191
|
export { COMPONENTS, ITEMS, MAX_CONTAINER_DEPTH, MarkdownRenderer, extractBlocks, getComponent, getItemSpec, isContainerWithItems, jsxBlocks, jsxFromMarkdown, jsxToMarkdown, listComponents, normalizeMarkdown, parse, parseOpenTag, remarkJsxBlocks };
|
|
854
|
-
//# sourceMappingURL=chunk-
|
|
855
|
-
//# sourceMappingURL=chunk-
|
|
1192
|
+
//# sourceMappingURL=chunk-SUI5PVAV.js.map
|
|
1193
|
+
//# sourceMappingURL=chunk-SUI5PVAV.js.map
|