@heikeson/md-engine 0.3.1 → 0.5.0
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/README.md +76 -6
- package/dist/{chunk-4TDQECWJ.js → chunk-KC6CBSFU.js} +114 -17
- package/dist/chunk-KC6CBSFU.js.map +1 -0
- package/dist/chunk-OG47YL3H.js +68 -0
- package/dist/chunk-OG47YL3H.js.map +1 -0
- package/dist/chunk-OJQDOFGL.js +313 -0
- package/dist/chunk-OJQDOFGL.js.map +1 -0
- package/dist/{chunk-SUI5PVAV.js → chunk-USP2GQZF.js} +321 -127
- package/dist/chunk-USP2GQZF.js.map +1 -0
- package/dist/chunk-YJGGJP2V.js +68 -0
- package/dist/chunk-YJGGJP2V.js.map +1 -0
- package/dist/components/index.d.ts +6 -2
- package/dist/components/index.js +4 -1
- package/dist/editor/index.d.ts +30 -11
- package/dist/editor/index.js +193 -88
- package/dist/editor/index.js.map +1 -1
- package/dist/index.d.ts +155 -41
- package/dist/index.js +6 -3
- package/dist/index.js.map +1 -1
- package/dist/locales/en-US.d.ts +68 -0
- package/dist/locales/en-US.js +3 -0
- package/dist/locales/en-US.js.map +1 -0
- package/dist/locales/zh-CN.d.ts +68 -0
- package/dist/locales/zh-CN.js +3 -0
- package/dist/locales/zh-CN.js.map +1 -0
- package/dist/mermaid/index.d.ts +209 -0
- package/dist/mermaid/index.js +708 -0
- package/dist/mermaid/index.js.map +1 -0
- package/dist/mermaid-placeholder-DYfFG3_f.d.ts +40 -0
- package/dist/render-DbUepYgE.d.ts +247 -0
- package/dist/styles.css +1 -1
- package/package.json +29 -3
- package/styles/accordion.css +1 -1
- package/styles/callout.css +2 -2
- package/styles/cards.css +9 -1
- package/styles/editor.css +1 -1
- package/styles/files.css +12 -5
- package/styles/image.css +5 -5
- package/styles/index.css +3 -0
- package/styles/mermaid.css +269 -0
- package/styles/steps.css +1 -1
- package/styles/tokens.css +32 -14
- package/styles/type-table.css +2 -2
- package//345/257/271/346/216/245/345/245/221/347/272/246.md +180 -7
- package/dist/chunk-4TDQECWJ.js.map +0 -1
- package/dist/chunk-SUI5PVAV.js.map +0 -1
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { mdxComponents, isSafeHref } from './chunk-
|
|
1
|
+
import { mdxComponents, isSafeHref } from './chunk-KC6CBSFU.js';
|
|
2
|
+
import { useShallowStable, MdEngineProvider } from './chunk-OJQDOFGL.js';
|
|
2
3
|
import { asciiAlpha, asciiAlphanumeric, markdownLineEnding } from 'micromark-util-character';
|
|
3
4
|
import { fromMarkdown } from 'mdast-util-from-markdown';
|
|
4
5
|
import { gfm } from 'micromark-extension-gfm';
|
|
@@ -6,6 +7,7 @@ import { gfmFromMarkdown } from 'mdast-util-gfm';
|
|
|
6
7
|
import { math } from 'micromark-extension-math';
|
|
7
8
|
import { mathFromMarkdown } from 'mdast-util-math';
|
|
8
9
|
import { visit } from 'unist-util-visit';
|
|
10
|
+
import GithubSlugger from 'github-slugger';
|
|
9
11
|
import { useMemo } from 'react';
|
|
10
12
|
import { jsxs, jsx, Fragment } from 'react/jsx-runtime';
|
|
11
13
|
import { unified } from 'unified';
|
|
@@ -59,7 +61,18 @@ var COMPONENTS = {
|
|
|
59
61
|
},
|
|
60
62
|
// 通用分块容器:作者需要一层可挂 class / id 的 div 做局部样式或锚点时用它;
|
|
61
63
|
// 属性原样透传到 DOM(不套组件),内容照常按 Markdown 解析。
|
|
62
|
-
div: { name: "div", mode: "visual", attrs: ["class", "id"] }
|
|
64
|
+
div: { name: "div", mode: "visual", attrs: ["class", "id"] },
|
|
65
|
+
// 图表容器:容器体是 mermaid 源码,必须原样保留(rawBody),否则 `#`、`|`、`-->`
|
|
66
|
+
// 与行首 `-` 会被当成 Markdown 语法。mode=source:编辑器里只按源码块处理,不做结构化编辑。
|
|
67
|
+
// 渲染与交互不在本仓库主入口:主入口只出源码骨架,交互由 `/mermaid` 入口在客户端增强。
|
|
68
|
+
Mermaid: {
|
|
69
|
+
name: "Mermaid",
|
|
70
|
+
mode: "source",
|
|
71
|
+
attrs: ["title", "height"],
|
|
72
|
+
rawBody: true,
|
|
73
|
+
// `<Mermaid />` 等价于空源码(给 MERMAID_EMPTY 诊断):允许自闭合,否则会被当成未知 HTML
|
|
74
|
+
selfClosing: true
|
|
75
|
+
}
|
|
63
76
|
};
|
|
64
77
|
function getComponent(name) {
|
|
65
78
|
return COMPONENTS[name];
|
|
@@ -484,7 +497,35 @@ function exitOpenTag(token) {
|
|
|
484
497
|
node.selfClosing = parsed.selfClosing;
|
|
485
498
|
node.data = { closed: true };
|
|
486
499
|
}
|
|
500
|
+
function openTagEnd(raw) {
|
|
501
|
+
let quote = "";
|
|
502
|
+
for (let i = 0; i < raw.length; i += 1) {
|
|
503
|
+
const ch = raw[i];
|
|
504
|
+
if (quote) {
|
|
505
|
+
if (ch === quote) quote = "";
|
|
506
|
+
continue;
|
|
507
|
+
}
|
|
508
|
+
if (ch === '"' || ch === "'") quote = ch;
|
|
509
|
+
else if (ch === ">") return i;
|
|
510
|
+
}
|
|
511
|
+
return -1;
|
|
512
|
+
}
|
|
513
|
+
function sliceRawBody(raw, name, selfClosing) {
|
|
514
|
+
if (selfClosing) return "";
|
|
515
|
+
const openEnd = openTagEnd(raw);
|
|
516
|
+
if (openEnd < 0) return "";
|
|
517
|
+
const bodyStart = openEnd + 1;
|
|
518
|
+
const close = new RegExp(`</${name}\\s*>\\s*$`).exec(raw);
|
|
519
|
+
if (!close) return raw.slice(bodyStart);
|
|
520
|
+
return raw.slice(bodyStart, close.index);
|
|
521
|
+
}
|
|
487
522
|
function exitBlock(token) {
|
|
523
|
+
const node = this.stack[this.stack.length - 1];
|
|
524
|
+
if (node && COMPONENTS[node.name]?.rawBody) {
|
|
525
|
+
node.children = [];
|
|
526
|
+
const rawBody = sliceRawBody(this.sliceSerialize(token), node.name, node.selfClosing);
|
|
527
|
+
node.data = { ...node.data ?? {}, rawBody };
|
|
528
|
+
}
|
|
488
529
|
this.exit(token);
|
|
489
530
|
}
|
|
490
531
|
function serializeAttributes(attributes) {
|
|
@@ -505,6 +546,10 @@ function handleJsxBlock(node, _parent, state, info) {
|
|
|
505
546
|
const open = `<${node.name}${attrs}>`;
|
|
506
547
|
if (node.data?.degraded && typeof node.data.degradedText === "string") return node.data.degradedText;
|
|
507
548
|
if (node.selfClosing) return `<${node.name}${attrs} />`;
|
|
549
|
+
if (typeof node.data?.rawBody === "string") {
|
|
550
|
+
const closing = node.data.closed === false ? "" : `</${node.name}>`;
|
|
551
|
+
return `${open}${node.data.rawBody}${closing}`;
|
|
552
|
+
}
|
|
508
553
|
const children = node.children ?? [];
|
|
509
554
|
if (children.length === 0) {
|
|
510
555
|
const position = node.position;
|
|
@@ -548,9 +593,156 @@ function remarkJsxBlocks() {
|
|
|
548
593
|
return void 0;
|
|
549
594
|
}
|
|
550
595
|
|
|
596
|
+
// src/policy.ts
|
|
597
|
+
var MERMAID_MAX_BYTES = 20 * 1024;
|
|
598
|
+
function isJsxBlock(node) {
|
|
599
|
+
return node.type === "jsxBlock";
|
|
600
|
+
}
|
|
601
|
+
function sliceNode(source, node) {
|
|
602
|
+
const start = node.position?.start.offset;
|
|
603
|
+
const end = node.position?.end.offset;
|
|
604
|
+
if (typeof start !== "number" || typeof end !== "number") return "";
|
|
605
|
+
return source.slice(start, end);
|
|
606
|
+
}
|
|
607
|
+
function walk(parent, source, depth, maxDepth, out) {
|
|
608
|
+
const children = parent.children;
|
|
609
|
+
if (!children) return;
|
|
610
|
+
const next = [];
|
|
611
|
+
for (const child of children) {
|
|
612
|
+
if (!isJsxBlock(child)) {
|
|
613
|
+
walk(child, source, depth, maxDepth, out);
|
|
614
|
+
next.push(child);
|
|
615
|
+
continue;
|
|
616
|
+
}
|
|
617
|
+
const isContainer = Boolean(COMPONENTS[child.name]);
|
|
618
|
+
const childDepth = depth + (isContainer ? 1 : 0);
|
|
619
|
+
if (isContainer && childDepth > maxDepth) {
|
|
620
|
+
const text = sliceNode(source, child);
|
|
621
|
+
const line = child.position?.start.line ?? 1;
|
|
622
|
+
out.push({
|
|
623
|
+
code: "DEPTH_EXCEEDED",
|
|
624
|
+
level: "warn",
|
|
625
|
+
line,
|
|
626
|
+
message: `\u7B2C ${childDepth} \u5C42\u5BB9\u5668 ${child.name} \u8D85\u51FA\u4E0A\u9650 ${maxDepth}\uFF0C\u5DF2\u6309\u6E90\u7801\u663E\u793A`,
|
|
627
|
+
params: { depth: childDepth, name: child.name, max: maxDepth },
|
|
628
|
+
range: [child.position?.start.offset ?? 0, child.position?.end.offset ?? 0]
|
|
629
|
+
});
|
|
630
|
+
child.data = { ...child.data ?? {}, degraded: true, degradedText: text };
|
|
631
|
+
next.push(child);
|
|
632
|
+
continue;
|
|
633
|
+
}
|
|
634
|
+
walk(child, source, childDepth, maxDepth, out);
|
|
635
|
+
next.push(child);
|
|
636
|
+
}
|
|
637
|
+
parent.children = next;
|
|
638
|
+
}
|
|
639
|
+
function enforceMaxDepth(tree, source, maxDepth = MAX_CONTAINER_DEPTH) {
|
|
640
|
+
const diagnostics = [];
|
|
641
|
+
walk(tree, source, 0, maxDepth, diagnostics);
|
|
642
|
+
return diagnostics;
|
|
643
|
+
}
|
|
644
|
+
function hasCloseTag(block, source) {
|
|
645
|
+
const start = block.position?.start.offset ?? 0;
|
|
646
|
+
const end = block.position?.end.offset ?? 0;
|
|
647
|
+
if (end <= start) return false;
|
|
648
|
+
const body = new RegExp(`</${block.name}\\s*>`);
|
|
649
|
+
if (body.test(source.slice(start, end))) return true;
|
|
650
|
+
return new RegExp(`^\\s*</${block.name}\\s*>`).test(source.slice(end, end + 64));
|
|
651
|
+
}
|
|
652
|
+
function collectIndentAsCode(tree, source) {
|
|
653
|
+
const diagnostics = [];
|
|
654
|
+
const visit7 = (node, inside) => {
|
|
655
|
+
if (node.type === "code") {
|
|
656
|
+
const start = node.position?.start.offset;
|
|
657
|
+
const end = node.position?.end.offset;
|
|
658
|
+
if (inside && typeof start === "number" && typeof end === "number") {
|
|
659
|
+
const raw = source.slice(start, end);
|
|
660
|
+
if (!/^ {0,3}(`{3,}|~{3,})/.test(raw)) {
|
|
661
|
+
diagnostics.push({
|
|
662
|
+
code: "INDENT_AS_CODE",
|
|
663
|
+
level: "warn",
|
|
664
|
+
line: node.position?.start.line ?? 1,
|
|
665
|
+
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",
|
|
666
|
+
range: [start, end]
|
|
667
|
+
});
|
|
668
|
+
}
|
|
669
|
+
}
|
|
670
|
+
return;
|
|
671
|
+
}
|
|
672
|
+
const children = node.children;
|
|
673
|
+
if (!children) return;
|
|
674
|
+
for (const child of children) visit7(child, inside || node.type === "jsxBlock");
|
|
675
|
+
};
|
|
676
|
+
visit7(tree, false);
|
|
677
|
+
return diagnostics;
|
|
678
|
+
}
|
|
679
|
+
function collectUnclosed(tree, source) {
|
|
680
|
+
const diagnostics = [];
|
|
681
|
+
const visit7 = (node) => {
|
|
682
|
+
if (node.type === "jsxBlock") {
|
|
683
|
+
const block = node;
|
|
684
|
+
if (!block.selfClosing && !hasCloseTag(block, source)) {
|
|
685
|
+
block.data = { ...block.data ?? {}, closed: false };
|
|
686
|
+
diagnostics.push({
|
|
687
|
+
code: "UNCLOSED_TAG",
|
|
688
|
+
level: "warn",
|
|
689
|
+
line: block.position?.start.line ?? 1,
|
|
690
|
+
message: `<${block.name}> \u672A\u95ED\u5408\uFF0C\u5DF2\u6309\u5EF6\u4F38\u5230\u6587\u672B\u5904\u7406`,
|
|
691
|
+
params: { name: block.name },
|
|
692
|
+
range: [block.position?.start.offset ?? 0, block.position?.end.offset ?? 0]
|
|
693
|
+
});
|
|
694
|
+
}
|
|
695
|
+
}
|
|
696
|
+
const children = node.children;
|
|
697
|
+
if (children) for (const child of children) visit7(child);
|
|
698
|
+
};
|
|
699
|
+
visit7(tree);
|
|
700
|
+
return diagnostics;
|
|
701
|
+
}
|
|
702
|
+
function collectMermaid(tree) {
|
|
703
|
+
const diagnostics = [];
|
|
704
|
+
const visit7 = (node) => {
|
|
705
|
+
if (node.type === "jsxBlock") {
|
|
706
|
+
const block = node;
|
|
707
|
+
if (COMPONENTS[block.name]?.rawBody) {
|
|
708
|
+
const raw = block.data?.rawBody ?? "";
|
|
709
|
+
const line = block.position?.start.line ?? 1;
|
|
710
|
+
const range = [
|
|
711
|
+
block.position?.start.offset ?? 0,
|
|
712
|
+
block.position?.end.offset ?? 0
|
|
713
|
+
];
|
|
714
|
+
if (raw.trim() === "") {
|
|
715
|
+
diagnostics.push({
|
|
716
|
+
code: "MERMAID_EMPTY",
|
|
717
|
+
level: "warn",
|
|
718
|
+
line,
|
|
719
|
+
message: `<${block.name}> \u5BB9\u5668\u4F53\u4E3A\u7A7A\uFF0C\u672A\u751F\u6210\u56FE\u8868`,
|
|
720
|
+
params: { name: block.name },
|
|
721
|
+
range
|
|
722
|
+
});
|
|
723
|
+
} else if (new TextEncoder().encode(raw).length > MERMAID_MAX_BYTES) {
|
|
724
|
+
block.data = { ...block.data ?? {}, mermaidTooLarge: true };
|
|
725
|
+
diagnostics.push({
|
|
726
|
+
code: "MERMAID_TOO_LARGE",
|
|
727
|
+
level: "warn",
|
|
728
|
+
line,
|
|
729
|
+
message: `<${block.name}> \u6E90\u7801\u8D85\u8FC7 ${MERMAID_MAX_BYTES / 1024} KB\uFF0C\u5DF2\u6539\u4E3A\u663E\u793A\u6E90\u7801`,
|
|
730
|
+
params: { name: block.name, kb: MERMAID_MAX_BYTES / 1024 },
|
|
731
|
+
range
|
|
732
|
+
});
|
|
733
|
+
}
|
|
734
|
+
}
|
|
735
|
+
}
|
|
736
|
+
const children = node.children;
|
|
737
|
+
if (children) for (const child of children) visit7(child);
|
|
738
|
+
};
|
|
739
|
+
visit7(tree);
|
|
740
|
+
return diagnostics;
|
|
741
|
+
}
|
|
742
|
+
|
|
551
743
|
// src/blocks.ts
|
|
552
744
|
var CONTAINERISH = /* @__PURE__ */ new Set(["list", "listItem", "blockquote"]);
|
|
553
|
-
function
|
|
745
|
+
function isJsxBlock2(node) {
|
|
554
746
|
return node.type === "jsxBlock";
|
|
555
747
|
}
|
|
556
748
|
function rangeOf(node) {
|
|
@@ -563,15 +755,18 @@ function toBlocks(children, source, depth) {
|
|
|
563
755
|
}
|
|
564
756
|
function toBlock(node, source, depth) {
|
|
565
757
|
const [start, end] = rangeOf(node);
|
|
566
|
-
if (
|
|
758
|
+
if (isJsxBlock2(node)) {
|
|
567
759
|
if (node.data?.degraded) {
|
|
568
760
|
return { kind: "source", text: source.slice(start, end), reason: "depth-exceeded", start, end };
|
|
569
761
|
}
|
|
570
762
|
const spec = COMPONENTS[node.name];
|
|
763
|
+
if (spec?.rawBody) {
|
|
764
|
+
return { kind: "source", text: source.slice(start, end), reason: "mermaid", start, end };
|
|
765
|
+
}
|
|
571
766
|
const children = node.children ?? [];
|
|
572
767
|
const itemNames = spec?.items ? Array.isArray(spec.items) ? spec.items : [spec.items] : [];
|
|
573
768
|
if (itemNames.length > 0) {
|
|
574
|
-
const items = children.filter(
|
|
769
|
+
const items = children.filter(isJsxBlock2).filter((child) => itemNames.includes(child.name)).map((child) => {
|
|
575
770
|
const [itemStart, itemEnd] = rangeOf(child);
|
|
576
771
|
return {
|
|
577
772
|
name: child.name,
|
|
@@ -747,14 +942,14 @@ function foldChildren(parent) {
|
|
|
747
942
|
index += folded.itemOffset + 1;
|
|
748
943
|
}
|
|
749
944
|
}
|
|
750
|
-
function
|
|
945
|
+
function walk2(parent) {
|
|
751
946
|
if (parent.type === "root" || parent.type === "jsxBlock") foldChildren(parent);
|
|
752
947
|
const children = parent.children;
|
|
753
948
|
if (!children) return;
|
|
754
|
-
for (const child of children)
|
|
949
|
+
for (const child of children) walk2(child);
|
|
755
950
|
}
|
|
756
951
|
function foldInlineItems(tree) {
|
|
757
|
-
|
|
952
|
+
walk2(tree);
|
|
758
953
|
}
|
|
759
954
|
function remarkInlineItems() {
|
|
760
955
|
return (tree) => {
|
|
@@ -796,112 +991,6 @@ function promoteBlockMath(tree, source) {
|
|
|
796
991
|
};
|
|
797
992
|
});
|
|
798
993
|
}
|
|
799
|
-
|
|
800
|
-
// src/policy.ts
|
|
801
|
-
function isJsxBlock2(node) {
|
|
802
|
-
return node.type === "jsxBlock";
|
|
803
|
-
}
|
|
804
|
-
function sliceNode(source, node) {
|
|
805
|
-
const start = node.position?.start.offset;
|
|
806
|
-
const end = node.position?.end.offset;
|
|
807
|
-
if (typeof start !== "number" || typeof end !== "number") return "";
|
|
808
|
-
return source.slice(start, end);
|
|
809
|
-
}
|
|
810
|
-
function walk2(parent, source, depth, maxDepth, out) {
|
|
811
|
-
const children = parent.children;
|
|
812
|
-
if (!children) return;
|
|
813
|
-
const next = [];
|
|
814
|
-
for (const child of children) {
|
|
815
|
-
if (!isJsxBlock2(child)) {
|
|
816
|
-
walk2(child, source, depth, maxDepth, out);
|
|
817
|
-
next.push(child);
|
|
818
|
-
continue;
|
|
819
|
-
}
|
|
820
|
-
const isContainer = Boolean(COMPONENTS[child.name]);
|
|
821
|
-
const childDepth = depth + (isContainer ? 1 : 0);
|
|
822
|
-
if (isContainer && childDepth > maxDepth) {
|
|
823
|
-
const text = sliceNode(source, child);
|
|
824
|
-
const line = child.position?.start.line ?? 1;
|
|
825
|
-
out.push({
|
|
826
|
-
code: "DEPTH_EXCEEDED",
|
|
827
|
-
level: "warn",
|
|
828
|
-
line,
|
|
829
|
-
message: `\u7B2C ${childDepth} \u5C42\u5BB9\u5668 ${child.name} \u8D85\u51FA\u4E0A\u9650 ${maxDepth}\uFF0C\u5DF2\u6309\u6E90\u7801\u663E\u793A`,
|
|
830
|
-
range: [child.position?.start.offset ?? 0, child.position?.end.offset ?? 0]
|
|
831
|
-
});
|
|
832
|
-
child.data = { ...child.data ?? {}, degraded: true, degradedText: text };
|
|
833
|
-
next.push(child);
|
|
834
|
-
continue;
|
|
835
|
-
}
|
|
836
|
-
walk2(child, source, childDepth, maxDepth, out);
|
|
837
|
-
next.push(child);
|
|
838
|
-
}
|
|
839
|
-
parent.children = next;
|
|
840
|
-
}
|
|
841
|
-
function enforceMaxDepth(tree, source, maxDepth = MAX_CONTAINER_DEPTH) {
|
|
842
|
-
const diagnostics = [];
|
|
843
|
-
walk2(tree, source, 0, maxDepth, diagnostics);
|
|
844
|
-
return diagnostics;
|
|
845
|
-
}
|
|
846
|
-
function hasCloseTag(block, source) {
|
|
847
|
-
const start = block.position?.start.offset ?? 0;
|
|
848
|
-
const end = block.position?.end.offset ?? 0;
|
|
849
|
-
if (end <= start) return false;
|
|
850
|
-
const body = new RegExp(`</${block.name}\\s*>`);
|
|
851
|
-
if (body.test(source.slice(start, end))) return true;
|
|
852
|
-
return new RegExp(`^\\s*</${block.name}\\s*>`).test(source.slice(end, end + 64));
|
|
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
|
-
}
|
|
881
|
-
function collectUnclosed(tree, source) {
|
|
882
|
-
const diagnostics = [];
|
|
883
|
-
const visit7 = (node) => {
|
|
884
|
-
if (node.type === "jsxBlock") {
|
|
885
|
-
const block = node;
|
|
886
|
-
if (!block.selfClosing && !hasCloseTag(block, source)) {
|
|
887
|
-
block.data = { ...block.data ?? {}, closed: false };
|
|
888
|
-
diagnostics.push({
|
|
889
|
-
code: "UNCLOSED_TAG",
|
|
890
|
-
level: "warn",
|
|
891
|
-
line: block.position?.start.line ?? 1,
|
|
892
|
-
message: `<${block.name}> \u672A\u95ED\u5408\uFF0C\u5DF2\u6309\u5EF6\u4F38\u5230\u6587\u672B\u5904\u7406`,
|
|
893
|
-
range: [block.position?.start.offset ?? 0, block.position?.end.offset ?? 0]
|
|
894
|
-
});
|
|
895
|
-
}
|
|
896
|
-
}
|
|
897
|
-
const children = node.children;
|
|
898
|
-
if (children) for (const child of children) visit7(child);
|
|
899
|
-
};
|
|
900
|
-
visit7(tree);
|
|
901
|
-
return diagnostics;
|
|
902
|
-
}
|
|
903
|
-
|
|
904
|
-
// src/policy-tags.ts
|
|
905
994
|
var URL_ATTRS = ["href", "src"];
|
|
906
995
|
var HTML_OPEN_TAG = /^<([A-Z][A-Za-z0-9]*)/;
|
|
907
996
|
var KNOWN_NAMES = /* @__PURE__ */ new Set([...Object.keys(COMPONENTS), ...Object.keys(ITEMS)]);
|
|
@@ -913,12 +1002,13 @@ function forEachNode(node, fn) {
|
|
|
913
1002
|
}
|
|
914
1003
|
function collectUnsafeUrls(tree) {
|
|
915
1004
|
const diagnostics = [];
|
|
916
|
-
const report = (line, label, url) => {
|
|
1005
|
+
const report = (line, kind, label, url) => {
|
|
917
1006
|
diagnostics.push({
|
|
918
1007
|
code: "UNSAFE_HREF",
|
|
919
1008
|
level: "warn",
|
|
920
1009
|
line,
|
|
921
|
-
message: `${label} \u7684\u534F\u8BAE\u4E0D\u53D7\u652F\u6301\uFF0C\u5DF2\u964D\u7EA7\u4E3A\u4E0D\u53EF\u70B9\u51FB\uFF1A${url}
|
|
1010
|
+
message: `${label} \u7684\u534F\u8BAE\u4E0D\u53D7\u652F\u6301\uFF0C\u5DF2\u964D\u7EA7\u4E3A\u4E0D\u53EF\u70B9\u51FB\uFF1A${url}`,
|
|
1011
|
+
params: { kind, label, url }
|
|
922
1012
|
});
|
|
923
1013
|
};
|
|
924
1014
|
forEachNode(tree, (node) => {
|
|
@@ -928,18 +1018,72 @@ function collectUnsafeUrls(tree) {
|
|
|
928
1018
|
for (const key of URL_ATTRS) {
|
|
929
1019
|
const value = block.attributes?.[key];
|
|
930
1020
|
if (typeof value === "string" && value.length > 0 && !isSafeHref(value)) {
|
|
931
|
-
report(line, `<${block.name} ${key}>`, value);
|
|
1021
|
+
report(line, "attribute", `<${block.name} ${key}>`, value);
|
|
932
1022
|
}
|
|
933
1023
|
}
|
|
934
1024
|
return;
|
|
935
1025
|
}
|
|
936
1026
|
if (node.type === "link" || node.type === "image") {
|
|
937
1027
|
const url = node.url;
|
|
938
|
-
|
|
1028
|
+
const isLink = node.type === "link";
|
|
1029
|
+
if (!isSafeHref(url)) report(line, isLink ? "link" : "image", isLink ? "\u94FE\u63A5" : "\u56FE\u7247", url);
|
|
939
1030
|
}
|
|
940
1031
|
});
|
|
941
1032
|
return diagnostics;
|
|
942
1033
|
}
|
|
1034
|
+
function nodeText(node) {
|
|
1035
|
+
if (Array.isArray(node)) return node.map(nodeText).join("");
|
|
1036
|
+
if (node.type === "text" || node.type === "inlineCode") return node.value;
|
|
1037
|
+
const children = node.children;
|
|
1038
|
+
return children ? nodeText(children) : "";
|
|
1039
|
+
}
|
|
1040
|
+
function collectDuplicateIds(tree) {
|
|
1041
|
+
const diagnostics = [];
|
|
1042
|
+
const slugger = new GithubSlugger();
|
|
1043
|
+
const headingSlugs = /* @__PURE__ */ new Set();
|
|
1044
|
+
const collectHeadings = (node) => {
|
|
1045
|
+
if (node.type === "heading") headingSlugs.add(slugger.slug(nodeText(node)));
|
|
1046
|
+
const children = node.children;
|
|
1047
|
+
if (children) for (const child of children) collectHeadings(child);
|
|
1048
|
+
};
|
|
1049
|
+
collectHeadings(tree);
|
|
1050
|
+
const seen = /* @__PURE__ */ new Map();
|
|
1051
|
+
const checkDivs = (node) => {
|
|
1052
|
+
if (node.type === "jsxBlock") {
|
|
1053
|
+
const block = node;
|
|
1054
|
+
if (block.name === "div") {
|
|
1055
|
+
const id = block.attributes?.id;
|
|
1056
|
+
const line = block.position?.start.line ?? 1;
|
|
1057
|
+
if (typeof id === "string" && id.length > 0) {
|
|
1058
|
+
const first = seen.get(id);
|
|
1059
|
+
if (headingSlugs.has(id)) {
|
|
1060
|
+
diagnostics.push({
|
|
1061
|
+
code: "DUPLICATE_ID_ANCHOR",
|
|
1062
|
+
level: "warn",
|
|
1063
|
+
line,
|
|
1064
|
+
message: `div id="${id}" \u4E0E\u6807\u9898\u751F\u6210\u7684\u951A\u70B9\u91CD\u590D\uFF0C\u9875\u9762\u5C06\u51FA\u73B0\u4E24\u4E2A\u540C\u540D id`,
|
|
1065
|
+
params: { id }
|
|
1066
|
+
});
|
|
1067
|
+
} else if (first !== void 0) {
|
|
1068
|
+
diagnostics.push({
|
|
1069
|
+
code: "DUPLICATE_ID_DIV",
|
|
1070
|
+
level: "warn",
|
|
1071
|
+
line,
|
|
1072
|
+
message: `div id="${id}" \u4E0E\u7B2C ${first} \u884C\u7684 div \u91CD\u590D\uFF0C\u9875\u9762\u5C06\u51FA\u73B0\u4E24\u4E2A\u540C\u540D id`,
|
|
1073
|
+
params: { id, first }
|
|
1074
|
+
});
|
|
1075
|
+
} else {
|
|
1076
|
+
seen.set(id, line);
|
|
1077
|
+
}
|
|
1078
|
+
}
|
|
1079
|
+
}
|
|
1080
|
+
}
|
|
1081
|
+
const children = node.children;
|
|
1082
|
+
if (children) for (const child of children) checkDivs(child);
|
|
1083
|
+
};
|
|
1084
|
+
checkDivs(tree);
|
|
1085
|
+
return diagnostics;
|
|
1086
|
+
}
|
|
943
1087
|
function collectUnknownTags(tree) {
|
|
944
1088
|
const diagnostics = [];
|
|
945
1089
|
forEachNode(tree, (node) => {
|
|
@@ -952,7 +1096,8 @@ function collectUnknownTags(tree) {
|
|
|
952
1096
|
code: "UNKNOWN_TAG",
|
|
953
1097
|
level: "warn",
|
|
954
1098
|
line: node.position?.start.line ?? 1,
|
|
955
|
-
message: `<${name}> \u4E0D\u662F\u5DF2\u6CE8\u518C\u7684\u7EC4\u4EF6\uFF0C\u5DF2\u6309\u6587\u672C\u663E\u793A
|
|
1099
|
+
message: `<${name}> \u4E0D\u662F\u5DF2\u6CE8\u518C\u7684\u7EC4\u4EF6\uFF0C\u5DF2\u6309\u6587\u672C\u663E\u793A`,
|
|
1100
|
+
params: { name }
|
|
956
1101
|
});
|
|
957
1102
|
});
|
|
958
1103
|
return diagnostics;
|
|
@@ -975,7 +1120,11 @@ function parse(md, options) {
|
|
|
975
1120
|
...collectUnclosed(tree, source),
|
|
976
1121
|
...collectUnsafeUrls(tree),
|
|
977
1122
|
...collectUnknownTags(tree),
|
|
978
|
-
...collectIndentAsCode(tree, source)
|
|
1123
|
+
...collectIndentAsCode(tree, source),
|
|
1124
|
+
...collectDuplicateIds(tree),
|
|
1125
|
+
// 图表容器:空源码 / 超限。必须在 enforceMaxDepth 之后——
|
|
1126
|
+
// 被深度裁剪降级的图表块不再出图,也就不该再报图表自身的诊断
|
|
1127
|
+
...collectMermaid(tree)
|
|
979
1128
|
];
|
|
980
1129
|
const blocks = extractBlocks(tree, source);
|
|
981
1130
|
return { tree, blocks, diagnostics };
|
|
@@ -1149,6 +1298,11 @@ function jsxBlockToHast(state, node) {
|
|
|
1149
1298
|
for (const [key, value] of Object.entries(node.attributes ?? {})) {
|
|
1150
1299
|
properties[key] = value;
|
|
1151
1300
|
}
|
|
1301
|
+
if (typeof node.data?.rawBody === "string") {
|
|
1302
|
+
properties.raw = node.data.rawBody;
|
|
1303
|
+
properties.code = node.data.rawBody;
|
|
1304
|
+
if (node.data.mermaidTooLarge) properties.tooLarge = true;
|
|
1305
|
+
}
|
|
1152
1306
|
return {
|
|
1153
1307
|
type: "element",
|
|
1154
1308
|
tagName: node.name,
|
|
@@ -1164,10 +1318,17 @@ function MarkdownRenderer({
|
|
|
1164
1318
|
className,
|
|
1165
1319
|
config
|
|
1166
1320
|
}) {
|
|
1167
|
-
const overrides = config?.components;
|
|
1168
|
-
const extraRemark = config?.remarkPlugins;
|
|
1169
|
-
const extraRehype = config?.rehypePlugins;
|
|
1321
|
+
const overrides = useShallowStable(config?.components);
|
|
1322
|
+
const extraRemark = useShallowStable(config?.remarkPlugins);
|
|
1323
|
+
const extraRehype = useShallowStable(config?.rehypePlugins);
|
|
1170
1324
|
const sourcePositions = config?.sourcePositions;
|
|
1325
|
+
const text = useShallowStable(config?.text);
|
|
1326
|
+
const messages = useShallowStable(config?.messages);
|
|
1327
|
+
const textPatch = useMemo(() => {
|
|
1328
|
+
if (!text) return messages;
|
|
1329
|
+
if (!messages) return text;
|
|
1330
|
+
return { ...text, ...messages };
|
|
1331
|
+
}, [text, messages]);
|
|
1171
1332
|
const processor = useMemo(
|
|
1172
1333
|
() => createProcessor(extraRemark, extraRehype, sourcePositions),
|
|
1173
1334
|
[extraRemark, extraRehype, sourcePositions]
|
|
@@ -1177,6 +1338,7 @@ function MarkdownRenderer({
|
|
|
1177
1338
|
const mdast = processor.parse(source);
|
|
1178
1339
|
promoteBlockMath(mdast, source);
|
|
1179
1340
|
enforceMaxDepth(mdast, source);
|
|
1341
|
+
collectMermaid(mdast);
|
|
1180
1342
|
const hast = processor.runSync(mdast);
|
|
1181
1343
|
return toJsxRuntime(hast, {
|
|
1182
1344
|
Fragment,
|
|
@@ -1185,9 +1347,41 @@ function MarkdownRenderer({
|
|
|
1185
1347
|
components: { ...mdxComponents, ...overrides }
|
|
1186
1348
|
});
|
|
1187
1349
|
}, [content, overrides, processor]);
|
|
1188
|
-
return /* @__PURE__ */ jsx(
|
|
1350
|
+
return /* @__PURE__ */ jsx(
|
|
1351
|
+
MdEngineProvider,
|
|
1352
|
+
{
|
|
1353
|
+
locale: config?.locale,
|
|
1354
|
+
fallbackLocale: config?.fallbackLocale,
|
|
1355
|
+
messages: textPatch,
|
|
1356
|
+
children: /* @__PURE__ */ jsx("div", { className, children: rendered })
|
|
1357
|
+
}
|
|
1358
|
+
);
|
|
1359
|
+
}
|
|
1360
|
+
|
|
1361
|
+
// src/internal/diagnostics.ts
|
|
1362
|
+
var KEY_BY_CODE = {
|
|
1363
|
+
UNCLOSED_TAG: "diagUnclosedTag",
|
|
1364
|
+
DEPTH_EXCEEDED: "diagDepthExceeded",
|
|
1365
|
+
UNKNOWN_TAG: "diagUnknownTag",
|
|
1366
|
+
UNSAFE_HREF: "diagUnsafeHref",
|
|
1367
|
+
INDENT_AS_CODE: "diagIndentAsCode",
|
|
1368
|
+
MERMAID_EMPTY: "diagMermaidEmpty",
|
|
1369
|
+
MERMAID_TOO_LARGE: "diagMermaidTooLarge",
|
|
1370
|
+
DUPLICATE_ID_ANCHOR: "diagDuplicateIdAnchor",
|
|
1371
|
+
DUPLICATE_ID_DIV: "diagDuplicateIdDiv",
|
|
1372
|
+
// 旧码保留在联合类型中,仍能翻译(按锚点冲突处理)
|
|
1373
|
+
DUPLICATE_ID: "diagDuplicateIdAnchor"
|
|
1374
|
+
};
|
|
1375
|
+
function diagnosticTextKey(code) {
|
|
1376
|
+
return KEY_BY_CODE[code] ?? null;
|
|
1377
|
+
}
|
|
1378
|
+
function formatDiagnostic(t, diagnostic) {
|
|
1379
|
+
const key = diagnosticTextKey(diagnostic.code);
|
|
1380
|
+
if (!key) return diagnostic.message;
|
|
1381
|
+
const rendered = t(key, diagnostic.params);
|
|
1382
|
+
return rendered || diagnostic.message;
|
|
1189
1383
|
}
|
|
1190
1384
|
|
|
1191
|
-
export { COMPONENTS, ITEMS, MAX_CONTAINER_DEPTH, MarkdownRenderer, extractBlocks, getComponent, getItemSpec, isContainerWithItems, jsxBlocks, jsxFromMarkdown, jsxToMarkdown, listComponents, normalizeMarkdown, parse, parseOpenTag, remarkJsxBlocks };
|
|
1192
|
-
//# sourceMappingURL=chunk-
|
|
1193
|
-
//# sourceMappingURL=chunk-
|
|
1385
|
+
export { COMPONENTS, ITEMS, MAX_CONTAINER_DEPTH, MERMAID_MAX_BYTES, MarkdownRenderer, diagnosticTextKey, extractBlocks, formatDiagnostic, getComponent, getItemSpec, isContainerWithItems, jsxBlocks, jsxFromMarkdown, jsxToMarkdown, listComponents, normalizeMarkdown, parse, parseOpenTag, remarkJsxBlocks, sliceRawBody };
|
|
1386
|
+
//# sourceMappingURL=chunk-USP2GQZF.js.map
|
|
1387
|
+
//# sourceMappingURL=chunk-USP2GQZF.js.map
|