DinoxDinox AI笔记

快速开始:第一个插件

从示例插件 Word Count 开始,构建并加载到 atomd,在右侧边栏看到当前笔记的字数统计。

这一页带你做出一个能用的插件:在右侧边栏显示当前笔记的字数、字符数和阅读时间。

准备

  1. 安装 atomd 1.0.0 或更新版本。

  2. 安装 NSH 命令行 0.7.1,版本要和 atomd 内置的 NSH 运行时一致:

    curl -fsSL https://nsh.dev/install.sh | NSH_VERSION=v0.7.1 sh
    nsh --version   # nsh 0.7.1

    Windows 用 PowerShell 运行 irm https://nsh.dev/install.ps1 | iex。

不要用更新的 NSH 来打包要发布的插件:签名包会记录打包时的运行时版本,更旧的 atomd 会拒绝加载它。日常开发和类型检查不受影响。

拿到示例插件

打开示例插件仓库 ryzencool/atomd-sample-plugin,点 Use this template 创建你自己的仓库,再克隆到本地:

atomd-word-count/
  manifest.json   atomd 读取的插件信息:id、名称、版本、侧边栏、命令
  nsh.json        NSH 应用信息:应用 id、版本、构建号、权限
  App.tsx         插件入口
  atomd.ts        atomd API 的类型化封装,原样复制即可
  .github/workflows/release.yml   推 tag 时自动签名并发布

起一个插件 ID

插件 ID 只能用小写字母、数字和连字符,3 到 40 个字符,例如 word-count。它在社区注册表里全局唯一,发布后不能再改。

把它写进两个地方:

manifest.json
{
  "id": "word-count",
  "name": "Word Count",
  "version": "1.0.0",
  "api": 1,
  ...
}
nsh.json
{
  "id": "dev.atomd.plugin.word-count",
  "version": "1.0.0",
  ...
}

nsh.json 的 id 必须是 dev.atomd.plugin. 加上你的插件 ID,两个文件的 version 也必须相同,否则 atomd 不会加载。

读懂 App.tsx

示例插件只做三件事:

import { notify, onCommand, onNoteChanged, onViewEvent, readNote, renderSidebar } from "./atomd";

async function refresh() {
  const note = await readNote();              // 读当前笔记(含未保存的输入)
  if (!note) {
    renderSidebar({ body: [{ type: "empty", icon: "note", title: "No note open" }] });
    return;
  }
  const s = stats(note.markdown);
  renderSidebar({                              // 把侧边栏内容整个换掉
    actions: [{ id: "refresh", icon: "rotate", label: "Refresh" }],
    body: [
      { type: "text", text: note.title, size: "lg", weight: "semibold" },
      { type: "card", children: [row("Words", String(s.words)) /* … */] },
    ],
  });
  return s;
}

onNoteChanged(() => void refresh());                         // 切换笔记时
onViewEvent((e) => { if (e.id === "refresh") void refresh(); }); // 点刷新按钮时
onCommand(async (id) => {                                    // 在 ⌘P 里运行命令时
  const s = await refresh();
  if (s) notify(`${s.words} words`);
});

它用到的 API 都在 nsh.json 里声明过:

nsh.json
"capabilities": {
  "host": ["atomd.note.read", "atomd.ui.*"]
}

构建并加载

类型检查

nsh check .

构建到 atomd 的插件目录

最简单的位置是当前笔记库里的 .atomd/plugins/<id>/,各平台都一样:

DEST="/path/to/your/vault/.atomd/plugins/word-count"
nsh build . -o "$DEST" && cp manifest.json "$DEST/"

也可以放在 atomd 的数据目录,对所有笔记库生效。macOS 是 ~/Library/Application Support/atomd/plugins/word-count。

打开开发者模式

在 atomd 里按 ⌘, 打开 Settings → Plugins,打开 Developer mode。只有开着它,atomd 才会加载本地构建的未签名插件,列表里会标出「Unsigned · development」。

启用插件

在插件列表里打开 Word Count,确认它申请的权限。按 ⌘J 打开插件侧边栏,就能看到字数统计。之后每次改完代码,重新运行上面的 nsh build 命令,再点 Reload。

下一步

atomd 自带的 Codex Agent 插件是一个更完整的例子:它运行 Codex CLI 和当前笔记对话,以流式方式显示回复,并把修改建议画成差异卡片,供用户应用或拒绝。

On this page