@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/dist/index.d.ts CHANGED
@@ -2,9 +2,11 @@ import { Extension as Extension$1 } from 'micromark-util-types';
2
2
  import { Parent, RootContent, Data, Root } from 'mdast';
3
3
  import { Extension } from 'mdast-util-from-markdown';
4
4
  import { Options } from 'mdast-util-to-markdown';
5
- import { Processor, PluggableList } from 'unified';
6
- import { ComponentType, ReactElement } from 'react';
5
+ import { Processor } from 'unified';
6
+ export { M as MarkdownRenderer, a as MarkdownRendererProps, R as RenderConfig } from './render-C5-vIUuK.js';
7
7
  export { mdxComponents } from './components/index.js';
8
+ export { M as MermaidPlaceholder, a as MermaidProps } from './mermaid-placeholder-A1N4puyc.js';
9
+ import 'react';
8
10
 
9
11
  /**
10
12
  * @file 内容语法注册表(唯一事实来源)
@@ -13,7 +15,7 @@ export { mdxComponents } from './components/index.js';
13
15
  * 禁止在别处再硬编码组件名或属性名。改动本文件即改动内容语法,须同步
14
16
  * `docs/markdown/02-语法规范.md` 与引擎对接契约。
15
17
  * @created 2026-10-24 20:07
16
- * @updated 2026-10-24 20:07
18
+ * @updated 2026-11-01 21:10 新增 Mermaid 图表容器与 rawBody(容器体按原始文本保留)声明
17
19
  */
18
20
  /** 组件的编辑形态:visual 允许结构化编辑;source 只按源码块保留 */
19
21
  type ComponentMode = "visual" | "source";
@@ -33,6 +35,13 @@ interface ComponentSpec {
33
35
  readonly selfClosing?: boolean;
34
36
  /** 属性缺省值 */
35
37
  readonly defaultAttrs?: Readonly<Record<string, string | boolean>>;
38
+ /**
39
+ * 容器体是否为**原始文本**(不按 Markdown 解析)
40
+ * @description 默认 false,既有容器行为不变。设为 true 时容器体整体作为一个字符串
41
+ * 保存在节点 `data.rawBody`,子节点为空——用于 Mermaid 源码这类不能被 Markdown
42
+ * 解释的内容(`#`、`|`、`-->`、行首 `-` 都会被误判)。
43
+ */
44
+ readonly rawBody?: boolean;
36
45
  }
37
46
  /** 子项声明(Tab / Step / Card / AccItem / Folder / File) */
38
47
  interface ItemSpec {
@@ -83,6 +92,7 @@ declare function isContainerWithItems(name: string): boolean;
83
92
  * `md → mdast → md` 要逐字一致。
84
93
  * @created 2026-10-24 20:16
85
94
  * @updated 2026-10-28 11:40 序列化改为保形:按原文行距写回换行/空行,不再统一加空行;行内形态由 data.inline 决定
95
+ * @updated 2026-11-01 21:10 rawBody 容器(Mermaid):exitBlock 用 token 原文切片覆盖子节点,序列化逐字回写
86
96
  */
87
97
 
88
98
  /** 尖括号块节点 */
@@ -104,6 +114,10 @@ interface JsxBlock extends Parent {
104
114
  inline?: boolean;
105
115
  degraded?: boolean;
106
116
  degradedText?: string;
117
+ /** rawBody 容器的容器体原文(不按 Markdown 解析,序列化时逐字回写) */
118
+ rawBody?: string;
119
+ /** 图表源码超出体积上限:渲染层据此只出源码块,不加载图表引擎 */
120
+ mermaidTooLarge?: boolean;
107
121
  };
108
122
  }
109
123
  /** 开标签解析结果 */
@@ -128,6 +142,16 @@ declare function jsxFromMarkdown(): Extension;
128
142
  * @returns 供 mdast-util-to-markdown 使用的扩展
129
143
  */
130
144
  declare function jsxToMarkdown(): Options;
