写 plugin
md2html 的 Plugin 契约让你自己加 engine(新命令)、transform(HTML 转换)、provider(新 LLM/image API)、publish target。
EnginePlugin 5 分钟入门
新建一个 npm 包 md2html-engine-poll(或 @youorg/engine-poll):
// index.ts
import type { EnginePlugin } from "@md2html-cli/md2html/plugin/contract";
import { contentHash } from "@md2html-cli/md2html/build/canonical";
const PollPlugin: EnginePlugin = {
id: "engine-poll",
kind: "engine",
cmd: "/插入投票",
effects: [],
cacheKey: (block) => contentHash({ body: block.body }),
async run(block, ctx) {
const lines = block.body.split("\n").filter(Boolean);
const question = lines[0];
const options = lines.slice(1).map((o) => `<li>${o}</li>`).join("");
return { html: `<div class="poll"><h4>${question}</h4><ul>${options}</ul></div>` };
},
};
export default PollPlugin;
package.json:
{
"name": "md2html-engine-poll",
"main": "index.ts",
"type": "module"
}
用户装:
npm install md2html-engine-poll
md2html 启动时自动扫 node_modules 中名字以 md2html-engine- 或 @org/md2html-engine- 开头的包,注册它们。
写 markdown 用:
::: /插入投票
你最喜欢哪个 widget?
filterable-table
tabs
slider-calc
:::
其他 plugin 类型
- TransformPlugin — 对 HTML 字符串 / hast tree 做转换(如 TOC 注入、KaTeX 渲染)
- PublishPlugin — 新发布目标(如 Vercel、Surge.sh)
- ProviderPlugin — 新 LLM 或 image provider(如 Claude API、Stable Diffusion)
完整契约
src/plugin/contract.ts 是权威定义。所有字段(effects、requires、cacheKey、estimateCost)的语义见类型注释。