@heikeson/md-engine 0.5.2 → 0.5.3
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 +35 -23
- package/dist/{chunk-RJZKMZOI.js → chunk-EJGRPSVC.js} +144 -91
- package/dist/chunk-EJGRPSVC.js.map +1 -0
- package/dist/{chunk-KC6CBSFU.js → chunk-ONVXIYKH.js} +10 -10
- package/dist/chunk-ONVXIYKH.js.map +1 -0
- package/dist/components/index.d.ts +14 -17
- package/dist/components/index.js +1 -1
- package/dist/editor/index.d.ts +2 -2
- package/dist/editor/index.js +3 -3
- package/dist/editor/index.js.map +1 -1
- package/dist/index.d.ts +20 -3
- package/dist/index.js +3 -3
- package/dist/mermaid/index.js +8 -2
- package/dist/mermaid/index.js.map +1 -1
- package/dist/{render-DbUepYgE.d.ts → render-BxUxfxMc.d.ts} +1 -0
- package/package.json +2 -1
- package//345/257/271/346/216/245/345/245/221/347/272/246.md +17 -3
- package/dist/chunk-KC6CBSFU.js.map +0 -1
- package/dist/chunk-RJZKMZOI.js.map +0 -1
package/README.md
CHANGED
|
@@ -1,28 +1,25 @@
|
|
|
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 包按子路径拆分入口(主入口 / editor / components / mermaid / styles.css,另有 locales/zh-CN 与 locales/en-US)。
|
|
5
5
|
> @created 2026-10-24 19:57
|
|
6
|
-
> @updated 2026-11-
|
|
7
|
-
> @updated 2026-11-
|
|
8
|
-
> @updated 2026-11-
|
|
9
|
-
> @
|
|
10
|
-
> @updated 2026-11-02 21:40 版本口径对齐 0.5.1
|
|
11
|
-
> @updated 2026-11-02 23:14 版本口径对齐 0.5.2
|
|
12
|
-
> @status v0.5.2
|
|
6
|
+
> @updated 2026-11-05 23:52 校对到 0.5.2 源码:入口按子路径列全;样式作用域与令牌来源;诊断码细分;Callout type 补 info
|
|
7
|
+
> @updated 2026-11-09 10:20 补充「依赖约定」(katex / @types 的位置原因)与 diagnose 用法
|
|
8
|
+
> @updated 2026-11-09 11:05 API 表补 `parse` 的 `blocks` 选项与 `diagnose` 行
|
|
9
|
+
> @status v0.5.3
|
|
13
10
|
|
|
14
11
|
一个面向 React 的 Markdown 内容引擎。在标准 Markdown(GFM + 数学公式)之上,把
|
|
15
|
-
`<Callout>`、`<Cards><Card /></Cards>`
|
|
12
|
+
`<Callout>`、`<Cards><Card /></Cards>` 这类**尖括号容器**纳入解析流程,并
|
|
16
13
|
`md → AST → md` 逐字往返。适用于文档站、博客、知识库等需要「作者写 Markdown、读者看组件」的场景。
|
|
17
14
|
|
|
18
15
|
## 特性
|
|
19
16
|
|
|
20
|
-
-
|
|
21
|
-
-
|
|
22
|
-
- **SSR
|
|
23
|
-
-
|
|
24
|
-
-
|
|
25
|
-
- **URL
|
|
17
|
+
- **容器是真正的节点**:容器在 mdast 层是 `jsxBlock` 节点,可解析、可渲染、可序列化、可定位,不是字符串替换。
|
|
18
|
+
- **往返保形**:`parse` 与 `serialize` 共用同一套扩展,`md → AST → md` 逐字一致。
|
|
19
|
+
- **SSR 安全**:解析与渲染不读 `window` / `document`,可在服务端直接调用。
|
|
20
|
+
- **入口隔离**:编辑器代码在独立入口,只引渲染器不会带上 CodeMirror。
|
|
21
|
+
- **容错降级**:未闭合容器、未知标签、超限嵌套降级显示并给出诊断码,不吞内容。
|
|
22
|
+
- **URL 白名单**:`href` / `src` 在 hast 层统一过白名单,非法 scheme 被拦截,组件覆写也绕不过。
|
|
26
23
|
|
|
27
24
|
## 安装
|
|
28
25
|
|
|
@@ -37,6 +34,15 @@ npm install @heikeson/md-engine
|
|
|
37
34
|
| Tailwind | 不需要。包内样式为原生 CSS,颜色走 `--lkes-*` 变量,可脱离 Tailwind 独立使用 |
|
|
38
35
|
| 模块格式 | **ESM-only**:`exports` 只提供 `import` 条件,无 CJS 入口。CommonJS 项目(老 Jest、webpack4)需经 ESM 互操作层或打包后使用 |
|
|
39
36
|
|
|
37
|
+
### 依赖约定
|
|
38
|
+
|
|
39
|
+
| 依赖 | 位置 | 说明 |
|
|
40
|
+
|---|---|---|
|
|
41
|
+
| `katex` | `dependencies` | 构建时被 external,产物里保留 `import "katex"`;由本包直接声明,避免在严格 `node_modules`(pnpm)下由 `rehype-katex` 的私有依赖树解析 |
|
|
42
|
+
| `@types/hast` / `@types/mdast` | `dependencies` | **有意为之**:`dist/*.d.ts` 直接引用 `hast` / `mdast` 模块类型,宿主 `skipLibCheck: false` 时需要能解析到它们。不要移到 `devDependencies` |
|
|
43
|
+
| `clsx` / `tailwind-merge` | `dependencies` | 仅用于包内 `cn()` 合并类名,宿主的同名依赖互不影响 |
|
|
44
|
+
| `mermaid` / `dompurify` | 可选 peer(`/mermaid` 入口) | 只在引入 `/mermaid` 时才需要,未安装时图表降级为源码 |
|
|
45
|
+
|
|
40
46
|
## 快速开始
|
|
41
47
|
|
|
42
48
|
### 渲染
|
|
@@ -56,20 +62,24 @@ export default function Article() {
|
|
|
56
62
|
}
|
|
57
63
|
```
|
|
58
64
|
|
|
59
|
-
外层的 `className`
|
|
65
|
+
外层的 `className` 要带上 `prose-site`:8 个容器样式(callout / tabs / steps / cards / accordion / files / type-table / image)的作用域挂在该类名下。`Mermaid` 用 `.lkes-mermaid`、编辑器用 `.md-editor`,两者不要求这个类。
|
|
60
66
|
|
|
61
67
|
### 解析、序列化与格式化
|
|
62
68
|
|
|
63
69
|
```ts
|
|
64
|
-
import { parse, serialize, roundtrip, normalizeMarkdown } from "@heikeson/md-engine";
|
|
70
|
+
import { parse, diagnose, serialize, roundtrip, normalizeMarkdown } from "@heikeson/md-engine";
|
|
65
71
|
|
|
66
72
|
const { tree, blocks, diagnostics } = parse(md);
|
|
67
73
|
const same = serialize(tree); // md → AST → md:形态(行内 / 块)与空行按原文保形
|
|
68
74
|
const again = roundtrip(md); // 等价写法
|
|
69
75
|
const tidy = normalizeMarkdown(md); // 需要统一排版时用它(补空行、收敛空行),serialize 不替你改
|
|
76
|
+
|
|
77
|
+
const onlyDiagnostics = diagnose(md); // 只要诊断:跳过块索引
|
|
78
|
+
const noBlocks = parse(md, { blocks: false }); // 同上,等价写法
|
|
70
79
|
```
|
|
71
80
|
|
|
72
81
|
`blocks` 是带全文 offset 的扁平块索引,可用来做目录、定位或分屏滚动同步。
|
|
82
|
+
输入时只需提示(如编辑器)就用 `diagnose()`:省掉 `extractBlocks` 的整树转换。
|
|
73
83
|
|
|
74
84
|
### 编辑器
|
|
75
85
|
|
|
@@ -93,7 +103,7 @@ export default function EditorPage() {
|
|
|
93
103
|
}
|
|
94
104
|
```
|
|
95
105
|
|
|
96
|
-
编辑器以 Markdown 文本为唯一真源:左 CodeMirror
|
|
106
|
+
编辑器以 Markdown 文本为唯一真源:左 CodeMirror、右实时预览;工具栏插入的容器骨架由 `registry.ts` 生成,属性名与子项名不会与解析规则脱节。
|
|
97
107
|
|
|
98
108
|
内置快捷键(与工具栏按钮同一动作实现):`Mod+B/I/E` 粗体/斜体/行内码、`Mod+Shift+S` 删除线、
|
|
99
109
|
`Mod+1` 二级标题、`Mod+Shift+Q` 引用、`Mod+Shift+L` 列表。
|
|
@@ -172,7 +182,7 @@ const components = { ...mdxComponents, ...mermaidComponents };
|
|
|
172
182
|
|
|
173
183
|
| 容器 | 属性 | 子项 |
|
|
174
184
|
|---|---|---|
|
|
175
|
-
| `Callout` | `type
|
|
185
|
+
| `Callout` | `type`(`note` / `info` / `tip` / `important` / `warning` / `caution` / `danger`,默认 `note`)/ `title` / `icon` | 正文 |
|
|
176
186
|
| `Tabs` | — | `Tab`:`value`、`default`(布尔) |
|
|
177
187
|
| `Steps` | — | `Step`:`title` |
|
|
178
188
|
| `Cards` | — | `Card`:`title`、`href`、`icon`、`description`(可自闭合) |
|
|
@@ -223,7 +233,8 @@ const components = { ...mdxComponents, ...mermaidComponents };
|
|
|
223
233
|
|
|
224
234
|
| 导出 | 说明 |
|
|
225
235
|
|---|---|
|
|
226
|
-
| `parse(md, { maxDepth? })` | 返回 `{ tree, blocks, diagnostics }` |
|
|
236
|
+
| `parse(md, { maxDepth?, blocks? })` | 返回 `{ tree, blocks, diagnostics }`;`blocks: false` 时不提取块索引 |
|
|
237
|
+
| `diagnose(md, { maxDepth? })` | 只取诊断,等价于 `parse(md, { blocks: false }).diagnostics` |
|
|
227
238
|
| `serialize(tree)` | mdast → Markdown,与解析对称 |
|
|
228
239
|
| `roundtrip(md)` | `serialize(parse(md).tree)` 的简写 |
|
|
229
240
|
| `normalizeMarkdown(md)` | 幂等的排版整理 |
|
|
@@ -252,8 +263,8 @@ const components = { ...mdxComponents, ...mermaidComponents };
|
|
|
252
263
|
|
|
253
264
|
### 样式入口 `@heikeson/md-engine/styles.css`
|
|
254
265
|
|
|
255
|
-
|
|
256
|
-
|
|
266
|
+
引入一次即可。8 个容器的类名挂在 `.prose-site` 作用域下;`Mermaid` 与编辑器分别用 `.lkes-mermaid` / `.md-editor`,不要求该类名。
|
|
267
|
+
配色由独立命名空间 `--lkes-*` 控制(9 个令牌,亮暗两套字面量),不读宿主的 `--background` / `--foreground` 等变量,宿主不配置也能正常显示;
|
|
257
268
|
覆盖方式与变量清单见 [`对接契约.md`](./对接契约.md) 第 9 节。
|
|
258
269
|
|
|
259
270
|
需要按组件引入时:
|
|
@@ -300,7 +311,8 @@ Markdown
|
|
|
300
311
|
| 非白名单协议的 `href` / `src` | `href` → `#`、`src` 移除 | `UNSAFE_HREF` |
|
|
301
312
|
| `<Mermaid>` 容器体为空 | 渲染为源码块 + 提示,不白屏 | `MERMAID_EMPTY` |
|
|
302
313
|
| `<Mermaid>` 源码 > 20 KB | 不渲染图表,原样显示源码 | `MERMAID_TOO_LARGE` |
|
|
303
|
-
| `div id`
|
|
314
|
+
| `div id` 与标题锚点重复 | 照常渲染(提示重复 id 风险),不改写内容 | `DUPLICATE_ID_ANCHOR` |
|
|
315
|
+
| `div id` 彼此重复 | 同上 | `DUPLICATE_ID_DIV` |
|
|
304
316
|
|
|
305
317
|
协议白名单为 `http(s)` / `mailto` / `tel`,以及任意无 scheme 的引用(相对路径、`//host`、`#锚点`、`?查询`)。
|
|
306
318
|
|
|
@@ -1,7 +1,9 @@
|
|
|
1
|
-
import { mdxComponents, isSafeHref } from './chunk-
|
|
1
|
+
import { mdxComponents, isSafeHref } from './chunk-ONVXIYKH.js';
|
|
2
2
|
import { useShallowStable, MdEngineProvider } from './chunk-OJQDOFGL.js';
|
|
3
3
|
import { asciiAlpha, asciiAlphanumeric, markdownLineEnding } from 'micromark-util-character';
|
|
4
4
|
import { fromMarkdown } from 'mdast-util-from-markdown';
|
|
5
|
+
import remarkGfm from 'remark-gfm';
|
|
6
|
+
import remarkMath from 'remark-math';
|
|
5
7
|
import { gfm } from 'micromark-extension-gfm';
|
|
6
8
|
import { gfmFromMarkdown } from 'mdast-util-gfm';
|
|
7
9
|
import { math } from 'micromark-extension-math';
|
|
@@ -12,8 +14,6 @@ import { useMemo } from 'react';
|
|
|
12
14
|
import { jsxs, jsx, Fragment } from 'react/jsx-runtime';
|
|
13
15
|
import { unified } from 'unified';
|
|
14
16
|
import remarkParse from 'remark-parse';
|
|
15
|
-
import remarkGfm from 'remark-gfm';
|
|
16
|
-
import remarkMath from 'remark-math';
|
|
17
17
|
import remarkRehype from 'remark-rehype';
|
|
18
18
|
import rehypeSlug from 'rehype-slug';
|
|
19
19
|
import rehypeKatex from 'rehype-katex';
|
|
@@ -453,6 +453,21 @@ function tokenizeJsxBlock(effects, ok, nok) {
|
|
|
453
453
|
// src/syntax/jsx-mdast.ts
|
|
454
454
|
var OPEN_TAG_RE = /^<([A-Za-z][\w]*)([\s\S]*?)(\/?)>$/;
|
|
455
455
|
var ATTR_RE = /([A-Za-z_][\w-]*)(?:\s*=\s*(?:"([^"]*)"|'([^']*)'))?/g;
|
|
456
|
+
var CLOSE_TAG_RE = /^<\/([A-Za-z][\w]*)\s*>$/;
|
|
457
|
+
var CLOSE_TAG_CACHE = /* @__PURE__ */ new Map();
|
|
458
|
+
function escapeRegExp(value) {
|
|
459
|
+
return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
460
|
+
}
|
|
461
|
+
function closeTagRe(name, where = "any") {
|
|
462
|
+
const key = `${where}:${name}`;
|
|
463
|
+
const cached = CLOSE_TAG_CACHE.get(key);
|
|
464
|
+
if (cached) return cached;
|
|
465
|
+
const escaped = escapeRegExp(name);
|
|
466
|
+
const pattern = where === "end" ? `</${escaped}\\s*>\\s*$` : where === "start" ? `^\\s*</${escaped}\\s*>` : `</${escaped}\\s*>`;
|
|
467
|
+
const created = new RegExp(pattern);
|
|
468
|
+
CLOSE_TAG_CACHE.set(key, created);
|
|
469
|
+
return created;
|
|
470
|
+
}
|
|
456
471
|
function parseOpenTag(raw) {
|
|
457
472
|
const match = OPEN_TAG_RE.exec(raw.trim());
|
|
458
473
|
if (!match) return { name: "", attributes: {}, selfClosing: false };
|
|
@@ -522,7 +537,7 @@ function sliceRawBody(raw, name, selfClosing) {
|
|
|
522
537
|
const openEnd = openTagEnd(raw);
|
|
523
538
|
if (openEnd < 0) return "";
|
|
524
539
|
const bodyStart = openEnd + 1;
|
|
525
|
-
const close =
|
|
540
|
+
const close = closeTagRe(name, "end").exec(raw);
|
|
526
541
|
if (!close) return raw.slice(bodyStart);
|
|
527
542
|
return raw.slice(bodyStart, close.index);
|
|
528
543
|
}
|
|
@@ -600,6 +615,19 @@ function remarkJsxBlocks() {
|
|
|
600
615
|
return void 0;
|
|
601
616
|
}
|
|
602
617
|
|
|
618
|
+
// src/internal/walk.ts
|
|
619
|
+
function childrenOf(node) {
|
|
620
|
+
return node.children;
|
|
621
|
+
}
|
|
622
|
+
function walkMdast(node, visit7) {
|
|
623
|
+
const children = childrenOf(node);
|
|
624
|
+
if (!children) return;
|
|
625
|
+
for (const child of children) {
|
|
626
|
+
visit7(child);
|
|
627
|
+
walkMdast(child, visit7);
|
|
628
|
+
}
|
|
629
|
+
}
|
|
630
|
+
|
|
603
631
|
// src/policy.ts
|
|
604
632
|
var MERMAID_MAX_BYTES = 20 * 1024;
|
|
605
633
|
function isJsxBlock(node) {
|
|
@@ -652,9 +680,8 @@ function hasCloseTag(block, source) {
|
|
|
652
680
|
const start = block.position?.start.offset ?? 0;
|
|
653
681
|
const end = block.position?.end.offset ?? 0;
|
|
654
682
|
if (end <= start) return false;
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
return new RegExp(`^\\s*</${block.name}\\s*>`).test(source.slice(end, end + 64));
|
|
683
|
+
if (closeTagRe(block.name).test(source.slice(start, end))) return true;
|
|
684
|
+
return closeTagRe(block.name, "start").test(source.slice(end, end + 64));
|
|
658
685
|
}
|
|
659
686
|
function collectIndentAsCode(tree, source) {
|
|
660
687
|
const diagnostics = [];
|
|
@@ -685,7 +712,7 @@ function collectIndentAsCode(tree, source) {
|
|
|
685
712
|
}
|
|
686
713
|
function collectUnclosed(tree, source) {
|
|
687
714
|
const diagnostics = [];
|
|
688
|
-
|
|
715
|
+
walkMdast(tree, (node) => {
|
|
689
716
|
if (node.type === "jsxBlock") {
|
|
690
717
|
const block = node;
|
|
691
718
|
if (!block.selfClosing && !hasCloseTag(block, source)) {
|
|
@@ -700,15 +727,12 @@ function collectUnclosed(tree, source) {
|
|
|
700
727
|
});
|
|
701
728
|
}
|
|
702
729
|
}
|
|
703
|
-
|
|
704
|
-
if (children) for (const child of children) visit7(child);
|
|
705
|
-
};
|
|
706
|
-
visit7(tree);
|
|
730
|
+
});
|
|
707
731
|
return diagnostics;
|
|
708
732
|
}
|
|
709
733
|
function collectMermaid(tree) {
|
|
710
734
|
const diagnostics = [];
|
|
711
|
-
|
|
735
|
+
walkMdast(tree, (node) => {
|
|
712
736
|
if (node.type === "jsxBlock") {
|
|
713
737
|
const block = node;
|
|
714
738
|
if (COMPONENTS[block.name]?.rawBody) {
|
|
@@ -740,10 +764,7 @@ function collectMermaid(tree) {
|
|
|
740
764
|
}
|
|
741
765
|
}
|
|
742
766
|
}
|
|
743
|
-
|
|
744
|
-
if (children) for (const child of children) visit7(child);
|
|
745
|
-
};
|
|
746
|
-
visit7(tree);
|
|
767
|
+
});
|
|
747
768
|
return diagnostics;
|
|
748
769
|
}
|
|
749
770
|
|
|
@@ -823,7 +844,6 @@ function extractBlocks(tree, source) {
|
|
|
823
844
|
}
|
|
824
845
|
|
|
825
846
|
// src/syntax/jsx-inline.ts
|
|
826
|
-
var CLOSE_TAG_RE = /^<\/([A-Za-z][\w]*)\s*>$/;
|
|
827
847
|
function itemsOf(name) {
|
|
828
848
|
const items = COMPONENTS[name]?.items;
|
|
829
849
|
if (!items) return [];
|
|
@@ -964,6 +984,17 @@ function remarkInlineItems() {
|
|
|
964
984
|
return void 0;
|
|
965
985
|
};
|
|
966
986
|
}
|
|
987
|
+
function remarkHtmlAsText() {
|
|
988
|
+
return (tree) => {
|
|
989
|
+
visit(tree, "html", (node, index, parent) => {
|
|
990
|
+
if (typeof index !== "number" || !parent) return;
|
|
991
|
+
const replacement = { type: "text", value: node.value };
|
|
992
|
+
if (node.position) replacement.position = node.position;
|
|
993
|
+
parent.children[index] = replacement;
|
|
994
|
+
});
|
|
995
|
+
return void 0;
|
|
996
|
+
};
|
|
997
|
+
}
|
|
967
998
|
function isSingleLineBlockMath(raw) {
|
|
968
999
|
if (!raw.startsWith("$$") || !raw.endsWith("$$")) return false;
|
|
969
1000
|
if (raw.length <= 4) return false;
|
|
@@ -998,15 +1029,25 @@ function promoteBlockMath(tree, source) {
|
|
|
998
1029
|
};
|
|
999
1030
|
});
|
|
1000
1031
|
}
|
|
1032
|
+
|
|
1033
|
+
// src/pipeline.ts
|
|
1034
|
+
var MARKDOWN_EXTENSIONS = [gfm(), math(), jsxBlocks()];
|
|
1035
|
+
var MARKDOWN_MDAST_EXTENSIONS = [
|
|
1036
|
+
gfmFromMarkdown(),
|
|
1037
|
+
mathFromMarkdown(),
|
|
1038
|
+
jsxFromMarkdown()
|
|
1039
|
+
];
|
|
1040
|
+
var REMARK_PARSE_PLUGINS = [remarkJsxBlocks, remarkGfm, remarkMath];
|
|
1041
|
+
var REMARK_TRANSFORMS = [remarkInlineItems, remarkHtmlAsText];
|
|
1042
|
+
function applySourceTransforms(tree, source, maxDepth) {
|
|
1043
|
+
promoteBlockMath(tree, source);
|
|
1044
|
+
const depth = enforceMaxDepth(tree, source, maxDepth);
|
|
1045
|
+
const mermaid = collectMermaid(tree);
|
|
1046
|
+
return { depth, mermaid };
|
|
1047
|
+
}
|
|
1001
1048
|
var URL_ATTRS = ["href", "src"];
|
|
1002
1049
|
var HTML_OPEN_TAG = /^<([A-Z][A-Za-z0-9]*)/;
|
|
1003
1050
|
var KNOWN_NAMES = /* @__PURE__ */ new Set([...Object.keys(COMPONENTS), ...Object.keys(ITEMS)]);
|
|
1004
|
-
function forEachNode(node, fn) {
|
|
1005
|
-
if (node.type !== "root") fn(node);
|
|
1006
|
-
const children = node.children;
|
|
1007
|
-
if (!children) return;
|
|
1008
|
-
for (const child of children) forEachNode(child, fn);
|
|
1009
|
-
}
|
|
1010
1051
|
function collectUnsafeUrls(tree) {
|
|
1011
1052
|
const diagnostics = [];
|
|
1012
1053
|
const report = (line, kind, label, url) => {
|
|
@@ -1018,7 +1059,7 @@ function collectUnsafeUrls(tree) {
|
|
|
1018
1059
|
params: { kind, label, url }
|
|
1019
1060
|
});
|
|
1020
1061
|
};
|
|
1021
|
-
|
|
1062
|
+
walkMdast(tree, (node) => {
|
|
1022
1063
|
const line = node.position?.start.line ?? 1;
|
|
1023
1064
|
if (node.type === "jsxBlock") {
|
|
1024
1065
|
const block = node;
|
|
@@ -1048,52 +1089,43 @@ function collectDuplicateIds(tree) {
|
|
|
1048
1089
|
const diagnostics = [];
|
|
1049
1090
|
const slugger = new GithubSlugger();
|
|
1050
1091
|
const headingSlugs = /* @__PURE__ */ new Set();
|
|
1051
|
-
|
|
1092
|
+
walkMdast(tree, (node) => {
|
|
1052
1093
|
if (node.type === "heading") headingSlugs.add(slugger.slug(nodeText(node)));
|
|
1053
|
-
|
|
1054
|
-
if (children) for (const child of children) collectHeadings(child);
|
|
1055
|
-
};
|
|
1056
|
-
collectHeadings(tree);
|
|
1094
|
+
});
|
|
1057
1095
|
const seen = /* @__PURE__ */ new Map();
|
|
1058
|
-
|
|
1059
|
-
if (node.type
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
}
|
|
1085
|
-
}
|
|
1086
|
-
}
|
|
1096
|
+
walkMdast(tree, (node) => {
|
|
1097
|
+
if (node.type !== "jsxBlock") return;
|
|
1098
|
+
const block = node;
|
|
1099
|
+
if (block.name !== "div") return;
|
|
1100
|
+
const id = block.attributes?.id;
|
|
1101
|
+
const line = block.position?.start.line ?? 1;
|
|
1102
|
+
if (typeof id !== "string" || id.length === 0) return;
|
|
1103
|
+
const first = seen.get(id);
|
|
1104
|
+
if (headingSlugs.has(id)) {
|
|
1105
|
+
diagnostics.push({
|
|
1106
|
+
code: "DUPLICATE_ID_ANCHOR",
|
|
1107
|
+
level: "warn",
|
|
1108
|
+
line,
|
|
1109
|
+
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`,
|
|
1110
|
+
params: { id }
|
|
1111
|
+
});
|
|
1112
|
+
} else if (first !== void 0) {
|
|
1113
|
+
diagnostics.push({
|
|
1114
|
+
code: "DUPLICATE_ID_DIV",
|
|
1115
|
+
level: "warn",
|
|
1116
|
+
line,
|
|
1117
|
+
message: `div id="${id}" \u4E0E\u7B2C ${first} \u884C\u7684 div \u91CD\u590D\uFF0C\u9875\u9762\u5C06\u51FA\u73B0\u4E24\u4E2A\u540C\u540D id`,
|
|
1118
|
+
params: { id, first }
|
|
1119
|
+
});
|
|
1120
|
+
} else {
|
|
1121
|
+
seen.set(id, line);
|
|
1087
1122
|
}
|
|
1088
|
-
|
|
1089
|
-
if (children) for (const child of children) checkDivs(child);
|
|
1090
|
-
};
|
|
1091
|
-
checkDivs(tree);
|
|
1123
|
+
});
|
|
1092
1124
|
return diagnostics;
|
|
1093
1125
|
}
|
|
1094
1126
|
function collectUnknownTags(tree) {
|
|
1095
1127
|
const diagnostics = [];
|
|
1096
|
-
|
|
1128
|
+
walkMdast(tree, (node) => {
|
|
1097
1129
|
if (node.type !== "html") return;
|
|
1098
1130
|
const match = HTML_OPEN_TAG.exec(String(node.value ?? "").trim());
|
|
1099
1131
|
if (!match) return;
|
|
@@ -1114,28 +1146,32 @@ function collectUnknownTags(tree) {
|
|
|
1114
1146
|
function parse(md, options) {
|
|
1115
1147
|
const source = md ?? "";
|
|
1116
1148
|
const tree = fromMarkdown(source, {
|
|
1117
|
-
// GFM(表格 / 删除线 /
|
|
1149
|
+
// 扩展在 pipeline 里构造一次并复用:GFM(表格 / 删除线 / 任务列表)、数学公式与
|
|
1150
|
+
// 尖括号块并列注册,解析与序列化保持同一口径。
|
|
1118
1151
|
// math 必须在此注册:否则 `$…$` 会退化成普通文本,序列化时把 `_`、`\` 当文本转义,
|
|
1119
1152
|
// 保存一次公式即变形(渲染侧走 remark-math,故表现为"看着正常、存下来坏了")。
|
|
1120
|
-
extensions:
|
|
1121
|
-
mdastExtensions:
|
|
1153
|
+
extensions: MARKDOWN_EXTENSIONS,
|
|
1154
|
+
mdastExtensions: MARKDOWN_MDAST_EXTENSIONS
|
|
1122
1155
|
});
|
|
1123
1156
|
foldInlineItems(tree);
|
|
1124
|
-
|
|
1157
|
+
const transforms = applySourceTransforms(tree, source, options?.maxDepth);
|
|
1125
1158
|
const diagnostics = [
|
|
1126
|
-
...
|
|
1159
|
+
...transforms.depth,
|
|
1127
1160
|
...collectUnclosed(tree, source),
|
|
1128
1161
|
...collectUnsafeUrls(tree),
|
|
1129
1162
|
...collectUnknownTags(tree),
|
|
1130
1163
|
...collectIndentAsCode(tree, source),
|
|
1131
1164
|
...collectDuplicateIds(tree),
|
|
1132
|
-
// 图表容器:空源码 /
|
|
1165
|
+
// 图表容器:空源码 / 超限。必须排在深度裁剪之后——
|
|
1133
1166
|
// 被深度裁剪降级的图表块不再出图,也就不该再报图表自身的诊断
|
|
1134
|
-
...
|
|
1167
|
+
...transforms.mermaid
|
|
1135
1168
|
];
|
|
1136
|
-
const blocks = extractBlocks(tree, source);
|
|
1169
|
+
const blocks = options?.blocks === false ? [] : extractBlocks(tree, source);
|
|
1137
1170
|
return { tree, blocks, diagnostics };
|
|
1138
1171
|
}
|
|
1172
|
+
function diagnose(md, options) {
|
|
1173
|
+
return parse(md, { ...options, blocks: false }).diagnostics;
|
|
1174
|
+
}
|
|
1139
1175
|
|
|
1140
1176
|
// src/format.ts
|
|
1141
1177
|
var TAG_LINE = /^(\s*)<(\/?)([A-Z][A-Za-z0-9]*)((?:\s[^>]*)?)(\/?)>\s*$/;
|
|
@@ -1178,17 +1214,6 @@ function normalizeMarkdown(md) {
|
|
|
1178
1214
|
while (out.length > 0 && out[out.length - 1] === "") out.pop();
|
|
1179
1215
|
return out.join("\n");
|
|
1180
1216
|
}
|
|
1181
|
-
function remarkHtmlAsText() {
|
|
1182
|
-
return (tree) => {
|
|
1183
|
-
visit(tree, "html", (node, index, parent) => {
|
|
1184
|
-
if (typeof index !== "number" || !parent) return;
|
|
1185
|
-
const replacement = { type: "text", value: node.value };
|
|
1186
|
-
if (node.position) replacement.position = node.position;
|
|
1187
|
-
parent.children[index] = replacement;
|
|
1188
|
-
});
|
|
1189
|
-
return void 0;
|
|
1190
|
-
};
|
|
1191
|
-
}
|
|
1192
1217
|
var URL_PROPS = ["href", "src"];
|
|
1193
1218
|
function rehypeSafeUrls() {
|
|
1194
1219
|
return (tree) => {
|
|
@@ -1318,7 +1343,37 @@ function jsxBlockToHast(state, node) {
|
|
|
1318
1343
|
};
|
|
1319
1344
|
}
|
|
1320
1345
|
function createProcessor(extraRemark, extraRehype, sourcePositions) {
|
|
1321
|
-
return unified().use(remarkParse).use(
|
|
1346
|
+
return unified().use(remarkParse).use(REMARK_PARSE_PLUGINS).use(REMARK_TRANSFORMS).use(extraRemark ?? []).use(remarkRehype, { handlers: { jsxBlock: jsxBlockToHast } }).use(rehypeSafeUrls).use(rehypeSafeAnchors).use(sourcePositions ? [rehypeSourcePositions] : []).use(rehypeCodeMeta).use(rehypeSlug).use(extraRehype ?? []).use(rehypeKatex);
|
|
1347
|
+
}
|
|
1348
|
+
var NO_PLUGINS = [];
|
|
1349
|
+
var baseProcessorCache = /* @__PURE__ */ new Map();
|
|
1350
|
+
var hostProcessorCache = /* @__PURE__ */ new WeakMap();
|
|
1351
|
+
function getProcessor(extraRemark, extraRehype, sourcePositions) {
|
|
1352
|
+
const remarkKey = extraRemark ?? NO_PLUGINS;
|
|
1353
|
+
const rehypeKey = extraRehype ?? NO_PLUGINS;
|
|
1354
|
+
const variant = sourcePositions ? "source-positions" : "default";
|
|
1355
|
+
if (remarkKey === NO_PLUGINS && rehypeKey === NO_PLUGINS) {
|
|
1356
|
+
const cached2 = baseProcessorCache.get(variant);
|
|
1357
|
+
if (cached2) return cached2;
|
|
1358
|
+
const created2 = createProcessor(void 0, void 0, sourcePositions);
|
|
1359
|
+
baseProcessorCache.set(variant, created2);
|
|
1360
|
+
return created2;
|
|
1361
|
+
}
|
|
1362
|
+
let byRehype = hostProcessorCache.get(remarkKey);
|
|
1363
|
+
if (!byRehype) {
|
|
1364
|
+
byRehype = /* @__PURE__ */ new WeakMap();
|
|
1365
|
+
hostProcessorCache.set(remarkKey, byRehype);
|
|
1366
|
+
}
|
|
1367
|
+
let byVariant = byRehype.get(rehypeKey);
|
|
1368
|
+
if (!byVariant) {
|
|
1369
|
+
byVariant = /* @__PURE__ */ new Map();
|
|
1370
|
+
byRehype.set(rehypeKey, byVariant);
|
|
1371
|
+
}
|
|
1372
|
+
const cached = byVariant.get(variant);
|
|
1373
|
+
if (cached) return cached;
|
|
1374
|
+
const created = createProcessor(extraRemark, extraRehype, sourcePositions);
|
|
1375
|
+
byVariant.set(variant, created);
|
|
1376
|
+
return created;
|
|
1322
1377
|
}
|
|
1323
1378
|
function MarkdownRenderer({
|
|
1324
1379
|
content,
|
|
@@ -1337,15 +1392,13 @@ function MarkdownRenderer({
|
|
|
1337
1392
|
return { ...text, ...messages };
|
|
1338
1393
|
}, [text, messages]);
|
|
1339
1394
|
const processor = useMemo(
|
|
1340
|
-
() =>
|
|
1395
|
+
() => getProcessor(extraRemark, extraRehype, sourcePositions),
|
|
1341
1396
|
[extraRemark, extraRehype, sourcePositions]
|
|
1342
1397
|
);
|
|
1343
1398
|
const rendered = useMemo(() => {
|
|
1344
1399
|
const source = content ?? "";
|
|
1345
1400
|
const mdast = processor.parse(source);
|
|
1346
|
-
|
|
1347
|
-
enforceMaxDepth(mdast, source);
|
|
1348
|
-
collectMermaid(mdast);
|
|
1401
|
+
applySourceTransforms(mdast, source);
|
|
1349
1402
|
const hast = processor.runSync(mdast);
|
|
1350
1403
|
return toJsxRuntime(hast, {
|
|
1351
1404
|
Fragment,
|
|
@@ -1389,6 +1442,6 @@ function formatDiagnostic(t, diagnostic) {
|
|
|
1389
1442
|
return rendered || diagnostic.message;
|
|
1390
1443
|
}
|
|
1391
1444
|
|
|
1392
|
-
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 };
|
|
1393
|
-
//# sourceMappingURL=chunk-
|
|
1394
|
-
//# sourceMappingURL=chunk-
|
|
1445
|
+
export { COMPONENTS, ITEMS, MAX_CONTAINER_DEPTH, MERMAID_MAX_BYTES, MarkdownRenderer, diagnose, diagnosticTextKey, extractBlocks, formatDiagnostic, getComponent, getItemSpec, isContainerWithItems, jsxBlocks, jsxFromMarkdown, jsxToMarkdown, listComponents, normalizeMarkdown, parse, parseOpenTag, remarkJsxBlocks, sliceRawBody };
|
|
1446
|
+
//# sourceMappingURL=chunk-EJGRPSVC.js.map
|
|
1447
|
+
//# sourceMappingURL=chunk-EJGRPSVC.js.map
|