@heikeson/md-engine 0.0.0-stage → 0.1.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.
@@ -0,0 +1,342 @@
1
+ /**
2
+ * @file 内容组件样式(引擎包自带)
3
+ * @description 由宿主 `app/globals.css` 以 `@import "@heikeson/md-engine/styles.css"` 引入。
4
+ * 作用域挂在宿主正文容器 `.prose-site` 下(该类由宿主传给渲染器,属对外契约的一部分)。
5
+ * 颜色一律引用宿主主题变量(`border-border`、`text-muted-foreground` 等),包内不写死色值;
6
+ * 换肤只改宿主主题,组件样式随包版本升级。
7
+ * @created 2026-10-24 20:31
8
+ * @updated 2026-10-24 20:31
9
+ */
10
+
11
+ @layer components {
12
+ /* ---------- Callout 提示框 ---------- */
13
+ .prose-site .callout {
14
+ @apply my-5 rounded-lg border-l-4 px-4 py-3;
15
+ }
16
+ .prose-site .callout-title {
17
+ @apply mb-1 flex items-center gap-2 text-sm font-semibold;
18
+ }
19
+ .prose-site .callout-icon {
20
+ @apply size-4 shrink-0;
21
+ }
22
+ .prose-site .callout-body > :first-child {
23
+ @apply mt-0;
24
+ }
25
+ .prose-site .callout-body > :last-child {
26
+ @apply mb-0;
27
+ }
28
+ .prose-site .callout-note {
29
+ @apply border-blue-500 bg-blue-500/10 text-blue-700 dark:text-blue-300;
30
+ }
31
+ .prose-site .callout-tip {
32
+ @apply border-emerald-500 bg-emerald-500/10 text-emerald-700 dark:text-emerald-300;
33
+ }
34
+ .prose-site .callout-important {
35
+ @apply border-violet-500 bg-violet-500/10 text-violet-700 dark:text-violet-300;
36
+ }
37
+ .prose-site .callout-warning {
38
+ @apply border-amber-500 bg-amber-500/10 text-amber-700 dark:text-amber-300;
39
+ }
40
+ .prose-site .callout-caution {
41
+ @apply border-red-500 bg-red-500/10 text-red-700 dark:text-red-300;
42
+ }
43
+
44
+ /* ---------- Tabs 选项卡 ---------- */
45
+ .prose-site .tabs {
46
+ @apply my-5 overflow-hidden rounded-lg border border-border;
47
+ }
48
+ /* 标准下划线形态:无底色、无阴影 */
49
+ .prose-site .tabs-header {
50
+ @apply flex flex-wrap gap-5 border-b border-border px-0 py-0;
51
+ }
52
+ .prose-site .tab-btn {
53
+ @apply border-b-2 border-transparent px-1 py-2.5 text-sm font-medium text-muted-foreground transition-colors hover:text-foreground;
54
+ }
55
+ .prose-site .tab-btn.active {
56
+ @apply border-primary text-foreground;
57
+ }
58
+ .prose-site .tab-panel {
59
+ @apply p-4;
60
+ }
61
+ .prose-site .tab-panel[hidden] {
62
+ @apply hidden;
63
+ }
64
+
65
+ /* ---------- Accordion 折叠 ---------- */
66
+ .prose-site .accordion {
67
+ @apply my-5 overflow-hidden rounded-lg border border-border bg-background;
68
+ }
69
+ .prose-site .accordion-item {
70
+ @apply border-b border-border last:border-b-0;
71
+ }
72
+ /* 触发区:高度 ≥44px(触摸目标),键盘可达,hover 与展开态分层。
73
+ 标题用 semibold + 前景色,与正文(常规字重 + 弱化色)拉开层级。 */
74
+ .prose-site .accordion-trigger {
75
+ @apply flex w-full items-center justify-between gap-3 px-4 py-3.5 text-left text-sm font-semibold text-foreground transition-colors;
76
+ }
77
+ .prose-site .accordion-trigger:hover {
78
+ @apply bg-muted/60;
79
+ }
80
+ .prose-site .accordion-trigger:focus-visible {
81
+ @apply outline-none ring-2 ring-ring ring-inset;
82
+ }
83
+ /* 注意:Tailwind 4 的 rotate-180 生成独立属性 `rotate:`,用 transition-transform 不会过渡;
84
+ transition 的默认属性表包含 transform/translate/scale/rotate,故此处用 transition。 */
85
+ .prose-site .accordion-chevron {
86
+ @apply size-4 shrink-0 text-muted-foreground transition duration-200;
87
+ }
88
+ .prose-site .accordion-item.open .accordion-chevron {
89
+ @apply rotate-180 text-foreground;
90
+ }
91
+ /* 展开项标记:用 inset 阴影画一条贯穿「标题 + 内容」的主色条。
92
+ 不用 border-l,因为 border 会挤占 padding 导致该项文字与其它项错位。 */
93
+ .prose-site .accordion-item.open {
94
+ box-shadow: inset 2px 0 0 var(--color-primary, currentColor);
95
+ }
96
+ /* 展开动画:grid 0fr → 1fr,无需测量高度 */
97
+ .prose-site .accordion-panel {
98
+ display: grid;
99
+ grid-template-rows: 0fr;
100
+ transition: grid-template-rows 220ms cubic-bezier(0.4, 0, 0.2, 1);
101
+ }
102
+ .prose-site .accordion-panel[data-open="true"] {
103
+ grid-template-rows: 1fr;
104
+ }
105
+ .prose-site .accordion-panel-inner {
106
+ overflow: hidden;
107
+ min-height: 0;
108
+ }
109
+ /* 正文与标题的区分:弱化文字色 + 浅底 + 顶部留白。
110
+ 收起时 inner 被 grid 压扁并裁剪,padding 不会撑出高度。 */
111
+ .prose-site .accordion-content {
112
+ @apply bg-muted/30 px-4 pt-3 pb-4 text-sm leading-relaxed text-muted-foreground;
113
+ }
114
+ .prose-site .accordion-content > :first-child {
115
+ @apply mt-0;
116
+ }
117
+ .prose-site .accordion-content > :last-child {
118
+ @apply mb-0;
119
+ }
120
+ /* 偏好减少动画时直接切换,不做过渡 */
121
+ @media (prefers-reduced-motion: reduce) {
122
+ .prose-site .accordion-panel,
123
+ .prose-site .accordion-chevron {
124
+ transition: none;
125
+ }
126
+ }
127
+
128
+ /* ---------- Image 图片(对齐 / 尺寸 / 题注) ---------- */
129
+ .prose-site .image {
130
+ @apply my-5;
131
+ }
132
+ .prose-site .image.align-left {
133
+ @apply mr-auto;
134
+ }
135
+ .prose-site .image.align-center {
136
+ @apply mx-auto text-center;
137
+ }
138
+ .prose-site .image.align-right {
139
+ @apply ml-auto text-right;
140
+ }
141
+ .prose-site .image-img {
142
+ /* 限制最大宽度:窄屏下宽图不撑破正文容器 */
143
+ @apply inline-block max-w-full rounded-lg;
144
+ height: auto;
145
+ }
146
+ .prose-site .image figcaption {
147
+ @apply mt-2 text-center text-sm text-muted-foreground;
148
+ }
149
+ /* 图片放大:点击图片全屏查看,点遮罩或 Esc 关闭 */
150
+ .prose-site .image-img-zoomable {
151
+ @apply cursor-zoom-in;
152
+ }
153
+ .prose-site .image-zoom-mask {
154
+ @apply fixed inset-0 z-50 flex items-center justify-center bg-black/80 p-4;
155
+ cursor: zoom-out;
156
+ }
157
+ .prose-site .image-zoom-img {
158
+ @apply max-h-full max-w-full rounded-lg object-contain;
159
+ }
160
+
161
+ /* ---------- TypeTable 类型表 ---------- */
162
+ .prose-site .type-table {
163
+ @apply my-5 overflow-hidden rounded-lg border border-border;
164
+ }
165
+ .prose-site .type-table table {
166
+ @apply w-full border-collapse text-sm;
167
+ }
168
+ .prose-site .type-table thead th {
169
+ @apply border-b border-border bg-muted/40 px-3 py-2 text-left font-medium text-foreground;
170
+ }
171
+ .prose-site .type-table tbody td {
172
+ @apply border-b border-border px-3 py-2 align-top;
173
+ }
174
+ .prose-site .type-table tbody tr:last-child td {
175
+ @apply border-b-0;
176
+ }
177
+ .prose-site .type-table code {
178
+ @apply rounded bg-muted px-1.5 py-0.5 text-[13px];
179
+ }
180
+ .prose-site .type-cell-name code {
181
+ @apply font-semibold text-foreground;
182
+ }
183
+ .prose-site .type-cell-default,
184
+ .prose-site .type-cell-desc {
185
+ @apply text-muted-foreground;
186
+ }
187
+ .prose-site .type-required {
188
+ @apply ml-2 rounded bg-red-500/10 px-1.5 py-0.5 text-[11px] text-red-600 dark:text-red-400;
189
+ }
190
+
191
+ /* ---------- Steps 步骤 ---------- */
192
+ .prose-site .steps {
193
+ @apply my-5;
194
+ }
195
+ .prose-site .step {
196
+ @apply relative flex gap-3 pb-5 last:pb-0;
197
+ }
198
+ .prose-site .step::before {
199
+ content: "";
200
+ @apply absolute left-3 top-7 bottom-0 w-px bg-border;
201
+ }
202
+ .prose-site .step:last-child::before {
203
+ @apply hidden;
204
+ }
205
+ .prose-site .step-marker {
206
+ @apply z-10 flex size-6 shrink-0 items-center justify-center rounded-full border border-border bg-background text-xs font-semibold;
207
+ }
208
+ .prose-site .step-body {
209
+ @apply flex-1 min-w-0;
210
+ }
211
+ .prose-site .step-title {
212
+ @apply mb-1 font-semibold;
213
+ }
214
+ .prose-site .step-content > :first-child {
215
+ @apply mt-0;
216
+ }
217
+ .prose-site .step-content > :last-child {
218
+ @apply mb-0;
219
+ }
220
+
221
+ /* ---------- Cards 卡片 ---------- */
222
+ .prose-site .cards {
223
+ @apply my-5 grid gap-3 sm:grid-cols-2;
224
+ }
225
+ .prose-site .card {
226
+ @apply block rounded-lg border border-border p-4 transition-colors;
227
+ }
228
+ .prose-site .card-link {
229
+ @apply hover:border-primary hover:bg-muted/50;
230
+ }
231
+ .prose-site .card-title {
232
+ @apply mb-1 font-semibold;
233
+ overflow-wrap: break-word;
234
+ }
235
+ /* 卡片图标(短文本 / emoji) */
236
+ .prose-site .card-icon {
237
+ @apply mr-1.5;
238
+ }
239
+ .prose-site .card-body {
240
+ @apply text-sm text-muted-foreground;
241
+ overflow-wrap: break-word;
242
+ }
243
+ .prose-site .card-body > :first-child {
244
+ @apply mt-0;
245
+ }
246
+ .prose-site .card-body > :last-child {
247
+ @apply mb-0;
248
+ }
249
+
250
+ /* ---------- Files 文件树(Files / Folder / File,原生 details 折叠) ---------- */
251
+ .prose-site .files {
252
+ @apply my-5 rounded-lg border border-border bg-muted/20 p-2 text-sm;
253
+ }
254
+ .prose-site .files-row {
255
+ @apply flex items-center gap-2 rounded-md px-2 py-1 text-muted-foreground;
256
+ }
257
+ .prose-site .files-folder > summary {
258
+ @apply cursor-pointer list-none transition-colors hover:bg-muted/60 hover:text-foreground;
259
+ }
260
+ .prose-site .files-folder > summary::-webkit-details-marker {
261
+ display: none;
262
+ }
263
+ /* Tailwind 4 的 rotate-90 是独立属性,过渡需用 transition 而非 transition-transform */
264
+ .prose-site .files-chevron {
265
+ @apply inline-flex shrink-0 transition duration-200;
266
+ }
267
+ .prose-site .files-folder[open] > summary .files-chevron {
268
+ @apply rotate-90;
269
+ }
270
+ /* 嵌套层级用左侧缩进线表示 */
271
+ .prose-site .files-children {
272
+ @apply ml-3 border-l border-border/70 pl-2;
273
+ }
274
+ .prose-site .files-icon {
275
+ @apply size-4 shrink-0;
276
+ }
277
+ .prose-site .files-name {
278
+ @apply truncate;
279
+ }
280
+ .prose-site .files-folder-name {
281
+ @apply font-medium text-foreground;
282
+ }
283
+ .prose-site .files-count {
284
+ @apply ml-auto text-[11px] tabular-nums text-muted-foreground;
285
+ }
286
+ .prose-site .files-file-row {
287
+ @apply transition-colors hover:bg-muted/60 hover:text-foreground;
288
+ }
289
+ /* 文件行无箭头,补等宽占位使其与目录名对齐 */
290
+ .prose-site .files-spacer {
291
+ @apply inline-block w-[10px] shrink-0;
292
+ }
293
+
294
+ /* ---------- 编辑器(后台,不套 .prose-site) ---------- */
295
+ .md-editor {
296
+ @apply flex h-full min-h-0 flex-col rounded-lg border border-border bg-background;
297
+ }
298
+ .md-editor-toolbar {
299
+ @apply flex flex-wrap items-center gap-1 border-b border-border px-2 py-1.5;
300
+ }
301
+ .md-editor-toolbar button {
302
+ @apply rounded px-2 py-1 text-xs text-muted-foreground transition-colors hover:bg-muted hover:text-foreground;
303
+ min-height: 28px;
304
+ }
305
+ .md-editor-toolbar button:disabled {
306
+ @apply cursor-not-allowed opacity-40;
307
+ }
308
+ .md-editor-sep {
309
+ @apply mx-1 h-4 w-px bg-border;
310
+ }
311
+ /* 工具栏右侧推到底:视图切换与全屏 */
312
+ .md-editor-grow {
313
+ @apply flex-1;
314
+ }
315
+ .md-editor-views {
316
+ @apply flex items-center gap-0.5 rounded-md bg-muted p-0.5;
317
+ }
318
+ .md-editor-views button {
319
+ @apply rounded px-2 py-0.5 text-xs text-muted-foreground transition-colors hover:text-foreground;
320
+ min-height: 24px;
321
+ }
322
+ .md-editor-views button[data-active="true"] {
323
+ @apply bg-background text-foreground shadow-sm;
324
+ }
325
+ .md-editor-body {
326
+ @apply flex min-h-0 flex-1;
327
+ }
328
+ /* 编辑区容器:交给 CodeMirror 自己排版(内边距与字体在 THEME 里定义) */
329
+ .md-editor-source {
330
+ @apply min-h-0 flex-1 overflow-hidden;
331
+ }
332
+ /* relative:滚动同步用 el.offsetTop,必须以预览容器作为定位父级,否则 top 会偏到外层 */
333
+ .md-editor-preview {
334
+ @apply relative min-h-0 flex-1 overflow-y-auto border-l border-border p-4;
335
+ }
336
+ .md-editor-status {
337
+ @apply flex items-center gap-3 border-t border-border px-3 py-1 text-xs text-muted-foreground;
338
+ }
339
+ .md-editor-status-warn {
340
+ @apply text-amber-600 dark:text-amber-400;
341
+ }
342
+ }
@@ -0,0 +1,348 @@
1
+ # 对接契约
2
+
3
+ > @file 对接契约.md
4
+ > @description `@heikeson/md-engine` 的对外接口契约:入口、类型签名、数据契约、降级矩阵、宿主接入步骤与兼容规则。
5
+ > @created 2026-10-24 19:57
6
+ > @updated 2026-10-24 20:03
7
+ > @status 规格已定·未开工
8
+
9
+ 本文件是引擎与宿主之间的**唯一接口依据**。实现可以变,签名与行为不得随意变。
10
+
11
+ ---
12
+
13
+ ## 1. 三条契约约束
14
+
15
+ | # | 约束 | 具体含义 | 违反即破坏性变更 |
16
+ |---|---|---|---|
17
+ | C1 | 内容语法只增不改 | 组件只能新增;不改名、不删除、不改属性语义、不改子项名 | 宿主历史文章渲染崩 |
18
+ | C2 | 配置 API 只增不改 | 新增配置项必须有默认值;已有字段不删、不改语义 | 宿主升级后编译失败或行为突变 |
19
+ | C3 | 样式随包走 | 组件样式由包提供,宿主只覆盖 CSS 变量 | 宿主重建遗漏则掉样式 |
20
+
21
+ 宿主明确**不做内容转换**,因此 C1 是不可协商项:这个包不允许出现 breaking change。
22
+
23
+ ---
24
+
25
+ ## 2. 入口
26
+
27
+ ```json
28
+ {
29
+ "name": "@heikeson/md-engine",
30
+ "exports": {
31
+ ".": "./src/index.ts",
32
+ "./editor": "./src/editor/index.ts",
33
+ "./styles.css": "./src/styles/engine.css"
34
+ },
35
+ "peerDependencies": { "react": ">=19", "react-dom": ">=19" }
36
+ }
37
+ ```
38
+
39
+ | 入口 | 内容 | 约束 |
40
+ |---|---|---|
41
+ | `@heikeson/md-engine` | `parse`、`serialize`、`normalizeMarkdown`、`MarkdownRenderer`、`registry` 常量、类型 | **SSR 安全**:不得在顶层访问 `window`/`document` |
42
+ | `@heikeson/md-engine/editor` | `MarkdownEditor` 及相关组件 | 文件头 `"use client"`;不得从主入口 re-export |
43
+ | `@heikeson/md-engine/styles.css` | 语义类与 CSS 变量默认值 | 宿主只 `@import`,不修改包内类名 |
44
+
45
+ ---
46
+
47
+ ## 3. registry
48
+
49
+ ```ts
50
+ export type ComponentMode = "visual" | "source";
51
+
52
+ export interface ComponentSpec {
53
+ /** 组件名,首字母大写,区分大小写 */
54
+ name: string;
55
+ /** 视觉即语义判定的结果:visual 可结构化编辑,source 只按源码块处理 */
56
+ mode: ComponentMode;
57
+ /** 子项组件名;无子项则不填 */
58
+ items?: string;
59
+ /** 允许的属性名(值为字符串) */
60
+ attrs?: readonly string[];
61
+ /** 允许的布尔属性名(无值形式) */
62
+ boolAttrs?: readonly string[];
63
+ /** 子项允许的属性名 */
64
+ itemAttrs?: readonly string[];
65
+ /** 是否叶子组件(允许自闭合,如 <Image />) */
66
+ selfClosing?: boolean;
67
+ /** 属性缺省值 */
68
+ defaultAttrs?: Readonly<Record<string, string | boolean>>;
69
+ }
70
+
71
+ /** 容器嵌套深度上限(只计容器,子项不计层) */
72
+ export const MAX_CONTAINER_DEPTH = 3;
73
+
74
+ export const COMPONENTS: Readonly<Record<string, ComponentSpec>>;
75
+
76
+ /** 全部受支持组件名,供编辑器与校验使用 */
77
+ export function listComponents(): string[];
78
+ ```
79
+
80
+ 当前注册内容(与宿主 `02-语法规范.md` 一致,改动须同步):
81
+
82
+ | name | mode | items | attrs |
83
+ |---|---|---|---|
84
+ | `Callout` | visual | — | `type`、`title` |
85
+ | `Tabs` | visual | `Tab` | — |
86
+ | `Steps` | visual | `Step` | — |
87
+ | `Cards` | visual | `Card` | — |
88
+ | `Accordion` | visual | `AccItem` | — |
89
+ | `Files` | visual | `Folder` / `File` | — |
90
+ | `Folder` | visual | `Folder` / `File` | `name`、布尔 `defaultOpen` |
91
+ | `File` | visual | — | `name`、`href`(`selfClosing: true`) |
92
+ | `TypeTable` | visual | `TypeItem` | — |
93
+ | `TypeItem` | visual | — | `name`、`type`、`default`、布尔 `required` |
94
+ | `Image` | visual | — | `src`、`alt`、`align`、`width`、`caption`(`selfClosing: true`) |
95
+
96
+ 子项属性:`Tab` → `value`(布尔 `default`);`Step` / `Card` / `AccItem` → `title`;`Card` 另有 `href`、`icon`;`AccItem` 另有 `type`(`open` 默认展开 / `close` 或省略默认收起,`#id` 深链命中优先于 `type`)。
97
+
98
+ ---
99
+
100
+ ## 4. parse
101
+
102
+ ```ts
103
+ export interface ParseOptions {
104
+ /** 覆盖默认深度上限,缺省用 MAX_CONTAINER_DEPTH */
105
+ maxDepth?: number;
106
+ }
107
+
108
+ export interface ParseResult {
109
+ tree: Root; // mdast,容器已转为自定义节点
110
+ blocks: Block[]; // 扁平块索引(按出现顺序)
111
+ diagnostics: Diagnostic[];
112
+ }
113
+
114
+ export function parse(md: string, options?: ParseOptions): ParseResult;
115
+ ```
116
+
117
+ ### 4.1 Block
118
+
119
+ ```ts
120
+ export type SourceReason = "html" | "definition" | "unknown" | "list-context" | "depth-exceeded";
121
+
122
+ export interface Attrs { readonly [key: string]: string | boolean }
123
+
124
+ export type Block =
125
+ | { kind: "visual"; type: string; node: MdastNode; start: number; end: number }
126
+ | { kind: "source"; text: string; reason: SourceReason; start: number; end: number }
127
+ | {
128
+ kind: "container";
129
+ name: string;
130
+ attrs: Attrs;
131
+ depth: number; // 1 起
132
+ items: ContainerItem[];
133
+ start: number; // 开标签起始 offset
134
+ end: number; // 闭标签结束 offset
135
+ };
136
+
137
+ export interface ContainerItem {
138
+ name: string;
139
+ attrs: Attrs;
140
+ blocks: Block[];
141
+ start: number;
142
+ end: number;
143
+ }
144
+ ```
145
+
146
+ ### 4.2 Diagnostic
147
+
148
+ ```ts
149
+ export type DiagnosticCode =
150
+ | "UNCLOSED_TAG"
151
+ | "DEPTH_EXCEEDED"
152
+ | "UNKNOWN_TAG"
153
+ | "UNSAFE_HREF";
154
+
155
+ export interface Diagnostic {
156
+ code: DiagnosticCode;
157
+ level: "warn" | "error";
158
+ line: number; // 1 起
159
+ message: string; // 中文
160
+ range?: [number, number]; // 全文 offset 区间
161
+ }
162
+ ```
163
+
164
+ ### 4.3 position 规则(硬要求)
165
+
166
+ 1. 所有 `start`/`end` 为**全文 offset**(0 起,`end` 不含)。
167
+ 2. 容器节点的 `start` = 开标签首字符,`end` = 闭标签末字符 + 1。
168
+ 3. offset 必须由 tokenizer **原生产出**;禁止"先粗解析、再按片段二次解析并累加偏移"的实现方式。
169
+ 4. 自检:`容器节点数 === 原文开标签数`,不等即抛内部错误(开发环境)或降级(生产)。
170
+
171
+ ### 4.4 实现要求:必须用 micromark 扩展
172
+
173
+ | 要求 | 说明 |
174
+ |---|---|
175
+ | 语法层必须是 micromark flow 扩展 | `flow: { [60]: jsxBlock }`(`60` 即 `<`),`concrete: true`,内容区递归走标准 Markdown 流 |
176
+ | 禁止用 html 节点拼装 | 那会退回 CommonMark HTML block 规则,空行重新变成硬边界,且标签紧贴段落时开标签会被吞进 paragraph |
177
+ | 名字不在 registry 时立即 `nok` | 交回内置 html flow,`<div>` 等按原规则处理,不受影响 |
178
+ | 闭合按名字就近匹配 | 同名嵌套用名字栈;未闭合到文件末尾按"未闭合"降级,内容不吞 |
179
+ | 参照实现 | `node_modules/micromark-extension-directive/lib/directive-container.js`(开序列 → 名字 → 属性 → 内容区 → 闭合) |
180
+
181
+ 结论:**语法不要求空行**。空行只影响排版可读性,解析结果与有无空行无关。
182
+
183
+ ### 4.5 解析行为
184
+
185
+ | 输入 | 行为 |
186
+ |---|---|
187
+ | `<Name>` 与 `</Name>` 各占一行 | 正常建容器节点(**不要求空行**) |
188
+ | 开标签紧贴上一段文字 | 正常建容器节点(flow 构造可打断段落) |
189
+ | 标签之间无空行 | 正常解析(`concrete` 内容区走标准 Markdown 流) |
190
+ | 同一行写 `<Callout>正文</Callout>` | 按行内文本处理(不支持) |
191
+ | 未闭合 | 该标签按文本输出,内容不吞;产出 `UNCLOSED_TAG` |
192
+ | 白名单外标签 | 交回内置 html flow,按文本显示;产出 `UNKNOWN_TAG` |
193
+ | 嵌套超限 | 超限层整块 `source`(reason `depth-exceeded`);产出 `DEPTH_EXCEEDED` |
194
+ | 写在列表项内的容器 | `source`(reason `list-context`) |
195
+
196
+ ---
197
+
198
+ ## 5. render
199
+
200
+ ```tsx
201
+ export interface RenderConfig {
202
+ /** 正文链接是否新窗口打开,默认 false */
203
+ linksOpenInNewTab?: boolean;
204
+ /** 代码块风格键,默认 "default" */
205
+ codeBlockStyle?: string;
206
+ /** 链接重写(宿主外链中转),默认恒等 */
207
+ rewriteHref?: (href: string) => string;
208
+ /** 单组件覆盖逃生舱;未覆盖的继续用包内默认实现 */
209
+ components?: Partial<Record<string, React.ComponentType<Record<string, unknown>>>>;
210
+ }
211
+
212
+ export interface MarkdownRendererProps {
213
+ content: string;
214
+ className?: string;
215
+ config?: RenderConfig;
216
+ /** 可选:把诊断回传给调用方(编辑器预览用) */
217
+ onDiagnostics?: (diagnostics: Diagnostic[]) => void;
218
+ }
219
+
220
+ export function MarkdownRenderer(props: MarkdownRendererProps): React.ReactElement;
221
+ ```
222
+
223
+ 规则:
224
+
225
+ 1. 属性 → 组件 props 原样传递(属性名即 props 名,不做驼峰转换)。
226
+ 2. `href` / `src` 必须过协议白名单(`http(s)`、`mailto`、`tel`、站内相对路径);不合规时链接降级为不可点击并产出 `UNSAFE_HREF`。
227
+ 3. 组件内部样式只用包内语义类,不得依赖宿主 Tailwind 类。
228
+ 4. 代码高亮(shiki)与图表(mermaid)必须动态加载,不进首屏。
229
+ 5. `onDiagnostics` 只在客户端渲染时可用(SSR 阶段忽略)。
230
+
231
+ ---
232
+
233
+ ## 6. serialize 与 format
234
+
235
+ ```ts
236
+ /** mdast → Markdown;容器节点由 handler 输出,空行由 handler 统一产出 */
237
+ export function serialize(tree: Root): string;
238
+
239
+ /** 补空行、收敛空行、统一引号;跳过围栏代码与行内代码;幂等 */
240
+ export function normalizeMarkdown(md: string): string;
241
+
242
+ /** 便捷验收:md → AST → md */
243
+ export function roundtrip(md: string): string;
244
+ ```
245
+
246
+ 保证:
247
+
248
+ 1. `roundtrip(md) === md`(对受支持的规范写法)。
249
+ 2. `normalizeMarkdown(normalizeMarkdown(x)) === normalizeMarkdown(x)`。
250
+ 3. 容器输出形态固定:开标签行、空行、内容、空行、闭标签行;顶格不缩进。
251
+ 4. 空行数量收敛为 1,不产生连续空行。
252
+
253
+ ---
254
+
255
+ ## 7. editor
256
+
257
+ ```tsx
258
+ export interface EditorConfig {
259
+ /** 图片上传落盘,返回可访问 URL;缺省时不支持粘贴上传 */
260
+ uploadImage?: (file: File) => Promise<string>;
261
+ /** 预览侧渲染配置 */
262
+ rendererConfig?: RenderConfig;
263
+ placeholder?: string;
264
+ /** 只读 */
265
+ readOnly?: boolean;
266
+ }
267
+
268
+ export interface MarkdownEditorProps {
269
+ value: string;
270
+ onChange: (value: string) => void;
271
+ /** 视图:默认 split */
272
+ view?: "split" | "source" | "preview";
273
+ onViewChange?: (view: "split" | "source" | "preview") => void;
274
+ config?: EditorConfig;
275
+ }
276
+
277
+ export function MarkdownEditor(props: MarkdownEditorProps): React.ReactElement;
278
+ ```
279
+
280
+ 规则:
281
+
282
+ 1. `onChange` 返回的永远是 Markdown 原文,已过 `normalizeMarkdown`。
283
+ 2. 编辑器内部只改文本,不产生 HTML。
284
+ 3. 块属性面板字段由 `registry` 生成,不硬编码表单。
285
+ 4. 诊断在状态栏展示,不弹窗、不阻断输入。
286
+
287
+ ---
288
+
289
+ ## 8. 宿主接入步骤
290
+
291
+ 1. 根 `package.json` 增加 `workspaces: ["packages/*"]`,依赖写 `"@heikeson/md-engine": "*"`。
292
+ 2. 全局样式加 `@import "@heikeson/md-engine/styles.css";`,需要换肤时覆盖第 9 节变量。
293
+ 3. 前台渲染替换:
294
+
295
+ ```tsx
296
+ import { MarkdownRenderer } from "@heikeson/md-engine";
297
+
298
+ <MarkdownRenderer content={post.content} config={{ linksOpenInNewTab, codeBlockStyle, rewriteHref }} />
299
+ ```
300
+
301
+ 4. 后台编辑替换:
302
+
303
+ ```tsx
304
+ import { MarkdownEditor } from "@heikeson/md-engine/editor";
305
+
306
+ <MarkdownEditor value={value} onChange={setValue} config={{ uploadImage }} />
307
+ ```
308
+
309
+ 5. 服务端写入前:`content = normalizeMarkdown(content)`。
310
+
311
+ 无需配置 `transpilePackages`(Turbopack 自动转译 workspace 包);若 CI 构建失败再补,并在宿主文档记录原因。
312
+
313
+ ---
314
+
315
+ ## 9. CSS 变量契约
316
+
317
+ 宿主可覆盖,包必须给默认值:
318
+
319
+ `--md-text`、`--md-muted`、`--md-border`、`--md-bg`、`--md-bg-subtle`、`--md-accent`、`--md-radius`、`--md-code-bg`。
320
+
321
+ 包内不得写死颜色字面量,一律引用上述变量。
322
+
323
+ ---
324
+
325
+ ## 10. 兼容性与发布
326
+
327
+ | 变更 | 版本 | 允许 |
328
+ |---|---|---|
329
+ | 新增组件 | minor | 是 |
330
+ | 新增可选属性 / 配置项(有默认值) | minor | 是 |
331
+ | 组件内部 UI 与样式调整 | patch / minor | 是 |
332
+ | 重命名或删除组件 | — | **禁止** |
333
+ | 修改属性语义 | — | **禁止** |
334
+ | 删除配置项 / 改默认值导致行为变化 | — | **禁止** |
335
+
336
+ ---
337
+
338
+ ## 11. 对接验收
339
+
340
+ | # | 项 | 判据 |
341
+ |---|---|---|
342
+ | 1 | 主入口 SSR | Node 中直接 `parse()` + `serialize()` 可跑通,无需 DOM |
343
+ | 2 | 往返 | 7 个组件样例均 `roundtrip(md) === md` |
344
+ | 3 | 容错 | 未闭合 / 未知标签 / **无空行且紧贴段落(应正常解析)** / 超限 / 列表内容器 五种输入均不崩、不吞内容 |
345
+ | 4 | 定位 | 任意容器与子项 `start`/`end` 与原文切片完全对应 |
346
+ | 5 | 安全 | `javascript:` 等协议被拒并产出 `UNSAFE_HREF` |
347
+ | 6 | 隔离 | 主入口产物不含 CodeMirror |
348
+ | 7 | 样式 | 仅覆盖 CSS 变量即可换肤,类名不变 |