@isdk/mdast-plus 0.1.2 → 0.1.3

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 (38) hide show
  1. package/README.cn.md +54 -20
  2. package/README.md +54 -20
  3. package/dist/index.d.mts +187 -133
  4. package/dist/index.d.ts +187 -133
  5. package/dist/index.js +1 -1
  6. package/dist/index.mjs +1 -1
  7. package/docs/README.md +54 -20
  8. package/docs/_media/CONTRIBUTING.md +39 -18
  9. package/docs/_media/README.cn.md +54 -20
  10. package/docs/classes/MdastBasePipeline.md +348 -0
  11. package/docs/classes/MdastPipeline.md +531 -0
  12. package/docs/enumerations/PipelineStage.md +62 -0
  13. package/docs/functions/astCompiler.md +25 -0
  14. package/docs/functions/jsonParser.md +24 -0
  15. package/docs/functions/mdast.md +4 -4
  16. package/docs/globals.md +12 -10
  17. package/docs/interfaces/MdastDataOrigin.md +8 -8
  18. package/docs/interfaces/MdastFormat.md +71 -0
  19. package/docs/interfaces/MdastMark.md +4 -4
  20. package/docs/interfaces/MdastPlugin.md +27 -27
  21. package/docs/interfaces/MdastSub.md +4 -4
  22. package/docs/interfaces/MdastSup.md +4 -4
  23. package/docs/type-aliases/PipelineStageName.md +13 -0
  24. package/docs/variables/DefaultPipelineStage.md +13 -0
  25. package/docs/variables/astFormat.md +15 -0
  26. package/docs/variables/htmlFormat.md +6 -4
  27. package/docs/variables/markdownFormat.md +6 -4
  28. package/package.json +11 -9
  29. package/docs/classes/FluentProcessor.md +0 -210
  30. package/docs/functions/htmlStringify.md +0 -23
  31. package/docs/functions/markdownCommon.md +0 -23
  32. package/docs/interfaces/ConvertResult.md +0 -39
  33. package/docs/interfaces/MdastAsset.md +0 -41
  34. package/docs/interfaces/MdastFormatDefinition.md +0 -51
  35. package/docs/interfaces/MdastReader.md +0 -41
  36. package/docs/interfaces/MdastTransformer.md +0 -33
  37. package/docs/interfaces/MdastWriter.md +0 -47
  38. package/docs/type-aliases/Stage.md +0 -13
@@ -8,11 +8,13 @@ Thank you for your interest in contributing to `@isdk/mdast-plus`! This document
8
8
 
9
9
  ### Core Concepts
10
10
 
11
- 1. **Fluent API**: The main entry point is the `mdast()` function in `src/pipeline.ts`.
12
- 2. **Staged Plugins**: Plugins are categorized into three stages:
11
+ 1. **Fluent API**: The main entry point is the `mdast()` function in `src/pipeline.ts`, backed by `MdastPipeline`.
12
+ 2. **Staged Plugins**: Plugins are categorized into 5 stages (`PipelineStage`):
13
+ * `parse`: Parsing input to AST.
13
14
  * `normalize`: Cleanup and canonicalize the tree.
14
15
  * `compile`: High-level semantic transformations.
15
16
  * `finalize`: Final preparation before output.
17
+ * `stringify`: Serializing AST to output.
16
18
  3. **Universal Data Protocols**: Nodes use `node.data` for metadata, and `node.data.hProperties` for HTML attributes.
17
19
 
18
20
  ## Getting Started
@@ -55,39 +57,58 @@ pnpm run lint
55
57
  ## Adding a New Plugin
56
58
 
57
59
  1. Create your plugin in `src/plugins/`.
58
- 2. Implement the `MdastPlugin` interface:
60
+ 2. Implement the `MdastPlugin` interface. The `plugin` property should be a standard unified plugin.
59
61
 
