开发步骤

  1. obsidian-sample-plugin克隆一个仓库
  2. 修改其中的 manifest.json 文件,其中 plugin id 是最重要的。
  3. 修改主文件 main.ts,使用 npm install 安装依赖库
  4. 使用命令 npm run dev 编译出 main.js
  5. 在 Obsidian 的 vault 目录 .obsidian/plugins/ 创建一个插件名称的文件夹,拷贝 manifest.json 和 main.js 到该目录,有的插件可能还有 style.css 等文件。
  6. 在 settings 页面加载插件

在开发过程中,可以通过 Ctrl-Shift-i 来打开调试页面。

创建您的第一个插件

开发插件

hot-reload: 热重载

首先,需要在插件目录中添加 .hotreload 文件,然后,下载插件并开启。

注意❗

  • 如果没有hot-reload,需要重新打开插件,代码才能生效。
  • 插件id为输出文件夹的名称。

常用命令

如何开启开发者模式 /控制台

  1. Win:Ctrl + Shitf + i.
  2. Mac:cmd+ option + i.

发布插件

使用 GitHub Actions 发布您的插件

name: Release Obsidian plugin
 
on:
  push:
    tags:
      - "*"
 
env:
  PLUGIN_NAME: your-plugin-id # Change this to match the id of your plugin.
 
jobs:
  build:
    runs-on: ubuntu-latest
 
    steps:
      - uses: actions/checkout@v2
      - name: Use Node.js
        uses: actions/setup-node@v1
        with:
          node-version: "14.x"
 
      - name: Build
        id: build
        run: |
          npm install
          npm run build
          mkdir ${{ env.PLUGIN_NAME }}
          cp main.js manifest.json styles.css ${{ env.PLUGIN_NAME }}
          zip -r ${{ env.PLUGIN_NAME }}.zip ${{ env.PLUGIN_NAME }}
          ls
          echo "::set-output name=tag_name::$(git tag --sort version:refname | tail -n 1)"
 
      - name: Create Release
        id: create_release
        uses: actions/create-release@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          VERSION: ${{ github.ref }}
        with:
          tag_name: ${{ github.ref }}
          release_name: ${{ github.ref }}
          draft: false
          prerelease: false
 
      - name: Upload zip file
        id: upload-zip
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ steps.create_release.outputs.upload_url }}
          asset_path: ./${{ env.PLUGIN_NAME }}.zip
          asset_name: ${{ env.PLUGIN_NAME }}-${{ steps.build.outputs.tag_name }}.zip
          asset_content_type: application/zip
 
      - name: Upload main.js
        id: upload-main
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ steps.create_release.outputs.upload_url }}
          asset_path: ./main.js
          asset_name: main.js
          asset_content_type: text/javascript
 
      - name: Upload manifest.json
        id: upload-manifest
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ steps.create_release.outputs.upload_url }}
          asset_path: ./manifest.json
          asset_name: manifest.json
          asset_content_type: application/json
 
      - name: Upload styles.css
        id: upload-css
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ steps.create_release.outputs.upload_url }}
          asset_path: ./styles.css
          asset_name: styles.css
          asset_content_type: text/css

提交插件

将插件添加到配置文件中,提交pull request 进行审核。

{
  "id": "recent-files-obsidian",
  "name": "Recent Files",
  "author": "Tony Grosinger",
  "description": "Display a list of recently opened files",
  "repo": "tgrosinger/recent-files-obsidian",
  "branch": "main"
}

扩展阅读