@heikeson/md-engine 0.5.3 → 0.6.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.
Files changed (44) hide show
  1. package/README.en.md +267 -0
  2. package/README.md +154 -271
  3. package/dist/{chunk-YJGGJP2V.js → chunk-BMAGJMFB.js} +1 -2
  4. package/dist/{chunk-OG47YL3H.js → chunk-JUWL5RG7.js} +1 -2
  5. package/dist/{chunk-EJGRPSVC.js → chunk-OKP7VFRV.js} +234 -143
  6. package/dist/{chunk-OJQDOFGL.js → chunk-W463Q7LK.js} +2 -4
  7. package/dist/{chunk-ONVXIYKH.js → chunk-Y3B7I6LZ.js} +140 -19
  8. package/dist/components/index.d.ts +72 -6
  9. package/dist/components/index.js +4 -6
  10. package/dist/editor/index.d.ts +17 -7
  11. package/dist/editor/index.js +72 -25
  12. package/dist/index.d.ts +19 -18
  13. package/dist/index.js +14 -10
  14. package/dist/locales/en-US.d.ts +1 -0
  15. package/dist/locales/en-US.js +1 -3
  16. package/dist/locales/zh-CN.d.ts +1 -0
  17. package/dist/locales/zh-CN.js +1 -3
  18. package/dist/mermaid/index.d.ts +1 -15
  19. package/dist/mermaid/index.js +3 -5
  20. package/dist/{mermaid-placeholder-DYfFG3_f.d.ts → mermaid-placeholder-D97goALt.d.ts} +0 -3
  21. package/dist/{render-BxUxfxMc.d.ts → render-Dv1LAIvk.d.ts} +3 -4
  22. package/dist/styles.css +1 -1
  23. package/package.json +6 -1
  24. package/styles/cards.css +0 -1
  25. package/styles/columns.css +41 -0
  26. package/styles/files.css +0 -1
  27. package/styles/image.css +45 -3
  28. package/styles/index.css +4 -3
  29. package/styles/mermaid.css +0 -6
  30. package/styles/timeline.css +89 -0
  31. package/styles/video.css +28 -0
  32. package//345/257/271/346/216/245/345/245/221/347/272/246.en.md +765 -0
  33. package//345/257/271/346/216/245/345/245/221/347/272/246.md +411 -235
  34. package/dist/chunk-EJGRPSVC.js.map +0 -1
  35. package/dist/chunk-OG47YL3H.js.map +0 -1
  36. package/dist/chunk-OJQDOFGL.js.map +0 -1
  37. package/dist/chunk-ONVXIYKH.js.map +0 -1
  38. package/dist/chunk-YJGGJP2V.js.map +0 -1
  39. package/dist/components/index.js.map +0 -1
  40. package/dist/editor/index.js.map +0 -1
  41. package/dist/index.js.map +0 -1
  42. package/dist/locales/en-US.js.map +0 -1
  43. package/dist/locales/zh-CN.js.map +0 -1
  44. package/dist/mermaid/index.js.map +0 -1