145
+ /**
146
+ * 从块原文中切出容器体(原始文本)
147
+ * @param raw 块原文(含开闭标签)
148
+ * @param name 组件名
149
+ * @param selfClosing 是否自闭合写法
150
+ * @returns 容器体原文;自闭合或无开标签时返回空串
151
+ * @description 只认**结尾处**的 `</Name>`:源码体内行内出现的同名闭合字面量不参与截断,
152
+ * 否则 mermaid 文本里的 `</Mermaid>` 字样会把图表截短。
153
+ */
154
+ declare function sliceRawBody(raw: string, name: string, selfClosing: boolean): string;
131
155
 
132
156
  /**
133
157
  * @file 自定义 token / 节点类型的模块扩展
@@ -204,10 +228,13 @@ declare function remarkJsxBlocks(this: Processor): undefined;
204
228
  * 与渲染实现解耦——将来换渲染方式,这里不用动。
205
229
  * @created 2026-10-24 20:24
206
230
  * @updated 2026-10-24 20:24
231
+ * @updated 2026-11-01 21:47 新增 MERMAID_EMPTY / MERMAID_TOO_LARGE 诊断与 MERMAID_MAX_BYTES
207
232
  */
208
233
 
209
234
  /** 诊断码 */
210
- type DiagnosticCode = "UNCLOSED_TAG" | "DEPTH_EXCEEDED" | "UNKNOWN_TAG" | "UNSAFE_HREF" | "INDENT_AS_CODE";
235
+ type DiagnosticCode = "UNCLOSED_TAG" | "DEPTH_EXCEEDED" | "UNKNOWN_TAG" | "UNSAFE_HREF" | "INDENT_AS_CODE" | "MERMAID_EMPTY" | "MERMAID_TOO_LARGE";
236
+ /** Mermaid 源码体积上限(字节):超限不渲染图表,原样显示源码 */
237
+ declare const MERMAID_MAX_BYTES: number;
211
238
  /** 一条诊断 */
