@isdk/mdast-plus 0.1.1
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/README.cn.md +109 -0
- package/README.md +109 -0
- package/dist/index.d.mts +1363 -0
- package/dist/index.d.ts +1363 -0
- package/dist/index.js +1 -0
- package/dist/index.mjs +1 -0
- package/docs/README.md +113 -0
- package/docs/_media/CONTRIBUTING.md +108 -0
- package/docs/_media/LICENSE-MIT +22 -0
- package/docs/_media/README.cn.md +109 -0
- package/docs/classes/FluentProcessor.md +194 -0
- package/docs/functions/htmlFormat.md +24 -0
- package/docs/functions/markdownFormat.md +24 -0
- package/docs/functions/mdast.md +27 -0
- package/docs/globals.md +33 -0
- package/docs/interfaces/ConvertResult.md +39 -0
- package/docs/interfaces/MdastAsset.md +41 -0
- package/docs/interfaces/MdastDataOrigin.md +45 -0
- package/docs/interfaces/MdastFormatDefinition.md +51 -0
- package/docs/interfaces/MdastMark.md +74 -0
- package/docs/interfaces/MdastPlugin.md +65 -0
- package/docs/interfaces/MdastReader.md +41 -0
- package/docs/interfaces/MdastSub.md +74 -0
- package/docs/interfaces/MdastSup.md +74 -0
- package/docs/interfaces/MdastTransformer.md +33 -0
- package/docs/interfaces/MdastWriter.md +47 -0
- package/docs/type-aliases/Stage.md +13 -0
- package/package.json +112 -0
package/README.cn.md
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
# @isdk/mdast-plus
|
|
2
|
+
|
|
3
|
+
> 基于 unified, remark 和 rehype 的“语义优先” Markdown 处理工具包。
|
|
4
|
+
|
|
5
|
+
[English](./README.md) | 简体中文 | [GitHub](https://github.com/isdk/mdast-plus.js)
|
|
6
|
+
|
|
7
|
+
[](https://www.npmjs.com/package/@isdk/mdast-plus)
|
|
8
|
+
|
|
9
|
+
`@isdk/mdast-plus` 是 unified 生态系统的强大扩展,旨在为 Markdown 内容提供一致且语义优先的规范化与转换。它通过 Fluent API 和分阶段的插件系统简化了复杂的处理工作流。
|
|
10
|
+
|
|
11
|
+
## 特性
|
|
12
|
+
|
|
13
|
+
- **Fluent API**: 链式调用接口 `mdast(input).use(plugin).toHTML()`。
|
|
14
|
+
- **分阶段插件**: 将转换组织为 `normalize`、`compile` 和 `finalize` 阶段,支持优先级排序。
|
|
15
|
+
- **语义化规范**:
|
|
16
|
+
- **指令 (Directives)**: 规范化提示框 (Admonition) 名称并从标签中提取标题。
|
|
17
|
+
- **表格跨行/跨列**: 支持 HTML 输出中的 `rowspan` 和 `colspan`。
|
|
18
|
+
- **代码元数据**: 对代码块元数据字符串进行结构化解析。
|
|
19
|
+
- **图片尺寸**: 支持 URL 糖语法 (例如 `image.png#=500x300`) 来设置图片尺寸。
|
|
20
|
+
- **深度类型支持**: 基于 TypeScript 构建,完整支持 unist/mdast 的模块扩充。
|
|
21
|
+
|
|
22
|
+
## 安装
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
npm install @isdk/mdast-plus
|
|
26
|
+
# 或
|
|
27
|
+
pnpm add @isdk/mdast-plus
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## 基本用法
|
|
31
|
+
|
|
32
|
+
### HTML 转换
|
|
33
|
+
|
|
34
|
+
```typescript
|
|
35
|
+
import { mdast } from '@isdk/mdast-plus';
|
|
36
|
+
|
|
37
|
+
const html = await mdast(':::warning[重要提示]\n请小心!\n:::')
|
|
38
|
+
.toHTML();
|
|
39
|
+
// 结果: <div title="重要提示" class="warning"><p>请小心!</p></div>
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
### 图片尺寸
|
|
43
|
+
|
|
44
|
+
```typescript
|
|
45
|
+
const html = await mdast('').toHTML();
|
|
46
|
+
// 结果: <img src="cat.png" alt="Cat" width="500" height="300">
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
### 高级工作流
|
|
50
|
+
|
|
51
|
+
```typescript
|
|
52
|
+
const { content, assets } = await mdast(myInput)
|
|
53
|
+
.data({ myGlobal: 'value' })
|
|
54
|
+
.use({
|
|
55
|
+
name: 'my-plugin',
|
|
56
|
+
stage: 'compile',
|
|
57
|
+
transform: async (tree) => {
|
|
58
|
+
// 转换 AST
|
|
59
|
+
}
|
|
60
|
+
})
|
|
61
|
+
.to('html');
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
### 任意格式支持
|
|
65
|
+
|
|
66
|
+
您可以注册自定义的输入或输出格式:
|
|
67
|
+
|
|
68
|
+
```typescript
|
|
69
|
+
import { FluentProcessor, mdast } from '@isdk/mdast-plus';
|
|
70
|
+
|
|
71
|
+
// 注册自定义输出格式
|
|
72
|
+
FluentProcessor.registerFormat('reverse', {
|
|
73
|
+
stringify: (p) => {
|
|
74
|
+
p.Compiler = (tree) => {
|
|
75
|
+
// 您的自定义序列化逻辑
|
|
76
|
+
return '...';
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
const result = await mdast('Hello').to('reverse');
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
> **注意**: 格式名称不区分大小写(内部始终转换为小写)。
|
|
85
|
+
|
|
86
|
+
## 分阶段处理
|
|
87
|
+
|
|
88
|
+
插件根据它们的 `stage` (阶段) 和 `order` (顺序) 执行:
|
|
89
|
+
|
|
90
|
+
1. **normalize** (order 0-100): 清理并规范化树。
|
|
91
|
+
2. **compile** (order 0-100): 高级语义转换。
|
|
92
|
+
3. **finalize** (order 0-100): 输出前的最后准备。
|
|
93
|
+
|
|
94
|
+
## 内置核心插件
|
|
95
|
+
|
|
96
|
+
| 插件 | 阶段 | 描述 |
|
|
97
|
+
| :--- | :--- | :--- |
|
|
98
|
+
| `normalize-directive` | normalize | 处理别名 (`warn` -> `warning`) 并提取标题。 |
|
|
99
|
+
| `normalize-table-span` | normalize | 将表格单元格跨度迁移到 `hProperties`。 |
|
|
100
|
+
| `extract-code-meta` | normalize | 从代码块元数据中解析 `title="foo"`。 |
|
|
101
|
+
| `image-size` | normalize | 从图片 URL 中解析 `#=WxH`。 |
|
|
102
|
+
|
|
103
|
+
## 贡献
|
|
104
|
+
|
|
105
|
+
请查看 [CONTRIBUTING.cn.md](./CONTRIBUTING.cn.md) 以获取有关如何贡献本项目的指南。
|
|
106
|
+
|
|
107
|
+
## 许可证
|
|
108
|
+
|
|
109
|
+
[MIT](./LICENSE-MIT)
|
package/README.md
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
# @isdk/mdast-plus
|
|
2
|
+
|
|
3
|
+
> A "Semantic-First" Markdown processing toolkit based on unified, remark and rehype.
|
|
4
|
+
|
|
5
|
+
English | [简体中文](./README.cn.md) | [GitHub](https://github.com/isdk/mdast-plus.js)
|
|
6
|
+
|
|
7
|
+
[](https://www.npmjs.com/package/@isdk/mdast-plus)
|
|
8
|
+
|
|
9
|
+
`@isdk/mdast-plus` is a powerful extension to the unified ecosystem, designed to provide consistent, semantic-first normalization and transformation of Markdown content. It simplifies complex processing pipelines with a fluent API and a staged plugin system.
|
|
10
|
+
|
|
11
|
+
## Features
|
|
12
|
+
|
|
13
|
+
- **Fluent API**: Chainable interface `mdast(input).use(plugin).toHTML()`.
|
|
14
|
+
- **Staged Plugins**: Organize transformations into `normalize`, `compile`, and `finalize` stages with priority ordering.
|
|
15
|
+
- **Semantic Normalization**:
|
|
16
|
+
- **Directives**: Canonicalizes admonition names and extracts titles from labels.
|
|
17
|
+
- **Table Spans**: Support for `rowspan` and `colspan` in HTML output.
|
|
18
|
+
- **Code Meta**: Structured parsing of code block metadata strings.
|
|
19
|
+
- **Image Sizing**: URL "sugar" support (e.g., `image.png#=500x300`) for image dimensions.
|
|
20
|
+
- **Deeply Typed**: Built on TypeScript with full support for unist/mdast module augmentation.
|
|
21
|
+
|
|
22
|
+
## Installation
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
npm install @isdk/mdast-plus
|
|
26
|
+
# or
|
|
27
|
+
pnpm add @isdk/mdast-plus
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## Basic Usage
|
|
31
|
+
|
|
32
|
+
### HTML Conversion
|
|
33
|
+
|
|
34
|
+
```typescript
|
|
35
|
+
import { mdast } from '@isdk/mdast-plus';
|
|
36
|
+
|
|
37
|
+
const html = await mdast(':::warning[Special Note]\nBe careful!\n:::')
|
|
38
|
+
.toHTML();
|
|
39
|
+
// Result: <div title="Special Note" class="warning"><p>Be careful!</p></div>
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
### Image Sizing
|
|
43
|
+
|
|
44
|
+
```typescript
|
|
45
|
+
const html = await mdast('').toHTML();
|
|
46
|
+
// Result: <img src="cat.png" alt="Cat" width="500" height="300">
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
### Advanced Pipeline
|
|
50
|
+
|
|
51
|
+
```typescript
|
|
52
|
+
const { content, assets } = await mdast(myInput)
|
|
53
|
+
.data({ myGlobal: 'value' })
|
|
54
|
+
.use({
|
|
55
|
+
name: 'my-plugin',
|
|
56
|
+
stage: 'compile',
|
|
57
|
+
transform: async (tree) => {
|
|
58
|
+
// transform the AST
|
|
59
|
+
}
|
|
60
|
+
})
|
|
61
|
+
.to('html');
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
### Arbitrary Formats
|
|
65
|
+
|
|
66
|
+
You can register custom input or output formats:
|
|
67
|
+
|
|
68
|
+
```typescript
|
|
69
|
+
import { FluentProcessor, mdast } from '@isdk/mdast-plus';
|
|
70
|
+
|
|
71
|
+
// Register a custom output format
|
|
72
|
+
FluentProcessor.registerFormat('reverse', {
|
|
73
|
+
stringify: (p) => {
|
|
74
|
+
p.Compiler = (tree) => {
|
|
75
|
+
// your custom stringification logic
|
|
76
|
+
return '...';
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
const result = await mdast('Hello').to('reverse');
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
> **Note**: Format names are case-insensitive (always converted to lowercase internally).
|
|
85
|
+
|
|
86
|
+
## Staged Processing
|
|
87
|
+
|
|
88
|
+
Plugins are executed based on their `stage` and `order`:
|
|
89
|
+
|
|
90
|
+
1. **normalize** (order 0-100): Cleanup and canonicalize the tree.
|
|
91
|
+
2. **compile** (order 0-100): High-level semantic transformations.
|
|
92
|
+
3. **finalize** (order 0-100): Final preparation before output.
|
|
93
|
+
|
|
94
|
+
## Core Plugins Included
|
|
95
|
+
|
|
96
|
+
| Plugin | Stage | Description |
|
|
97
|
+
| :--- | :--- | :--- |
|
|
98
|
+
| `normalize-directive` | normalize | Handles aliases (`warn` -> `warning`) and extracts titles. |
|
|
99
|
+
| `normalize-table-span` | normalize | Migrates table cell spans to `hProperties`. |
|
|
100
|
+
| `extract-code-meta` | normalize | Parses `title="foo"` from code block meta. |
|
|
101
|
+
| `image-size` | normalize | Parses `#=WxH` from image URLs. |
|
|
102
|
+
|
|
103
|
+
## Contributing
|
|
104
|
+
|
|
105
|
+
Please see [CONTRIBUTING.md](./CONTRIBUTING.md) for guidelines on how to contribute to this project.
|
|
106
|
+
|
|
107
|
+
## License
|
|
108
|
+
|
|
109
|
+
[MIT](./LICENSE-MIT)
|