package/README.en.md ADDED
@@ -0,0 +1,267 @@
1
+ # @heikeson/md-engine
2
+
3
+ [简体中文](./README.md) | English
4
+
5
+ A Markdown renderer for React, plus an editor built on CodeMirror 6. GFM and math are supported.
6
+ The interesting part is this:
7
+
8
+ ```md
9
+ <Callout type="tip" title="Heads up">
10
+ The body is still Markdown — `inline code` and [links](https://example.com) work as usual.
11
+ </Callout>
12
+ ```
13
+
14
+ These angle-bracket containers are not scraped out of the rendered HTML with a regex; they land in
15
+ the mdast just like headings and lists. They therefore carry position info, can be serialized back
16
+ to the source, and can be swapped for your own component as a whole block. Run `md → AST → md` and
17
+ the text comes back byte for byte.
18
+
19
+ ## Install
20
+
21
+ ```bash
22
+ npm install @heikeson/md-engine
23
+ ```
24
+
25
+ Requires React 19 or later. The package ships ESM only; older Jest or webpack 4 setups need their
26
+ own interop layer. Styles are plain CSS and have nothing to do with Tailwind — they render fine even
27
+ if the host defines no theme variables.
28
+
29
+ ## Rendering
30
+
31
+ ```tsx
32
+ import { MarkdownRenderer } from "@heikeson/md-engine";
33
+ import "@heikeson/md-engine/styles.css";
34
+
35
+ export default function Article({ content }: { content: string }) {
36
+ return <MarkdownRenderer content={content} className="prose-site" />;
37
+ }
38
+ ```
39
+
40
+ Do not drop `prose-site` — the styles for all eleven containers hang off it. Mermaid uses
41
+ `.lkes-mermaid` and the editor uses `.md-editor`, neither of which you need here.
42
+
43
+ Parsing and rendering never touch `window` or `document`, so the server can call them directly.
44
+
45
+ ## Containers
46
+
47
+ | Container | Props | Children |
48
+ | --- | --- | --- |
49
+ | `Callout` | `type` (`note` `info` `tip` `important` `warning` `caution` `danger`, default `note`), `title`, `icon` | body |
50
+ | `Tabs` | none | `Tab`: `value`, `default` |
51
+ | `Steps` | none | `Step`: `title` |
52
+ | `Cards` | none | `Card`: `title`, `href`, `icon`, `description` |
53
+ | `Accordion` | none | `AccItem`: `title`, `type` (`open` / `close`) |
54
+ | `Files` | none | `Folder`: `name`, `defaultOpen`; `File`: `name`, `href`, `icon` |
55
+ | `TypeTable` | none | `TypeItem`: `name`, `type`, `default`, `required` |
56
+ | `Columns` | none | `Column`: `width` |
57
+ | `Timeline` | none | `TimelineItem`: `date`, `title`, `type` |
58
+ | `Video` | `src`, `poster`, `caption`, `width` | self-closing |
59
+ | `Image` | `src`, `alt`, `align`, `width`, `caption`, `zoom` | self-closing |
60
+ | `Mermaid` | `title`, `height` | raw text, not parsed as Markdown |
61
+ | `div` | `class`, `id` | body; attributes pass through to the DOM |
62
+
63
+ ```md
64
+ <Tabs>
65
+ <Tab value="npm">npm install @heikeson/md-engine</Tab>
66
+ <Tab value="pnpm">pnpm add @heikeson/md-engine</Tab>
67
+ </Tabs>
68
+
69
+ <Cards>
70
+ <Card title="Quick start" description="Smallest example that renders a section" href="/docs/start" />
71
+ </Cards>
72
+
73
+ <Columns>
74
+ <Column width="2">Wide column</Column>
75
+ <Column width="1">Narrow column</Column>
76
+ </Columns>
77
+
78
+ <Timeline>
79
+ <TimelineItem date="2026-11" title="Point">Body</TimelineItem>
80
+ </Timeline>
81
+
82
+ <Video src="/demo.mp4" poster="/demo.png" caption="Demo" width="60%" />
83
+ ```
84
+
85
+ `Image` and `Video` share one `width` grammar: `auto` (natural size), `60%` (share of the column),
86
+ `1/3` (fraction), `360` (pixels), `20rem` (length); anything above 100% is clamped to full width.
87
+ `Image` also takes `align` (`left` / `center` / `right`), which positions the whole figure including
88
+ its caption — block alignment, not text wrapping. `Column`'s `width` means something else: a bare
89
+ number splits by ratio, a value with a unit sets a fixed width.
90
+
91
+ A few hard rules — break them and you get no error, just mangled output:
92
+
93
+ - Containers must start at column 0. Children may be indented, but body lines indented to 4 spaces
94
+ are treated as a code block by CommonMark.
95
+ - Only `a="b"` attributes and bare booleans are understood; `{...}` expressions are not supported.
96
+ - Tags outside the whitelist are shown as literal text, contents included. If you want a plain block
97
+ you can hang a `class` on, use `div`.
98
+ - `Image`, `Video`, `Card` and `File` may self-close; every other container needs a closing tag.
99
+ - Nesting stops at 3 levels; anything deeper is emitted as raw text.
100
+
101
+ ## Parsing and serialization
102
+
103
+ ```ts
104
+ import { parse, serialize, roundtrip, normalizeMarkdown, diagnose } from "@heikeson/md-engine";
105
+
106
+ const { tree, blocks, diagnostics } = parse(md);
107
+ serialize(tree); // back to Markdown, preserving shape and blank lines
108
+ roundtrip(md); // shorthand for the two lines above
109
+ normalizeMarkdown(md); // when you want unified formatting; serialize does not reformat for you
110
+ diagnose(md); // diagnostics only, same as parse(md, { blocks: false }).diagnostics
111
+ ```
112
+
113
+ `blocks` is a flat block index carrying document-wide offsets — handy for tables of contents,
114
+ jumping to a location, and scroll sync. When the editor only needs diagnostics, call `diagnose()` and
115
+ skip the full tree conversion.
116
+
117
+ ## Editor
118
+
119
+ ```tsx
120
+ "use client";
121
+
122
+ import { useState } from "react";
123
+ import { MarkdownEditor } from "@heikeson/md-engine/editor";
124
+ import "@heikeson/md-engine/styles.css";
125
+
126
+ export default function EditorPage() {
127
+ const [value, setValue] = useState("");
128
+ return (
129
+ <MarkdownEditor
130
+ value={value}
131
+ onChange={setValue}
132
+ view="split"
133
+ config={{ placeholder: "Start writing…" }}
134
+ />
135
+ );
136
+ }
137
+ ```
138
+
139
+ It is a controlled component: `value` is the Markdown text, editing on the left and previewing on the
140
+ right. The snippets the toolbar inserts are generated by `registry.ts`, sharing names and props with
141
+ the parsing rules, so they cannot drift apart. Shortcuts go through the same implementation as the
142
+ buttons: `Mod+B`, `Mod+I` and `Mod+E` for bold, italic and inline code, `Mod+Shift+S` for strikethrough,
143
+ `Mod+1` for a level-2 heading, `Mod+Shift+Q` for a quote, `Mod+Shift+L` for a list.
144
+
145
+ ## Diagrams
146
+
147
+ `<Mermaid>` renders source code only by default. To draw actual diagrams on the page, merge the
148
+ overrides inside a client component:
149
+
150
+ ```tsx
151
+ "use client";
152
+ import { MarkdownRenderer, mdxComponents } from "@heikeson/md-engine";
153
+ import { mermaidComponents } from "@heikeson/md-engine/mermaid";
154
+
155
+ const components = { ...mdxComponents, ...mermaidComponents };
156
+
157
+ <MarkdownRenderer content={md} config={{ components }} />;
158
+ ```
159
+
160
+ The key is `Mermaid`, capitalized. Lowercase raises no error — the diagram just stays as source.
161
+ `mermaid` is an optional peer; without it, rendering degrades to source code and parsing/serialization
162
+ are unaffected.
163
+
164
+ ## Locales
165
+
166
+ Built-in `zh-CN` and `en-US`. Pick a locale bundle with `config.locale`, or override individual strings
167
+ with `config.messages`. To set it once at the root, wrap the app in a Provider:
168
+
169
+ ```tsx
170
+ import { MdEngineProvider } from "@heikeson/md-engine";
171
+
172
+ <MdEngineProvider locale={locale}>
173
+ <App />
174
+ </MdEngineProvider>
175
+ ```
176
+
177
+ `locale` must be a controlled value that matches on server and client — usually taken from a URL
178
+ segment or a request header. The library does not read `navigator`, cookies or `<html lang>`, because
179
+ those make the first paint disagree with the server under SSR. If you really need client-side
180
+ detection, call `detectLocale()` inside an effect.
181
+
182
+ ## Swapping components
183
+
184
+ ```tsx
185
+ <MarkdownRenderer
186
+ content={md}
187
+ config={{
188
+ components: { Callout: MyCallout }, // replace just this one, everything else uses the built-in
189
+ remarkPlugins: [],
190
+ rehypePlugins: [], // must be synchronous, otherwise SSR is unsafe
191
+ sourcePositions: false, // only needed for editor scroll sync
192
+ }}
193
+ />
194
+ ```
195
+
196
+ Inside your own components, read strings with `useUIText()` and format with `useMessage()` using ICU
197
+ syntax.
198
+
199
+ ## Styles
200
+
201
+ Import `@heikeson/md-engine/styles.css` once. Colors come from standalone `--lkes-*` variables: nine
202
+ tokens with light and dark literals. Add `.dark` or `[data-theme="dark"]` on an ancestor to switch the
203
+ whole thing to dark. The token list and override recipes are in section 10 of
204
+ [the integration contract](./对接契约.en.md).
205
+
206
+ Importing per component also works — `styles/*.css` is published as-is in the npm package. But the
207
+ tokens live in `tokens.css` alone: import only a slice and you get no `--lkes-*`, so dark mode stops
208
+ working and colors fall back to hardcoded values. Order matters too — `tokens` first, so the
209
+ `components` layer can win over the token layer:
210
+
211
+ ```ts
212
+ import "@heikeson/md-engine/styles/tokens.css";
213
+ import "@heikeson/md-engine/styles/callout.css";
214
+ ```
215
+
216
+ ## Development
217
+
218
+ ```bash
219
+ npm run dev # playground for renderer + editor work
220
+ npm run snapshot # static snapshot with roundtrip and diagnostics self-checks
221
+ npm run selfcheck # parse, render and serialize assertions against the build
222
+ npm run typecheck # tsc --noEmit
223
+ npm run build # dist and dist/styles.css
224
+ ```
225
+
226
+ `dev/`, `docs/` and `site/` are not published; `files` ships only `dist`, `styles`, the READMEs,
227
+ `对接契约.md`, `对接契约.en.md` and `LICENSE`.
228
+
229
+ ```
230
+ src/
231
+ ├─ registry.ts syntax declarations, the single source of truth
232
+ ├─ policy.ts depth trimming, unclosed detection, diagnostics
233
+ ├─ parse.ts / blocks.ts
234
+ ├─ render.tsx / serialize.ts / format.ts
235
+ ├─ syntax/ micromark angle-bracket extension
236
+ ├─ components/ container components and mapping
237
+ ├─ mermaid/ the /mermaid entry
238
+ ├─ editor/ CodeMirror wrapper, toolbar, scroll sync
239
+ └─ locales/ locale bundles
240
+ styles/ plain CSS slices
241
+ ```
242
+
243
+ ## Entries
244
+
245
+ ```
246
+ @heikeson/md-engine parse, serialize, MarkdownRenderer, Provider
247
+ @heikeson/md-engine/editor MarkdownEditor
248
+ @heikeson/md-engine/mermaid interactive diagrams
249
+ @heikeson/md-engine/components individual container components
250
+ @heikeson/md-engine/locales/zh-CN locale bundle
251
+ @heikeson/md-engine/locales/en-US
252
+ @heikeson/md-engine/styles.css all styles in one file
253
+ ```
254
+
255
+ The main entry exports no editor code at all. Full signatures, diagnostic codes and CSS variables live
256
+ in [the integration contract](./对接契约.en.md) (Chinese original: [对接契约.md](./对接契约.md)); version
257
+ history is in [update.md](./update.md) (Chinese).
258
+
259
+ ## Compatibility
260
+
261
+ Shipped API stays put: component names, prop names and semantics only grow, new config options must
262
+ carry a default, and style class names and variables are supplied by the package. Anything else is a
263
+ breaking change and needs a major version.
264
+
265
+ ## License
266
+
267
+ MIT