# manifest.json 与 nsh.json

> atomd 插件的两个配置文件：manifest.json 描述插件本身和它给 atomd 加的东西，nsh.json 描述 NSH 应用、构建号和权限。

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

每个插件有两个配置文件，分工不同：

* **`manifest.json`**：atomd 不运行插件就能读到的信息：ID、名称、版本、兼容性、侧边栏和命令。它也会随 Release 一起发布，社区插件市场靠它检查更新。
* **`nsh.json`**：NSH 用来构建和签名的应用信息：应用 ID、版本、构建号、入口文件、权限。

## manifest.json

```json
{
  "id": "word-count",
  "name": "Word Count",
  "version": "1.2.0",
  "minAtomdVersion": "1.0.0",
  "description": "Shows the words, characters and reading time of the open note.",
  "author": "Ada Lovelace",
  "authorUrl": "https://github.com/ada",
  "fundingUrl": "https://github.com/sponsors/ada",
  "repo": "ada/atomd-word-count",
  "api": 1,
  "contributes": {
    "sidebar": { "title": "Word count", "icon": "list" },
    "commands": [{ "id": "word-count.refresh", "title": "Count words in this note" }]
  }
}
```

| 字段                   | 必填 | 说明                                                                             |
| -------------------- | -- | ------------------------------------------------------------------------------ |
| `id`                 | 是  | 插件 ID：小写字母、数字、连字符，3–40 个字符，首尾不能是连字符。必须和插件文件夹同名，发布后不能改。不要包含 `atomd` 或 `plugin`。 |
| `name`               | 是  | 显示名称。                                                                          |
| `version`            | 是  | 语义化版本 `x.y.z`。必须等于 `nsh.json` 的 `version`，发布时也等于 Release 的 tag。                |
| `api`                | 否  | 插件 API 的大版本，目前是 `1`（默认）。atomd 不加载 API 版本不符的插件。                                 |
| `minAtomdVersion`    | 否  | 需要的最低 atomd 版本。用到较新的 Host API 时要写上，旧版 atomd 会提示用户升级，而不是加载后报错。                  |
| `description`        | 否  | 一句话说明插件做什么，250 字符以内。会显示在设置和社区插件市场里。                                            |
| `author`、`authorUrl` | 否  | 作者名和主页。                                                                        |
| `fundingUrl`         | 否  | 赞助链接。                                                                          |
| `repo`               | 否  | GitHub 仓库 `owner/name`。atomd 用它检查更新。                                           |
| `contributes`        | 否  | 插件给 atomd 加的东西，见下文。                                                            |

### contributes

这些是 atomd 不运行插件就知道的「贡献点」：

