@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.
- package/LICENSE +21 -0
- package/README.md +297 -3
- package/dist/chunk-JQUYUM7A.js +335 -0
- package/dist/chunk-JQUYUM7A.js.map +1 -0
- package/dist/chunk-KY27P34E.js +761 -0
- package/dist/chunk-KY27P34E.js.map +1 -0
- package/dist/components/index.d.ts +213 -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 +27 -0
- package/dist/index.js.map +1 -0
- package/package.json +101 -4
- package/styles/engine.css +342 -0
- package//345/257/271/346/216/245/345/245/221/347/272/246.md +348 -0
|
@@ -0,0 +1,761 @@
|
|
|
1
|
+
import { mdxComponents } from './chunk-JQUYUM7A.js';
|
|
2
|
+
import { asciiAlpha, asciiAlphanumeric, markdownLineEnding } from 'micromark-util-character';
|
|
3
|
+
import { fromMarkdown } from 'mdast-util-from-markdown';
|
|
4
|
+
import { gfm } from 'micromark-extension-gfm';
|
|
5
|
+
import { gfmFromMarkdown } from 'mdast-util-gfm';
|
|
6
|
+
import { useMemo } from 'react';
|
|
7
|
+
import { jsxs, jsx, Fragment } from 'react/jsx-runtime';
|
|
8
|
+
import { unified } from 'unified';
|
|
9
|
+
import remarkParse from 'remark-parse';
|
|
10
|
+
import remarkGfm from 'remark-gfm';
|
|
11
|
+
import remarkMath from 'remark-math';
|
|
12
|
+
import remarkRehype from 'remark-rehype';
|
|
13
|
+
import rehypeSlug from 'rehype-slug';
|
|
14
|
+
import rehypeKatex from 'rehype-katex';
|
|
15
|
+
import { toJsxRuntime } from 'hast-util-to-jsx-runtime';
|
|
16
|
+
import { visit } from 'unist-util-visit';
|
|
17
|
+
|
|
18
|
+
// src/registry.ts
|
|
19
|
+
var MAX_CONTAINER_DEPTH = 3;
|
|
20
|
+
var ITEMS = {
|
|
21
|
+
Tab: { name: "Tab", attrs: ["value"], boolAttrs: ["default"] },
|
|
22
|
+
Step: { name: "Step", attrs: ["title"] },
|
|
23
|
+
// 允许自闭合:fumadocs 风格 `<Card title="…" description="…" />`(描述写在属性里)
|
|
24
|
+
Card: { name: "Card", attrs: ["title", "href", "icon", "description"], selfClosing: true },
|
|
25
|
+
// type: open / close —— 控制默认展开还是收起
|
|
26
|
+
AccItem: { name: "AccItem", attrs: ["title", "type"] },
|
|
27
|
+
// 文件树目录:可嵌套,defaultOpen 布尔控制默认展开
|
|
28
|
+
Folder: { name: "Folder", attrs: ["name"], boolAttrs: ["defaultOpen"] },
|
|
29
|
+
// 文件树叶子:自闭合,href 可选(给了就渲染成链接)
|
|
30
|
+
File: { name: "File", attrs: ["name", "href"], selfClosing: true },
|
|
31
|
+
// 类型表行:name 属性名、type 类型、default 默认值、required 布尔必填
|
|
32
|
+
TypeItem: { name: "TypeItem", attrs: ["name", "type", "default"], boolAttrs: ["required"] }
|
|
33
|
+
};
|
|
34
|
+
var COMPONENTS = {
|
|
35
|
+
Callout: {
|
|
36
|
+
name: "Callout",
|
|
37
|
+
mode: "visual",
|
|
38
|
+
attrs: ["type", "title"],
|
|
39
|
+
defaultAttrs: { type: "note" }
|
|
40
|
+
},
|
|
41
|
+
Tabs: { name: "Tabs", mode: "visual", items: "Tab" },
|
|
42
|
+
Steps: { name: "Steps", mode: "visual", items: "Step" },
|
|
43
|
+
Cards: { name: "Cards", mode: "visual", items: "Card" },
|
|
44
|
+
Accordion: { name: "Accordion", mode: "visual", items: "AccItem" },
|
|
45
|
+
// 文件树容器:子项可混用 Folder / File
|
|
46
|
+
Files: { name: "Files", mode: "visual", items: ["Folder", "File"] },
|
|
47
|
+
// 类型表:fumadocs 的 TypeTable,但属性只支持字符串,故用子项而非对象表达式
|
|
48
|
+
TypeTable: { name: "TypeTable", mode: "visual", items: "TypeItem" },
|
|
49
|
+
Image: {
|
|
50
|
+
name: "Image",
|
|
51
|
+
mode: "visual",
|
|
52
|
+
selfClosing: true,
|
|
53
|
+
attrs: ["src", "alt", "align", "width", "caption"],
|
|
54
|
+
boolAttrs: ["zoom"],
|
|
55
|
+
defaultAttrs: { align: "center" }
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
function getComponent(name) {
|
|
59
|
+
return COMPONENTS[name];
|
|
60
|
+
}
|
|
61
|
+
function getItemSpec(name) {
|
|
62
|
+
return ITEMS[name];
|
|
63
|
+
}
|
|
64
|
+
function listComponents() {
|
|
65
|
+
return Object.keys(COMPONENTS);
|
|
66
|
+
}
|
|
67
|
+
function isContainerWithItems(name) {
|
|
68
|
+
return Boolean(COMPONENTS[name]?.items);
|
|
69
|
+
}
|
|
70
|
+
var LT = 60;
|
|
71
|
+
var GT = 62;
|
|
72
|
+
var SLASH = 47;
|
|
73
|
+
var DQUOTE = 34;
|
|
74
|
+
var SQUOTE = 39;
|
|
75
|
+
var SPACE = 32;
|
|
76
|
+
function isKnownName(name) {
|
|
77
|
+
return Boolean(COMPONENTS[name]) || Boolean(ITEMS[name]);
|
|
78
|
+
}
|
|
79
|
+
function isSelfCloseAllowed(name) {
|
|
80
|
+
return COMPONENTS[name]?.selfClosing === true || ITEMS[name]?.selfClosing === true;
|
|
81
|
+
}
|
|
82
|
+
var OPEN_STATE = /* @__PURE__ */ new WeakMap();
|
|
83
|
+
function stateOf(context) {
|
|
84
|
+
let state = OPEN_STATE.get(context.parser);
|
|
85
|
+
if (!state) {
|
|
86
|
+
state = { seq: 0, stack: [] };
|
|
87
|
+
OPEN_STATE.set(context.parser, state);
|
|
88
|
+
}
|
|
89
|
+
return state;
|
|
90
|
+
}
|
|
91
|
+
var jsxBlock = {
|
|
92
|
+
tokenize: tokenizeJsxBlock,
|
|
93
|
+
concrete: true
|
|
94
|
+
};
|
|
95
|
+
function jsxBlocks() {
|
|
96
|
+
return {
|
|
97
|
+
flow: {
|
|
98
|
+
[LT]: jsxBlock
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
function tokenizeJsxBlock(effects, ok, nok) {
|
|
103
|
+
const self = this;
|
|
104
|
+
let name = "";
|
|
105
|
+
let closingName = "";
|
|
106
|
+
let prevCode = null;
|
|
107
|
+
let quote = null;
|
|
108
|
+
let hasAttributes = false;
|
|
109
|
+
let previous;
|
|
110
|
+
const state = stateOf(self);
|
|
111
|
+
let myId = -1;
|
|
112
|
+
function popSelf() {
|
|
113
|
+
if (state.stack[state.stack.length - 1] === myId) state.stack.pop();
|
|
114
|
+
}
|
|
115
|
+
const nonLazyLine = { tokenize: tokenizeNonLazyLine, partial: true };
|
|
116
|
+
let nestedDepth = 0;
|
|
117
|
+
const closeConstruct = { tokenize: tokenizeCloseTag, partial: true };
|
|
118
|
+
const openConstruct = { tokenize: tokenizeOpenProbe, partial: true };
|
|
119
|
+
return start;
|
|
120
|
+
function start(code) {
|
|
121
|
+
if (code !== LT) return nok(code);
|
|
122
|
+
effects.enter("jsxBlock");
|
|
123
|
+
effects.enter("jsxBlockOpenTag");
|
|
124
|
+
effects.enter("jsxBlockOpenTagMarker");
|
|
125
|
+
effects.consume(code);
|
|
126
|
+
effects.exit("jsxBlockOpenTagMarker");
|
|
127
|
+
return openName;
|
|
128
|
+
}
|
|
129
|
+
function openName(code) {
|
|
130
|
+
if (code !== null && asciiAlpha(code)) {
|
|
131
|
+
effects.enter("jsxBlockName");
|
|
132
|
+
effects.consume(code);
|
|
133
|
+
return inName;
|
|
134
|
+
}
|
|
135
|
+
return nok(code);
|
|
136
|
+
}
|
|
137
|
+
function inName(code) {
|
|
138
|
+
if (code !== null && asciiAlphanumeric(code)) {
|
|
139
|
+
effects.consume(code);
|
|
140
|
+
return inName;
|
|
141
|
+
}
|
|
142
|
+
name = self.sliceSerialize(effects.exit("jsxBlockName"), true);
|
|
143
|
+
if (!isKnownName(name)) return nok(code);
|
|
144
|
+
return afterName(code);
|
|
145
|
+
}
|
|
146
|
+
function afterName(code) {
|
|
147
|
+
if (code === GT) return closeOpenTag(code, false);
|
|
148
|
+
if (code === null || markdownLineEnding(code)) return nok(code);
|
|
149
|
+
hasAttributes = true;
|
|
150
|
+
effects.enter("jsxBlockAttributes");
|
|
151
|
+
return attributes(code);
|
|
152
|
+
}
|
|
153
|
+
function attributes(code) {
|
|
154
|
+
if (code === null || markdownLineEnding(code)) return nok(code);
|
|
155
|
+
if (quote !== null) {
|
|
156
|
+
effects.consume(code);
|
|
157
|
+
if (code === quote) quote = null;
|
|
158
|
+
prevCode = code;
|
|
159
|
+
return attributes;
|
|
160
|
+
}
|
|
161
|
+
if (code === DQUOTE || code === SQUOTE) {
|
|
162
|
+
quote = code;
|
|
163
|
+
effects.consume(code);
|
|
164
|
+
prevCode = code;
|
|
165
|
+
return attributes;
|
|
166
|
+
}
|
|
167
|
+
if (code === GT) return closeOpenTag(code, prevCode === SLASH);
|
|
168
|
+
effects.consume(code);
|
|
169
|
+
prevCode = code;
|
|
170
|
+
return attributes;
|
|
171
|
+
}
|
|
172
|
+
function closeOpenTag(code, selfClosing) {
|
|
173
|
+
if (selfClosing && !isSelfCloseAllowed(name)) return nok(code);
|
|
174
|
+
effects.enter("jsxBlockOpenTagEnd");
|
|
175
|
+
effects.consume(code);
|
|
176
|
+
effects.exit("jsxBlockOpenTagEnd");
|
|
177
|
+
if (hasAttributes) effects.exit("jsxBlockAttributes");
|
|
178
|
+
effects.exit("jsxBlockOpenTag");
|
|
179
|
+
if (!selfClosing) {
|
|
180
|
+
myId = state.seq + 1;
|
|
181
|
+
state.seq = myId;
|
|
182
|
+
state.stack.push(myId);
|
|
183
|
+
}
|
|
184
|
+
return selfClosing ? finishBlock : openAfter;
|
|
185
|
+
}
|
|
186
|
+
function openAfter(code) {
|
|
187
|
+
if (code === null) return unclosed(code);
|
|
188
|
+
if (markdownLineEnding(code)) {
|
|
189
|
+
if (self.interrupt) {
|
|
190
|
+
popSelf();
|
|
191
|
+
effects.exit("jsxBlock");
|
|
192
|
+
return ok(code);
|
|
193
|
+
}
|
|
194
|
+
return effects.attempt(nonLazyLine, contentStart, unclosed)(code);
|
|
195
|
+
}
|
|
196
|
+
popSelf();
|
|
197
|
+
return nok(code);
|
|
198
|
+
}
|
|
199
|
+
function contentStart(code) {
|
|
200
|
+
effects.enter("jsxBlockContent");
|
|
201
|
+
return lineStart(code);
|
|
202
|
+
}
|
|
203
|
+
function lineStart(code) {
|
|
204
|
+
return effects.check(closeConstruct, closeDetected, notClose)(code);
|
|
205
|
+
}
|
|
206
|
+
function closeDetected(code) {
|
|
207
|
+
if (nestedDepth > 0) {
|
|
208
|
+
nestedDepth -= 1;
|
|
209
|
+
return chunkStart(code);
|
|
210
|
+
}
|
|
211
|
+
return effects.attempt(closeConstruct, afterContent, chunkStart)(code);
|
|
212
|
+
}
|
|
213
|
+
function notClose(code) {
|
|
214
|
+
return effects.check(openConstruct, openDetected, chunkStart)(code);
|
|
215
|
+
}
|
|
216
|
+
function openDetected(code) {
|
|
217
|
+
nestedDepth += 1;
|
|
218
|
+
return chunkStart(code);
|
|
219
|
+
}
|
|
220
|
+
function chunkStart(code) {
|
|
221
|
+
if (code === null) return afterContent(code);
|
|
222
|
+
return chunkNonLazyStart(code);
|
|
223
|
+
}
|
|
224
|
+
function chunkNonLazyStart(code) {
|
|
225
|
+
const token = effects.enter("chunkDocument", { contentType: "document", previous });
|
|
226
|
+
if (previous) previous.next = token;
|
|
227
|
+
previous = token;
|
|
228
|
+
return contentContinue(code);
|
|
229
|
+
}
|
|
230
|
+
function contentContinue(code) {
|
|
231
|
+
if (code === null) {
|
|
232
|
+
const t = effects.exit("chunkDocument");
|
|
233
|
+
self.parser.lazy[t.start.line] = false;
|
|
234
|
+
return afterContent(code);
|
|
235
|
+
}
|
|
236
|
+
if (markdownLineEnding(code)) {
|
|
237
|
+
return nonLazyLineAfter(code);
|
|
238
|
+
}
|
|
239
|
+
effects.consume(code);
|
|
240
|
+
return contentContinue;
|
|
241
|
+
}
|
|
242
|
+
function nonLazyLineAfter(code) {
|
|
243
|
+
effects.consume(code);
|
|
244
|
+
const t = effects.exit("chunkDocument");
|
|
245
|
+
self.parser.lazy[t.start.line] = false;
|
|
246
|
+
return lineStart;
|
|
247
|
+
}
|
|
248
|
+
function afterContent(code) {
|
|
249
|
+
effects.exit("jsxBlockContent");
|
|
250
|
+
return finishBlock(code);
|
|
251
|
+
}
|
|
252
|
+
function finishBlock(code) {
|
|
253
|
+
popSelf();
|
|
254
|
+
effects.exit("jsxBlock");
|
|
255
|
+
return ok(code);
|
|
256
|
+
}
|
|
257
|
+
function unclosed(code) {
|
|
258
|
+
popSelf();
|
|
259
|
+
effects.exit("jsxBlock");
|
|
260
|
+
return ok(code);
|
|
261
|
+
}
|
|
262
|
+
function tokenizeCloseTag(effects2, ok2, nok2) {
|
|
263
|
+
return closeStart;
|
|
264
|
+
function closeStart(code) {
|
|
265
|
+
if (code !== LT) return nok2(code);
|
|
266
|
+
effects2.enter("jsxBlockCloseTag");
|
|
267
|
+
effects2.enter("jsxBlockCloseTagMarker");
|
|
268
|
+
effects2.consume(code);
|
|
269
|
+
return closeSlash;
|
|
270
|
+
}
|
|
271
|
+
function closeSlash(code) {
|
|
272
|
+
if (code !== SLASH) return nok2(code);
|
|
273
|
+
effects2.consume(code);
|
|
274
|
+
effects2.exit("jsxBlockCloseTagMarker");
|
|
275
|
+
return closeNameStart;
|
|
276
|
+
}
|
|
277
|
+
function closeNameStart(code) {
|
|
278
|
+
if (code !== null && asciiAlpha(code)) {
|
|
279
|
+
effects2.enter("jsxBlockCloseName");
|
|
280
|
+
effects2.consume(code);
|
|
281
|
+
return closeNameRest;
|
|
282
|
+
}
|
|
283
|
+
return nok2(code);
|
|
284
|
+
}
|
|
285
|
+
function closeNameRest(code) {
|
|
286
|
+
if (code !== null && asciiAlphanumeric(code)) {
|
|
287
|
+
effects2.consume(code);
|
|
288
|
+
return closeNameRest;
|
|
289
|
+
}
|
|
290
|
+
closingName = self.sliceSerialize(effects2.exit("jsxBlockCloseName"), true);
|
|
291
|
+
if (closingName !== name) return nok2(code);
|
|
292
|
+
if (state.stack[state.stack.length - 1] !== myId) return nok2(code);
|
|
293
|
+
if (code !== GT) return nok2(code);
|
|
294
|
+
effects2.enter("jsxBlockCloseTagEnd");
|
|
295
|
+
effects2.consume(code);
|
|
296
|
+
effects2.exit("jsxBlockCloseTagEnd");
|
|
297
|
+
effects2.exit("jsxBlockCloseTag");
|
|
298
|
+
return ok2(code);
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
function tokenizeOpenProbe(effects2, ok2, nok2) {
|
|
302
|
+
let index = 0;
|
|
303
|
+
let probeQuote = null;
|
|
304
|
+
let probePrev = null;
|
|
305
|
+
return probeStart;
|
|
306
|
+
function probeStart(code) {
|
|
307
|
+
if (code !== LT) return nok2(code);
|
|
308
|
+
effects2.enter("jsxBlockProbe");
|
|
309
|
+
effects2.consume(code);
|
|
310
|
+
return probeName;
|
|
311
|
+
}
|
|
312
|
+
function probeName(code) {
|
|
313
|
+
if (index < name.length) {
|
|
314
|
+
if (code !== name.charCodeAt(index)) return nok2(code);
|
|
315
|
+
effects2.consume(code);
|
|
316
|
+
index += 1;
|
|
317
|
+
return probeName;
|
|
318
|
+
}
|
|
319
|
+
if (code === SPACE) {
|
|
320
|
+
effects2.consume(code);
|
|
321
|
+
return probeAttrs;
|
|
322
|
+
}
|
|
323
|
+
if (code === GT) {
|
|
324
|
+
effects2.consume(code);
|
|
325
|
+
effects2.exit("jsxBlockProbe");
|
|
326
|
+
return ok2(code);
|
|
327
|
+
}
|
|
328
|
+
return nok2(code);
|
|
329
|
+
}
|
|
330
|
+
function probeAttrs(code) {
|
|
331
|
+
if (code === null || markdownLineEnding(code)) return nok2(code);
|
|
332
|
+
if (probeQuote !== null) {
|
|
333
|
+
effects2.consume(code);
|
|
334
|
+
if (code === probeQuote) probeQuote = null;
|
|
335
|
+
return probeAttrs;
|
|
336
|
+
}
|
|
337
|
+
if (code === DQUOTE || code === SQUOTE) {
|
|
338
|
+
probeQuote = code;
|
|
339
|
+
effects2.consume(code);
|
|
340
|
+
return probeAttrs;
|
|
341
|
+
}
|
|
342
|
+
if (code === GT) {
|
|
343
|
+
effects2.consume(code);
|
|
344
|
+
effects2.exit("jsxBlockProbe");
|
|
345
|
+
return probePrev === SLASH ? nok2(code) : ok2(code);
|
|
346
|
+
}
|
|
347
|
+
probePrev = code;
|
|
348
|
+
effects2.consume(code);
|
|
349
|
+
return probeAttrs;
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
function tokenizeNonLazyLine(effects2, ok2, nok2) {
|
|
353
|
+
return nonLazyStart;
|
|
354
|
+
function nonLazyStart(code) {
|
|
355
|
+
effects2.enter("lineEnding");
|
|
356
|
+
effects2.consume(code);
|
|
357
|
+
effects2.exit("lineEnding");
|
|
358
|
+
return nonLazyAfter;
|
|
359
|
+
}
|
|
360
|
+
function nonLazyAfter(code) {
|
|
361
|
+
return self.parser.lazy[self.now().line] ? nok2(code) : ok2(code);
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
// src/syntax/jsx-mdast.ts
|
|
367
|
+
var OPEN_TAG_RE = /^<([A-Za-z][\w]*)([\s\S]*?)(\/?)>$/;
|
|
368
|
+
var ATTR_RE = /([A-Za-z_][\w-]*)(?:\s*=\s*(?:"([^"]*)"|'([^']*)'))?/g;
|
|
369
|
+
function parseOpenTag(raw) {
|
|
370
|
+
const match = OPEN_TAG_RE.exec(raw.trim());
|
|
371
|
+
if (!match) return { name: "", attributes: {}, selfClosing: false };
|
|
372
|
+
const attributes = {};
|
|
373
|
+
const rest = match[2] ?? "";
|
|
374
|
+
ATTR_RE.lastIndex = 0;
|
|
375
|
+
let item;
|
|
376
|
+
while ((item = ATTR_RE.exec(rest)) !== null) {
|
|
377
|
+
const key = item[1];
|
|
378
|
+
if (!key) continue;
|
|
379
|
+
const value = item[2] ?? item[3];
|
|
380
|
+
attributes[key] = value === void 0 ? true : value;
|
|
381
|
+
}
|
|
382
|
+
return { name: match[1], attributes, selfClosing: match[3] === "/" };
|
|
383
|
+
}
|
|
384
|
+
function jsxFromMarkdown() {
|
|
385
|
+
return {
|
|
386
|
+
enter: {
|
|
387
|
+
jsxBlock: enterBlock
|
|
388
|
+
},
|
|
389
|
+
exit: {
|
|
390
|
+
jsxBlockOpenTag: exitOpenTag,
|
|
391
|
+
jsxBlock: exitBlock
|
|
392
|
+
}
|
|
393
|
+
};
|
|
394
|
+
}
|
|
395
|
+
function jsxToMarkdown() {
|
|
396
|
+
return {
|
|
397
|
+
handlers: {
|
|
398
|
+
jsxBlock: handleJsxBlock
|
|
399
|
+
}
|
|
400
|
+
};
|
|
401
|
+
}
|
|
402
|
+
function enterBlock(token) {
|
|
403
|
+
const node = {
|
|
404
|
+
type: "jsxBlock",
|
|
405
|
+
name: "",
|
|
406
|
+
attributes: {},
|
|
407
|
+
selfClosing: false,
|
|
408
|
+
children: []
|
|
409
|
+
};
|
|
410
|
+
this.enter(node, token);
|
|
411
|
+
}
|
|
412
|
+
function exitOpenTag(token) {
|
|
413
|
+
const node = this.stack[this.stack.length - 1];
|
|
414
|
+
const parsed = parseOpenTag(this.sliceSerialize(token));
|
|
415
|
+
node.name = parsed.name;
|
|
416
|
+
node.attributes = parsed.attributes;
|
|
417
|
+
node.selfClosing = parsed.selfClosing;
|
|
418
|
+
node.data = { closed: true };
|
|
419
|
+
}
|
|
420
|
+
function exitBlock(token) {
|
|
421
|
+
this.exit(token);
|
|
422
|
+
}
|
|
423
|
+
function serializeAttributes(attributes) {
|
|
424
|
+
return Object.entries(attributes).map(([key, value]) => {
|
|
425
|
+
if (value === true) return ` ${key}`;
|
|
426
|
+
return ` ${key}="${String(value).replace(/"/g, """)}"`;
|
|
427
|
+
}).join("");
|
|
428
|
+
}
|
|
429
|
+
function handleJsxBlock(node, _parent, state, info) {
|
|
430
|
+
const attrs = serializeAttributes(node.attributes ?? {});
|
|
431
|
+
if (node.selfClosing) return `<${node.name}${attrs} />`;
|
|
432
|
+
const inner = state.containerFlow(node, info);
|
|
433
|
+
return `<${node.name}${attrs}>
|
|
434
|
+
|
|
435
|
+
${inner}
|
|
436
|
+
|
|
437
|
+
</${node.name}>`;
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
// src/syntax/jsx-remark.ts
|
|
441
|
+
function remarkJsxBlocks() {
|
|
442
|
+
const data = this.data();
|
|
443
|
+
(data.micromarkExtensions ??= []).push(jsxBlocks());
|
|
444
|
+
(data.fromMarkdownExtensions ??= []).push(jsxFromMarkdown());
|
|
445
|
+
(data.toMarkdownExtensions ??= []).push(jsxToMarkdown());
|
|
446
|
+
return void 0;
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
// src/blocks.ts
|
|
450
|
+
function isJsxBlock(node) {
|
|
451
|
+
return node.type === "jsxBlock";
|
|
452
|
+
}
|
|
453
|
+
function rangeOf(node) {
|
|
454
|
+
const start = node.position?.start.offset ?? 0;
|
|
455
|
+
const end = node.position?.end.offset ?? start;
|
|
456
|
+
return [start, end];
|
|
457
|
+
}
|
|
458
|
+
function toBlocks(children, source, depth) {
|
|
459
|
+
return children.map((child) => toBlock(child, source, depth));
|
|
460
|
+
}
|
|
461
|
+
function toBlock(node, source, depth) {
|
|
462
|
+
const [start, end] = rangeOf(node);
|
|
463
|
+
if (isJsxBlock(node)) {
|
|
464
|
+
const spec = COMPONENTS[node.name];
|
|
465
|
+
const children = node.children ?? [];
|
|
466
|
+
const itemNames = spec?.items ? Array.isArray(spec.items) ? spec.items : [spec.items] : [];
|
|
467
|
+
if (itemNames.length > 0) {
|
|
468
|
+
const items = children.filter(isJsxBlock).filter((child) => itemNames.includes(child.name)).map((child) => {
|
|
469
|
+
const [itemStart, itemEnd] = rangeOf(child);
|
|
470
|
+
return {
|
|
471
|
+
name: child.name,
|
|
472
|
+
attrs: child.attributes,
|
|
473
|
+
// 子项内部再出现的容器属于下一层(与 policy 的计数口径一致)
|
|
474
|
+
blocks: toBlocks(child.children ?? [], source, depth + 1),
|
|
475
|
+
start: itemStart,
|
|
476
|
+
end: itemEnd
|
|
477
|
+
};
|
|
478
|
+
});
|
|
479
|
+
return {
|
|
480
|
+
kind: "container",
|
|
481
|
+
name: node.name,
|
|
482
|
+
attrs: node.attributes,
|
|
483
|
+
depth,
|
|
484
|
+
blocks: [],
|
|
485
|
+
items,
|
|
486
|
+
start,
|
|
487
|
+
end
|
|
488
|
+
};
|
|
489
|
+
}
|
|
490
|
+
return {
|
|
491
|
+
kind: "container",
|
|
492
|
+
name: node.name,
|
|
493
|
+
attrs: node.attributes,
|
|
494
|
+
depth,
|
|
495
|
+
blocks: toBlocks(children, source, depth + 1),
|
|
496
|
+
items: [],
|
|
497
|
+
start,
|
|
498
|
+
end
|
|
499
|
+
};
|
|
500
|
+
}
|
|
501
|
+
if (node.type === "html") {
|
|
502
|
+
return { kind: "source", text: source.slice(start, end), reason: "html", start, end };
|
|
503
|
+
}
|
|
504
|
+
if (node.type === "definition") {
|
|
505
|
+
return { kind: "source", text: source.slice(start, end), reason: "definition", start, end };
|
|
506
|
+
}
|
|
507
|
+
return { kind: "visual", type: node.type, start, end };
|
|
508
|
+
}
|
|
509
|
+
function extractBlocks(tree, source) {
|
|
510
|
+
return toBlocks(tree.children, source, 1);
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
// src/policy.ts
|
|
514
|
+
function isJsxBlock2(node) {
|
|
515
|
+
return node.type === "jsxBlock";
|
|
516
|
+
}
|
|
517
|
+
function sliceNode(source, node) {
|
|
518
|
+
const start = node.position?.start.offset;
|
|
519
|
+
const end = node.position?.end.offset;
|
|
520
|
+
if (typeof start !== "number" || typeof end !== "number") return "";
|
|
521
|
+
return source.slice(start, end);
|
|
522
|
+
}
|
|
523
|
+
function walk(parent, source, depth, maxDepth, out) {
|
|
524
|
+
const children = parent.children;
|
|
525
|
+
if (!children) return;
|
|
526
|
+
const next = [];
|
|
527
|
+
for (const child of children) {
|
|
528
|
+
if (!isJsxBlock2(child)) {
|
|
529
|
+
walk(child, source, depth, maxDepth, out);
|
|
530
|
+
next.push(child);
|
|
531
|
+
continue;
|
|
532
|
+
}
|
|
533
|
+
const isContainer = Boolean(COMPONENTS[child.name]);
|
|
534
|
+
const childDepth = depth + (isContainer ? 1 : 0);
|
|
535
|
+
if (isContainer && childDepth > maxDepth) {
|
|
536
|
+
const text = sliceNode(source, child);
|
|
537
|
+
const line = child.position?.start.line ?? 1;
|
|
538
|
+
out.push({
|
|
539
|
+
code: "DEPTH_EXCEEDED",
|
|
540
|
+
level: "warn",
|
|
541
|
+
line,
|
|
542
|
+
message: `\u7B2C ${childDepth} \u5C42\u5BB9\u5668 ${child.name} \u8D85\u51FA\u4E0A\u9650 ${maxDepth}\uFF0C\u5DF2\u6309\u6E90\u7801\u663E\u793A`,
|
|
543
|
+
range: [child.position?.start.offset ?? 0, child.position?.end.offset ?? 0]
|
|
544
|
+
});
|
|
545
|
+
const fallback = { type: "text", value: text };
|
|
546
|
+
next.push(fallback);
|
|
547
|
+
continue;
|
|
548
|
+
}
|
|
549
|
+
walk(child, source, childDepth, maxDepth, out);
|
|
550
|
+
next.push(child);
|
|
551
|
+
}
|
|
552
|
+
parent.children = next;
|
|
553
|
+
}
|
|
554
|
+
function enforceMaxDepth(tree, source, maxDepth = MAX_CONTAINER_DEPTH) {
|
|
555
|
+
const diagnostics = [];
|
|
556
|
+
walk(tree, source, 0, maxDepth, diagnostics);
|
|
557
|
+
return diagnostics;
|
|
558
|
+
}
|
|
559
|
+
function hasCloseTag(block, source) {
|
|
560
|
+
const start = block.position?.start.offset ?? 0;
|
|
561
|
+
const end = block.position?.end.offset ?? 0;
|
|
562
|
+
if (end <= start) return false;
|
|
563
|
+
const body = new RegExp(`</${block.name}\\s*>`);
|
|
564
|
+
if (body.test(source.slice(start, end))) return true;
|
|
565
|
+
return new RegExp(`^\\s*</${block.name}\\s*>`).test(source.slice(end, end + 64));
|
|
566
|
+
}
|
|
567
|
+
function collectUnclosed(tree, source) {
|
|
568
|
+
const diagnostics = [];
|
|
569
|
+
const visit3 = (node) => {
|
|
570
|
+
if (node.type === "jsxBlock") {
|
|
571
|
+
const block = node;
|
|
572
|
+
if (!block.selfClosing && !hasCloseTag(block, source)) {
|
|
573
|
+
diagnostics.push({
|
|
574
|
+
code: "UNCLOSED_TAG",
|
|
575
|
+
level: "warn",
|
|
576
|
+
line: block.position?.start.line ?? 1,
|
|
577
|
+
message: `<${block.name}> \u672A\u95ED\u5408\uFF0C\u5DF2\u6309\u5EF6\u4F38\u5230\u6587\u672B\u5904\u7406`,
|
|
578
|
+
range: [block.position?.start.offset ?? 0, block.position?.end.offset ?? 0]
|
|
579
|
+
});
|
|
580
|
+
}
|
|
581
|
+
}
|
|
582
|
+
const children = node.children;
|
|
583
|
+
if (children) for (const child of children) visit3(child);
|
|
584
|
+
};
|
|
585
|
+
visit3(tree);
|
|
586
|
+
return diagnostics;
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
// src/parse.ts
|
|
590
|
+
function parse(md, options) {
|
|
591
|
+
const source = md ?? "";
|
|
592
|
+
const tree = fromMarkdown(source, {
|
|
593
|
+
// GFM(表格 / 删除线 / 任务列表)与尖括号块并列注册,解析与序列化保持同一口径
|
|
594
|
+
extensions: [gfm(), jsxBlocks()],
|
|
595
|
+
mdastExtensions: [gfmFromMarkdown(), jsxFromMarkdown()]
|
|
596
|
+
});
|
|
597
|
+
const diagnostics = [
|
|
598
|
+
...enforceMaxDepth(tree, source, options?.maxDepth),
|
|
599
|
+
...collectUnclosed(tree, source)
|
|
600
|
+
];
|
|
601
|
+
const blocks = extractBlocks(tree, source);
|
|
602
|
+
return { tree, blocks, diagnostics };
|
|
603
|
+
}
|
|
604
|
+
|
|
605
|
+
// src/format.ts
|
|
606
|
+
var TAG_LINE = /^(\s*)<(\/?)([A-Z][A-Za-z0-9]*)((?:\s[^>]*)?)(\/?)>\s*$/;
|
|
607
|
+
var FENCE_LINE = /^\s*(`{3,}|~{3,})/;
|
|
608
|
+
function normalizeMarkdown(md) {
|
|
609
|
+
const lines = (md ?? "").replace(/\r\n?/g, "\n").split("\n");
|
|
610
|
+
const out = [];
|
|
611
|
+
let fenceChar = null;
|
|
612
|
+
const pushBlank = () => {
|
|
613
|
+
if (out.length > 0 && out[out.length - 1].trim() !== "") out.push("");
|
|
614
|
+
};
|
|
615
|
+
for (let i = 0; i < lines.length; i += 1) {
|
|
616
|
+
const line = lines[i];
|
|
617
|
+
const fence = FENCE_LINE.exec(line);
|
|
618
|
+
if (fenceChar !== null) {
|
|
619
|
+
out.push(line);
|
|
620
|
+
if (fence && fence[1].startsWith(fenceChar)) fenceChar = null;
|
|
621
|
+
continue;
|
|
622
|
+
}
|
|
623
|
+
if (fence) {
|
|
624
|
+
fenceChar = fence[1][0];
|
|
625
|
+
out.push(line);
|
|
626
|
+
continue;
|
|
627
|
+
}
|
|
628
|
+
const tag = TAG_LINE.exec(line);
|
|
629
|
+
if (tag && tag[1] === "") {
|
|
630
|
+
pushBlank();
|
|
631
|
+
out.push(line.trim());
|
|
632
|
+
const next = lines[i + 1];
|
|
633
|
+
if (next !== void 0 && next.trim() !== "") out.push("");
|
|
634
|
+
continue;
|
|
635
|
+
}
|
|
636
|
+
if (line.trim() === "") {
|
|
637
|
+
if (out.length > 0 && out[out.length - 1].trim() === "") continue;
|
|
638
|
+
out.push("");
|
|
639
|
+
continue;
|
|
640
|
+
}
|
|
641
|
+
out.push(line);
|
|
642
|
+
}
|
|
643
|
+
while (out.length > 0 && out[out.length - 1] === "") out.pop();
|
|
644
|
+
return out.join("\n");
|
|
645
|
+
}
|
|
646
|
+
function textOf(node) {
|
|
647
|
+
let out = "";
|
|
648
|
+
for (const child of node.children ?? []) {
|
|
649
|
+
if (child.type === "text") out += child.value;
|
|
650
|
+
else if (child.type === "element") out += textOf(child);
|
|
651
|
+
}
|
|
652
|
+
return out;
|
|
653
|
+
}
|
|
654
|
+
function langOf(code) {
|
|
655
|
+
const classes = code.properties?.className;
|
|
656
|
+
const list = Array.isArray(classes) ? classes.map(String) : [];
|
|
657
|
+
const found = list.find((item) => item.startsWith("language-"));
|
|
658
|
+
return found ? found.slice("language-".length) : "text";
|
|
659
|
+
}
|
|
660
|
+
function titleOf(code) {
|
|
661
|
+
const meta = code.data?.meta;
|
|
662
|
+
if (typeof meta !== "string") return void 0;
|
|
663
|
+
const matched = /title=["']?([^"'\s]+)["']?/.exec(meta);
|
|
664
|
+
return matched ? matched[1] : void 0;
|
|
665
|
+
}
|
|
666
|
+
function isCodeElement(child) {
|
|
667
|
+
return child.type === "element" && child.tagName === "code";
|
|
668
|
+
}
|
|
669
|
+
function rehypeCodeMeta() {
|
|
670
|
+
return (tree) => {
|
|
671
|
+
visit(tree, "element", (node) => {
|
|
672
|
+
if (node.tagName !== "pre") return;
|
|
673
|
+
const code = (node.children ?? []).find(isCodeElement);
|
|
674
|
+
if (!code) return;
|
|
675
|
+
const title = titleOf(code);
|
|
676
|
+
node.properties = {
|
|
677
|
+
...node.properties,
|
|
678
|
+
"data-code-raw": textOf(code),
|
|
679
|
+
"data-code-lang": langOf(code),
|
|
680
|
+
...title ? { "data-code-title": title } : {}
|
|
681
|
+
};
|
|
682
|
+
});
|
|
683
|
+
return void 0;
|
|
684
|
+
};
|
|
685
|
+
}
|
|
686
|
+
var BLOCK_TAGS = /* @__PURE__ */ new Set([
|
|
687
|
+
"p",
|
|
688
|
+
"h1",
|
|
689
|
+
"h2",
|
|
690
|
+
"h3",
|
|
691
|
+
"h4",
|
|
692
|
+
"h5",
|
|
693
|
+
"h6",
|
|
694
|
+
"ul",
|
|
695
|
+
"ol",
|
|
696
|
+
"li",
|
|
697
|
+
"blockquote",
|
|
698
|
+
"pre",
|
|
699
|
+
"table",
|
|
700
|
+
"figure",
|
|
701
|
+
"hr"
|
|
702
|
+
]);
|
|
703
|
+
function rehypeSourcePositions() {
|
|
704
|
+
return (tree) => {
|
|
705
|
+
visit(tree, "element", (node) => {
|
|
706
|
+
const tag = node.tagName.toLowerCase();
|
|
707
|
+
if (!BLOCK_TAGS.has(tag)) return;
|
|
708
|
+
const start = node.position?.start.offset;
|
|
709
|
+
if (typeof start !== "number") return;
|
|
710
|
+
node.properties = node.properties ?? {};
|
|
711
|
+
node.properties["data-src-start"] = String(start);
|
|
712
|
+
});
|
|
713
|
+
return void 0;
|
|
714
|
+
};
|
|
715
|
+
}
|
|
716
|
+
function jsxBlockToHast(state, node) {
|
|
717
|
+
const properties = {};
|
|
718
|
+
for (const [key, value] of Object.entries(node.attributes ?? {})) {
|
|
719
|
+
properties[key] = value;
|
|
720
|
+
}
|
|
721
|
+
return {
|
|
722
|
+
type: "element",
|
|
723
|
+
tagName: node.name,
|
|
724
|
+
properties,
|
|
725
|
+
children: state.all(node)
|
|
726
|
+
};
|
|
727
|
+
}
|
|
728
|
+
function createProcessor(extraRemark, extraRehype, sourcePositions) {
|
|
729
|
+
return unified().use(remarkParse).use(remarkJsxBlocks).use(remarkGfm).use(remarkMath).use(extraRemark ?? []).use(remarkRehype, { handlers: { jsxBlock: jsxBlockToHast } }).use(sourcePositions ? [rehypeSourcePositions] : []).use(rehypeCodeMeta).use(rehypeSlug).use(extraRehype ?? []).use(rehypeKatex);
|
|
730
|
+
}
|
|
731
|
+
function MarkdownRenderer({
|
|
732
|
+
content,
|
|
733
|
+
className,
|
|
734
|
+
config
|
|
735
|
+
}) {
|
|
736
|
+
const overrides = config?.components;
|
|
737
|
+
const extraRemark = config?.remarkPlugins;
|
|
738
|
+
const extraRehype = config?.rehypePlugins;
|
|
739
|
+
const sourcePositions = config?.sourcePositions;
|
|
740
|
+
const processor = useMemo(
|
|
741
|
+
() => createProcessor(extraRemark, extraRehype, sourcePositions),
|
|
742
|
+
[extraRemark, extraRehype, sourcePositions]
|
|
743
|
+
);
|
|
744
|
+
const rendered = useMemo(() => {
|
|
745
|
+
const source = content ?? "";
|
|
746
|
+
const mdast = processor.parse(source);
|
|
747
|
+
enforceMaxDepth(mdast, source);
|
|
748
|
+
const hast = processor.runSync(mdast);
|
|
749
|
+
return toJsxRuntime(hast, {
|
|
750
|
+
Fragment,
|
|
751
|
+
jsx,
|
|
752
|
+
jsxs,
|
|
753
|
+
components: { ...mdxComponents, ...overrides }
|
|
754
|
+
});
|
|
755
|
+
}, [content, overrides, processor]);
|
|
756
|
+
return /* @__PURE__ */ jsx("div", { className, children: rendered });
|
|
757
|
+
}
|
|
758
|
+
|
|
759
|
+
export { COMPONENTS, ITEMS, MAX_CONTAINER_DEPTH, MarkdownRenderer, extractBlocks, getComponent, getItemSpec, isContainerWithItems, jsxBlocks, jsxFromMarkdown, jsxToMarkdown, listComponents, normalizeMarkdown, parse, parseOpenTag, remarkJsxBlocks };
|
|
760
|
+
//# sourceMappingURL=chunk-KY27P34E.js.map
|
|
761
|
+
//# sourceMappingURL=chunk-KY27P34E.js.map
|