@isdk/mdast-plus 0.1.3 → 0.2.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 +24 -9
- package/README.md +19 -4
- package/dist/index.d.mts +409 -85
- package/dist/index.d.ts +409 -85
- package/dist/index.js +1 -1
- package/dist/index.mjs +1 -1
- package/docs/README.md +19 -4
- package/docs/_media/CONTRIBUTING.md +23 -13
- package/docs/_media/README.cn.md +24 -9
- package/docs/classes/MdastBasePipeline.md +129 -29
- package/docs/classes/MdastPipeline.md +155 -39
- package/docs/enumerations/PipelineStage.md +6 -6
- package/docs/functions/astCompiler.md +1 -1
- package/docs/functions/jsonParser.md +1 -1
- package/docs/functions/mdast.md +1 -1
- package/docs/globals.md +5 -0
- package/docs/interfaces/MdastDataOrigin.md +4 -4
- package/docs/interfaces/MdastFormat.md +7 -7
- package/docs/interfaces/MdastMark.md +3 -3
- package/docs/interfaces/MdastPlugin.md +38 -6
- package/docs/interfaces/MdastSub.md +3 -3
- package/docs/interfaces/MdastSup.md +3 -3
- package/docs/interfaces/ReadabilityOptions.md +41 -0
- package/docs/type-aliases/PipelineStageName.md +1 -1
- package/docs/variables/DefaultPipelineStage.md +1 -1
- package/docs/variables/astFormat.md +1 -1
- package/docs/variables/htmlFormat.md +1 -1
- package/docs/variables/htmlReadability.md +13 -0
- package/docs/variables/htmlReadabilityPlugin.md +31 -0
- package/docs/variables/htmlReadabilityPlugins.md +13 -0
- package/docs/variables/markdownFormat.md +1 -1
- package/docs/variables/restoreReadabilityMetaPlugin.md +49 -0
- package/package.json +8 -1
package/README.cn.md
CHANGED
|
@@ -71,18 +71,26 @@ const rawAst = await mdast('==高亮内容==').toAST({ stage: 'parse' });
|
|
|
71
71
|
### 高级工作流
|
|
72
72
|
|
|
73
73
|
```typescript
|
|
74
|
-
|
|
74
|
+
import { htmlReadabilityPlugins } from '@isdk/mdast-plus';
|
|
75
|
+
|
|
76
|
+
const vfile = await mdast(myInput)
|
|
75
77
|
.data({ myGlobal: 'value' })
|
|
76
|
-
//
|
|
77
|
-
.
|
|
78
|
+
// 以数组形式在 'compile' 阶段添加多个插件
|
|
79
|
+
.use([pluginA, pluginB])
|
|
80
|
+
// 或在特定阶段添加一组插件
|
|
81
|
+
.useAt('parse', htmlReadabilityPlugins)
|
|
78
82
|
.priority(10) // 比默认插件更晚执行
|
|
79
83
|
.to('html');
|
|
84
|
+
|
|
85
|
+
console.log(vfile.value); // 序列化后的 HTML 字符串
|
|
80
86
|
```
|
|
81
87
|
|
|
82
88
|
### 插件行为
|
|
83
89
|
|
|
84
90
|
`mdast-plus` 内部使用 [unified](https://github.com/unifiedjs/unified)。如果您多次添加同一个插件函数,最后的配置将**覆盖**之前的配置。
|
|
85
91
|
|
|
92
|
+
> **警告**: 将 `false` 作为插件选项传递(例如 `.use(myPlugin, false)`)将**完全禁用**该插件。对于普通插件,这意味着它们将不会运行。对于标记为 `main: true` 的插件(如默认解析器的替代者),如果它们被 `false` 禁用,它们将**不会**替换该阶段的默认插件,从而安全地回退到默认行为。如果你需要跳过插件逻辑但保持其激活状态(例如为了保留其解析器),请改用选项对象,例如 `{ enable: false }`。
|
|
93
|
+
|
|
86
94
|
```typescript
|
|
87
95
|
// 插件将只执行一次,且选项为: 2
|
|
88
96
|
pipeline.use(myPlugin, { option: 1 });
|
|
@@ -125,13 +133,19 @@ const result = await mdast('Hello').to('reverse');
|
|
|
125
133
|
|
|
126
134
|
## 分阶段处理
|
|
127
135
|
|
|
128
|
-
插件根据它们的 `stage` (阶段)
|
|
136
|
+
插件根据它们的 `stage` (阶段)、`order` (顺序) 以及语义约束 (`before`/`after`) 执行:
|
|
137
|
+
|
|
138
|
+
1. **parse** (0): 输入解析 (例如 `remark-parse`)。
|
|
139
|
+
2. **normalize** (100): 清理并规范化树。
|
|
140
|
+
3. **compile** (200): 高级语义转换。
|
|
141
|
+
4. **finalize** (300): 输出前的最后准备 (例如 `rehype-sanitize`)。
|
|
142
|
+
5. **stringify** (400): 输出生成。
|
|
143
|
+
|
|
144
|
+
### 主插件替换 (Main Plugin Replacement)
|
|
145
|
+
|
|
146
|
+
每个阶段可以有一个“主”插件。如果一个插件被标记为 `main: true`,它将 **替换** 该阶段中的第一个插件。这对于在保持管道其余部分不变的情况下更换默认解析器或编译器非常有用。
|
|
129
147
|
|
|
130
|
-
|
|
131
|
-
2. **normalize** (100): 清理并规范化树。
|
|
132
|
-
3. **compile** (200): 高级语义转换。
|
|
133
|
-
4. **finalize** (300): 输出前的最后准备 (例如 `rehype-sanitize`)。
|
|
134
|
-
5. **stringify** (400): 输出生成。
|
|
148
|
+
> **注意**: 每个阶段只允许存在一个主插件。如果多个插件被标记为 main,则只有最后定义的那个会作为替换生效。
|
|
135
149
|
|
|
136
150
|
## 内置核心插件
|
|
137
151
|
|
|
@@ -142,6 +156,7 @@ const result = await mdast('Hello').to('reverse');
|
|
|
142
156
|
| `extract-code-meta` | normalize | 从代码块元数据中解析 `title="foo"`。 |
|
|
143
157
|
| `image-size` | normalize | 从图片 URL 中解析 `#=WxH`。 |
|
|
144
158
|
| `normalize-inline-styles` | normalize | 标准化 `==mark==`、`~sub~` 和 `^sup^`。 |
|
|
159
|
+
| `html-readability` | parse | 使用 Mozilla 的 Readability 从 HTML 中提取主体内容。使用 `htmlReadabilityPlugins` 数组可以简化配置。 |
|
|
145
160
|
|
|
146
161
|
## 贡献
|
|
147
162
|
|
package/README.md
CHANGED
|
@@ -71,18 +71,26 @@ const rawAst = await mdast('==Highlighted==').toAST({ stage: 'parse' });
|
|
|
71
71
|
### Advanced Pipeline
|
|
72
72
|
|
|
73
73
|
```typescript
|
|
74
|
-
|
|
74
|
+
import { htmlReadabilityPlugins } from '@isdk/mdast-plus';
|
|
75
|
+
|
|
76
|
+
const vfile = await mdast(myInput)
|
|
75
77
|
.data({ myGlobal: 'value' })
|
|
76
|
-
// Add
|
|
77
|
-
.
|
|
78
|
+
// Add multiple plugins as an array at the 'compile' stage
|
|
79
|
+
.use([pluginA, pluginB])
|
|
80
|
+
// Or add a set of plugins at a specific stage
|
|
81
|
+
.useAt('parse', htmlReadabilityPlugins)
|
|
78
82
|
.priority(10) // Run later than default plugins
|
|
79
83
|
.to('html');
|
|
84
|
+
|
|
85
|
+
console.log(vfile.value); // The serialized HTML string
|
|
80
86
|
```
|
|
81
87
|
|
|
82
88
|
### Plugin Behavior
|
|
83
89
|
|
|
84
90
|
`mdast-plus` uses [unified](https://github.com/unifiedjs/unified) internally. If you add the same plugin function multiple times, the last configuration **overrides** the previous ones.
|
|
85
91
|
|
|
92
|
+
> **Warning**: Passing `false` as a plugin option (e.g., `.use(myPlugin, false)`) will **disable** the plugin entirely. For regular plugins, this means they simply won't run. For plugins marked as `main: true` (like replacements for the default parser), if they are disabled with `false`, they will **not** replace the default plugin of that stage, providing a safe fallback to the default behavior. If you want to bypass a plugin's logic while keeping it active (e.g. to maintain its parser), use an options object like `{ enable: false }` instead.
|
|
93
|
+
|
|
86
94
|
```typescript
|
|
87
95
|
// The plugin will run ONCE with option: 2
|
|
88
96
|
pipeline.use(myPlugin, { option: 1 });
|
|
@@ -125,7 +133,7 @@ const result = await mdast('Hello').to('reverse');
|
|
|
125
133
|
|
|
126
134
|
## Staged Processing
|
|
127
135
|
|
|
128
|
-
Plugins are executed based on their `stage` and `
|
|
136
|
+
Plugins are executed based on their `stage`, `order`, and semantic constraints (`before`/`after`):
|
|
129
137
|
|
|
130
138
|
1. **parse** (0): Input parsing (e.g., `remark-parse`).
|
|
131
139
|
2. **normalize** (100): Cleanup and canonicalize the tree.
|
|
@@ -133,6 +141,12 @@ Plugins are executed based on their `stage` and `order`:
|
|
|
133
141
|
4. **finalize** (300): Final preparation before output (e.g. `rehype-sanitize`).
|
|
134
142
|
5. **stringify** (400): Output generation.
|
|
135
143
|
|
|
144
|
+
### Main Plugin Replacement
|
|
145
|
+
|
|
146
|
+
Each stage can have one "main" plugin. If a plugin is marked with `main: true`, it will **replace** the first plugin in that same stage. This is useful for swapping out default parsers or compilers while keeping the rest of the pipeline intact.
|
|
147
|
+
|
|
148
|
+
> **Note**: Only one main plugin is allowed per stage. If multiple plugins are marked as main, only the last one defined will take effect as the replacement.
|
|
149
|
+
|
|
136
150
|
## Core Plugins Included
|
|
137
151
|
|
|
138
152
|
| Plugin | Stage | Description |
|
|
@@ -142,6 +156,7 @@ Plugins are executed based on their `stage` and `order`:
|
|
|
142
156
|
| `extract-code-meta` | normalize | Parses `title="foo"` from code block meta. |
|
|
143
157
|
| `image-size` | normalize | Parses `#=WxH` from image URLs. |
|
|
144
158
|
| `normalize-inline-styles` | normalize | Standardizes `==mark==`, `~sub~`, and `^sup^`. |
|
|
159
|
+
| `html-readability` | parse | Uses Mozilla's Readability to extract main content from HTML. Use `htmlReadabilityPlugins` array for easier setup. |
|
|
145
160
|
|
|
146
161
|
## Contributing
|
|
147
162
|
|