@heikeson/md-engine 0.3.1 → 0.4.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 +27 -3
- package/dist/{chunk-SUI5PVAV.js → chunk-7RXVCGFM.js} +210 -116
- package/dist/chunk-7RXVCGFM.js.map +1 -0
- package/dist/{chunk-4TDQECWJ.js → chunk-V7T7I5HE.js} +53 -4
- package/dist/chunk-V7T7I5HE.js.map +1 -0
- package/dist/components/index.d.ts +5 -1
- package/dist/components/index.js +1 -1
- package/dist/editor/index.d.ts +11 -9
- package/dist/editor/index.js +89 -46
- package/dist/editor/index.js.map +1 -1
- package/dist/index.d.ts +37 -45
- package/dist/index.js +3 -3
- package/dist/mermaid/index.d.ts +207 -0
- package/dist/mermaid/index.js +678 -0
- package/dist/mermaid/index.js.map +1 -0
- package/dist/mermaid-placeholder-A1N4puyc.d.ts +39 -0
- package/dist/render-C5-vIUuK.d.ts +44 -0
- package/dist/styles.css +1 -1
- package/package.json +15 -3
- package/styles/index.css +2 -0
- package/styles/mermaid.css +269 -0
- package/styles/tokens.css +32 -14
- package//345/257/271/346/216/245/345/245/221/347/272/246.md +160 -6
- package/dist/chunk-4TDQECWJ.js.map +0 -1
- package/dist/chunk-SUI5PVAV.js.map +0 -1
package/README.md
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
# @heikeson/md-engine
|
|
2
2
|
|
|
3
3
|
> @file README.md
|
|
4
|
-
> @description React Markdown 内容引擎:尖括号容器语法的解析、渲染、序列化、格式化与 CodeMirror 6 编辑器内核,单一 npm
|
|
4
|
+
> @description React Markdown 内容引擎:尖括号容器语法的解析、渲染、序列化、格式化与 CodeMirror 6 编辑器内核,单一 npm 包五个入口。
|
|
5
5
|
> @created 2026-10-24 19:57
|
|
6
6
|
> @updated 2026-10-27 19:10 版本口径对齐 0.3.0(样式原生 CSS + 令牌,样式目录改分片)
|
|
7
7
|
> @status v0.3.0
|
|
@@ -106,6 +106,25 @@ export default function EditorPage() {
|
|
|
106
106
|
/>
|
|
107
107
|
```
|
|
108
108
|
|
|
109
|
+
### 图表(`/mermaid` 入口)
|
|
110
|
+
|
|
111
|
+
`<Mermaid>` 默认只渲染**源码骨架**(不加载 mermaid)。要变成可缩放、可导出的图表,在**客户端组件**里合并覆写表:
|
|
112
|
+
|
|
113
|
+
```tsx
|
|
114
|
+
"use client";
|
|
115
|
+
import { MarkdownRenderer, mdxComponents } from "@heikeson/md-engine";
|
|
116
|
+
import { mermaidComponents } from "@heikeson/md-engine/mermaid";
|
|
117
|
+
import "@heikeson/md-engine/styles.css";
|
|
118
|
+
|
|
119
|
+
const components = { ...mdxComponents, ...mermaidComponents };
|
|
120
|
+
|
|
121
|
+
<MarkdownRenderer content={md} config={{ components }} />;
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
- `mermaid` 是**可选 peer**(未装则不打包、运行时降级为源码);`dompurify` 为直接依赖。
|
|
125
|
+
- 键必须是 `Mermaid`(大写开头):写成小写不报错,只会静默停留在骨架。
|
|
126
|
+
- 解析、渲染、序列化在主入口全部可用,与本入口无关;未接入时 `parse` / `roundtrip` 行为完全一致。
|
|
127
|
+
|
|
109
128
|
## 容器语法
|
|
110
129
|
|
|
111
130
|
| 容器 | 属性 | 子项 |
|
|
@@ -119,6 +138,7 @@ export default function EditorPage() {
|
|
|
119
138
|
| `TypeTable` | — | `TypeItem`:`name`、`type`、`default`、`required`(布尔) |
|
|
120
139
|
| `Image` | `src` / `alt` / `align`(默认 `center`)/ `width` / `caption` / `zoom`(布尔) | 自闭合叶子 |
|
|
121
140
|
| `div` | `class` / `id` | 正文(属性透传到 DOM,用于局部样式或锚点) |
|
|
141
|
+
| `Mermaid` | `title` / `height`(默认自动高度;给数字才限高) | **原始文本**:容器体不按 Markdown 解析,整段是图表源码 |
|
|
122
142
|
|
|
123
143
|
示例:
|
|
124
144
|
|
|
@@ -151,7 +171,8 @@ export default function EditorPage() {
|
|
|
151
171
|
- 容器**顶格**书写;子项可按层缩进(行首是已知标签时会自动归位)。**正文行**缩进 ≥4 空格(或 tab)仍按 CommonMark 判成缩进代码块,此时 `parse` 会给出 `INDENT_AS_CODE` 提示,内容不会被改写;要写代码请用围栏。
|
|
152
172
|
- 放进列表时对齐内容列(`- ` 后 2 空格、`1. ` 后 3 空格)。
|
|
153
173
|
- 容器嵌套上限 3 层(子项不计层),超限那一层整块降级为原文。
|
|
154
|
-
- 只有 `Image`、`Card`、`File` 等叶子允许 `<X />`,其余容器必须写 `</X
|
|
174
|
+
- 只有 `Image`、`Card`、`File` 等叶子允许 `<X />`,其余容器必须写 `</X>`;`<Mermaid />` 是特例:等价于空源码(产 `MERMAID_EMPTY`)。
|
|
175
|
+
- `Mermaid` 的容器体是**原始文本**:里面的 `#`、`|`、`-->`、行首 `-` 都不会被当成 Markdown 语法,保存时逐字写回。
|
|
155
176
|
|
|
156
177
|
## API
|
|
157
178
|
|
|
@@ -229,6 +250,8 @@ Markdown
|
|
|
229
250
|
| 未闭合容器 | 渲染到文末,序列化不补闭合标签 | `UNCLOSED_TAG` |
|
|
230
251
|
| 嵌套超过 3 层 | 超限那层整块降级为原文 | `DEPTH_EXCEEDED` |
|
|
231
252
|
| 非白名单协议的 `href` / `src` | `href` → `#`、`src` 移除 | `UNSAFE_HREF` |
|
|
253
|
+
| `<Mermaid>` 容器体为空 | 渲染为源码块 + 提示,不白屏 | `MERMAID_EMPTY` |
|
|
254
|
+
| `<Mermaid>` 源码 > 20 KB | 不渲染图表,原样显示源码 | `MERMAID_TOO_LARGE` |
|
|
232
255
|
|
|
233
256
|
协议白名单为 `http(s)` / `mailto` / `tel`,以及任意无 scheme 的引用(相对路径、`//host`、`#锚点`、`?查询`)。
|
|
234
257
|
|
|
@@ -256,7 +279,8 @@ src/
|
|
|
256
279
|
├─ render.tsx / serialize.ts / format.ts
|
|
257
280
|
├─ rehype-*.ts URL 闸门 / 锚点还原 / 代码元信息 / 源码位置
|
|
258
281
|
├─ syntax/ micromark 尖括号扩展(词法 → mdast → remark 注入)
|
|
259
|
-
├─ components/
|
|
282
|
+
├─ components/ 容器组件 + 子项 + 映射(含图表占位骨架)
|
|
283
|
+
├─ mermaid/ /mermaid 入口:交互视图、渲染队列、令牌映射、导出
|
|
260
284
|
├─ internal/ cn、isSafeHref
|
|
261
285
|
└─ editor/ CodeMirror 封装、文本动作、滚动同步、工具栏
|
|
262
286
|
styles/*.css 原生 CSS 分片(按组件拆分,index.css 汇总;engine.css 为旧路径转发)
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { mdxComponents, isSafeHref } from './chunk-
|
|
1
|
+
import { mdxComponents, isSafeHref } from './chunk-V7T7I5HE.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';
|
|
@@ -59,7 +59,18 @@ var COMPONENTS = {
|
|
|
59
59
|
},
|
|
60
60
|
// 通用分块容器:作者需要一层可挂 class / id 的 div 做局部样式或锚点时用它;
|
|
61
61
|
// 属性原样透传到 DOM(不套组件),内容照常按 Markdown 解析。
|
|
62
|
-
div: { name: "div", mode: "visual", attrs: ["class", "id"] }
|
|
62
|
+
div: { name: "div", mode: "visual", attrs: ["class", "id"] },
|
|
63
|
+
// 图表容器:容器体是 mermaid 源码,必须原样保留(rawBody),否则 `#`、`|`、`-->`
|
|
64
|
+
// 与行首 `-` 会被当成 Markdown 语法。mode=source:编辑器里只按源码块处理,不做结构化编辑。
|
|
65
|
+
// 渲染与交互不在本仓库主入口:主入口只出源码骨架,交互由 `/mermaid` 入口在客户端增强。
|
|
66
|
+
Mermaid: {
|
|
67
|
+
name: "Mermaid",
|
|
68
|
+
mode: "source",
|
|
69
|
+
attrs: ["title", "height"],
|
|
70
|
+
rawBody: true,
|
|
71
|
+
// `<Mermaid />` 等价于空源码(给 MERMAID_EMPTY 诊断):允许自闭合,否则会被当成未知 HTML
|
|
72
|
+
selfClosing: true
|
|
73
|
+
}
|
|
63
74
|
};
|
|
64
75
|
function getComponent(name) {
|
|
65
76
|
return COMPONENTS[name];
|
|
@@ -484,7 +495,35 @@ function exitOpenTag(token) {
|
|
|
484
495
|
node.selfClosing = parsed.selfClosing;
|
|
485
496
|
node.data = { closed: true };
|
|
486
497
|
}
|
|
498
|
+
function openTagEnd(raw) {
|
|
499
|
+
let quote = "";
|
|
500
|
+
for (let i = 0; i < raw.length; i += 1) {
|
|
501
|
+
const ch = raw[i];
|
|
502
|
+
if (quote) {
|
|
503
|
+
if (ch === quote) quote = "";
|
|
504
|
+
continue;
|
|
505
|
+
}
|
|
506
|
+
if (ch === '"' || ch === "'") quote = ch;
|
|
507
|
+
else if (ch === ">") return i;
|
|
508
|
+
}
|
|
509
|
+
return -1;
|
|
510
|
+
}
|
|
511
|
+
function sliceRawBody(raw, name, selfClosing) {
|
|
512
|
+
if (selfClosing) return "";
|
|
513
|
+
const openEnd = openTagEnd(raw);
|
|
514
|
+
if (openEnd < 0) return "";
|
|
515
|
+
const bodyStart = openEnd + 1;
|
|
516
|
+
const close = new RegExp(`</${name}\\s*>\\s*$`).exec(raw);
|
|
517
|
+
if (!close) return raw.slice(bodyStart);
|
|
518
|
+
return raw.slice(bodyStart, close.index);
|
|
519
|
+
}
|
|
487
520
|
function exitBlock(token) {
|
|
521
|
+
const node = this.stack[this.stack.length - 1];
|
|
522
|
+
if (node && COMPONENTS[node.name]?.rawBody) {
|
|
523
|
+
node.children = [];
|
|
524
|
+
const rawBody = sliceRawBody(this.sliceSerialize(token), node.name, node.selfClosing);
|
|
525
|
+
node.data = { ...node.data ?? {}, rawBody };
|
|
526
|
+
}
|
|
488
527
|
this.exit(token);
|
|
489
528
|
}
|
|
490
529
|
function serializeAttributes(attributes) {
|
|
@@ -505,6 +544,10 @@ function handleJsxBlock(node, _parent, state, info) {
|
|
|
505
544
|
const open = `<${node.name}${attrs}>`;
|
|
506
545
|
if (node.data?.degraded && typeof node.data.degradedText === "string") return node.data.degradedText;
|
|
507
546
|
if (node.selfClosing) return `<${node.name}${attrs} />`;
|
|
547
|
+
if (typeof node.data?.rawBody === "string") {
|
|
548
|
+
const closing = node.data.closed === false ? "" : `</${node.name}>`;
|
|
549
|
+
return `${open}${node.data.rawBody}${closing}`;
|
|
550
|
+
}
|
|
508
551
|
const children = node.children ?? [];
|
|
509
552
|
if (children.length === 0) {
|
|
510
553
|
const position = node.position;
|
|
@@ -548,9 +591,152 @@ function remarkJsxBlocks() {
|
|
|
548
591
|
return void 0;
|
|
549
592
|
}
|
|
550
593
|
|
|
594
|
+
// src/policy.ts
|
|
595
|
+
var MERMAID_MAX_BYTES = 20 * 1024;
|
|
596
|
+
function isJsxBlock(node) {
|
|
597
|
+
return node.type === "jsxBlock";
|
|
598
|
+
}
|
|
599
|
+
function sliceNode(source, node) {
|
|
600
|
+
const start = node.position?.start.offset;
|
|
601
|
+
const end = node.position?.end.offset;
|
|
602
|
+
if (typeof start !== "number" || typeof end !== "number") return "";
|
|
603
|
+
return source.slice(start, end);
|
|
604
|
+
}
|
|
605
|
+
function walk(parent, source, depth, maxDepth, out) {
|
|
606
|
+
const children = parent.children;
|
|
607
|
+
if (!children) return;
|
|
608
|
+
const next = [];
|
|
609
|
+
for (const child of children) {
|
|
610
|
+
if (!isJsxBlock(child)) {
|
|
611
|
+
walk(child, source, depth, maxDepth, out);
|
|
612
|
+
next.push(child);
|
|
613
|
+
continue;
|
|
614
|
+
}
|
|
615
|
+
const isContainer = Boolean(COMPONENTS[child.name]);
|
|
616
|
+
const childDepth = depth + (isContainer ? 1 : 0);
|
|
617
|
+
if (isContainer && childDepth > maxDepth) {
|
|
618
|
+
const text = sliceNode(source, child);
|
|
619
|
+
const line = child.position?.start.line ?? 1;
|
|
620
|
+
out.push({
|
|
621
|
+
code: "DEPTH_EXCEEDED",
|
|
622
|
+
level: "warn",
|
|
623
|
+
line,
|
|
624
|
+
message: `\u7B2C ${childDepth} \u5C42\u5BB9\u5668 ${child.name} \u8D85\u51FA\u4E0A\u9650 ${maxDepth}\uFF0C\u5DF2\u6309\u6E90\u7801\u663E\u793A`,
|
|
625
|
+
range: [child.position?.start.offset ?? 0, child.position?.end.offset ?? 0]
|
|
626
|
+
});
|
|
627
|
+
child.data = { ...child.data ?? {}, degraded: true, degradedText: text };
|
|
628
|
+
next.push(child);
|
|
629
|
+
continue;
|
|
630
|
+
}
|
|
631
|
+
walk(child, source, childDepth, maxDepth, out);
|
|
632
|
+
next.push(child);
|
|
633
|
+
}
|
|
634
|
+
parent.children = next;
|
|
635
|
+
}
|
|
636
|
+
function enforceMaxDepth(tree, source, maxDepth = MAX_CONTAINER_DEPTH) {
|
|
637
|
+
const diagnostics = [];
|
|
638
|
+
walk(tree, source, 0, maxDepth, diagnostics);
|
|
639
|
+
return diagnostics;
|
|
640
|
+
}
|
|
641
|
+
function hasCloseTag(block, source) {
|
|
642
|
+
const start = block.position?.start.offset ?? 0;
|
|
643
|
+
const end = block.position?.end.offset ?? 0;
|
|
644
|
+
if (end <= start) return false;
|
|
645
|
+
const body = new RegExp(`</${block.name}\\s*>`);
|
|
646
|
+
if (body.test(source.slice(start, end))) return true;
|
|
647
|
+
return new RegExp(`^\\s*</${block.name}\\s*>`).test(source.slice(end, end + 64));
|
|
648
|
+
}
|
|
649
|
+
function collectIndentAsCode(tree, source) {
|
|
650
|
+
const diagnostics = [];
|
|
651
|
+
const visit7 = (node, inside) => {
|
|
652
|
+
if (node.type === "code") {
|
|
653
|
+
const start = node.position?.start.offset;
|
|
654
|
+
const end = node.position?.end.offset;
|
|
655
|
+
if (inside && typeof start === "number" && typeof end === "number") {
|
|
656
|
+
const raw = source.slice(start, end);
|
|
657
|
+
if (!/^ {0,3}(`{3,}|~{3,})/.test(raw)) {
|
|
658
|
+
diagnostics.push({
|
|
659
|
+
code: "INDENT_AS_CODE",
|
|
660
|
+
level: "warn",
|
|
661
|
+
line: node.position?.start.line ?? 1,
|
|
662
|
+
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",
|
|
663
|
+
range: [start, end]
|
|
664
|
+
});
|
|
665
|
+
}
|
|
666
|
+
}
|
|
667
|
+
return;
|
|
668
|
+
}
|
|
669
|
+
const children = node.children;
|
|
670
|
+
if (!children) return;
|
|
671
|
+
for (const child of children) visit7(child, inside || node.type === "jsxBlock");
|
|
672
|
+
};
|
|
673
|
+
visit7(tree, false);
|
|
674
|
+
return diagnostics;
|
|
675
|
+
}
|
|
676
|
+
function collectUnclosed(tree, source) {
|
|
677
|
+
const diagnostics = [];
|
|
678
|
+
const visit7 = (node) => {
|
|
679
|
+
if (node.type === "jsxBlock") {
|
|
680
|
+
const block = node;
|
|
681
|
+
if (!block.selfClosing && !hasCloseTag(block, source)) {
|
|
682
|
+
block.data = { ...block.data ?? {}, closed: false };
|
|
683
|
+
diagnostics.push({
|
|
684
|
+
code: "UNCLOSED_TAG",
|
|
685
|
+
level: "warn",
|
|
686
|
+
line: block.position?.start.line ?? 1,
|
|
687
|
+
message: `<${block.name}> \u672A\u95ED\u5408\uFF0C\u5DF2\u6309\u5EF6\u4F38\u5230\u6587\u672B\u5904\u7406`,
|
|
688
|
+
range: [block.position?.start.offset ?? 0, block.position?.end.offset ?? 0]
|
|
689
|
+
});
|
|
690
|
+
}
|
|
691
|
+
}
|
|
692
|
+
const children = node.children;
|
|
693
|
+
if (children) for (const child of children) visit7(child);
|
|
694
|
+
};
|
|
695
|
+
visit7(tree);
|
|
696
|
+
return diagnostics;
|
|
697
|
+
}
|
|
698
|
+
function collectMermaid(tree) {
|
|
699
|
+
const diagnostics = [];
|
|
700
|
+
const visit7 = (node) => {
|
|
701
|
+
if (node.type === "jsxBlock") {
|
|
702
|
+
const block = node;
|
|
703
|
+
if (COMPONENTS[block.name]?.rawBody) {
|
|
704
|
+
const raw = block.data?.rawBody ?? "";
|
|
705
|
+
const line = block.position?.start.line ?? 1;
|
|
706
|
+
const range = [
|
|
707
|
+
block.position?.start.offset ?? 0,
|
|
708
|
+
block.position?.end.offset ?? 0
|
|
709
|
+
];
|
|
710
|
+
if (raw.trim() === "") {
|
|
711
|
+
diagnostics.push({
|
|
712
|
+
code: "MERMAID_EMPTY",
|
|
713
|
+
level: "warn",
|
|
714
|
+
line,
|
|
715
|
+
message: `<${block.name}> \u5BB9\u5668\u4F53\u4E3A\u7A7A\uFF0C\u672A\u751F\u6210\u56FE\u8868`,
|
|
716
|
+
range
|
|
717
|
+
});
|
|
718
|
+
} else if (new TextEncoder().encode(raw).length > MERMAID_MAX_BYTES) {
|
|
719
|
+
block.data = { ...block.data ?? {}, mermaidTooLarge: true };
|
|
720
|
+
diagnostics.push({
|
|
721
|
+
code: "MERMAID_TOO_LARGE",
|
|
722
|
+
level: "warn",
|
|
723
|
+
line,
|
|
724
|
+
message: `<${block.name}> \u6E90\u7801\u8D85\u8FC7 ${MERMAID_MAX_BYTES / 1024} KB\uFF0C\u5DF2\u6539\u4E3A\u663E\u793A\u6E90\u7801`,
|
|
725
|
+
range
|
|
726
|
+
});
|
|
727
|
+
}
|
|
728
|
+
}
|
|
729
|
+
}
|
|
730
|
+
const children = node.children;
|
|
731
|
+
if (children) for (const child of children) visit7(child);
|
|
732
|
+
};
|
|
733
|
+
visit7(tree);
|
|
734
|
+
return diagnostics;
|
|
735
|
+
}
|
|
736
|
+
|
|
551
737
|
// src/blocks.ts
|
|
552
738
|
var CONTAINERISH = /* @__PURE__ */ new Set(["list", "listItem", "blockquote"]);
|
|
553
|
-
function
|
|
739
|
+
function isJsxBlock2(node) {
|
|
554
740
|
return node.type === "jsxBlock";
|
|
555
741
|
}
|
|
556
742
|
function rangeOf(node) {
|
|
@@ -563,15 +749,18 @@ function toBlocks(children, source, depth) {
|
|
|
563
749
|
}
|
|
564
750
|
function toBlock(node, source, depth) {
|
|
565
751
|
const [start, end] = rangeOf(node);
|
|
566
|
-
if (
|
|
752
|
+
if (isJsxBlock2(node)) {
|
|
567
753
|
if (node.data?.degraded) {
|
|
568
754
|
return { kind: "source", text: source.slice(start, end), reason: "depth-exceeded", start, end };
|
|
569
755
|
}
|
|
570
756
|
const spec = COMPONENTS[node.name];
|
|
757
|
+
if (spec?.rawBody) {
|
|
758
|
+
return { kind: "source", text: source.slice(start, end), reason: "mermaid", start, end };
|
|
759
|
+
}
|
|
571
760
|
const children = node.children ?? [];
|
|
572
761
|
const itemNames = spec?.items ? Array.isArray(spec.items) ? spec.items : [spec.items] : [];
|
|
573
762
|
if (itemNames.length > 0) {
|
|
574
|
-
const items = children.filter(
|
|
763
|
+
const items = children.filter(isJsxBlock2).filter((child) => itemNames.includes(child.name)).map((child) => {
|
|
575
764
|
const [itemStart, itemEnd] = rangeOf(child);
|
|
576
765
|
return {
|
|
577
766
|
name: child.name,
|
|
@@ -747,14 +936,14 @@ function foldChildren(parent) {
|
|
|
747
936
|
index += folded.itemOffset + 1;
|
|
748
937
|
}
|
|
749
938
|
}
|
|
750
|
-
function
|
|
939
|
+
function walk2(parent) {
|
|
751
940
|
if (parent.type === "root" || parent.type === "jsxBlock") foldChildren(parent);
|
|
752
941
|
const children = parent.children;
|
|
753
942
|
if (!children) return;
|
|
754
|
-
for (const child of children)
|
|
943
|
+
for (const child of children) walk2(child);
|
|
755
944
|
}
|
|
756
945
|
function foldInlineItems(tree) {
|
|
757
|
-
|
|
946
|
+
walk2(tree);
|
|
758
947
|
}
|
|
759
948
|
function remarkInlineItems() {
|
|
760
949
|
return (tree) => {
|
|
@@ -797,110 +986,6 @@ function promoteBlockMath(tree, source) {
|
|
|
797
986
|
});
|
|
798
987
|
}
|
|
799
988
|
|
|
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
989
|
// src/policy-tags.ts
|
|
905
990
|
var URL_ATTRS = ["href", "src"];
|
|
906
991
|
var HTML_OPEN_TAG = /^<([A-Z][A-Za-z0-9]*)/;
|
|
@@ -975,7 +1060,10 @@ function parse(md, options) {
|
|
|
975
1060
|
...collectUnclosed(tree, source),
|
|
976
1061
|
...collectUnsafeUrls(tree),
|
|
977
1062
|
...collectUnknownTags(tree),
|
|
978
|
-
...collectIndentAsCode(tree, source)
|
|
1063
|
+
...collectIndentAsCode(tree, source),
|
|
1064
|
+
// 图表容器:空源码 / 超限。必须在 enforceMaxDepth 之后——
|
|
1065
|
+
// 被深度裁剪降级的图表块不再出图,也就不该再报图表自身的诊断
|
|
1066
|
+
...collectMermaid(tree)
|
|
979
1067
|
];
|
|
980
1068
|
const blocks = extractBlocks(tree, source);
|
|
981
1069
|
return { tree, blocks, diagnostics };
|
|
@@ -1149,6 +1237,11 @@ function jsxBlockToHast(state, node) {
|
|
|
1149
1237
|
for (const [key, value] of Object.entries(node.attributes ?? {})) {
|
|
1150
1238
|
properties[key] = value;
|
|
1151
1239
|
}
|
|
1240
|
+
if (typeof node.data?.rawBody === "string") {
|
|
1241
|
+
properties.raw = node.data.rawBody;
|
|
1242
|
+
properties.code = node.data.rawBody;
|
|
1243
|
+
if (node.data.mermaidTooLarge) properties.tooLarge = true;
|
|
1244
|
+
}
|
|
1152
1245
|
return {
|
|
1153
1246
|
type: "element",
|
|
1154
1247
|
tagName: node.name,
|
|
@@ -1177,6 +1270,7 @@ function MarkdownRenderer({
|
|
|
1177
1270
|
const mdast = processor.parse(source);
|
|
1178
1271
|
promoteBlockMath(mdast, source);
|
|
1179
1272
|
enforceMaxDepth(mdast, source);
|
|
1273
|
+
collectMermaid(mdast);
|
|
1180
1274
|
const hast = processor.runSync(mdast);
|
|
1181
1275
|
return toJsxRuntime(hast, {
|
|
1182
1276
|
Fragment,
|
|
@@ -1188,6 +1282,6 @@ function MarkdownRenderer({
|
|
|
1188
1282
|
return /* @__PURE__ */ jsx("div", { className, children: rendered });
|
|
1189
1283
|
}
|
|
1190
1284
|
|
|
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-
|
|
1285
|
+
export { COMPONENTS, ITEMS, MAX_CONTAINER_DEPTH, MERMAID_MAX_BYTES, MarkdownRenderer, extractBlocks, getComponent, getItemSpec, isContainerWithItems, jsxBlocks, jsxFromMarkdown, jsxToMarkdown, listComponents, normalizeMarkdown, parse, parseOpenTag, remarkJsxBlocks, sliceRawBody };
|
|
1286
|
+
//# sourceMappingURL=chunk-7RXVCGFM.js.map
|
|
1287
|
+
//# sourceMappingURL=chunk-7RXVCGFM.js.map
|