写 plugin

阅读约 2 分钟

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 类型

完整契约

src/plugin/contract.ts 是权威定义。所有字段(effectsrequirescacheKeyestimateCost)的语义见类型注释。

完整教程