Skip to content

Plugin

Official

Base class for all plugins.

Import:

import { Plugin } from 'obsidian';

Signature:

export class Plugin extends Component

Extends: Component

Constructor

new Plugin(app: App, manifest: PluginManifest)

Constructor.

To extract the constructor type, use ExtractConstructor<Plugin>.

Properties

PropertyTypeDescription
_childrenComponent[]Child Components attached to current component, will be unloaded on unloading parent component.
(Inherited from Component)
_eventsEventRef[]Events that are attached to the current component, will be detached on unloading parent component.
(Inherited from Component)
_lastDataModifiedTimenumberThe last time the plugin's data was modified.
_loadedbooleanWhether the component and its children are loaded.
(Inherited from Component)
_userDisabledbooleanWhether the plugin was disabled by the user.
appAppThe Obsidian app instance.
manifestPluginManifestThe plugin manifest.
onConfigFileChangeDebouncer<[], Promise<void>>Debounced handler for config file changes.
settings?unknownobsidian#Plugin settings. Assign loaded data here in onload. Declare a concrete type on your subclass to type it.
settingTab?SettingTabThe plugin's registered setting tab, if any.

Methods

MethodReturnsDescription
_onConfigFileChange()Promise<void>Internal handler invoked when the plugin's config file changes on disk.
addChild(component)TAdds a child component, loading it if this component is loaded.
(Inherited from Component)
addCommand(command)CommandRegister a command globally. Registered commands will be available from the https://help.obsidian.md/Plugins/Command+palette Command palette. The command id and name will be automatically prefixed with this plugin's id and name.
addRibbonIcon(icon, title, callback)HTMLElementAdds a ribbon icon to the left bar.
addSettingTab(settingTab)voidRegister a settings tab, which allows users to change settings.
addStatusBarItem()HTMLElementAdds a status bar item to the bottom of the app. Not available on mobile.
getModifiedTime()Promise<number>Gets the latest modification time across the plugin's files.
load()voidLoad this component and its children.
(Inherited from Component)
loadCSS()Promise<void>Loads the plugin's styles.css into the document.
loadData()Promise<any>Load settings data from disk. Data is stored in data.json in the plugin folder.
onExternalSettingsChange()anyCalled when the data.json file is modified on disk externally from Obsidian. This usually means that a Sync service or external program has modified the plugin settings.

Implement this method to reload plugin settings when they have changed externally.
onload()Promise<void> | voidCalled when the plugin is loaded.
onunload()voidOverride this to unload your component
(Inherited from Component)
onUserEnable()voidPerform any initial setup code. The user has explicitly interacted with the plugin so its safe to engage with the user. If your plugin registers a custom view, you can open it here.
register(cb)voidRegisters a callback to be called when unloading.
(Inherited from Component)
registerBasesView(viewId, registration)booleanRegister a Base view handler that can be used to render data from property queries.
registerCliHandler(command, description, flags, handler)voidRegister a CLI handler to handle a command from the CLI. obsidian#Command IDs must be globally unique. Attempting to register a command that is already registered will throw an Error.

Use the format <plugin-id> for your default command, and <plugin-id>:<action> for sub-commands and actions.
registerCodeMirror(callback)voidRegisters a legacy CodeMirror 5 callback. No-op in current Obsidian versions.
registerDomEvent(el, type, callback, options?)voidRegisters a DOM event to be detached when unloading.
(Inherited from Component)
registerDomEvent(el, type, callback, options?)voidRegisters a DOM event to be detached when unloading
(Inherited from Component)
registerDomEvent(el, type, callback, options?)voidRegisters a DOM event to be detached when unloading
(Inherited from Component)
registerEditorExtension(extension)voidRegisters a CodeMirror 6 extension. To reconfigure cm6 extensions for a plugin on the fly, an array should be passed in, and modified dynamically. Once this array is modified, calling obsidian#Workspace.updateOptions will apply the changes.
registerEditorSuggest(editorSuggest)voidRegister an obsidian#EditorSuggest which can provide live suggestions while the user is typing.
registerEvent(eventRef)voidRegisters an event to be detached when unloading.
(Inherited from Component)
registerExtensions(extensions, viewType)voidRegister a set of extensions for a view type.
registerGlobalFunc(func)voidRegisters a global Bases formula function, unregistered automatically on unload.
registerHoverLinkSource(id, info)voidRegisters a view with the 'Page preview' core plugin as an emitter of the 'hover-link' event.
registerInstanceFunc(type, func)voidRegisters a Bases formula function for a specific value type, unregistered automatically on unload.
registerInterval(id)numberRegisters an interval (from setInterval) to be cancelled when unloading. Use window.setInterval instead of setInterval to avoid TypeScript confusing between NodeJS vs Browser API
(Inherited from Component)
registerMarkdownCodeBlockProcessor(language, handler, sortOrder?)MarkdownPostProcessorRegister a special post processor that handles fenced code given a language and a handler. This special post processor takes care of removing the <pre><code> and create a <div> that will be passed to the handler, and is expected to be filled with custom elements.
registerMarkdownPostProcessor(postProcessor, sortOrder?)MarkdownPostProcessorRegisters a post processor, to change how the document looks in reading mode.
registerObsidianProtocolHandler(action, handler)voidRegister a handler for obsidian:// URLs.
registerScopeEvent(keymapEventHandler)voidRegister a scope keymap event handler to be removed on unload.
(Inherited from Component)
registerView(type, viewCreator)voidRegister a custom view.
removeChild(component)TRemoves a child component, unloading it.
(Inherited from Component)
removeCommand(commandId)voidManually remove a command from the list of global commands. This should not be needed unless your plugin registers commands dynamically.
saveData(data)Promise<void>Write settings data to disk. Data is stored in data.json in the plugin folder.
unload()voidOverride this to unload your component.
(Inherited from Component)

Links to this page: