mcpField
O
obsidian-plugin

obsidian-plugin

github.com/akillness开发53221 次安装4 天前更新
代码仓库

关于 obsidian-plugin

obsidian-plugin 是用于开发和准备 Obsidian 插件的技能,结合项目脚手架生成、ESLint 检查、API 与生命周期指南、无障碍实践及提交验证。

主要功能

  1. 01生成插件项目脚手架
  2. 02检查 27 条 Obsidian ESLint 规则
  3. 03验证插件元数据和命名
  4. 04采用安全的生命周期模式
  5. 05指导无障碍界面和主题适配 CSS
  6. 06准备社区目录提交

适用场景

  1. 01启动新的 Obsidian 插件项目
  2. 02检查并修复 lint 问题
  3. 03提交前验证元数据
  4. 04改进无障碍性和生命周期管理

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/akillness/jeo-skills --skill obsidian-plugin

安装后在 Claude Code 中输入/obsidian-plugin确认已加载。

包含的文件

除 SKILL.md 外,安装时一起下载的参考资料和脚本。

SKILL.toon
evals/
evals.json
references/
accessibility.md
code-quality.md
css-styling.md
file-operations.md
memory-management.md
submission.md
type-safety.md
ui-ux.md
scripts/
create-plugin.js
install.sh

详细介绍

obsidian-plugin — Obsidian 插件开发技能

关键词:obsidian plugin · create obsidian plugin · obsidian eslint · obsidian submission

构建高质量的 Obsidian 插件,力求一次通过社区审核。 涵盖 eslint-plugin-obsidianmd v0.1.9 的全部 27 条规则、样板代码生成、 vault API 模式、无障碍要求和提交验证。

何时使用此技能

  • 生成全新的 Obsidian 插件项目,并使用精简的样板代码(不包含臃肿的示例代码)
  • 检查并修复 eslint-plugin-obsidianmd 的 ESLint 违规项
  • 为插件提交到 Obsidian 社区目录做好准备
  • 应用内存安全的生命周期模式(registerEvent,不存储视图引用)
  • 实现恰当的类型安全(不使用不安全的 as TFile 类型断言,也不使用 any)
  • 满足无障碍要求(键盘导航、ARIA 标签、焦点管理)
  • 使用 Obsidian CSS 变量,确保样式兼容主题
  • 验证插件元数据(manifest.json、插件 ID/名称/描述规则)

指南

步骤 1:生成新的插件项目
# Interactive boilerplate generator — validates metadata against submission rules
node scripts/create-plugin.js

# Or use npx directly from the source repo
npx github:gapmiss/obsidian-plugin-skill create-plugin

生成器会创建:

  • src/main.ts — 集成设置的插件类
  • src/settings.ts — 设置接口和 PluginSettingTab
  • manifest.json — 通过验证的插件元数据
  • styles.css — 包含 Obsidian 变量注释的 CSS 框架
  • tsconfig.json、package.json、esbuild.config.mjs — 构建工具链
  • version-bump.mjs、versions.json — 版本管理
  • LICENSE — MIT 许可证,自动填入年份和作者
步骤 2:安装 ESLint 验证工具
npm install --save-dev eslint eslint-plugin-obsidianmd
// eslint.config.mjs
import pluginObsidianmd from "eslint-plugin-obsidianmd";
export default [
  pluginObsidianmd.configs.recommended,
  // For locale string checking:
  // pluginObsidianmd.configs.recommendedWithLocalesEn,
];

运行验证:

npx eslint src/
npx eslint src/ --fix   # auto-fix where possible
步骤 3:遵循提交验证工作流
  1. 运行 bash scripts/install.sh,设置开发环境
  2. 根据命名规则验证插件 ID 和名称(见下文)
  3. 运行 ESLint — 必须通过全部 27 条规则
  4. 完成代码审查清单(参见参考资料)
  5. 向 obsidianmd/obsidian-releases 提交 PR

插件命名规则

