跳至正文

快速入门

为 Capacitor 插件生成文档,包括通过 TypeScript extends 继承的成员。这是独立维护的 Ionic @capacitor/docgen 分支。

在小型沙盒中试用

mkdir docgen-demo
cd docgen-demo
npm init -y
npm install --save-dev @rdlabo/capacitor-docgen@0.4.1

不要在同一项目中同时安装上游的 @capacitor/docgen,两个包都提供名为 docgen 的可执行文件。

创建 src/definitions.ts:

export interface SharedOptions {
  requestId?: string;
}

export interface CreateOptions extends SharedOptions {
  value: string;
}

export interface MyPlugin {
  create(options: CreateOptions): Promise<void>;
}

创建 tsconfig.json:

{
  "compilerOptions": {
    "target": "ES2020",
    "module": "commonjs",
    "strict": true
  },
  "files": ["src/definitions.ts"]
}

创建 README.md,加入供 docgen 更新的占位符:

<docgen-index></docgen-index>

<docgen-api></docgen-api>

运行:

npx docgen --project tsconfig.json --api MyPlugin --output-readme README.md --output-json dist/docs.json

生成的 CreateOptions 文档会同时包含 value 和 requestId。如需修改生成内容,请编辑 TypeScript 接口或 JSDoc,然后重新运行同一命令。标记以外的正文会保留。

在已有插件的工作流中,可以在 package.json 添加脚本,例如 "docgen": "docgen --api MyPlugin --output-readme README.md"。本示例无需添加脚本。

文档