@heikeson/md-engine 0.5.2 → 0.5.4

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.
Files changed (35) hide show
  1. package/README.md +133 -259
  2. package/dist/{chunk-KC6CBSFU.js → chunk-2A72VTCO.js} +9 -11
  3. package/dist/{chunk-YJGGJP2V.js → chunk-P4SAIRGF.js} +0 -2
  4. package/dist/{chunk-RJZKMZOI.js → chunk-QEMURI4I.js} +353 -224
  5. package/dist/{chunk-OG47YL3H.js → chunk-RWT2RMCY.js} +0 -2
  6. package/dist/{chunk-OJQDOFGL.js → chunk-SGORBOHN.js} +2 -4
  7. package/dist/components/index.d.ts +16 -20
  8. package/dist/components/index.js +4 -6
  9. package/dist/editor/index.d.ts +15 -5
  10. package/dist/editor/index.js +50 -24
  11. package/dist/index.d.ts +36 -18
  12. package/dist/index.js +14 -10
  13. package/dist/locales/en-US.js +1 -3
  14. package/dist/locales/zh-CN.js +1 -3
  15. package/dist/mermaid/index.d.ts +1 -15
  16. package/dist/mermaid/index.js +11 -7
  17. package/dist/{mermaid-placeholder-DYfFG3_f.d.ts → mermaid-placeholder-D97goALt.d.ts} +0 -3
  18. package/dist/{render-DbUepYgE.d.ts → render-CCAfJ__u.d.ts} +1 -3
  19. package/package.json +5 -1
  20. package/styles/cards.css +0 -1
  21. package/styles/files.css +0 -1
  22. package/styles/index.css +0 -2
  23. package/styles/mermaid.css +0 -6
  24. package//345/257/271/346/216/245/345/245/221/347/272/246.md +17 -7
  25. package/dist/chunk-KC6CBSFU.js.map +0 -1
  26. package/dist/chunk-OG47YL3H.js.map +0 -1
  27. package/dist/chunk-OJQDOFGL.js.map +0 -1
  28. package/dist/chunk-RJZKMZOI.js.map +0 -1
  29. package/dist/chunk-YJGGJP2V.js.map +0 -1
  30. package/dist/components/index.js.map +0 -1
  31. package/dist/editor/index.js.map +0 -1
  32. package/dist/index.js.map +0 -1
  33. package/dist/locales/en-US.js.map +0 -1
  34. package/dist/locales/zh-CN.js.map +0 -1
  35. package/dist/mermaid/index.js.map +0 -1
@@ -64,5 +64,3 @@ var zhCN = {
64
64
  };
65
65
 
66
66
  export { zhCN };
67
- //# sourceMappingURL=chunk-OG47YL3H.js.map
68
- //# sourceMappingURL=chunk-OG47YL3H.js.map
@@ -1,5 +1,5 @@
1
- import { zhCN } from './chunk-OG47YL3H.js';
2
- import { enUS } from './chunk-YJGGJP2V.js';
1
+ import { zhCN } from './chunk-RWT2RMCY.js';
2
+ import { enUS } from './chunk-P4SAIRGF.js';
3
3
  import { IntlMessageFormat } from 'intl-messageformat';
4
4
  import { createContext, useRef, useContext, useState, useEffect, useMemo } from 'react';
5
5
  import { jsx } from 'react/jsx-runtime';
@@ -309,5 +309,3 @@ function useMessage() {
309
309
  }
310
310
 
311
311
  export { BUILTIN_LOCALES, DEFAULT_LOCALE, DEFAULT_LOCALE_STORAGE_KEY, DEFAULT_UI_TEXT, MdEngineProvider, createLocaleStorage, detectLocale, formatMessage, fromRouteSegment, isRTL, languageOf, localeCandidates, matchLocale, mergeUIText, normalizeLocale, resolveLocale, supportsIntlLocale, toRouteSegment, useLocale, useMessage, useShallowStable, useUIText };
312
- //# sourceMappingURL=chunk-OJQDOFGL.js.map
313
- //# sourceMappingURL=chunk-OJQDOFGL.js.map
@@ -1,7 +1,7 @@
1
1
  import * as react from 'react';
2
2
  import { ReactNode } from 'react';
3
- import { M as MermaidPlaceholder } from '../mermaid-placeholder-DYfFG3_f.js';
4
- export { a as MermaidProps } from '../mermaid-placeholder-DYfFG3_f.js';
3
+ import { M as MermaidPlaceholder } from '../mermaid-placeholder-D97goALt.js';
4
+ export { a as MermaidProps } from '../mermaid-placeholder-D97goALt.js';
5
5
 
6
6
  /** 子项参数 */