字段规则
插件 ID使用小写字母、数字以及连字符/下划线;不得包含 "obsidian";不得以 "plugin" 为后缀
插件名称不得包含 "Obsidian";不得以 "Plugin" 为后缀;不得以 "Obsi" 为前缀或以 "dian" 为后缀
描述不得包含 "Obsidian" 或 "This plugin";必须以 ., ?, ! 或 ) 结尾
# Validate your manifest.json
node scripts/create-plugin.js --validate-only

ESLint 规则摘要(27 条规则)

提交与命名
规则自动修复描述
no-obsidian-in-id否插件 ID 不得包含 "obsidian"
no-plugin-suffix-in-id否插件 ID 不得以 "-plugin" 结尾
no-obsidian-in-name否插件名称不得包含 "Obsidian"
no-plugin-suffix-in-name否插件名称不得以 "Plugin" 结尾
valid-description否描述格式验证
内存与生命周期
规则自动修复描述
prefer-register-event否使用 this.registerEvent() 自动清理
no-view-reference否切勿存储直接的视图引用(会导致内存泄漏)
类型安全
规则自动修复描述
no-tfile-cast否避免使用 as TFile / as TFolder(改用 instanceof)
UI/UX(句首字母大写规则 — 大多可自动修复)
规则自动修复描述
sentence-case是所有 UI 文本均使用句首字母大写格式
no-plugin-name-in-command否命令中不得重复插件名称
no-command-in-command否命令中不得包含单词 "command"
no-default-hotkey否不要定义默认快捷键
no-manual-headings否设置 UI:使用 setHeading(),不要手写 HTML
API 最佳实践
规则自动修复描述
no-global-app否使用 this.app,不要使用全局 app
prefer-request-url否使用 requestUrl(),不要使用 fetch()
no-console-log否提交前移除 console.log
prefer-abstract-input-suggest否使用 AbstractInputSuggest 提供建议
样式
规则自动修复描述
no-inline-styles否使用 CSS 类;不要使用内联 style 属性
prefer-css-variables否使用 Obsidian CSS 变量,不要使用硬编码颜色
scope-plugin-styles否所有 CSS 都必须限定在插件 ID 选择器内
无障碍(必需)
规则自动修复描述
require-aria-label否图标按钮必须具有 aria-label
require-keyboard-nav否所有交互都必须支持键盘操作
require-focus-visible否使用 :focus-visible CSS 显示焦点指示器

关键代码模式

内存安全的事件注册
// ✅ Correct — auto-cleaned on plugin unload
this.registerEvent(
  this.app.vault.on('create', (file) => this.handleCreate(file))
);

// ❌ Wrong — leaks memory
this.app.vault.on('create', (file) => this.handleCreate(file));
类型安全的文件访问
// ✅ Correct — instanceof narrowing
const file = this.app.workspace.getActiveFile();
if (file instanceof TFile) {
  await this.app.vault.read(file);
}

// ❌ Wrong — unsafe cast
const file = this.app.workspace.getActiveFile() as TFile;
无障碍 — 图标按钮上的 ARIA
// ✅ Correct
const btn = containerEl.createEl('button', { cls: 'clickable-icon' });
btn.setAttribute('aria-label', 'Delete note');
setIcon(btn, 'trash');

// ❌ Wrong — no aria-label
const btn = containerEl.createEl('button', { cls: 'clickable-icon' });
setIcon(btn, 'trash');
使用 Obsidian 变量的 CSS
/* ✅ Correct — theme-compatible */
.my-plugin-button {
  background-color: var(--interactive-accent);
  color: var(--text-on-accent);
  border-radius: var(--radius-m);
}

/* ❌ Wrong — hardcoded colors */
.my-plugin-button {
  background-color: #7c3aed;
  color: white;
}

快速参考

任务命令
生成样板代码node scripts/create-plugin.js
安装 ESLintnpm install --save-dev eslint eslint-plugin-obsidianmd
运行 ESLintnpx eslint src/
自动修复 ESLintnpx eslint src/ --fix
构建插件npm run build
开发监听模式npm run dev
更新版本号npm run version

参考资料

评论

支持 Markdown