Obsidian cheatsheet
Plugin development
- API reference
- Sample plugin code
- To develop, create symlink in
VAULT/.obsidian/plugins
to directory withmain.js
andmanifest.json
file. - Just toggle the plugin in the "Community plugin" menu to reload changes.
Useful APIs
- Get active file:
this.app.workspace.getActiveFile()
Show a notice
import { Notice, sanitizeHTMLToDom } from "obsidian";
new Notice("Hello, world", /* durationMs */ 5000);
const document = sanitizeHTMLToDom("<strong>Hello, world!</p>");
new Notice(document);
Tips:
- Set duration to 0 to display indefinitely.
- If you include a
<pre>
block inside the notice, use this style to prevent overflowing:max-height: 500px; overflow: auto;
Fuzzy selection
class ShellCommandModal extends FuzzySuggestModal<T> {
choices: T[]
onTrigger: (choice: T) => void;
constructor(choices: T[], onTrigger: (choice: T) => void) {
super(plugin.app);
this.choices = choices;
this.onTrigger = onTrigger;
}
getItems() {
return this.choices;
}
getItemText(item: T) {
return /* ... */;
}
onChooseItem(item: T) {
this.onTrigger(item);
}
}
const modal = ShellCommandModal(/* ... */);
modal.open();