60
62
  ```typescript
61
- import { MdastPlugin } from '../types';
63
+ import { MdastPlugin, PipelineStage } from '../types';
64
+ import { visit } from 'unist-util-visit';
65
+
66
+ const myUnifiedPlugin = (options) => {
67
+ return (tree) => {
68
+ visit(tree, 'text', (node) => {
69
+ // transform logic
70
+ });
71
+ };
72
+ };
62
73
 
63
74
  export const myPlugin: MdastPlugin = {
64
- name: 'my-plugin',
65
- stage: 'normalize', // 'normalize' | 'compile' | 'finalize'
75
+ plugin: myUnifiedPlugin,
76
+ stage: PipelineStage.normalize, // or 'compile', 'finalize'
66
77
  order: 50, // 0-100
67
- transform: async (tree, processor) => {
68
- // Visit nodes and transform the tree
69
- }
70
78
  };
71
79
  ```
72
80
 
73
- 3. Register your plugin in `src/pipeline.ts` in the `FluentProcessor` constructor if it should be a default plugin.
81
+ 3. If it's a default plugin, add it to the `input` or `output` list of the relevant format in `src/formats/`.
74
82
 
75
83
  ## Adding a New Format
76
84
 
77
- 1. Implement the `MdastFormatDefinition` interface.
78
- 2. Register it using `FluentProcessor.registerFormat(name, definition)`.
85
+ 1. Define a `MdastFormat` object.
86
+ 2. Register it using `MdastPipeline.register(format)`.
79
87
 
80
88
  ```typescript
81
- import { FluentProcessor } from '../pipeline';
82
-
83
- FluentProcessor.registerFormat('json', {
84
- stringify: (processor) => {
85
- processor.Compiler = (tree) => JSON.stringify(tree);
86
- }
89
+ import { MdastPipeline, PipelineStage } from '../src/pipeline';
90
+
91
+ MdastPipeline.register({
92
+ id: 'json',
93
+ title: 'JSON Format',
94
+ output: [{
95
+ plugin: function() {
96
+ this.Compiler = (tree) => JSON.stringify(tree);
97
+ },
98
+ stage: PipelineStage.stringify
99
+ }]
87
100
  });
88
101
  ```
89
102
 
90
103
  > **Note**: Format names are case-insensitive.
104
+ >
105
+ > **Important**: `unified` requires a `Compiler` to be attached for the process to complete successfully. If your format is intended to return an object (like the AST itself) rather than a string, you must provide a "pass-through" compiler:
106
+ >
107
+ > ```typescript
108
+ > function astCompiler() {
109
+ > this.Compiler = (tree) => tree;
110
+ > }
111
+ > ```
91
112
 
92
113
  ## Coding Standards
93
114
 
@@ -11,7 +11,7 @@
11
11
  ## 特性
12
12
 
13
13
  - **Fluent API**: 链式调用接口 `mdast(input).use(plugin).toHTML()`。
14
- - **分阶段插件**: 将转换组织为 `normalize`、`compile` 和 `finalize` 阶段,支持优先级排序。
14
+ - **分阶段插件**: 将转换组织为 `parse`, `normalize`, `compile`, `finalize` 和 `stringify` 阶段。
15
15
  - **语义化规范**:
16
16
  - **指令 (Directives)**: 规范化提示框 (Admonition) 名称并从标签中提取标题。
17
17
  - **表格跨行/跨列**: 支持 HTML 输出中的 `rowspan` 和 `colspan`。
@@ -40,6 +40,17 @@ const html = await mdast(':::warning[重要提示]\n请小心!\n:::')
40
40
  // 结果: <div title="重要提示" class="warning"><p>请小心!</p></div>
41
41
  ```
42
42
 
43
+ ### 配置输入选项
44
+
45
+ 您可以通过 `.from()` 的第二个参数向输入插件(如 `remark-gfm` 或 `remark-parse`)传递选项:
46
+
47
+ ```typescript
48
+ // 启用单个波浪线删除线 (~text~)
49
+ const md = await mdast('Hello ~world~')
50
+ .from('markdown', { remarkGfm: { singleTilde: true } })
51
+ .toMarkdown();
52
+ ```
53
+
43
54
  ### 图片尺寸
44
55
 
45
56
  ```typescript
