Obsidian Plugin Development

wiki obsidian plugin dev

#obsidian #plugin #dev

How to develop Obsidian Community Plugins

Summary Concepts

Mental model

  • Plugin = TypeScript/JS bundle (main.js) + manifest.json (+ optional styles.css), loaded into Obsidian’s Electron renderer (desktop) or WebView (mobile).
  • Runs in-process with full access to the app. No sandbox, so you own the performance and safety.
  • Entry point: a class extending Plugin with onload() / onunload().

Toolchain

  • Start from obsidianmd/obsidian-sample-plugin: TypeScript, esbuild, npm.
  • Dev loop: clone into <vault>/.obsidian/plugins/<id>/, run npm run dev (watch build), then reload with the Hot Reload plugin or Ctrl+R.
  • Use a dedicated test vault, not your real one.
  • Types come from the obsidian package (obsidian.d.ts is the best docs).

Core API surface

  • app.vault: file I/O (read, cachedRead, modify, create, process). Use process for atomic read-modify-write.
  • app.metadataCache: parsed frontmatter, tags, links, headings. Use this instead of re-parsing files yourself. Tag queries need it.
  • app.workspace: leaves, views, active file, layout events.
  • app.fileManager: rename/delete that keeps links intact, plus processFrontMatter.
  • UI: addCommand, addRibbonIcon, addSettingTab, Modal, Notice, SuggestModal/FuzzySuggestModal, ItemView (custom panes), registerView.
  • Editor: editorCallback commands; CodeMirror 6 extensions via registerEditorExtension for decorations/live preview.
  • Markdown: registerMarkdownPostProcessor, registerMarkdownCodeBlockProcessor for rendering.

Lifecycle and hygiene (where people go wrong)

  • Register everything through this.registerEvent, registerDomEvent, registerInterval. Obsidian cleans these up on unload. Raw listeners leak.
  • Don’t touch the vault at load time. Wait for app.workspace.onLayoutReady(), or you get create events for every existing file.
  • Persist settings via loadData() / saveData() (data.json). Merge with defaults: Object.assign({}, DEFAULTS, await this.loadData()).
  • Debounce metadataCache.on('changed') and vault.on('modify'). They fire a lot.
  • Mobile: avoid Node/Electron APIs (fs, path) unless isDesktopOnly: true.

Release

  • GitHub release with main.js, manifest.json, styles.css, then PR to obsidianmd/obsidian-releases.
  • Follow the plugin guidelines: no telemetry without consent, no innerHTML with user content, and clean up on unload.

### Architecture advice (given my background)

  • Keep Obsidian-coupled code thin. Put pure logic (e.g. tag-set algebra, filtering) in plain TS modules with no obsidian imports, so it’s unit-testable with vitest/jest.
  • Treat App/Vault as an adapter boundary: define a small interface, inject it, and mock it in tests. The Obsidian API can’t be run headless.
  • Prefer immutable data and pure functions for the domain model. TS handles this well with discriminated unions and readonly.
  • Build indexes once from metadataCache, then update them incrementally from events. Don’t rescan the vault per query.
  • Settings are a versioned schema, so plan migrations early.

Resources

  • docs.obsidian.md/Plugins (guides plus API reference)
  • obsidianmd/obsidian-api repo (obsidian.d.ts)
  • obsidianmd/obsidian-sample-plugin
  • Read the source of well-known plugins (Dataview, Templater) for patterns.