@heikeson/md-engine 0.0.0-stage → 0.2.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/LICENSE +21 -0
- package/README.md +330 -3
- package/dist/chunk-3GDOWIMO.js +855 -0
- package/dist/chunk-3GDOWIMO.js.map +1 -0
- package/dist/chunk-QDDS7MPU.js +502 -0
- package/dist/chunk-QDDS7MPU.js.map +1 -0
- package/dist/components/index.d.ts +217 -0
- package/dist/components/index.js +3 -0
- package/dist/components/index.js.map +1 -0
- package/dist/editor/index.d.ts +100 -0
- package/dist/editor/index.js +525 -0
- package/dist/editor/index.js.map +1 -0
- package/dist/index.d.ts +382 -0
- package/dist/index.js +29 -0
- package/dist/index.js.map +1 -0
- package/package.json +112 -4
- package/styles/engine.css +373 -0
- package//345/257/271/346/216/245/345/245/221/347/272/246.md +352 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,382 @@
|
|
|
1
|
+
import { Extension as Extension$1 } from 'micromark-util-types';
|
|
2
|
+
import { Parent, RootContent, Data, Root } from 'mdast';
|
|
3
|
+
import { Extension } from 'mdast-util-from-markdown';
|
|
4
|
+
import { Options } from 'mdast-util-to-markdown';
|
|
5
|
+
import { Processor, PluggableList } from 'unified';
|
|
6
|
+
import { ComponentType, ReactElement } from 'react';
|
|
7
|
+
export { mdxComponents } from './components/index.js';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* @file 内容语法注册表(唯一事实来源)
|
|
11
|
+
* @description 声明受支持的容器组件、子项、属性白名单与嵌套深度上限。
|
|
12
|
+
* 解析(micromark 扩展)、序列化(mdast handler)、编辑器块 UI 全部读取本文件,
|
|
13
|
+
* 禁止在别处再硬编码组件名或属性名。改动本文件即改动内容语法,须同步
|
|
14
|
+
* `docs/markdown/02-语法规范.md` 与引擎对接契约。
|
|
15
|
+
* @created 2026-10-24 20:07
|
|
16
|
+
* @updated 2026-10-24 20:07
|
|
17
|
+
*/
|
|
18
|
+
/** 组件的编辑形态:visual 允许结构化编辑;source 只按源码块保留 */
|
|
19
|
+
type ComponentMode = "visual" | "source";
|
|
20
|
+
/** 容器组件声明 */
|
|
21
|
+
interface ComponentSpec {
|
|
22
|
+
/** 组件名,首字母大写,区分大小写 */
|
|
23
|
+
readonly name: string;
|
|
24
|
+
/** 编辑形态 */
|
|
25
|
+
readonly mode: ComponentMode;
|
|
26
|
+
/** 子项组件名;单一子项传字符串,混用多种子项传数组(如 Files 的 Folder/File) */
|
|
27
|
+
readonly items?: string | readonly string[];
|
|
28
|
+
/** 允许的字符串属性名 */
|
|
29
|
+
readonly attrs?: readonly string[];
|
|
30
|
+
/** 允许的布尔属性名(无值写法,如 `default`) */
|
|
31
|
+
readonly boolAttrs?: readonly string[];
|
|
32
|
+
/** 叶子组件,允许自闭合写法(仅 Image) */
|
|
33
|
+
readonly selfClosing?: boolean;
|
|
34
|
+
/** 属性缺省值 */
|
|
35
|
+
readonly defaultAttrs?: Readonly<Record<string, string | boolean>>;
|
|
36
|
+
}
|
|
37
|
+
/** 子项声明(Tab / Step / Card / AccItem / Folder / File) */
|
|
38
|
+
interface ItemSpec {
|
|
39
|
+
readonly name: string;
|
|
40
|
+
readonly attrs?: readonly string[];
|
|
41
|
+
readonly boolAttrs?: readonly string[];
|
|
42
|
+
/** 叶子子项,允许自闭合写法(如 `<File name="x" />`) */
|
|
43
|
+
readonly selfClosing?: boolean;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* 容器嵌套深度上限。
|
|
47
|
+
* 只统计容器组件,子项(Tab/Step/Card/AccItem)不计层。
|
|
48
|
+
* 取 3 的依据:2 层已覆盖绝大多数写作场景,第 4 层起文本与 UI 均不可读。
|
|
49
|
+
*/
|
|
50
|
+
declare const MAX_CONTAINER_DEPTH = 3;
|
|
51
|
+
/** 子项声明表 */
|
|
52
|
+
declare const ITEMS: Readonly<Record<string, ItemSpec>>;
|
|
53
|
+
/** 容器组件声明表 */
|
|
54
|
+
declare const COMPONENTS: Readonly<Record<string, ComponentSpec>>;
|
|
55
|
+
/**
|
|
56
|
+
* 取容器组件声明
|
|
57
|
+
* @param name 组件名(区分大小写)
|
|
58
|
+
* @returns 声明;不在白名单内返回 undefined
|
|
59
|
+
*/
|
|
60
|
+
declare function getComponent(name: string): ComponentSpec | undefined;
|
|
61
|
+
/**
|
|
62
|
+
* 取子项声明
|
|
63
|
+
* @param name 子项组件名
|
|
64
|
+
* @returns 声明;不是子项返回 undefined
|
|
65
|
+
*/
|
|
66
|
+
declare function getItemSpec(name: string): ItemSpec | undefined;
|
|
67
|
+
/**
|
|
68
|
+
* 列出全部容器组件名(供编辑器菜单与校验使用)
|
|
69
|
+
* @returns 组件名数组
|
|
70
|
+
*/
|
|
71
|
+
declare function listComponents(): string[];
|
|
72
|
+
/**
|
|
73
|
+
* 判断是否为带子项的容器(Tabs / Steps / Cards / Accordion)
|
|
74
|
+
* @param name 组件名
|
|
75
|
+
* @returns 有子项声明返回 true
|
|
76
|
+
*/
|
|
77
|
+
declare function isContainerWithItems(name: string): boolean;
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* @file 尖括号块的 mdast 双向扩展
|
|
81
|
+
* @description 把词法层产出的 `jsxBlock` token 转成 mdast 节点(fromMarkdown),
|
|
82
|
+
* 以及把节点序列化回 `<Name attr="v">` 文本(toMarkdown)。两侧必须对称:
|
|
83
|
+
* `md → mdast → md` 要逐字一致。
|
|
84
|
+
* @created 2026-10-24 20:16
|
|
85
|
+
* @updated 2026-10-24 20:16
|
|
86
|
+
*/
|
|
87
|
+
|
|
88
|
+
/** 尖括号块节点 */
|
|
89
|
+
interface JsxBlock extends Parent {
|
|
90
|
+
type: "jsxBlock";
|
|
91
|
+
/** 组件名(首字母大写) */
|
|
92
|
+
name: string;
|
|
93
|
+
/** 属性表:字符串或布尔(true 表示无值写法) */
|
|
94
|
+
attributes: Record<string, string | boolean>;
|
|
95
|
+
/** 是否为自闭合叶子(`<Image />`) */
|
|
96
|
+
selfClosing: boolean;
|
|
97
|
+
children: RootContent[];
|
|
98
|
+
/** closed=false 表示未闭合到文件末尾(由 policy 决定是否降级) */
|
|
99
|
+
data?: Data & {
|
|
100
|
+
closed?: boolean;
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
/** 开标签解析结果 */
|
|
104
|
+
interface OpenTag {
|
|
105
|
+
name: string;
|
|
106
|
+
attributes: Record<string, string | boolean>;
|
|
107
|
+
selfClosing: boolean;
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* 解析开标签原文为名字、属性与自闭合标记
|
|
111
|
+
* @param raw 开标签原文,如 `<Card title="A" href="/x">`
|
|
112
|
+
* @returns 解析结果;格式异常时 name 为空串
|
|
113
|
+
*/
|
|
114
|
+
declare function parseOpenTag(raw: string): OpenTag;
|
|
115
|
+
/**
|
|
116
|
+
* 生成 fromMarkdown 扩展
|
|
117
|
+
* @returns 供 mdast-util-from-markdown 使用的扩展
|
|
118
|
+
*/
|
|
119
|
+
declare function jsxFromMarkdown(): Extension;
|
|
120
|
+
/**
|
|
121
|
+
* 生成 toMarkdown 扩展
|
|
122
|
+
* @returns 供 mdast-util-to-markdown 使用的扩展
|
|
123
|
+
*/
|
|
124
|
+
declare function jsxToMarkdown(): Options;
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* @file 自定义 token / 节点类型的模块扩展
|
|
128
|
+
* @description micromark 与 mdast 的类型表是封闭的,自定义构造必须通过 declaration merging 注册,
|
|
129
|
+
* 否则 `effects.enter("jsxBlock")` 与节点类型都不合法。这是 unified 生态规定的接入方式:
|
|
130
|
+
* - `micromark-util-types` 的 `TokenTypeMap`:注册 token 名
|
|
131
|
+
* - `mdast` 的 `RootContentMap` / `BlockContentMap`:注册节点类型
|
|
132
|
+
* `mdast-util-to-markdown` 的 `Handlers` 由 `Nodes['type']` 推导,因此无需单独扩展。
|
|
133
|
+
* @created 2026-10-24 20:22
|
|
134
|
+
* @updated 2026-10-24 20:22
|
|
135
|
+
*/
|
|
136
|
+
|
|
137
|
+
declare module "micromark-util-types" {
|
|
138
|
+
interface TokenTypeMap {
|
|
139
|
+
jsxBlock: "jsxBlock";
|
|
140
|
+
jsxBlockOpenTag: "jsxBlockOpenTag";
|
|
141
|
+
jsxBlockOpenTagMarker: "jsxBlockOpenTagMarker";
|
|
142
|
+
jsxBlockName: "jsxBlockName";
|
|
143
|
+
jsxBlockAttributes: "jsxBlockAttributes";
|
|
144
|
+
jsxBlockOpenTagEnd: "jsxBlockOpenTagEnd";
|
|
145
|
+
jsxBlockContent: "jsxBlockContent";
|
|
146
|
+
jsxBlockCloseTag: "jsxBlockCloseTag";
|
|
147
|
+
jsxBlockCloseTagMarker: "jsxBlockCloseTagMarker";
|
|
148
|
+
jsxBlockCloseName: "jsxBlockCloseName";
|
|
149
|
+
jsxBlockCloseTagEnd: "jsxBlockCloseTagEnd";
|
|
150
|
+
jsxBlockProbe: "jsxBlockProbe";
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
declare module "mdast" {
|
|
154
|
+
interface RootContentMap {
|
|
155
|
+
jsxBlock: JsxBlock;
|
|
156
|
+
}
|
|
157
|
+
interface BlockContentMap {
|
|
158
|
+
jsxBlock: JsxBlock;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* @file 尖括号块语法的 micromark 扩展(词法层)
|
|
164
|
+
* @description 在行首遇到 `<` 时尝试识别 `<Name ...>` 块容器,内容区以 `concrete` 子文档
|
|
165
|
+
* 递归走标准 Markdown 流,遇行首 `</Name>` 闭合。
|
|
166
|
+
* 之所以写在词法层:CommonMark 的 HTML block 规则要求标签前后有空行,且标签紧贴上一段时
|
|
167
|
+
* 开标签会被吞进段落;只有 flow 构造能绕开这些限制(详见 docs/markdown/03-引擎设计.md)。
|
|
168
|
+
* @created 2026-10-24 20:16
|
|
169
|
+
* @updated 2026-10-24 20:16
|
|
170
|
+
*/
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* 生成 micromark 扩展
|
|
174
|
+
* @returns flow 扩展,键为 `<` 的字符码
|
|
175
|
+
*/
|
|
176
|
+
declare function jsxBlocks(): Extension$1;
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* @file remark 插件封装(把尖括号扩展注入 unified 管线)
|
|
180
|
+
* @description `remark-parse` 的 options 不会透传 micromark 扩展,unified 生态的标准做法是
|
|
181
|
+
* 在插件里往 `data.micromarkExtensions` / `data.fromMarkdownExtensions` /
|
|
182
|
+
* `data.toMarkdownExtensions` 追加(`remark-gfm` 就是这么做的)。
|
|
183
|
+
* @created 2026-10-24 20:36
|
|
184
|
+
* @updated 2026-10-24 20:36
|
|
185
|
+
*/
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* 注册尖括号块的解析与序列化扩展
|
|
189
|
+
* @param this unified 处理器
|
|
190
|
+
* @returns 无
|
|
191
|
+
*/
|
|
192
|
+
declare function remarkJsxBlocks(this: Processor): undefined;
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* @file 策略层(纯函数,不依赖 React / DOM)
|
|
196
|
+
* @description 负责"不崩、不丢内容"的部分:嵌套深度裁剪、未闭合与超限诊断。
|
|
197
|
+
* 与渲染实现解耦——将来换渲染方式,这里不用动。
|
|
198
|
+
* @created 2026-10-24 20:24
|
|
199
|
+
* @updated 2026-10-24 20:24
|
|
200
|
+
*/
|
|
201
|
+
|
|
202
|
+
/** 诊断码 */
|
|
203
|
+
type DiagnosticCode = "UNCLOSED_TAG" | "DEPTH_EXCEEDED" | "UNKNOWN_TAG" | "UNSAFE_HREF";
|
|
204
|
+
/** 一条诊断 */
|
|
205
|
+
interface Diagnostic {
|
|
206
|
+
code: DiagnosticCode;
|
|
207
|
+
level: "warn" | "error";
|
|
208
|
+
/** 行号(1 起) */
|
|
209
|
+
line: number;
|
|
210
|
+
/** 中文提示 */
|
|
211
|
+
message: string;
|
|
212
|
+
/** 全文 offset 区间 */
|
|
213
|
+
range?: [number, number];
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* @file 块索引(供编辑器定位与出块 UI)
|
|
218
|
+
* @description 把 mdast 压平成带全文 offset 的块列表:容器块带子项、普通块只留类型与区间。
|
|
219
|
+
* 编辑器据此"点哪块改哪块",不需要自己再遍历一遍树。
|
|
220
|
+
* 区间一律是原文 offset(0 起,end 不含),可直接用于 `source.slice(start, end)`。
|
|
221
|
+
* @created 2026-10-24 21:06
|
|
222
|
+
* @updated 2026-10-24 21:06
|
|
223
|
+
*/
|
|
224
|
+
|
|
225
|
+
/** 容器属性表 */
|
|
226
|
+
type Attrs = Readonly<Record<string, string | boolean>>;
|
|
227
|
+
/** 降级为源码块的原因 */
|
|
228
|
+
type SourceReason = "html" | "definition" | "unknown" | "list-context" | "depth-exceeded";
|
|
229
|
+
/** 可视化块(标题、段落、列表、表格、代码块等) */
|
|
230
|
+
interface VisualBlock {
|
|
231
|
+
kind: "visual";
|
|
232
|
+
/** mdast 节点类型,如 heading / paragraph / table */
|
|
233
|
+
type: string;
|
|
234
|
+
start: number;
|
|
235
|
+
end: number;
|
|
236
|
+
}
|
|
237
|
+
/** 源码块(原样保留,不做可视化) */
|
|
238
|
+
interface SourceBlock {
|
|
239
|
+
kind: "source";
|
|
240
|
+
/** 原文片段 */
|
|
241
|
+
text: string;
|
|
242
|
+
reason: SourceReason;
|
|
243
|
+
start: number;
|
|
244
|
+
end: number;
|
|
245
|
+
}
|
|
246
|
+
/** 容器子项(Tab / Step / Card / AccItem) */
|
|
247
|
+
interface ContainerItem {
|
|
248
|
+
name: string;
|
|
249
|
+
attrs: Attrs;
|
|
250
|
+
blocks: Block[];
|
|
251
|
+
start: number;
|
|
252
|
+
end: number;
|
|
253
|
+
}
|
|
254
|
+
/** 容器块(Callout / Tabs / Steps / Cards / Accordion / FileTree / Image) */
|
|
255
|
+
interface ContainerBlock {
|
|
256
|
+
kind: "container";
|
|
257
|
+
name: string;
|
|
258
|
+
attrs: Attrs;
|
|
259
|
+
/** 容器深度,1 起;子项不计层 */
|
|
260
|
+
depth: number;
|
|
261
|
+
/** 直接内容块(Callout / FileTree 这类无子项的容器) */
|
|
262
|
+
blocks: Block[];
|
|
263
|
+
/** 子项(仅 Tabs / Steps / Cards / Accordion 有) */
|
|
264
|
+
items: ContainerItem[];
|
|
265
|
+
start: number;
|
|
266
|
+
end: number;
|
|
267
|
+
}
|
|
268
|
+
/** 块 */
|
|
269
|
+
type Block = VisualBlock | SourceBlock | ContainerBlock;
|
|
270
|
+
/**
|
|
271
|
+
* 从 mdast 提取块索引
|
|
272
|
+
* @param tree mdast 根节点(应在策略层裁剪之后)
|
|
273
|
+
* @param source 原始 Markdown
|
|
274
|
+
* @returns 顶层块列表
|
|
275
|
+
*/
|
|
276
|
+
declare function extractBlocks(tree: Root, source: string): Block[];
|
|
277
|
+
|
|
278
|
+
/**
|
|
279
|
+
* @file 唯一解析入口
|
|
280
|
+
* @description Markdown → mdast(已含尖括号块与数学公式节点),再跑策略层得到诊断。
|
|
281
|
+
* 渲染、序列化、编辑器块索引都基于这里产出的同一棵 mdast,避免重复解析。
|
|
282
|
+
* @created 2026-10-24 20:24
|
|
283
|
+
* @updated 2026-10-26 22:02
|
|
284
|
+
*/
|
|
285
|
+
|
|
286
|
+
/** 解析选项 */
|
|
287
|
+
interface ParseOptions {
|
|
288
|
+
/** 覆盖默认嵌套深度上限 */
|
|
289
|
+
maxDepth?: number;
|
|
290
|
+
}
|
|
291
|
+
/** 解析结果 */
|
|
292
|
+
interface ParseResult {
|
|
293
|
+
/** mdast 根节点(容器已是 jsxBlock 节点) */
|
|
294
|
+
tree: Root;
|
|
295
|
+
/** 扁平块索引,编辑器用于定位与出块 UI */
|
|
296
|
+
blocks: Block[];
|
|
297
|
+
/** 诊断列表,供编辑器提示 */
|
|
298
|
+
diagnostics: Diagnostic[];
|
|
299
|
+
}
|
|
300
|
+
/**
|
|
301
|
+
* 解析 Markdown 为 mdast 并执行策略层
|
|
302
|
+
* @param md Markdown 原文
|
|
303
|
+
* @param options 解析选项
|
|
304
|
+
* @returns mdast 与诊断
|
|
305
|
+
*/
|
|
306
|
+
declare function parse(md: string, options?: ParseOptions): ParseResult;
|
|
307
|
+
|
|
308
|
+
/**
|
|
309
|
+
* @file 序列化:mdast → Markdown
|
|
310
|
+
* @description 与解析对称:解析支持的写法,序列化必须能产出。GFM(表格/删除线/任务列表)、
|
|
311
|
+
* 数学公式与尖括号块各自注册 handler,空行由 handler 统一产出。
|
|
312
|
+
* @created 2026-10-24 20:30
|
|
313
|
+
* @updated 2026-10-26 22:02
|
|
314
|
+
*/
|
|
315
|
+
|
|
316
|
+
/**
|
|
317
|
+
* 把 mdast 序列化为 Markdown
|
|
318
|
+
* @param tree mdast 根节点
|
|
319
|
+
* @returns Markdown 文本
|
|
320
|
+
*/
|
|
321
|
+
declare function serialize(tree: Root): string;
|
|
322
|
+
/**
|
|
323
|
+
* 便捷往返:Markdown → mdast → Markdown
|
|
324
|
+
* @param md 原文
|
|
325
|
+
* @returns 序列化结果(用于验收 `md → AST → md` 是否一致)
|
|
326
|
+
*/
|
|
327
|
+
declare function roundtrip(md: string): string;
|
|
328
|
+
|
|
329
|
+
/**
|
|
330
|
+
* @file 排版规范化(不承担语法正确性)
|
|
331
|
+
* @description 尖括号块的解析不依赖空行,本函数只负责"看起来整齐":容器标签前后各留一个空行、
|
|
332
|
+
* 连续空行收敛为一个。围栏代码块内的内容一律跳过;幂等(跑两遍结果一致)。
|
|
333
|
+
* @created 2026-10-24 20:30
|
|
334
|
+
* @updated 2026-10-24 20:30
|
|
335
|
+
*/
|
|
336
|
+
/**
|
|
337
|
+
* 规范化 Markdown 排版
|
|
338
|
+
* @param md 原文
|
|
339
|
+
* @returns 规范化后的文本
|
|
340
|
+
*/
|
|
341
|
+
declare function normalizeMarkdown(md: string): string;
|
|
342
|
+
|
|
343
|
+
/**
|
|
344
|
+
* @file 渲染器(SSR 安全)
|
|
345
|
+
* @description 一条 unified 管线:remark-parse(挂尖括号扩展)→ gfm / math → mdast-to-hast
|
|
346
|
+
* (自定义 jsxBlock handler)→ slug / katex → React 元素。渲染与解析共用同一套扩展,
|
|
347
|
+
* 保证"能解析的就能渲染"。
|
|
348
|
+
* @created 2026-10-24 20:24
|
|
349
|
+
* @updated 2026-10-24 20:24
|
|
350
|
+
*/
|
|
351
|
+
|
|
352
|
+
/** 宿主可覆盖的组件类型 */
|
|
353
|
+
type AnyComponent = ComponentType<Record<string, unknown>>;
|
|
354
|
+
/** 渲染配置(只增不改,新增项必须有默认值) */
|
|
355
|
+
interface RenderConfig {
|
|
356
|
+
/** 单组件覆盖逃生舱;未覆盖的继续用包内默认实现 */
|
|
357
|
+
components?: Partial<Record<string, AnyComponent>>;
|
|
358
|
+
/** 宿主自定义 rehype 插件(须为同步插件,保证 SSR 安全) */
|
|
359
|
+
rehypePlugins?: PluggableList;
|
|
360
|
+
/** 宿主自定义 remark 插件(在 mdast → hast 之前执行) */
|
|
361
|
+
remarkPlugins?: PluggableList;
|
|
362
|
+
/**
|
|
363
|
+
* 是否在块级元素上输出 `data-src-start`(源码起始 offset)
|
|
364
|
+
* @default false
|
|
365
|
+
* @description 仅供编辑器分屏做内容对齐的滚动同步;前台文章保持关闭以省体积
|
|
366
|
+
*/
|
|
367
|
+
sourcePositions?: boolean;
|
|
368
|
+
}
|
|
369
|
+
/** 渲染器参数 */
|
|
370
|
+
interface MarkdownRendererProps {
|
|
371
|
+
content: string;
|
|
372
|
+
className?: string;
|
|
373
|
+
config?: RenderConfig;
|
|
374
|
+
}
|
|
375
|
+
/**
|
|
376
|
+
* 渲染 Markdown 为 React 元素
|
|
377
|
+
* @param props 内容、外层类名与配置
|
|
378
|
+
* @returns 渲染结果
|
|
379
|
+
*/
|
|
380
|
+
declare function MarkdownRenderer({ content, className, config, }: MarkdownRendererProps): ReactElement;
|
|
381
|
+
|
|
382
|
+
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 };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { jsxToMarkdown, parse } from './chunk-3GDOWIMO.js';
|
|
2
|
+
export { COMPONENTS, ITEMS, MAX_CONTAINER_DEPTH, MarkdownRenderer, extractBlocks, getComponent, getItemSpec, isContainerWithItems, jsxBlocks, jsxFromMarkdown, jsxToMarkdown, listComponents, normalizeMarkdown, parse, parseOpenTag, remarkJsxBlocks } from './chunk-3GDOWIMO.js';
|
|
3
|
+
export { mdxComponents } from './chunk-QDDS7MPU.js';
|
|
4
|
+
import { toMarkdown } from 'mdast-util-to-markdown';
|
|
5
|
+
import { gfmToMarkdown } from 'mdast-util-gfm';
|
|
6
|
+
import { mathToMarkdown } from 'mdast-util-math';
|
|
7
|
+
|
|
8
|
+
var OPTIONS = {
|
|
9
|
+
bullet: "-",
|
|
10
|
+
emphasis: "*",
|
|
11
|
+
strong: "*",
|
|
12
|
+
rule: "-",
|
|
13
|
+
fences: true,
|
|
14
|
+
listItemIndent: "one"
|
|
15
|
+
};
|
|
16
|
+
function serialize(tree) {
|
|
17
|
+
return toMarkdown(tree, {
|
|
18
|
+
...OPTIONS,
|
|
19
|
+
// mathToMarkdown 直接把公式原文写回 `$…$` / `$$…$$`,不再走文本转义
|
|
20
|
+
extensions: [gfmToMarkdown(), mathToMarkdown(), jsxToMarkdown()]
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
function roundtrip(md) {
|
|
24
|
+
return serialize(parse(md).tree);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export { roundtrip, serialize };
|
|
28
|
+
//# sourceMappingURL=index.js.map
|
|
29
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/serialize.ts"],"names":[],"mappings":";;;;;;;AAgBA,IAAM,OAAA,GAAU;AAAA,EACd,MAAA,EAAQ,GAAA;AAAA,EACR,QAAA,EAAU,GAAA;AAAA,EACV,MAAA,EAAQ,GAAA;AAAA,EACR,IAAA,EAAM,GAAA;AAAA,EACN,MAAA,EAAQ,IAAA;AAAA,EACR,cAAA,EAAgB;AAClB,CAAA;AAOO,SAAS,UAAU,IAAA,EAAoB;AAC5C,EAAA,OAAO,WAAW,IAAA,EAAM;AAAA,IACtB,GAAG,OAAA;AAAA;AAAA,IAEH,YAAY,CAAC,aAAA,IAAiB,cAAA,EAAe,EAAG,eAAe;AAAA,GAChE,CAAA;AACH;AAOO,SAAS,UAAU,EAAA,EAAoB;AAC5C,EAAA,OAAO,SAAA,CAAU,KAAA,CAAM,EAAE,CAAA,CAAE,IAAI,CAAA;AACjC","file":"index.js","sourcesContent":["/**\r\n * @file 序列化:mdast → Markdown\r\n * @description 与解析对称:解析支持的写法,序列化必须能产出。GFM(表格/删除线/任务列表)、\r\n * 数学公式与尖括号块各自注册 handler,空行由 handler 统一产出。\r\n * @created 2026-10-24 20:30\r\n * @updated 2026-10-26 22:02\r\n */\r\n\r\nimport { toMarkdown } from \"mdast-util-to-markdown\";\r\nimport { gfmToMarkdown } from \"mdast-util-gfm\";\r\nimport { mathToMarkdown } from \"mdast-util-math\";\r\nimport type { Root } from \"mdast\";\r\nimport { jsxToMarkdown } from \"./syntax/jsx-mdast\";\r\nimport { parse } from \"./parse\";\r\n\r\n/** 序列化选项:保持与项目既有风格一致(`-` 列表、`*` 强调、围栏代码块) */\r\nconst OPTIONS = {\r\n bullet: \"-\",\r\n emphasis: \"*\",\r\n strong: \"*\",\r\n rule: \"-\",\r\n fences: true,\r\n listItemIndent: \"one\",\r\n} as const;\r\n\r\n/**\r\n * 把 mdast 序列化为 Markdown\r\n * @param tree mdast 根节点\r\n * @returns Markdown 文本\r\n */\r\nexport function serialize(tree: Root): string {\r\n return toMarkdown(tree, {\r\n ...OPTIONS,\r\n // mathToMarkdown 直接把公式原文写回 `$…$` / `$$…$$`,不再走文本转义\r\n extensions: [gfmToMarkdown(), mathToMarkdown(), jsxToMarkdown()],\r\n });\r\n}\r\n\r\n/**\r\n * 便捷往返:Markdown → mdast → Markdown\r\n * @param md 原文\r\n * @returns 序列化结果(用于验收 `md → AST → md` 是否一致)\r\n */\r\nexport function roundtrip(md: string): string {\r\n return serialize(parse(md).tree);\r\n}\r\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,114 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@heikeson/md-engine",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"description": "
|
|
6
|
-
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "Markdown 内容引擎:尖括号容器语法(<Callout> 等)解析、渲染、序列化与编辑器内核,可直接消费 TS 源码",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"author": "heikeson",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/heikeson/md-engine.git"
|
|
11
|
+
},
|
|
12
|
+
"homepage": "https://github.com/heikeson/md-engine#readme",
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/heikeson/md-engine/issues"
|
|
15
|
+
},
|
|
16
|
+
"keywords": [
|
|
17
|
+
"markdown",
|
|
18
|
+
"micromark",
|
|
19
|
+
"mdast",
|
|
20
|
+
"unified",
|
|
21
|
+
"remark",
|
|
22
|
+
"codemirror",
|
|
23
|
+
"callout",
|
|
24
|
+
"fumadocs"
|
|
25
|
+
],
|
|
26
|
+
"sideEffects": [
|
|
27
|
+
"**/*.css"
|
|
28
|
+
],
|
|
29
|
+
"main": "./dist/index.js",
|
|
30
|
+
"types": "./dist/index.d.ts",
|
|
31
|
+
"exports": {
|
|
32
|
+
".": {
|
|
33
|
+
"types": "./dist/index.d.ts",
|
|
34
|
+
"import": "./dist/index.js"
|
|
35
|
+
},
|
|
36
|
+
"./editor": {
|
|
37
|
+
"types": "./dist/editor/index.d.ts",
|
|
38
|
+
"import": "./dist/editor/index.js"
|
|
39
|
+
},
|
|
40
|
+
"./components": {
|
|
41
|
+
"types": "./dist/components/index.d.ts",
|
|
42
|
+
"import": "./dist/components/index.js"
|
|
43
|
+
},
|
|
44
|
+
"./styles.css": "./styles/engine.css"
|
|
45
|
+
},
|
|
46
|
+
"files": [
|
|
47
|
+
"dist",
|
|
48
|
+
"styles",
|
|
49
|
+
"README.md",
|
|
50
|
+
"对接契约.md",
|
|
51
|
+
"LICENSE"
|
|
52
|
+
],
|
|
53
|
+
"scripts": {
|
|
54
|
+
"dev": "vite --config dev/vite.config.ts",
|
|
55
|
+
"build": "tsup",
|
|
56
|
+
"snapshot": "vite build --config dev/snapshot.vite.config.ts && vite build --config dev/snapshot.css.vite.config.ts && node dev/.out/snapshot.mjs",
|
|
57
|
+
"typecheck": "tsc --noEmit",
|
|
58
|
+
"prepublishOnly": "npm run build && npm run typecheck"
|
|
59
|
+
},
|
|
60
|
+
"dependencies": {
|
|
61
|
+
"@codemirror/commands": "^6.11.1",
|
|
62
|
+
"@codemirror/lang-markdown": "^6.5.2",
|
|
63
|
+
"@codemirror/language": "^6.13.1",
|
|
64
|
+
"@codemirror/state": "^6.7.6",
|
|
65
|
+
"@codemirror/view": "^6.43.15",
|
|
66
|
+
"@types/hast": "^3.0.5",
|
|
67
|
+
"@types/mdast": "4.0.4",
|
|
68
|
+
"clsx": "2.1.1",
|
|
69
|
+
"hast-util-to-jsx-runtime": "2.3.6",
|
|
70
|
+
"lucide-react": "^1.61.0",
|
|
71
|
+
"mdast-util-from-markdown": "2.0.3",
|
|
72
|
+
"mdast-util-gfm": "3.1.0",
|
|
73
|
+
"mdast-util-math": "3.0.0",
|
|
74
|
+
"mdast-util-to-hast": "13.2.1",
|
|
75
|
+
"mdast-util-to-markdown": "2.1.2",
|
|
76
|
+
"micromark-extension-gfm": "3.0.0",
|
|
77
|
+
"micromark-extension-math": "3.0.0",
|
|
78
|
+
"micromark-util-character": "2.1.1",
|
|
79
|
+
"micromark-util-types": "2.0.2",
|
|
80
|
+
"rehype-katex": "7.0.1",
|
|
81
|
+
"rehype-slug": "6.0.0",
|
|
82
|
+
"remark-gfm": "4.0.1",
|
|
83
|
+
"remark-math": "6.0.0",
|
|
84
|
+
"remark-parse": "11.0.0",
|
|
85
|
+
"remark-rehype": "11.1.2",
|
|
86
|
+
"tailwind-merge": "3.6.0",
|
|
87
|
+
"unified": "11.0.5",
|
|
88
|
+
"unist-util-visit": "5.1.0"
|
|
89
|
+
},
|
|
90
|
+
"peerDependencies": {
|
|
91
|
+
"react": ">=19",
|
|
92
|
+
"react-dom": ">=19"
|
|
93
|
+
},
|
|
94
|
+
"devDependencies": {
|
|
95
|
+
"@tailwindcss/vite": "^4.3.3",
|
|
96
|
+
"@types/node": "^26.6.5",
|
|
97
|
+
"@types/react": "^19.0.0",
|
|
98
|
+
"@types/react-dom": "^19.0.0",
|
|
99
|
+
"@vitejs/plugin-react": "^6.1.2",
|
|
100
|
+
"react": "^19.3.1",
|
|
101
|
+
"react-dom": "^19.3.1",
|
|
102
|
+
"tailwindcss": "^4.3.3",
|
|
103
|
+
"tsup": "^8.3.0",
|
|
104
|
+
"typescript": "^5.7.0",
|
|
105
|
+
"vite": "^8.3.5"
|
|
106
|
+
},
|
|
107
|
+
"engines": {
|
|
108
|
+
"node": ">=20"
|
|
109
|
+
},
|
|
110
|
+
"publishConfig": {
|
|
111
|
+
"access": "public",
|
|
112
|
+
"registry": "https://registry.npmjs.org/"
|
|
113
|
+
}
|
|
114
|
+
}
|