@@ -50,8 +61,11 @@ const html = await mdast('![Cat](cat.png#=500x300)').toHTML();
50
61
  ### AST 输出
51
62
 
52
63
  ```typescript
64
+ // 获取处理后的完整 AST (在 normalization 之后)
53
65
  const ast = await mdast('==高亮内容==').toAST();
54
- // 返回 mdast Root 对象
66
+
67
+ // 获取原始 AST (在 parse 之后, normalization 之前)
68
+ const rawAst = await mdast('==高亮内容==').toAST({ stage: 'parse' });
55
69
  ```
56
70
 
57
71
  ### 高级工作流
@@ -59,31 +73,49 @@ const ast = await mdast('==高亮内容==').toAST();
59
73
  ```typescript
60
74
  const { content, assets } = await mdast(myInput)
61
75
  .data({ myGlobal: 'value' })
62
- .use({
63
- name: 'my-plugin',
64
- stage: 'compile',
65
- transform: async (tree) => {
66
- // 转换 AST
67
- }
68
- })
76
+ // 在 'compile' 阶段添加自定义插件
77
+ .useAt('compile', myPlugin, { option: 1 })
78
+ .priority(10) // 比默认插件更晚执行
69
79
  .to('html');
70
80
  ```
71
81
 
82
+ ### 插件行为
83
+
84
+ `mdast-plus` 内部使用 [unified](https://github.com/unifiedjs/unified)。如果您多次添加同一个插件函数,最后的配置将**覆盖**之前的配置。
85
+
86
+ ```typescript
87
+ // 插件将只执行一次,且选项为: 2
88
+ pipeline.use(myPlugin, { option: 1 });
89
+ pipeline.use(myPlugin, { option: 2 });
90
+ ```
91
+
92
+ 若要多次运行相同的插件逻辑(例如用于不同目的),请提供不同的函数引用:
93
+
94
+ ```typescript
95
+ // 插件将执行两次
96
+ pipeline.use(myPlugin, { option: 1 });
97
+ pipeline.use(myPlugin.bind({}), { option: 2 });
98
+ ```
99
+
72
100
  ### 任意格式支持
73
101
 
74
102
  您可以注册自定义的输入或输出格式:
75
103
 
76
104
  ```typescript
77
- import { FluentProcessor, mdast } from '@isdk/mdast-plus';
105
+ import { MdastPipeline, mdast, PipelineStage } from '@isdk/mdast-plus';
78
106
 
79
107
  // 注册自定义输出格式
80
- FluentProcessor.registerFormat('reverse', {
81
- stringify: (p) => {
82
- p.Compiler = (tree) => {
83
- // 您的自定义序列化逻辑
84
- return '...';
85
- };
86
- }
108
+ MdastPipeline.register({
109
+ id: 'reverse',
110
+ output: [{
111
+ plugin: function() {
112
+ this.Compiler = (tree) => {
113
+ // 您的自定义序列化逻辑
114
+ return '...';
115
+ };
116
+ },
117
+ stage: PipelineStage.stringify
118
+ }]
87
119
  });
88
120
 
89
121
  const result = await mdast('Hello').to('reverse');
@@ -95,9 +127,11 @@ const result = await mdast('Hello').to('reverse');
95
127
 
96
128
  插件根据它们的 `stage` (阶段) 和 `order` (顺序) 执行:
97
129
 
98
- 1. **normalize** (order 0-100): 清理并规范化树。
99
- 2. **compile** (order 0-100): 高级语义转换。
100
- 3. **finalize** (order 0-100): 输出前的最后准备。
130
+ 1. **parse** (0): 输入解析 (例如 `remark-parse`)。
131
+ 2. **normalize** (100): 清理并规范化树。
132
+ 3. **compile** (200): 高级语义转换。
133
+ 4. **finalize** (300): 输出前的最后准备 (例如 `rehype-sanitize`)。
134
+ 5. **stringify** (400): 输出生成。
101
135
 
102
136
  ## 内置核心插件
103
137
 
@@ -0,0 +1,348 @@
1
+ [**@isdk/mdast-plus**](../README.md)
2
+
3
+ ***
4
+
5
+ [@isdk/mdast-plus](../globals.md) / MdastBasePipeline
6
+
7
+ # Class: MdastBasePipeline
8
+
9
+ Defined in: [packages/mdast-plus/src/pipeline.ts:26](https://github.com/isdk/mdast-plus.js/blob/bacb4922529058fef775e3f4b4e71c98ac1cab17/src/pipeline.ts#L26)
10
+
11
+ Base implementation of the fluent mdast processing pipeline.
12
+ Manages the plugin registry and the execution queue.
13
+
14
+ ## Extended by
15
+
16
+ - [`MdastPipeline`](MdastPipeline.md)
17
+
18
+ ## Constructors
19
+
20
+ ### Constructor
21
+
22
+ > **new MdastBasePipeline**(`input`): `MdastBasePipeline`
23
+
24
+ Defined in: [packages/mdast-plus/src/pipeline.ts:53](https://github.com/isdk/mdast-plus.js/blob/bacb4922529058fef775e3f4b4e71c98ac1cab17/src/pipeline.ts#L53)
25
+
26
+ Initializes a new pipeline instance with the given input.
27
+
28
+ #### Parameters
29
+
30
+ ##### input
31
+
32
+ `Compatible`
33
+
34
+ Content to process (string, Buffer, VFile, or AST Node).
35
+
36
+ #### Returns
37
+
38
+ `MdastBasePipeline`
39
+
40
+ ## Properties
41
+
42
+ ### input
43
+
44
+ > `protected` **input**: `Compatible`
45
+
46
+ Defined in: [packages/mdast-plus/src/pipeline.ts:46](https://github.com/isdk/mdast-plus.js/blob/bacb4922529058fef775e3f4b4e71c98ac1cab17/src/pipeline.ts#L46)
47
+
48
+ ***
49
+
50
+ ### queue
51
+
52
+ > `protected` **queue**: [`MdastPlugin`](../interfaces/MdastPlugin.md)[] = `[]`
53
+
54
+ Defined in: [packages/mdast-plus/src/pipeline.ts:47](https://github.com/isdk/mdast-plus.js/blob/bacb4922529058fef775e3f4b4e71c98ac1cab17/src/pipeline.ts#L47)
55
+
56
+ ## Methods
57
+
58
+ ### assembleProcessor()
59
+
60
+ > `protected` **assembleProcessor**(`queue`): `Processor`
61
+
62
+ Defined in: [packages/mdast-plus/src/pipeline.ts:254](https://github.com/isdk/mdast-plus.js/blob/bacb4922529058fef775e3f4b4e71c98ac1cab17/src/pipeline.ts#L254)
63
+
64
+ Assembles a unified processor based on the sorted plugin queue.
65
+
66
+ #### Parameters
67
+
68
+ ##### queue
69
+
70
+ [`MdastPlugin`](../interfaces/MdastPlugin.md)[]
71
+
72
+ #### Returns
73
+
74
+ `Processor`
75
+
76
+ ***
77
+
78
+ ### ensureInputPlugins()
79
+
80
+ > `protected` **ensureInputPlugins**(`queue`, `overrides?`, `maxStage?`): `void`
81
+
82
+ Defined in: [packages/mdast-plus/src/pipeline.ts:123](https://github.com/isdk/mdast-plus.js/blob/bacb4922529058fef775e3f4b4e71c98ac1cab17/src/pipeline.ts#L123)
83
+
84
+ Ensures that input plugins (parser, normalizers) are present in the queue.
85
+ Adds implicit plugins if no parser is detected.
86
+
87
+ #### Parameters
88
+
89
+ ##### queue
90
+
91
+ [`MdastPlugin`](../interfaces/MdastPlugin.md)[]
92
+
93
+ ##### overrides?
94
+
95
+ `Record`\<`string`, `any`\>
96
+
97
+ ##### maxStage?
98
+
99
+ [`PipelineStage`](../enumerations/PipelineStage.md) = `PipelineStage.stringify`
100
+
101
+ #### Returns
102
+
103
+ `void`
104
+
105
+ ***
106
+
107
+ ### from()
108
+
109
+ > **from**(`fmt`, `overrides?`): `this`
110
+
111
+ Defined in: [packages/mdast-plus/src/pipeline.ts:152](https://github.com/isdk/mdast-plus.js/blob/bacb4922529058fef775e3f4b4e71c98ac1cab17/src/pipeline.ts#L152)
112
+
113
+ Configures the input format and adds its associated plugins to the pipeline.
114
+
115
+ #### Parameters
116
+
117
+ ##### fmt
118
+
119
+ Format ID or definition.
120
+
121
+ `string` | [`MdastFormat`](../interfaces/MdastFormat.md)
122
+
123
+ ##### overrides?
124
+
125
+ `Record`\<`string`, `any`\>
126
+
127
+ Optional map to override plugin options by plugin name.
128
+
129
+ #### Returns
130
+
131
+ `this`
132
+
133
+ The pipeline instance for chaining.
134
+
135
+ ***
136
+
137
+ ### getFormat()
138
+
139
+ > **getFormat**(`id`): `undefined` \| [`MdastFormat`](../interfaces/MdastFormat.md)
140
+
141
+ Defined in: [packages/mdast-plus/src/pipeline.ts:60](https://github.com/isdk/mdast-plus.js/blob/bacb4922529058fef775e3f4b4e71c98ac1cab17/src/pipeline.ts#L60)
142
+
143
+ Instance-level access to the global format registry.
144
+
145
+ #### Parameters
146
+
147
+ ##### id
148
+
149
+ `string`
150
+
151
+ #### Returns
152
+
153
+ `undefined` \| [`MdastFormat`](../interfaces/MdastFormat.md)
154
+
155
+ ***
156
+
157
+ ### priority()
158
+
159
+ > **priority**(`order`): `this`
160
+
161
+ Defined in: [packages/mdast-plus/src/pipeline.ts:242](https://github.com/isdk/mdast-plus.js/blob/bacb4922529058fef775e3f4b4e71c98ac1cab17/src/pipeline.ts#L242)
162
+
163
+ Sets the priority order for the most recently added plugin.
164
+ Plugins with lower order run earlier within the same stage.
165
+
166
+ #### Parameters
167
+
168
+ ##### order
169
+
170
+ `number`
171
+
172
+ Numeric priority.
173
+
174
+ #### Returns
175
+
176
+ `this`
177
+
178
+ The pipeline instance for chaining.
179
+
180
+ ***
181
+
182
+ ### to()
183
+
184
+ > **to**(`fmt`, `overrides?`): `Promise`\<`VFile`\>
185
+
186
+ Defined in: [packages/mdast-plus/src/pipeline.ts:172](https://github.com/isdk/mdast-plus.js/blob/bacb4922529058fef775e3f4b4e71c98ac1cab17/src/pipeline.ts#L172)
187
+
188
+ Processes the pipeline and serializes the result into the specified format.
189
+
190
+ #### Parameters
191
+
192
+ ##### fmt
193
+
194
+ Target format ID or definition.
195
+
196
+ `string` | [`MdastFormat`](../interfaces/MdastFormat.md)
197
+
198
+ ##### overrides?
199
+
200
+ `Record`\<`string`, `any`\>
201
+
202
+ Optional map to override plugin options.
203
+
204
+ #### Returns
205
+
206
+ `Promise`\<`VFile`\>
207
+
208
+ A promise resolving to a VFile containing the result.
209
+
210
+ ***
211
+
212
+ ### toRuntimeEntry()
213
+
214
+ > `protected` **toRuntimeEntry**(`entry`, `defaultStage`, `overrides?`): [`MdastPlugin`](../interfaces/MdastPlugin.md) & `object`
215
+
216
+ Defined in: [packages/mdast-plus/src/pipeline.ts:84](https://github.com/isdk/mdast-plus.js/blob/bacb4922529058fef775e3f4b4e71c98ac1cab17/src/pipeline.ts#L84)
217
+
218
+ Normalizes a plugin entry for runtime execution.
219
+
220
+ #### Parameters
221
+
222
+ ##### entry
223
+
224
+ [`MdastPlugin`](../interfaces/MdastPlugin.md)
225
+
226
+ ##### defaultStage
227
+
228
+ [`PipelineStage`](../enumerations/PipelineStage.md)
229
+
230
+ ##### overrides?
231
+
232
+ `Record`\<`string`, `any`\>
233
+
234
+ #### Returns
235
+
236
+ [`MdastPlugin`](../interfaces/MdastPlugin.md) & `object`
237
+
238
+ ***
239
+
240
+ ### use()
241
+
242
+ > **use**(`plugin`, ...`options`): `this`
243
+
244
+ Defined in: [packages/mdast-plus/src/pipeline.ts:215](https://github.com/isdk/mdast-plus.js/blob/bacb4922529058fef775e3f4b4e71c98ac1cab17/src/pipeline.ts#L215)
245
+
246
+ Adds a plugin to the pipeline's compile stage.
247
+
248
+ #### Parameters
249
+
250
+ ##### plugin
251
+
252
+ `any`
253
+
254
+ The unified plugin function.
255
+
256
+ ##### options
257
+
258
+ ...`any`[]
259
+
260
+ Arguments for the plugin.
261
+
262
+ #### Returns
263
+
264
+ `this`
265
+
266
+ The pipeline instance for chaining.
267
+
268
+ ***
269
+
270
+ ### useAt()
271
+
272
+ > **useAt**(`stage`, `plugin`, ...`options`): `this`
273
+
274
+ Defined in: [packages/mdast-plus/src/pipeline.ts:226](https://github.com/isdk/mdast-plus.js/blob/bacb4922529058fef775e3f4b4e71c98ac1cab17/src/pipeline.ts#L226)
275
+
276
+ Adds a plugin to the pipeline at a specific stage.
277
+
278
+ #### Parameters
279
+
280
+ ##### stage
281
+
282
+ The stage name or numeric value.
283
+
284
+ `"parse"` | `"normalize"` | `"compile"` | `"finalize"` | `"stringify"`
285
+
286
+ ##### plugin
287
+
288
+ `any`
289
+
290
+ The unified plugin function.
291
+
292
+ ##### options
293
+
294
+ ...`any`[]
295
+
296
+ Arguments for the plugin.
297
+
298
+ #### Returns
299
+
300
+ `this`
301
+
302
+ The pipeline instance for chaining.
303
+
304
+ ***
305
+
306
+ ### getFormat()
307
+
308
+ > `static` **getFormat**(`id`): `undefined` \| [`MdastFormat`](../interfaces/MdastFormat.md)
309
+
310
+ Defined in: [packages/mdast-plus/src/pipeline.ts:42](https://github.com/isdk/mdast-plus.js/blob/bacb4922529058fef775e3f4b4e71c98ac1cab17/src/pipeline.ts#L42)
311
+
312
+ Retrieves a registered format by its ID.
313
+
314
+ #### Parameters
315
+
316
+ ##### id
317
+
318
+ `string`
319
+
320
+ The format identifier.
321
+
322
+ #### Returns
323
+
324
+ `undefined` \| [`MdastFormat`](../interfaces/MdastFormat.md)
325
+
326
+ The format definition or undefined if not found.
327
+
328
+ ***
329
+
330
+ ### register()
331
+
332
+ > `static` **register**(`format`): `void`
333
+
334
+ Defined in: [packages/mdast-plus/src/pipeline.ts:33](https://github.com/isdk/mdast-plus.js/blob/bacb4922529058fef775e3f4b4e71c98ac1cab17/src/pipeline.ts#L33)
335
+
336
+ Registers a global document format.
337
+
338
+ #### Parameters
339
+
340
+ ##### format
341
+
342
+ [`MdastFormat`](../interfaces/MdastFormat.md)
343
+
344
+ The format definition to register.
345
+
346
+ #### Returns
347
+
348
+ `void`