# 发布插件

> 把插件提交到 atomd 社区插件注册表：生成发布密钥、提交 Pull Request、获得证书、用 GitHub Actions 签名发布，以及之后怎样更新、续期和处理密钥丢失。

- 网页：https://dinox.info/guides/atomd-plugins/publish
- 本页 Markdown：https://dinox.info/guides/atomd-plugins/publish.md
- 全部文档索引：https://dinox.info/llms.txt

atomd 的社区插件注册表是 GitHub 仓库 [ryzencool/community-plugins](https://github.com/ryzencool/community-plugins)。流程和 Obsidian 很像：首次上架提交一次 Pull Request，审核通过后，之后的版本推 tag 就会自动发布。

不同的是，atomd 只加载**签名包**。你会有一把自己的发布密钥，atomd 维护者审核你的插件后，为这把密钥签发一张只对你的插件 ID 有效的证书。

## 发布前检查

* 仓库在 GitHub 上公开，有开源许可证和说明插件用途的 README。
* `manifest.json` 的 `id`、`name`、`author`、`description`、`version` 都填好了，`repo` 指向这个仓库。
* `nsh.json` 的 `id` 是 `dev.atomd.plugin.<id>`，`version` 和 `manifest.json` 相同。
* 仓库里有示例插件的 `.github/workflows/release.yml`。
* 已经在 Developer mode 下完整测过一遍，并读过 [审核规范](https://dinox.info/guides/atomd-plugins/review.md)。

## 第一次上架

### 第 1 步：生成发布密钥

在自己的电脑上运行：

```bash
mkdir -p ~/.atomd-release && cd ~/.atomd-release
nsh keygen word-count
```

得到两个文件：

* `word-count.secret`：**私钥**。只存在你自己的电脑和 GitHub Secrets 里，不要提交到任何仓库，也不要发给任何人，包括 atomd 维护者。丢了它就要走[密钥丢失](#密钥丢失或泄露)流程。
* `word-count.pub`：公钥，下一步要提交。

### 第 2 步：提交 Pull Request

Fork [ryzencool/community-plugins](https://github.com/ryzencool/community-plugins)，做两处修改：

1. 在 `plugins.json` 的**末尾**加一条：

   ```json
   {
     "id": "word-count",
     "name": "Word Count",
     "author": "Ada Lovelace",
     "description": "Shows the words, characters and reading time of the open note.",
     "repo": "ada/atomd-word-count"
   }
   ```

   这几个字段必须和你仓库里 `manifest.json` 的一致。

2. 把 `word-count.pub` 复制为 `keys/word-count.pub`。

提交 Pull Request，按模板勾选检查项。CI 会检查条目格式、ID 是否重复、你仓库里的 `manifest.json` 和 `nsh.json`，并把你声明的权限列在检查结果里，方便审核。

### 第 3 步：等待审核

维护者会阅读你的源代码和权限声明，可能在 Pull Request 里提问或要求修改。审核标准见 [审核规范](https://dinox.info/guides/atomd-plugins/review.md)。

### 第 4 步：拿到证书

审核通过后，维护者把证书 `certs/word-count.cert` 加进你的 Pull Request。证书只授权你的密钥签名 `dev.atomd.plugin.word-count`，有效期一年。

在你的插件仓库里打开 **Settings → Secrets and variables → Actions**，添加两个 Repository secret：

| 名称                   | 内容                          |
| -------------------- | --------------------------- |
| `ATOMD_RELEASE_KEY`  | `word-count.secret` 的内容     |
| `ATOMD_RELEASE_CERT` | `certs/word-count.cert` 的内容 |

### 第 5 步：发布第一个版本

tag 名等于 `manifest.json` 的 `version`，**不加 `v` 前缀**：

```bash
git tag 1.0.0
git push origin 1.0.0
```

release workflow 会依次：

1. 检查 tag、`manifest.json`、`nsh.json` 的版本和 ID 是否一致；
2. 用 NSH 0.7.1 做类型检查，用你的密钥和证书签名，生成 `plugin.nshpkg`；
3. 创建 GitHub Release，附上 `manifest.json` 和 `plugin.nshpkg`。

在 Pull Request 里留言告诉维护者已经发布。CI 会下载 `plugin.nshpkg`，用 atomd 的根公钥校验签名，通过后合并。合并后几分钟内，你的插件就出现在所有用户的 **Settings → Plugins → Browse community plugins** 里。

也可以不用 GitHub Actions，在本地签名后手动上传：

```bash
nsh publish . --key ~/.atomd-release/word-count.secret --cert word-count.cert -o plugin.nshpkg
nsh verify plugin.nshpkg
gh release create 1.0.0 manifest.json plugin.nshpkg
```

## 发布新版本

不需要再提交 Pull Request：

1. 同时改 `manifest.json` 和 `nsh.json` 的 `version`，并把 `nsh.json` 的 `build` 加一。
2. 用到了新的 Host API 时，更新 `minAtomdVersion`。
3. 提交并推送到默认分支，再推一个同名 tag。

atomd 通过读取你仓库**默认分支**上的 `manifest.json` 得知最新版本，再从同名 tag 的 Release 下载。所以顺序很重要：**先确保 Release 已经创建成功**，再把新版本号合并进默认分支。最稳妥的做法是在一个分支上打 tag 发布，确认 Release 正常后再合并。

如果新版本改变了权限声明，用户更新后插件会暂停，等用户在设置里确认新权限。请在 Release 说明里写清楚原因。

## 证书续期

证书有效期一年。到期前一个月左右，在注册表仓库开一个 Issue 申请续期，维护者会签发新证书，你更新 `ATOMD_RELEASE_CERT` 即可。已经发布的版本不受证书到期影响，只是到期后不能再签新版本。

## 密钥丢失或泄露

立刻在注册表仓库开 Issue（泄露请同时私信维护者）。维护者会：

1. 把旧密钥加入撤销列表。用旧密钥签名的版本会在用户下次启动 atomd 时停止加载，所以请尽快发布新版本。
2. 为你提交的新公钥签发证书。

## 下架插件

想停止维护时，在注册表仓库开 Pull Request 删除你的条目。已安装的用户可以继续使用，但不会再收到更新，也无法再从市场安装。

## 注册表文件格式

供需要自己实现工具的开发者参考：

| 地址                                                                                    | 内容                                             |
| ------------------------------------------------------------------------------------- | ---------------------------------------------- |
| `https://raw.githubusercontent.com/ryzencool/community-plugins/main/plugins.json`     | 插件列表：`id`、`name`、`author`、`description`、`repo` |
| `https://raw.githubusercontent.com/ryzencool/community-plugins/main/revocations.json` | 签名的撤销列表                                        |
| `https://raw.githubusercontent.com/<repo>/HEAD/manifest.json`                         | 插件的最新版本信息                                      |
| `https://github.com/<repo>/releases/download/<version>/plugin.nshpkg`                 | 某个版本的签名包                                       |