| 字段         | 说明                                                                                                           |
| ---------- | ------------------------------------------------------------------------------------------------------------ |
| `sidebar`  | 右侧边栏的一个面板。`title` 是标题，`icon` 是 [图标名](https://dinox.info/guides/atomd-plugins/views.md)。插件用 `atomd.ui.render` 决定面板里显示什么。        |
| `commands` | 快速打开（`⌘P`）里的命令，每个有 `id` 和 `title`。用户运行时，插件收到 `atomd.command` 事件。命令 `id` 建议以插件 ID 开头，例如 `word-count.refresh`。 |
| `menus`    | 把命令放进右键菜单，见 [menus](#menus)。                                                                                 |
| `settings` | 插件的设置项，由 atomd 在设置页绘制，见 [settings](#settings)。                                                               |

atomd 发现插件时就会检查这些字段。不合理的声明（未知的设置类型、重复的 `key`、没有选项的 `select`、类型不对的默认值、菜单里写了不是自己命令的 id）会让插件不被加载，原因显示在 **Settings → Plugins**。

一个完整的例子：

```json
"contributes": {
  "sidebar": { "title": "Codex", "icon": "sparkles" },
  "commands": [
    { "id": "codex.ask", "title": "Ask Codex about this note", "hotkey": "mod+shift+k" },
    { "id": "codex.explain", "title": "Explain with Codex" }
  ],
  "menus": { "editor": ["codex.explain"], "file": ["codex.ask"] },
  "settings": [
    { "key": "model", "type": "select", "title": "Model",
      "options": [{ "value": "fast", "label": "Fast" }, { "value": "best", "label": "Best" }] },
    { "key": "token", "type": "secret", "title": "API token", "placeholder": "sk-…" }
  ]
}
```

### hotkey

`commands[].hotkey` 可选，给命令绑一个快捷键，例如 `mod+shift+k`（`mod` 在 macOS 上是 Command，其他系统是 Control）。

* 插件运行时才生效，并显示在快速打开里命令的旁边。
* 必须带修饰键（`mod`、`ctrl`、`alt`、`shift`）。
* atomd 自己的快捷键（`mod+n`、`mod+p`、`mod+o`、`mod+j`、`mod+,`、编辑器的格式快捷键等）优先，其次是列表里更早的插件。被占用、无法识别或系统保留的快捷键会被忽略，插件那一行会提示，例如「Hotkey mod+n of “…” is ignored: …」。

### menus

`menus` 可选，把已声明的命令 id 放进右键菜单，排在 atomd 自己的菜单项后面：

| 键        | 位置                   |
| -------- | -------------------- |
| `editor` | 编辑器的右键菜单             |
| `file`   | 文件树里笔记的菜单（行尾的「…」或右键） |

从菜单运行时，`atomd.command` 事件的 `context` 会带上对应笔记的路径，编辑器菜单还会带上选中的文字，见 [atomd.command](https://dinox.info/guides/atomd-plugins/host-api.md)。

### settings

`settings` 可选，声明插件的设置项。atomd 会在 **Settings → Plugins** 里插件那一行显示 **Options**，并绘制对应的输入控件。

| 字段            | 必填          | 说明                                                 |
| ------------- | ----------- | -------------------------------------------------- |
| `key`         | 是           | 设置项的键，不能重复                                         |
| `type`        | 是           | `string`、`number`、`boolean`、`select`、`secret`      |
| `title`       | 是           | 显示的名称                                              |
| `description` | 否           | 名称下方的说明                                            |
| `default`     | 否           | 默认值，类型必须匹配。不写时为 `""`、`0`、`false`，或 `select` 的第一个选项 |
| `options`     | `select` 必填 | `[{ value, label }]`                               |
| `placeholder` | 否           | 输入框的占位文字                                           |

`secret` 是掩码显示的字符串，适合放 API Key。声明了设置项的插件可以直接调用 `atomd.settings.get` / `set`，不用写进 `capabilities`，见 [插件设置](https://dinox.info/guides/atomd-plugins/host-api.md)。

## nsh.json

```json
{
  "name": "Word Count",
  "id": "dev.atomd.plugin.word-count",
  "version": "1.2.0",
  "build": 3,
  "entry": "App.tsx",
  "capabilities": {
    "host": ["atomd.note.read", "atomd.ui.*"]
  }
}
```

| 字段             | 说明                                                           |
| -------------- | ------------------------------------------------------------ |
| `id`           | 固定为 `dev.atomd.plugin.<插件 ID>`。atomd 给你的签名证书只对这个 ID 有效。      |
| `version`      | 和 `manifest.json` 相同。                                        |
| `build`        | 构建号，整数，**每次发布都必须比上一次大**。atomd 会拒绝构建号比已安装版本小的包，防止被降级到有漏洞的旧版本。 |
| `entry`        | 入口文件，默认 `App.tsx`。                                           |
| `capabilities` | 插件要用的权限，见 [权限与沙箱](https://dinox.info/guides/atomd-plugins/capabilities.md)。       |

## 安装后的文件夹

atomd 在两个地方找插件。同一个 ID 两处都有时，笔记库里的优先：

* atomd 数据目录下的 `plugins/<id>/`，对所有笔记库生效。从社区插件市场安装的插件放在这里。macOS 是 `~/Library/Application Support/atomd/plugins/`。
* 当前笔记库的 `.atomd/plugins/<id>/`，只对这个笔记库生效。

文件夹里有两种形式：

| 形式   | 文件                                                                            | 什么时候加载                      |
| ---- | ----------------------------------------------------------------------------- | --------------------------- |
| 签名包  | `manifest.json`、`plugin.nshpkg`                                               | 总是加载（签名校验通过时）               |
| 开发构建 | `manifest.json`，以及 `nsh build` 生成的 `bundle.js`、`capabilities.json`、`app.json` | 只在 **Developer mode** 打开时加载 |

插件自己的数据目录是 atomd 数据目录下的 `plugin-data/<id>/`（插件里是 `process.dataDir`）。卸载插件时保留这个目录。