7
7
  interface AccItemProps {
@@ -29,6 +29,11 @@ declare function Accordion({ children }: {
29
29
  children?: ReactNode;
30
30
  }): react.JSX.Element;
31
31
 
32
+ /**
33
+ * 提示框
34
+ * @param props 类型、标题、自定义图标与正文
35
+ * @returns 提示框节点
36
+ */
32
37
  declare function Callout({ type, title, icon, children, }: {
33
38
  type?: string;
34
39
  title?: string;
@@ -38,11 +43,9 @@ declare function Callout({ type, title, icon, children, }: {
38
43
  }): react.JSX.Element;
39
44
 
40
45
  /**
41
- * 卡片子项(`<Card title="…" href="…" icon="…">`)透传容器。
42
- * 真实渲染由父组件 Cards 读取其 title / href / icon / children 后统一绘制。
46
+ * 卡片子项
43
47
  * @param props 含 children 及指令透传的属性(title / href 等)
44
- * @created 2026-09-03 22:17
45
- * @updated 2026-09-03 22:17 随嵌套指令方案新增
48
+ * @returns 子元素
46
49
  */
47
50
  declare function Card(props: {
48
51
  children?: ReactNode;
@@ -115,11 +118,9 @@ interface ImageProps {
115
118
  declare function Image({ src, alt, align, width, caption, zoom, children, }: ImageProps): react.JSX.Element;
116
119
 
117
120
  /**
118
- * 步骤子项(`<Step title="…">`)透传容器。
119
- * 真实渲染由父组件 Steps 读取其 title / children 后统一绘制,故此组件仅作占位。
121
+ * 步骤子项
120
122
  * @param props 含 children 及指令透传的属性(title 等)
121
- * @created 2026-09-03 22:17
122
- * @updated 2026-09-03 22:17 随嵌套指令方案新增
123
+ * @returns 子元素
123
124
  */
124
125
  declare function Step(props: {
125
126
  children?: ReactNode;
@@ -127,22 +128,18 @@ declare function Step(props: {
127
128
  }): react.JSX.Element;
128
129
 
129
130
  /**
130
- * 步骤容器:聚合内层 `<Step title="…">` 为带序号竖线的步骤列表。
131
- * 每个 step 的 title 取自指令属性,正文为其内部 Markdown 内容(可含任意块级元素)。
131
+ * 步骤列表
132
132
  * @param children 内层 step 元素集合
133
- * @created 2026-09-03 21:54
134
- * @updated 2026-10-24 21:32 语法改为尖括号 `<Step>`,支持任意嵌套内容
133
+ * @returns 步骤列表节点
135
134
  */
136
135
  declare function Steps({ children }: {
137
136
  children?: ReactNode;
138
137
  }): react.JSX.Element;
139
138
 
140
139
  /**
141
- * 选项卡子项(`<Tab value="…">`)透传容器。
142
- * 真实渲染由父组件 Tabs 读取其 label / children 后统一绘制。
143
- * @param props 含 children 及指令透传的属性(label 等)
144
- * @created 2026-09-03 22:17
145
- * @updated 2026-09-03 22:17 随嵌套指令方案新增
140
+ * 选项卡子项
141
+ * @param props 含 children 及指令透传的属性(value 等)
142
+ * @returns 子元素
146
143
  */
147
144
  declare function Tab(props: {
148
145
  children?: ReactNode;
@@ -191,7 +188,6 @@ declare function TypeTable({ children }: {
191
188
  * @description 映射键与 mdast 节点名完全一致(首字母大写),由渲染层的 hast tagName 直接查表。
192
189
  * 新增组件时:先在 registry 声明,再在此登记组件实现。
193
190
  * @created 2026-10-24 20:10
194
- * @updated 2026-10-24 21:58 文件树改为 Files / Folder / File
195
191
  * @updated 2026-11-01 21:47 登记 Mermaid 占位组件
196
192
  */
197
193
 
@@ -1,6 +1,4 @@
1
- export { AccItem, Accordion, Callout, Card, Cards, File, Files, Folder, Image as MdImage, MermaidPlaceholder, Step, Steps, Tab, Tabs, TypeItem, TypeTable, mdxComponents } from '../chunk-KC6CBSFU.js';
2
- import '../chunk-OJQDOFGL.js';
3
- import '../chunk-OG47YL3H.js';
4
- import '../chunk-YJGGJP2V.js';
5
- //# sourceMappingURL=index.js.map
6
- //# sourceMappingURL=index.js.map
1
+ export { AccItem, Accordion, Callout, Card, Cards, File, Files, Folder, Image as MdImage, MermaidPlaceholder, Step, Steps, Tab, Tabs, TypeItem, TypeTable, mdxComponents } from '../chunk-2A72VTCO.js';
2
+ import '../chunk-SGORBOHN.js';
3
+ import '../chunk-RWT2RMCY.js';
4
+ import '../chunk-P4SAIRGF.js';
@@ -1,6 +1,6 @@
1
1
  import * as react from 'react';
2
- import { R as RenderConfig, U as UITextPatch } from '../render-DbUepYgE.js';
3
- export { a as UIText } from '../render-DbUepYgE.js';
2
+ import { R as RenderConfig, U as UITextPatch } from '../render-CCAfJ__u.js';
3
+ export { a as UIText } from '../render-CCAfJ__u.js';
4
4
  import 'unified';
5
5
 
6
6
  /** 编辑器配置(只增不改) */
@@ -59,7 +59,6 @@ declare function MarkdownEditor(props: MarkdownEditorProps): react.JSX.Element;
59
59
  * @description 组件名一律取自 `registry`,模板只描述"骨架"(标签 + 子项 + 空行),
60
60
  * 禁止在别处硬编码 `<Callout` 之类的字符串。
61
61
  * @created 2026-10-24 21:12
62
- * @updated 2026-10-24 21:58 容器子项支持数组(Files 混用 Folder / File)
63
62
  * @updated 2026-11-01 21:47 图表片段:rawBody 容器给流程图骨架,可插入列表加 Mermaid
64
63
  */
65
64
  /**
@@ -76,7 +75,7 @@ declare const INSERTABLE_BLOCKS: readonly ["Callout", "Tabs", "Steps", "Cards",
76
75
  * @description 编辑器只改 Markdown 文本,不维护第二份文档模型。这里提供三个基础动作:
77
76
  * 选区包裹、行首前缀切换、光标处插入。全部返回新文本与新选区,由调用方写回。
78
77
  * @created 2026-10-24 21:12
79
- * @updated 2026-10-24 21:12
78
+ * @updated 2026-11-09 12:40 块级插入沿用当前行缩进,光标落到内容占位处(正文占位文字选中),不再停在闭标签之后
80
79
  */
81
80
  /** 选区(全文 offset,end 不含) */
82
81
  interface Selection {
@@ -115,5 +114,16 @@ declare function toggleLinePrefix(value: string, selection: Selection, prefix: s
115
114
  * @returns 新文本与光标位置(落在片段之后)
116
115
  */
117
116
  declare function insertAtCursor(value: string, position: number, snippet: string): EditResult;
117
+ /**
118
+ * 在光标所在行插入块级片段(容器组件专用)
119
+ * @description 容器是块级构造:与前后内容隔开空行、并按当前行缩进对齐。
120
+ * 按字符插入会把 `<Callout>` 塞进行中间,导致整块解析失败或落到错误的层级;
121
+ * 缩进同理——在列表项或容器内部插入时不带缩进会把整块提到列 0,容器直接被截断。
122
+ * @param value 全文
123
+ * @param position 光标位置(取其所在行的行首作为插入点)
124
+ * @param snippet 片段文本(通常以换行结尾)
125
+ * @returns 新文本与新选区(落在内容占位处,正文占位文字会被选中)
126
+ */
127
+ declare function insertBlockAt(value: string, position: number, snippet: string): EditResult;
118
128
 
119
- export { type EditResult, type EditorConfig, type EditorView, INSERTABLE_BLOCKS, MarkdownEditor, type MarkdownEditorProps, type Selection, UITextPatch, blockSnippet, insertAtCursor, toggleLinePrefix, wrapSelection };
129
+ export { type EditResult, type EditorConfig, type EditorView, INSERTABLE_BLOCKS, MarkdownEditor, type MarkdownEditorProps, type Selection, UITextPatch, blockSnippet, insertAtCursor, insertBlockAt, toggleLinePrefix, wrapSelection };
@@ -1,8 +1,8 @@
1
- import { COMPONENTS, parse, normalizeMarkdown, MarkdownRenderer, formatDiagnostic } from '../chunk-RJZKMZOI.js';
2
- import '../chunk-KC6CBSFU.js';
3
- import { useShallowStable, MdEngineProvider, useUIText, useMessage } from '../chunk-OJQDOFGL.js';
4
- import '../chunk-OG47YL3H.js';
5
- import '../chunk-YJGGJP2V.js';
1
+ import { COMPONENTS, parse, MarkdownTreeRenderer, formatDiagnostic } from '../chunk-QEMURI4I.js';
2
+ import '../chunk-2A72VTCO.js';
3
+ import { useShallowStable, MdEngineProvider, useUIText, useMessage } from '../chunk-SGORBOHN.js';
4
+ import '../chunk-RWT2RMCY.js';
5
+ import '../chunk-P4SAIRGF.js';
6
6
  import { useMemo, useState, useRef, useDeferredValue, useEffect, useCallback } from 'react';
7
7
  import { Compartment, EditorState } from '@codemirror/state';
8
8
  import { EditorView, keymap, lineNumbers, drawSelection, placeholder } from '@codemirror/view';
@@ -32,19 +32,53 @@ function insertAtCursor(value, position, snippet) {
32
32
  const cursor = position + snippet.length;
33
33
  return { value: next, selection: { start: cursor, end: cursor } };
34
34
  }
35
+ function leadingIndent(line) {
36
+ return /^[ \t]*/.exec(line)?.[0] ?? "";
37
+ }
38
+ function indentSnippet(snippet, indent) {
39
+ if (indent === "") return snippet;
40
+ return snippet.split("\n").map((line) => line === "" ? "" : indent + line).join("\n");
41
+ }
42
+ function contentSelection(body) {
43
+ const lines = body.split("\n");
44
+ const starts = [];
45
+ let offset = 0;
46
+ for (const line of lines) {
47
+ starts.push(offset);
48
+ offset += line.length + 1;
49
+ }
50
+ if (lines.length <= 2) {
51
+ const end2 = starts[0] + (lines[0] ?? "").length;
52
+ return { start: end2, end: end2 };
53
+ }
54
+ for (let index = 1; index < lines.length; index += 1) {
55
+ const text = lines[index] ?? "";
56
+ const trimmed = text.trim();
57
+ const at = starts[index] + leadingIndent(text).length;
58
+ if (trimmed === "" || trimmed.startsWith("</") || trimmed.endsWith("/>")) {
59
+ return { start: at, end: at };
60
+ }
61
+ if (trimmed.startsWith("<")) continue;
62
+ const next = (lines[index + 1] ?? "").trim();
63
+ return { start: at, end: next.startsWith("</") ? starts[index] + text.trimEnd().length : at };
64
+ }
65
+ const end = Math.max(0, body.length - 1);
66
+ return { start: end, end };
67
+ }
35
68
  function insertBlockAt(value, position, snippet) {
36
69
  const lineStart = value.lastIndexOf("\n", position - 1) + 1;
37
70
  const before = value.slice(0, lineStart);
38
71
  const after = value.slice(lineStart);
72
+ const indent = leadingIndent(after);
39
73
  const lead = before === "" || before.endsWith("\n\n") ? "" : before.endsWith("\n") ? "\n" : "\n\n";
40
- const body = snippet.endsWith("\n") ? snippet : `${snippet}
41
- `;
74
+ const body = indentSnippet(snippet.endsWith("\n") ? snippet : `${snippet}
75
+ `, indent);
42
76
  const tail = after === "" || after.startsWith("\n") ? "" : "\n";
43
77
  const inserted = lead + body + tail;
44
78
  const next = before + inserted + after;
45
- const bodyWithoutBreak = body.replace(/\n+$/, "");
46
- const cursor = before.length + lead.length + bodyWithoutBreak.length;
47
- return { value: next, selection: { start: cursor, end: cursor } };
79
+ const caret = contentSelection(body);
80
+ const base = before.length + lead.length;
81
+ return { value: next, selection: { start: base + caret.start, end: base + caret.end } };
48
82
  }
49
83
  function buildShortcuts(wrapPlaceholder) {
50
84
  return [
@@ -545,14 +579,8 @@ function EditorShell({ value, onChange, view = "split", config, fullscreen, onTo
545
579
  useEffect(() => {
546
580
  handleRef.current?.setDoc(value);
547
581
  }, [value]);
548
- const diagnostics = useMemo(() => parse(deferredValue).diagnostics, [deferredValue]);
549
- const handleBlur = useCallback(() => {
550
- const current = valueRef.current;
551
- const normalized = normalizeMarkdown(current);
552
- if (normalized === current) return;
553
- onChangeRef.current(normalized);
554
- handleRef.current?.setDoc(normalized);
555
- }, []);
582
+ const parsed = useMemo(() => parse(deferredValue, { blocks: false }), [deferredValue]);
583
+ const diagnostics = parsed.diagnostics;
556
584
  return /* @__PURE__ */ jsxs("div", { className: "md-editor", children: [
557
585
  /* @__PURE__ */ jsxs("div", { className: "md-editor-toolbar", children: [
558
586
  WRAP_ITEMS.map((item) => /* @__PURE__ */ jsx(
@@ -604,12 +632,12 @@ function EditorShell({ value, onChange, view = "split", config, fullscreen, onTo
604
632
  )) }),
605
633
  onToggleFullscreen ? /* @__PURE__ */ jsx("button", { type: "button", onClick: onToggleFullscreen, children: fullscreen ? text.editorExitFullscreen : text.editorFullscreen }) : null
606
634
  ] }),
607
- /* @__PURE__ */ jsxs("div", { className: "md-editor-body", onBlur: handleBlur, children: [
635
+ /* @__PURE__ */ jsxs("div", { className: "md-editor-body", children: [
608
636
  viewMode !== "preview" && /* @__PURE__ */ jsx("div", { ref: hostRef, className: "md-editor-source" }),
609
637
  viewMode !== "source" && /* @__PURE__ */ jsx("div", { ref: previewRef, className: "md-editor-preview", children: /* @__PURE__ */ jsx(
610
- MarkdownRenderer,
638
+ MarkdownTreeRenderer,
611
639
  {
612
- content: deferredValue,
640
+ tree: parsed.tree,
613
641
  className: "prose-site",
614
642
  config: { ...config?.rendererConfig, sourcePositions: true }
615
643
  }
@@ -625,6 +653,4 @@ function EditorShell({ value, onChange, view = "split", config, fullscreen, onTo
625
653
  ] });
626
654
  }
627
655
 
628
- export { INSERTABLE_BLOCKS, MarkdownEditor, blockSnippet, insertAtCursor, toggleLinePrefix, wrapSelection };
629
- //# sourceMappingURL=index.js.map
630
- //# sourceMappingURL=index.js.map
656
+ export { INSERTABLE_BLOCKS, MarkdownEditor, blockSnippet, insertAtCursor, insertBlockAt, toggleLinePrefix, wrapSelection };
package/dist/index.d.ts CHANGED
@@ -3,12 +3,12 @@ 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
5
  import { Processor } from 'unified';
6
- import { a as UIText } from './render-DbUepYgE.js';
7
- export { B as BUILTIN_LOCALES, D as DEFAULT_LOCALE_STORAGE_KEY, L as LocaleStorage, b as LocaleStorageOptions, M as MarkdownRenderer, c as MarkdownRendererProps, d as MdEngineProvider, e as MdEngineProviderProps, R as RenderConfig, T as TextDirection, U as UITextPatch, f as createLocaleStorage, g as defaultUIText, m as mergeUIText, u as useLocale, h as useMessage, i as useUIText } from './render-DbUepYgE.js';
6
+ import { a as UIText } from './render-CCAfJ__u.js';
7
+ export { B as BUILTIN_LOCALES, D as DEFAULT_LOCALE_STORAGE_KEY, L as LocaleStorage, b as LocaleStorageOptions, M as MarkdownRenderer, c as MarkdownRendererProps, d as MdEngineProvider, e as MdEngineProviderProps, R as RenderConfig, T as TextDirection, U as UITextPatch, f as createLocaleStorage, g as defaultUIText, m as mergeUIText, u as useLocale, h as useMessage, i as useUIText } from './render-CCAfJ__u.js';
8
8
  export { mdxComponents } from './components/index.js';
9
9
  export { zhCN } from './locales/zh-CN.js';
10
10
  export { enUS } from './locales/en-US.js';
11
- export { M as MermaidPlaceholder, a as MermaidProps } from './mermaid-placeholder-DYfFG3_f.js';
11
+ export { M as MermaidPlaceholder, a as MermaidProps } from './mermaid-placeholder-D97goALt.js';
12
12
  import 'react';
13
13
 
14
14
  /**
@@ -18,7 +18,6 @@ import 'react';
18
18
  * 禁止在别处再硬编码组件名或属性名。改动本文件即改动内容语法,须同步
19
19
  * `docs/markdown/02-语法规范.md` 与引擎对接契约。
20
20
  * @created 2026-10-24 20:07
21
- * @updated 2026-11-01 21:10 新增 Mermaid 图表容器与 rawBody(容器体按原始文本保留)声明
22
21
  * @updated 2026-11-02 21:33 TypeItem 声明 selfClosing:支持 `<TypeItem … />` 自闭合写法
23
22
  */
24
23
  /** 组件的编辑形态:visual 允许结构化编辑;source 只按源码块保留 */
@@ -95,8 +94,8 @@ declare function isContainerWithItems(name: string): boolean;
95
94
  * 以及把节点序列化回 `<Name attr="v">` 文本(toMarkdown)。两侧必须对称:
96
95
  * `md → mdast → md` 要逐字一致。
97
96
  * @created 2026-10-24 20:16
98
- * @updated 2026-10-28 11:40 序列化改为保形:按原文行距写回换行/空行,不再统一加空行;行内形态由 data.inline 决定
99
- * @updated 2026-11-01 21:10 rawBody 容器(Mermaid):exitBlock 用 token 原文切片覆盖子节点,序列化逐字回写
97
+ * @updated 2026-11-09 13:40 空行改为按原文行距逐行写回(原实现固定最多一个空行,既加也删);
98
+ * 新增 join:容器块与兄弟节点之间不再被库默认规则插入空行
100
99
  */
101
100
 
102
101
  /** 尖括号块节点 */
@@ -201,7 +200,8 @@ declare module "mdast" {
201
200
  * 之所以写在词法层:CommonMark 的 HTML block 规则要求标签前后有空行,且标签紧贴上一段时
202
201
  * 开标签会被吞进段落;只有 flow 构造能绕开这些限制(详见 docs/markdown/03-引擎设计.md)。
203
202
  * @created 2026-10-24 20:16
204
- * @updated 2026-10-28 11:32 内容区增加围栏状态感知:围栏代码块内的 `</Name>` 与同名 `<Name` 不再参与闭合/嵌套判定
203
+ * @updated 2026-11-09 12:40 行首缩进统一按 `markdownSpace` 判定:tab(-2)与它展开的虚拟空格(-1)不再漏判,
204
+ * tab 缩进的子项不再整行退化成缩进代码块(子项与其 position 一并恢复)
205
205
  */
206
206
 
207
207
  /**
@@ -231,7 +231,6 @@ declare function remarkJsxBlocks(this: Processor): undefined;
231
231
  * @description 负责"不崩、不丢内容"的部分:嵌套深度裁剪、未闭合与超限诊断。
232
232
  * 与渲染实现解耦——将来换渲染方式,这里不用动。
233
233
  * @created 2026-10-24 20:24
234
- * @updated 2026-10-24 20:24
235
234
  * @updated 2026-11-01 21:47 新增 MERMAID_EMPTY / MERMAID_TOO_LARGE 诊断与 MERMAID_MAX_BYTES
236
235
  */
237
236
 
@@ -265,7 +264,6 @@ interface Diagnostic {
265
264
  * 编辑器据此"点哪块改哪块",不需要自己再遍历一遍树。
266
265
  * 区间一律是原文 offset(0 起,end 不含),可直接用于 `source.slice(start, end)`。
267
266
  * @created 2026-10-24 21:06
268
- * @updated 2026-10-24 21:06
269
267
  * @updated 2026-11-01 21:47 rawBody 容器按 source + reason:mermaid 暴露
270
268
  */
271
269
 
@@ -335,14 +333,20 @@ declare function extractBlocks(tree: Root, source: string): Block[];
335
333
  * @description Markdown → mdast(已含尖括号块与数学公式节点),再跑策略层得到诊断。
336
334
  * 渲染、序列化、编辑器块索引都基于这里产出的同一棵 mdast,避免重复解析。
337
335
  * @created 2026-10-24 20:24
338
- * @updated 2026-10-26 22:02
339
- * @updated 2026-11-01 21:47 诊断链加入 collectMermaid(图表空源码 / 超限)
336
+ * @updated 2026-11-09 10:20 扩展与解析期后处理改为复用 pipeline 的模块级实例;新增 blocks 选项与 diagnose
340
337
  */
341
338
 
342
339
  /** 解析选项 */
343
340
  interface ParseOptions {
344
341
  /** 覆盖默认嵌套深度上限 */
345
342
  maxDepth?: number;
343
+ /**
344
+ * 是否提取扁平块索引
345
+ * @default true
346
+ * @description 只取诊断(如编辑器输入提示)时传 `false`,可省掉一次整树转换。
347
+ * 块索引是纯只读派生,跳过它不影响诊断、序列化与渲染。
348
+ */
349
+ blocks?: boolean;
346
350
  }
347
351
  /** 解析结果 */
348
352
  interface ParseResult {
@@ -360,13 +364,22 @@ interface ParseResult {
360
364
  * @returns mdast 与诊断
361
365
  */
362
366
  declare function parse(md: string, options?: ParseOptions): ParseResult;
367
+ /**
368
+ * 只取诊断(不建块索引)
369
+ * @param md Markdown 原文
370
+ * @param options 解析选项(`blocks` 固定为 false)
371
+ * @returns 诊断列表
372
+ * @description 编辑器每次输入只需提示文案,不需要块索引:`parse` 走全量会多付出
373
+ * `extractBlocks` 的整树转换开销。
374
+ */
375
+ declare function diagnose(md: string, options?: ParseOptions): Diagnostic[];
363
376
 
364
377
  /**
365
378
  * @file 序列化:mdast → Markdown
366
379
  * @description 与解析对称:解析支持的写法,序列化必须能产出。GFM(表格/删除线/任务列表)、
367
380
  * 数学公式与尖括号块各自注册 handler,空行由 handler 统一产出。
368
381
  * @created 2026-10-24 20:30
369
- * @updated 2026-10-26 22:02
382
+ * @updated 2026-11-18 10:10 扩展表提为模块级常量:三个工厂返回的是纯描述对象,无需每次重建
370
383
  */
371
384
 
372
385
  /**
@@ -383,16 +396,21 @@ declare function serialize(tree: Root): string;
383
396
  declare function roundtrip(md: string): string;
384
397
 
385
398
  /**
386
- * @file 排版规范化(不承担语法正确性)
387
- * @description 尖括号块的解析不依赖空行,本函数只负责"看起来整齐":容器标签前后各留一个空行、
388
- * 连续空行收敛为一个。围栏代码块内的内容一律跳过;幂等(跑两遍结果一致)。
399
+ * @file 排版规范化(已停用改写,纯原样返回)
400
+ * @description 空行与缩进属于作者内容,引擎一律不处理:不加、不删、不合并。
401
+ * 本函数保留导出只为兼容既有调用点(对接契约只增不改),行为是**原样返回**,
402
+ * 连 CRLF 都不转换——调用方拿到的就是自己传进来的字符串。
403
+ * 编辑器也不会自动调用它:`onChange` 交出的就是用户键入的原文。
389
404
  * @created 2026-10-24 20:30
390
- * @updated 2026-10-24 20:30
405
+ * @updated 2026-11-09 13:40 停用空行处理:原样返回,不增删空行、不改缩进、不动换行符
391
406
  */
392
407
  /**
393
408
  * 规范化 Markdown 排版
394
409
  * @param md 原文
395
- * @returns 规范化后的文本
410
+ * @returns 与入参完全相同的文本
411
+ * @description 历史实现会"补空行 + 收敛空行",实测会改坏作者排版(容器内部被撑开、
412
+ * 顶层块之间被插入空行),因此改为原样返回。要做排版整理请在宿主侧自己实现,
413
+ * 引擎不再替用户决定空行。
396
414
  */
397
415
  declare function normalizeMarkdown(md: string): string;
398
416
 
@@ -507,4 +525,4 @@ declare function diagnosticTextKey(code: DiagnosticCode): keyof UIText | null;
507
525
  */
508
526
  declare function formatDiagnostic(t: (key: keyof UIText, values?: Record<string, string | number>) => string, diagnostic: Diagnostic): string;
509
527
 
510
- export { type Attrs, type Block, COMPONENTS, type ComponentMode, type ComponentSpec, type ContainerBlock, type ContainerItem, DEFAULT_LOCALE, type Diagnostic, type DiagnosticCode, ITEMS, type ItemSpec, type JsxBlock, MAX_CONTAINER_DEPTH, MERMAID_MAX_BYTES, type ParseOptions, type ParseResult, type SourceBlock, type SourceReason, UIText, type VisualBlock, detectLocale, diagnosticTextKey, extractBlocks, formatDiagnostic, formatMessage, fromRouteSegment, getComponent, getItemSpec, isContainerWithItems, isRTL, jsxBlocks, jsxFromMarkdown, jsxToMarkdown, languageOf, listComponents, localeCandidates, matchLocale, normalizeLocale, normalizeMarkdown, parse, parseOpenTag, remarkJsxBlocks, resolveLocale, roundtrip, serialize, sliceRawBody, supportsIntlLocale, toRouteSegment };
528
+ export { type Attrs, type Block, COMPONENTS, type ComponentMode, type ComponentSpec, type ContainerBlock, type ContainerItem, DEFAULT_LOCALE, type Diagnostic, type DiagnosticCode, ITEMS, type ItemSpec, type JsxBlock, MAX_CONTAINER_DEPTH, MERMAID_MAX_BYTES, type ParseOptions, type ParseResult, type SourceBlock, type SourceReason, UIText, type VisualBlock, detectLocale, diagnose, diagnosticTextKey, extractBlocks, formatDiagnostic, formatMessage, fromRouteSegment, getComponent, getItemSpec, isContainerWithItems, isRTL, jsxBlocks, jsxFromMarkdown, jsxToMarkdown, languageOf, listComponents, localeCandidates, matchLocale, normalizeLocale, normalizeMarkdown, parse, parseOpenTag, remarkJsxBlocks, resolveLocale, roundtrip, serialize, sliceRawBody, supportsIntlLocale, toRouteSegment };
package/dist/index.js CHANGED
@@ -1,9 +1,9 @@
1
- import { jsxToMarkdown, parse } from './chunk-RJZKMZOI.js';
2
- 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 } from './chunk-RJZKMZOI.js';
3
- export { MermaidPlaceholder, mdxComponents } from './chunk-KC6CBSFU.js';
4
- export { BUILTIN_LOCALES, DEFAULT_LOCALE, DEFAULT_LOCALE_STORAGE_KEY, MdEngineProvider, createLocaleStorage, DEFAULT_UI_TEXT as defaultUIText, detectLocale, formatMessage, fromRouteSegment, isRTL, languageOf, localeCandidates, matchLocale, mergeUIText, normalizeLocale, resolveLocale, supportsIntlLocale, toRouteSegment, useLocale, useMessage, useUIText } from './chunk-OJQDOFGL.js';
5
- export { zhCN } from './chunk-OG47YL3H.js';
6
- export { enUS } from './chunk-YJGGJP2V.js';
1
+ import { jsxToMarkdown, parse } from './chunk-QEMURI4I.js';
2
+ export { COMPONENTS, ITEMS, MAX_CONTAINER_DEPTH, MERMAID_MAX_BYTES, MarkdownRenderer, diagnose, diagnosticTextKey, extractBlocks, formatDiagnostic, getComponent, getItemSpec, isContainerWithItems, jsxBlocks, jsxFromMarkdown, jsxToMarkdown, listComponents, parse, parseOpenTag, remarkJsxBlocks, sliceRawBody } from './chunk-QEMURI4I.js';
3
+ export { MermaidPlaceholder, mdxComponents } from './chunk-2A72VTCO.js';
4
+ export { BUILTIN_LOCALES, DEFAULT_LOCALE, DEFAULT_LOCALE_STORAGE_KEY, MdEngineProvider, createLocaleStorage, DEFAULT_UI_TEXT as defaultUIText, detectLocale, formatMessage, fromRouteSegment, isRTL, languageOf, localeCandidates, matchLocale, mergeUIText, normalizeLocale, resolveLocale, supportsIntlLocale, toRouteSegment, useLocale, useMessage, useUIText } from './chunk-SGORBOHN.js';
5
+ export { zhCN } from './chunk-RWT2RMCY.js';
6
+ export { enUS } from './chunk-P4SAIRGF.js';
7
7
  import { toMarkdown } from 'mdast-util-to-markdown';
8
8
  import { gfmToMarkdown } from 'mdast-util-gfm';
9
9
  import { mathToMarkdown } from 'mdast-util-math';
@@ -16,17 +16,21 @@ var OPTIONS = {
16
16
  fences: true,
17
17
  listItemIndent: "one"
18
18
  };
19
+ var EXTENSIONS = [gfmToMarkdown(), mathToMarkdown(), jsxToMarkdown()];
19
20
  function serialize(tree) {
20
21
  return toMarkdown(tree, {
21
22
  ...OPTIONS,
22
23
  // mathToMarkdown 直接把公式原文写回 `$…$` / `$$…$$`,不再走文本转义
23
- extensions: [gfmToMarkdown(), mathToMarkdown(), jsxToMarkdown()]
24
+ extensions: EXTENSIONS
24
25
  });
25
26
  }
26
27
  function roundtrip(md) {
27
28
  return serialize(parse(md).tree);
28
29
  }
29
30
 
30
- export { roundtrip, serialize };
31
- //# sourceMappingURL=index.js.map
32
- //# sourceMappingURL=index.js.map
31
+ // src/format.ts
32
+ function normalizeMarkdown(md) {
33
+ return md ?? "";
34
+ }
35
+
36
+ export { normalizeMarkdown, roundtrip, serialize };
@@ -1,3 +1 @@
1
- export { enUS } from '../chunk-YJGGJP2V.js';
2
- //# sourceMappingURL=en-US.js.map
3
- //# sourceMappingURL=en-US.js.map
1
+ export { enUS } from '../chunk-P4SAIRGF.js';
@@ -1,3 +1 @@
1
- export { zhCN } from '../chunk-OG47YL3H.js';
2
- //# sourceMappingURL=zh-CN.js.map
3
- //# sourceMappingURL=zh-CN.js.map
1
+ export { zhCN } from '../chunk-RWT2RMCY.js';
@@ -1,5 +1,5 @@
1
1
  import { ReactElement, RefObject, ComponentType } from 'react';
2
- import { a as MermaidProps } from '../mermaid-placeholder-DYfFG3_f.js';
2
+ import { a as MermaidProps } from '../mermaid-placeholder-D97goALt.js';
3
3
 
4
4
  /**
5
5
  * @file 交互式图表视图(客户端)
@@ -7,12 +7,6 @@ import { a as MermaidProps } from '../mermaid-placeholder-DYfFG3_f.js';
7
7
  * 未出图前一律显示源码——无 JS、加载失败、超限、空源码四种情况都不会白屏。
8
8
  * 图表区里手势绑在「视口」层:整块区域都能拖,不再只有图片本身能拖。
9
9
  * @created 2026-11-01 21:30
10
- * @updated 2026-11-01 22:29 改为「顶部工具栏 + 右下角缩放条」的卡片式布局
11
- * @updated 2026-11-01 22:37 标题并入工具栏;暗色与取色按图表容器判定;手势仅在出图后启用
12
- * @updated 2026-11-01 22:51 导出 PNG 底色改走 resolveChartTokens
13
- * @updated 2026-11-01 23:18 height 未指定 / 空 / auto 一律自动高度
14
- * @updated 2026-11-02 10:38 固定文案改走 UIText(config.text 可覆写)
15
- * @updated 2026-11-02 22:58 新增视口层:手势面铺满图表区,初始按适配比例居中
16
10
  * @updated 2026-11-02 23:12 变换层改用回调 ref,transform 交由 use-zoom 直写(手势期不再触发重渲染)
17
11
  */
18
12
 
@@ -29,8 +23,6 @@ declare function MermaidChart({ raw, code, title, height, tooLarge }: MermaidPro
29
23
  * 变量清单自宿主站点既有实现平移(含 ER 属性行、饼图 12 扇区、甘特任务条等派生型变量的
30
24
  * 逐个点名覆盖)——这些变量不显式给值就会回落到 mermaid 预设的深色/白色,暗色下不可读。
31
25
  * @created 2026-11-01 21:20
32
- * @updated 2026-11-01 22:31 兜底色值改为薄荷系(与 styles/tokens.css 默认值一致)
33
- * @updated 2026-11-01 22:37 兜底色值改为跟随宿主语义色(不再写死薄荷)
34
26
  * @updated 2026-11-01 22:51 内置亮/暗两套配色 + configureChartTheme(零宿主依赖)
35
27
  */
36
28
  /** 图表可用的设计令牌(由调用方从 `--lkes-chart-*` 计算值取来) */
@@ -155,10 +147,6 @@ declare function downloadPng(source: string, background: string): Promise<void>;
155
147
  * `render(id)` 又按 id 在文档里找节点,重复 id 会命中原节点(表现为拿到旧结果或图被删掉)。
156
148
  * 因此本模块把渲染收敛成一条串行链 + 每次唯一 id,并把失败收敛成可展示的错误。
157
149
  * @created 2026-11-01 21:24
158
- * @updated 2026-11-01 21:24
159
- * @updated 2026-11-01 22:37 令牌三级回落(引擎令牌→宿主语义变量→内置)+ oklch/hsl 归一;取色改按图表元素
160
- * @updated 2026-11-01 22:51 取色改为「JS 配置 → 组件自带 --lkes-chart-* → 内置」,不再读宿主变量
161
- * @updated 2026-11-02 10:35 缓存加 LRU 上限(防编辑长文时内存只涨不落);超时改为可识别异常,文案由视图侧本地化
162
150
  * @updated 2026-11-02 22:57 新增 readSvgSize:从产物解析图表自然尺寸,供交互视图做初始适配
163
151
  */
164
152
 
@@ -179,7 +167,6 @@ declare function resolveChartTokens(dark: boolean, element?: HTMLElement | null)
179
167
  * 某个布局容器,甚至只在局部区域切换(开发台就是 `<div class="dark">`)。
180
168
  * 因此按「从图表元素向上找最近的 `.dark`」判定,并订阅整棵树的 class 变化。
181
169
  * @created 2026-11-01 21:22
182
- * @updated 2026-11-01 22:37 支持容器级 `.dark`(不再只看 html),改用单例 MutationObserver
183
170
  * @updated 2026-11-01 22:51 判据补 [data-theme="dark"]
184
171
  */
185
172
 
@@ -197,7 +184,6 @@ declare function useIsDark(elementRef?: RefObject<HTMLElement | null>): boolean;
197
184
  * 不用图表的页面不承担这些体积;未引入本入口时 `<Mermaid>` 停留在源码骨架。
198
185
  * 本文件只做导出,不执行任何副作用(不注册全局、不带 'use client' 之外的语句)。
199
186
  * @created 2026-11-01 21:32
200
- * @updated 2026-11-01 21:32
201
187
  * @updated 2026-11-01 22:51 导出配色配置 API(configureChartTheme / getChartTheme / DEFAULT_CHART_THEME)
202
188
  */
203
189
 
@@ -1,6 +1,6 @@
1
- import { useUIText } from '../chunk-OJQDOFGL.js';
2
- import '../chunk-OG47YL3H.js';
3
- import '../chunk-YJGGJP2V.js';
1
+ import { useUIText } from '../chunk-SGORBOHN.js';
2
+ import '../chunk-RWT2RMCY.js';
3
+ import '../chunk-P4SAIRGF.js';
4
4
  import { useState, useEffect, useRef, useCallback, useMemo, useLayoutEffect } from 'react';
5
5
  import { jsxs, jsx } from 'react/jsx-runtime';
6
6
  import DOMPurify from 'dompurify';
@@ -426,9 +426,15 @@ function sanitizeSvg(svg) {
426
426
  ADD_ATTR: ["dominant-baseline"]
427
427
  });
428
428
  }
429
+ var TAG_ATTR_RE = /\s([A-Za-z_:][-A-Za-z0-9_:.]*)\s*=\s*"([^"]*)"/g;
429
430
  function attribute(tag, name) {
430
- const matched = new RegExp(`\\s${name}\\s*=\\s*"([^"]*)"`, "i").exec(tag);
431
- return matched ? matched[1] : "";
431
+ TAG_ATTR_RE.lastIndex = 0;
432
+ const target = name.toLowerCase();
433
+ let matched;
434
+ while ((matched = TAG_ATTR_RE.exec(tag)) !== null) {
435
+ if ((matched[1] ?? "").toLowerCase() === target) return matched[2] ?? "";
436
+ }
437
+ return "";
432
438
  }
433
439
  function readSvgSize(svg) {
434
440
  if (!svg) return null;
@@ -969,5 +975,3 @@ var mermaidComponents = {
969
975
  };
970
976
 
971
977
  export { DARK_TOKENS, DEFAULT_CHART_THEME, LIGHT_TOKENS, MermaidChart, buildMermaidConfig, buildThemeVariables, configureChartTheme, downloadPng, downloadSvg, getChartTheme, hasCustomChartTheme, mermaidComponents, prepareSvg, resetChartTheme, resolveChartTokens, svgToPngBlob, tokenFingerprint, useIsDark };
972
- //# sourceMappingURL=index.js.map
973
- //# sourceMappingURL=index.js.map
@@ -6,9 +6,6 @@ import { ReactElement } from 'react';
6
6
  * 本文件不引入 mermaid、不碰 DOM,也不引用 `/mermaid` 目录下的任何模块;
7
7
  * 未接入交互入口时,读者看到的是源码而不是空白(渐进增强的降级面)。
8
8
  * @created 2026-11-01 21:12
9
- * @updated 2026-11-01 22:29 改为卡片式骨架(与交互视图同布局),内联源码图标以保持主入口零图表依赖
10
- * @updated 2026-11-01 22:37 标题并入工具栏左侧,与交互视图同布局
11
- * @updated 2026-11-01 23:18 height 未指定 / 空 / auto 一律自动高度(不再固定 420px)
12
9
  * @updated 2026-11-02 10:42 「源码视图」文案走 UIText(与交互视图同口径)
13
10
  */
14
11
 
@@ -193,9 +193,7 @@ declare function useMessage(): (key: keyof UIText, values?: Record<string, strin
193
193
  * (自定义 jsxBlock handler)→ slug / katex → React 元素。渲染与解析共用同一套扩展,
194
194
  * 保证"能解析的就能渲染"。
195
195
  * @created 2026-10-24 20:24
196
- * @updated 2026-10-24 20:24
197
- * @updated 2026-11-01 21:47 rawBody 容器把源码以 props 交给组件;渲染侧同步 collectMermaid 标记
198
- * @updated 2026-11-02 10:33 新增 config.text 文案覆写;components/plugins 浅比较稳定化(内联配置不再击穿 memo)
196
+ * @updated 2026-11-18 10:40 四个自带 rehype 插件合并为 rehype-postprocess 单次遍历;新增内部入口 MarkdownTreeRenderer
199
197
  */
200
198
 
201
199
  /** 宿主可覆盖的组件类型 */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@heikeson/md-engine",
3
- "version": "0.5.2",
3
+ "version": "0.5.4",
4
4
  "type": "module",
5
5
  "description": "Markdown 内容引擎:尖括号容器语法(<Callout> 等)解析、渲染、序列化与编辑器内核,可直接消费 TS 源码",
6
6
  "license": "MIT",
@@ -88,6 +88,7 @@
88
88
  "github-slugger": "2.0.0",
89
89
  "hast-util-to-jsx-runtime": "2.3.6",
90
90
  "intl-messageformat": "^12.1.4",
91
+ "katex": "^0.16.0",
91
92
  "lucide-react": "^1.61.0",
92
93
  "mdast-util-from-markdown": "2.0.3",
93
94
  "mdast-util-gfm": "3.1.0",
@@ -137,6 +138,9 @@
137
138
  "engines": {
138
139
  "node": ">=20"
139
140
  },
141
+ "overrides": {
142
+ "sucrase": "^3.35.1"
143
+ },
140
144
  "publishConfig": {
141
145
  "access": "public",
142
146
  "registry": "https://registry.npmjs.org/"
package/styles/cards.css CHANGED
@@ -2,7 +2,6 @@
2
2
  * @file Cards 卡片样式
3
3
  * @description 两列网格(≥640px),颜色取 --lkes-* 令牌;长标题/正文自动换行。
4
4
  * @created 2026-10-27 13:43
5
- * @updated 2026-10-27 13:49
6
5
  * @updated 2026-11-02 20:46 链接形态整卡自带颜色与去下划线,不吃宿主的链接外观;标题补前景色
7
6
  */
8
7
 
package/styles/files.css CHANGED
@@ -2,7 +2,6 @@
2
2
  * @file Files 文件树样式(Files / Folder / File)
3
3
  * @description 原生 details 折叠,零 JS;目录图标双形态靠 details[open] 切换。颜色取 --lkes-* 令牌。
4
4
  * @created 2026-10-27 13:43
5
- * @updated 2026-10-27 13:49
6
5
  * @updated 2026-11-02 20:46 链接形态的文件名不继承宿主链接色与下划线
7
6
  */
8
7
 
package/styles/index.css CHANGED
@@ -5,8 +5,6 @@
5
5
  * 颜色由 styles/tokens.css 的 --lkes-* 令牌提供,令牌未定义时各处的 var() 兜底值生效。
6
6
  * 层顺序:lkes.tokens → lkes.components;两层都低于宿主未分层的声明,宿主覆盖无需抢顺序。
7
7
  * @created 2026-10-27 13:43
8
- * @updated 2026-10-27 13:49
9
- * @updated 2026-11-01 21:34 汇总 mermaid.css
10
8
  * @updated 2026-11-02 20:37 补充层序风险提示:宿主用未分层的元素重置(如 button{border:0})会盖掉组件样式
11
9
  */
12
10