212
239
  interface Diagnostic {
213
240
  code: DiagnosticCode;
@@ -227,12 +254,15 @@ interface Diagnostic {
227
254
  * 区间一律是原文 offset(0 起,end 不含),可直接用于 `source.slice(start, end)`。
228
255
  * @created 2026-10-24 21:06
229
256
  * @updated 2026-10-24 21:06
257
+ * @updated 2026-11-01 21:47 rawBody 容器按 source + reason:mermaid 暴露
230
258
  */
231
259
 
232
260
  /** 容器属性表 */
233
261
  type Attrs = Readonly<Record<string, string | boolean>>;
234
262
  /** 降级为源码块的原因 */
235
- type SourceReason = "html" | "definition" | "unknown" | "list-context" | "depth-exceeded";
263
+ type SourceReason = "html" | "definition" | "unknown" | "list-context" | "depth-exceeded"
264
+ /** rawBody 容器(Mermaid):容器体是原始文本,编辑器只按源码块处理 */
265
+ | "mermaid";
236
266
  /** 可视化块(标题、段落、列表、表格、代码块等) */
237
267
  interface VisualBlock {
238
268
  kind: "visual";
@@ -294,6 +324,7 @@ declare function extractBlocks(tree: Root, source: string): Block[];
294
324
  * 渲染、序列化、编辑器块索引都基于这里产出的同一棵 mdast,避免重复解析。
295
325
  * @created 2026-10-24 20:24
296
326
  * @updated 2026-10-26 22:02
327
+ * @updated 2026-11-01 21:47 诊断链加入 collectMermaid(图表空源码 / 超限)
297
328
  */
298
329
 
299
330
  /** 解析选项 */
@@ -353,43 +384,4 @@ declare function roundtrip(md: string): string;
353
384
  */
354
385
  declare function normalizeMarkdown(md: string): string;
355
386
 
356
- /**
357
- * @file 渲染器(SSR 安全)
358
- * @description 一条 unified 管线:remark-parse(挂尖括号扩展)→ gfm / math → mdast-to-hast
359
- * (自定义 jsxBlock handler)→ slug / katex → React 元素。渲染与解析共用同一套扩展,
360
- * 保证"能解析的就能渲染"。
361
- * @created 2026-10-24 20:24
362
- * @updated 2026-10-24 20:24
363
- */
364
-
365
- /** 宿主可覆盖的组件类型 */
366
- type AnyComponent = ComponentType<Record<string, unknown>>;
367
- /** 渲染配置(只增不改,新增项必须有默认值) */
368
- interface RenderConfig {
369
- /** 单组件覆盖逃生舱;未覆盖的继续用包内默认实现 */
370
- components?: Partial<Record<string, AnyComponent>>;
371
- /** 宿主自定义 rehype 插件(须为同步插件,保证 SSR 安全) */
372
- rehypePlugins?: PluggableList;
373
- /** 宿主自定义 remark 插件(在 mdast → hast 之前执行) */
374
- remarkPlugins?: PluggableList;
375
- /**
376
- * 是否在块级元素上输出 `data-src-start`(源码起始 offset)
377
- * @default false
378
- * @description 仅供编辑器分屏做内容对齐的滚动同步;前台文章保持关闭以省体积
379
- */
380
- sourcePositions?: boolean;
381
- }
382
- /** 渲染器参数 */
383
- interface MarkdownRendererProps {
384
- content: string;
385
- className?: string;
386
- config?: RenderConfig;
387
- }
388
- /**
389
- * 渲染 Markdown 为 React 元素
390
- * @param props 内容、外层类名与配置
391
- * @returns 渲染结果
392
- */
393
- declare function MarkdownRenderer({ content, className, config, }: MarkdownRendererProps): ReactElement;
394
-
395
- export { type Attrs, type Block, COMPONENTS, type ComponentMode, type ComponentSpec, type ContainerBlock, type ContainerItem, ITEMS, type ItemSpec, type JsxBlock, MAX_CONTAINER_DEPTH, MarkdownRenderer, type MarkdownRendererProps, type ParseOptions, type ParseResult, type RenderConfig, type SourceBlock, type SourceReason, type VisualBlock, extractBlocks, getComponent, getItemSpec, isContainerWithItems, jsxBlocks, jsxFromMarkdown, jsxToMarkdown, listComponents, normalizeMarkdown, parse, parseOpenTag, remarkJsxBlocks, roundtrip, serialize };
387
+ export { type Attrs, type Block, COMPONENTS, type ComponentMode, type ComponentSpec, type ContainerBlock, type ContainerItem, type Diagnostic, type DiagnosticCode, ITEMS, type ItemSpec, type JsxBlock, MAX_CONTAINER_DEPTH, MERMAID_MAX_BYTES, type ParseOptions, type ParseResult, type SourceBlock, type SourceReason, type VisualBlock, extractBlocks, getComponent, getItemSpec, isContainerWithItems, jsxBlocks, jsxFromMarkdown, jsxToMarkdown, listComponents, normalizeMarkdown, parse, parseOpenTag, remarkJsxBlocks, roundtrip, serialize, sliceRawBody };
package/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
- import { jsxToMarkdown, parse } from './chunk-SUI5PVAV.js';
2
- export { COMPONENTS, ITEMS, MAX_CONTAINER_DEPTH, MarkdownRenderer, extractBlocks, getComponent, getItemSpec, isContainerWithItems, jsxBlocks, jsxFromMarkdown, jsxToMarkdown, listComponents, normalizeMarkdown, parse, parseOpenTag, remarkJsxBlocks } from './chunk-SUI5PVAV.js';
3
- export { mdxComponents } from './chunk-4TDQECWJ.js';
1
+ import { jsxToMarkdown, parse } from './chunk-7RXVCGFM.js';
2
+ export { COMPONENTS, ITEMS, MAX_CONTAINER_DEPTH, MERMAID_MAX_BYTES, MarkdownRenderer, extractBlocks, getComponent, getItemSpec, isContainerWithItems, jsxBlocks, jsxFromMarkdown, jsxToMarkdown, listComponents, normalizeMarkdown, parse, parseOpenTag, remarkJsxBlocks, sliceRawBody } from './chunk-7RXVCGFM.js';
3
+ export { MermaidPlaceholder, mdxComponents } from './chunk-V7T7I5HE.js';
4
4
  import { toMarkdown } from 'mdast-util-to-markdown';
5
5
  import { gfmToMarkdown } from 'mdast-util-gfm';
6
6
  import { mathToMarkdown } from 'mdast-util-math';
@@ -0,0 +1,207 @@
1
+ import { ReactElement, RefObject, ComponentType } from 'react';
2
+ import { a as MermaidProps } from '../mermaid-placeholder-A1N4puyc.js';
3
+
4
+ /**
5
+ * @file 交互式图表视图(客户端)
6
+ * @description 卡片结构:顶部工具栏 + 图表区(或源码区)+ 右下角悬浮缩放条。
7
+ * 未出图前一律显示源码——无 JS、加载失败、超限、空源码四种情况都不会白屏。
8
+ * @created 2026-11-01 21:30
9
+ * @updated 2026-11-01 22:29 改为「顶部工具栏 + 右下角缩放条」的卡片式布局
10
+ * @updated 2026-11-01 22:37 标题并入工具栏;暗色与取色按图表容器判定;手势仅在出图后启用
11
+ * @updated 2026-11-01 22:51 导出 PNG 底色改走 resolveChartTokens
12
+ * @updated 2026-11-01 23:18 height 未指定 / 空 / auto 一律自动高度
13
+ */
14
+
15
+ /**
16
+ * 交互式图表
17
+ * @param props 源码、标题、高度与超限标记(与占位骨架同一签名)
18
+ * @returns 卡片式图表视图
19
+ */
20
+ declare function MermaidChart({ raw, code, title, height, tooLarge }: MermaidProps): ReactElement;
21
+
22
+ /**
23
+ * @file 图表配色:设计令牌 → mermaid 主题变量(纯函数)
24
+ * @description 只做"字符串进、字符串出"的映射,不读 DOM、不引 React,便于单测与复用。
25
+ * 变量清单自宿主站点既有实现平移(含 ER 属性行、饼图 12 扇区、甘特任务条等派生型变量的
26
+ * 逐个点名覆盖)——这些变量不显式给值就会回落到 mermaid 预设的深色/白色,暗色下不可读。
27
+ * @created 2026-11-01 21:20
28
+ * @updated 2026-11-01 22:31 兜底色值改为薄荷系(与 styles/tokens.css 默认值一致)
29
+ * @updated 2026-11-01 22:37 兜底色值改为跟随宿主语义色(不再写死薄荷)
30
+ * @updated 2026-11-01 22:51 内置亮/暗两套配色 + configureChartTheme(零宿主依赖)
31
+ */
32
+ /** 图表可用的设计令牌(由调用方从 `--lkes-chart-*` 计算值取来) */
33
+ interface ChartTokens {
34
+ /** 图表底板 */
35
+ bg: string;
36
+ /** 图表文字 */
37
+ fg: string;
38
+ /** 节点 / 分支面 */
39
+ node: string;
40
+ /** 节点描边 */
41
+ nodeBorder: string;
42
+ /** 连线 */
43
+ line: string;
44
+ /** 序列色(至少 6 个,用于扇区与分支) */
45
+ series: readonly string[];
46
+ /** 正文字体栈(SVG 内文字不支持 inherit,必须给具体值) */
47
+ fontFamily: string;
48
+ }
49
+ /** mermaid 配置(不引 mermaid 类型,避免可选依赖在类型层也被强制安装) */
50
+ type MermaidConfigLike = Record<string, unknown>;
51
+ /** 内置亮色配色(引擎自带,不读宿主任何变量) */
52
+ declare const LIGHT_TOKENS: ChartTokens;
53
+ /** 内置暗色配色(引擎自带) */
54
+ declare const DARK_TOKENS: ChartTokens;
55
+ /** 亮暗两套配色 */
56
+ interface ChartTheme {
57
+ /** 亮色 */
58
+ light: ChartTokens;
59
+ /** 暗色 */
60
+ dark: ChartTokens;
61
+ }
62
+ /** 默认配色:安装后不配置也能直接用 */
63
+ declare const DEFAULT_CHART_THEME: ChartTheme;
64
+ /**
65
+ * 配置图表配色(安装时调一次即可)
66
+ * @param theme 只想改其中一项时只传那一项,其余沿用默认
67
+ * @returns 合并后的完整配色
68
+ * @description JS 侧配置优先级最高;配置后再改 CSS 变量不会影响图表 SVG 的取色。
69
+ */
70
+ declare function configureChartTheme(theme: Partial<ChartTheme>): ChartTheme;
71
+ /**
72
+ * 取当前生效的配色
73
+ * @returns 使用方配置的或内置默认的完整配色
74
+ */
75
+ declare function getChartTheme(): ChartTheme;
76
+ /**
77
+ * 是否已由使用方显式配置
78
+ * @returns 配置过返回 true
79
+ * @description 供取色逻辑决定"以 JS 配置为准"还是"读 CSS 变量"。
80
+ */
81
+ declare function hasCustomChartTheme(): boolean;
82
+ /**
83
+ * 复位为内置默认配色
84
+ * @returns 无
85
+ */
86
+ declare function resetChartTheme(): void;
87
+ /**
88
+ * 构建 mermaid `themeVariables`
89
+ * @param tokens 设计令牌取值
90
+ * @returns mermaid 主题变量表
91
+ */
92
+ declare function buildThemeVariables(tokens: ChartTokens): Record<string, string | boolean>;
93
+ /**
94
+ * 构建 mermaid 初始化配置
95
+ * @param tokens 设计令牌取值
96
+ * @returns `mermaid.initialize` 参数
97
+ * @description `securityLevel` 固定为 `strict`:禁用图表源码里的 HTML 标签与点击事件,
98
+ * 与宿主安全审计结论一致,不随调用方改变。
99
+ */
100
+ declare function buildMermaidConfig(tokens: ChartTokens): MermaidConfigLike;
101
+ /**
102
+ * 令牌指纹(用于渲染缓存键)
103
+ * @param tokens 设计令牌取值
104
+ * @returns 稳定字符串
105
+ */
106
+ declare function tokenFingerprint(tokens: ChartTokens): string;
107
+
108
+ /**
109
+ * @file 图表导出(SVG / PNG)
110
+ * @description 导出用的就是页面显示的那一份产物,不重新渲染,避免"看到的和下载的不一致"。
111
+ * PNG 空白的三条根因都在这里处理:净化后丢失 `xmlns`、仍挂着自适应宽度、文本走
112
+ * `foreignObject` 时 Canvas 不绘制(后者由 `securityLevel: strict` 关闭 HTML 标签解决)。
113
+ * @created 2026-11-01 21:28
114
+ * @updated 2026-11-01 21:28
115
+ */
116
+ /** 规范化后的产物 */
117
+ interface PreparedSvg {
118
+ /** 带命名空间、显式宽高的标记 */
119
+ markup: string;
120
+ width: number;
121
+ height: number;
122
+ }
123
+ /**
124
+ * 规范化 SVG 根元素
125
+ * @param source 净化后的 SVG 标记
126
+ * @returns 带命名空间与显式宽高的标记和尺寸
127
+ */
128
+ declare function prepareSvg(source: string): PreparedSvg;
129
+ /**
130
+ * 下载 SVG
131
+ * @param source 净化后的 SVG 标记
132
+ */
133
+ declare function downloadSvg(source: string): void;
134
+ /**
135
+ * 把 SVG 画到 Canvas 并转成 PNG
136
+ * @param source 净化后的 SVG 标记
137
+ * @param background 画布底色(与图表底板一致,透明底在部分看图软件里显示为黑)
138
+ * @returns PNG Blob
139
+ */
140
+ declare function svgToPngBlob(source: string, background: string): Promise<Blob>;
141
+ /**
142
+ * 下载 PNG
143
+ * @param source 净化后的 SVG 标记
144
+ * @param background 画布底色
145
+ */
146
+ declare function downloadPng(source: string, background: string): Promise<void>;
147
+
148
+ /**
149
+ * @file 图表渲染(客户端):懒加载、串行队列、唯一 id、超时、缓存、净化
150
+ * @description mermaid 是全局单例:`initialize` 改的是全局配置,并发渲染会互相覆盖主题;
151
+ * `render(id)` 又按 id 在文档里找节点,重复 id 会命中原节点(表现为拿到旧结果或图被删掉)。
152
+ * 因此本模块把渲染收敛成一条串行链 + 每次唯一 id,并把失败收敛成可展示的错误。
153
+ * @created 2026-11-01 21:24
154
+ * @updated 2026-11-01 21:24
155
+ * @updated 2026-11-01 22:37 令牌三级回落(引擎令牌→宿主语义变量→内置)+ oklch/hsl 归一;取色改按图表元素
156
+ * @updated 2026-11-01 22:51 取色改为「JS 配置 → 组件自带 --lkes-chart-* → 内置」,不再读宿主变量
157
+ */
158
+
159
+ /**
160
+ * 解析当前生效的图表配色
161
+ * @param dark 是否暗色
162
+ * @param element 图表元素(用于读取 CSS 层的 `--lkes-chart-*` 覆盖)
163
+ * @returns 该明暗下的完整配色
164
+ * @description 优先级:**JS 配置**(`configureChartTheme`)→ 包自带的 `--lkes-chart-*` → 内置默认。
165
+ * 全程不读宿主的 `--card` / `--foreground` / `--muted` 等语义变量:
166
+ * 装完即可用,宿主换主题也不会让图表串色。
167
+ */
168
+ declare function resolveChartTokens(dark: boolean, element?: HTMLElement | null): ChartTokens;
169
+
170
+ /**
171
+ * @file 站点明暗订阅(客户端)
172
+ * @description 判据是 `.dark` 类,但**不假设它只挂在 `<html>` 上**:站点主题可能挂在 body、
173
+ * 某个布局容器,甚至只在局部区域切换(开发台就是 `<div class="dark">`)。
174
+ * 因此按「从图表元素向上找最近的 `.dark`」判定,并订阅整棵树的 class 变化。
175
+ * @created 2026-11-01 21:22
176
+ * @updated 2026-11-01 22:37 支持容器级 `.dark`(不再只看 html),改用单例 MutationObserver
177
+ * @updated 2026-11-01 22:51 判据补 [data-theme="dark"]
178
+ */
179
+
180
+ /**
181
+ * 跟随站点明暗
182
+ * @param elementRef 图表容器(可选;不给则只看 html / body)
183
+ * @returns 当前是否为暗色
184
+ * @description 首帧返回 false 与服务端一致,避免水合不一致;挂载后立即按真实祖先链校正。
185
+ */
186
+ declare function useIsDark(elementRef?: RefObject<HTMLElement | null>): boolean;
187
+
188
+ /**
189
+ * @file `/mermaid` 入口:交互式图表
190
+ * @description 唯一允许接触 mermaid、dompurify 与 DOM 的层。主入口零引用,
191
+ * 不用图表的页面不承担这些体积;未引入本入口时 `<Mermaid>` 停留在源码骨架。
192
+ * 本文件只做导出,不执行任何副作用(不注册全局、不带 'use client' 之外的语句)。
193
+ * @created 2026-11-01 21:32
194
+ * @updated 2026-11-01 21:32
195
+ * @updated 2026-11-01 22:51 导出配色配置 API(configureChartTheme / getChartTheme / DEFAULT_CHART_THEME)
196
+ */
197
+
198
+ /**
199
+ * 使用方覆写表
200
+ * @example
201
+ * const components = { ...mdxComponents, ...mermaidComponents };
202
+ * @description 键必须与容器名完全一致(首字母大写):渲染层按 hast tagName 查表,
203
+ * 写成小写不会报错,只会静默停留在源码骨架。
204
+ */
205
+ declare const mermaidComponents: Record<string, ComponentType<MermaidProps>>;
206
+
207
+ export { type ChartTheme, type ChartTokens, DARK_TOKENS, DEFAULT_CHART_THEME, LIGHT_TOKENS, MermaidChart, type MermaidConfigLike, MermaidProps, buildMermaidConfig, buildThemeVariables, configureChartTheme, downloadPng, downloadSvg, getChartTheme, hasCustomChartTheme, mermaidComponents, prepareSvg, resetChartTheme, resolveChartTokens, svgToPngBlob, tokenFingerprint, useIsDark };