-
Notifications
You must be signed in to change notification settings - Fork 4
About views and registries
Note
This page is a work-in-progress. Some topics might be incomplete due to limited information. Please keep this in mind while reading.
- https://docs.obsidian.md/Plugins/User+interface/Views
- https://help.obsidian.md/Files+and+folders/Accepted+file+formats
- https://docs.obsidian.md/Plugins/User+interface/Workspace
- https://help.obsidian.md/Files+and+folders/How+Obsidian+stores+data
- https://docs.obsidian.md/Plugins/Editor/Editor
The view registry, referenced as viewRegistry
in Obsidian's runtime API, provides
several useful functions. One notable feature is its ability to "read" everything
related to views and their extensions (types).
-
Hidden State of View Registry (Field
_
):Contains three types of view-extensions: "extensions updated," "view-registered," and "view-unregistered," each storing relevant data.
Note
Their type is [{}]
, so to access the data, use [0]
.
Each contains various instances with names and more. More details will be added
to the API module as .d.ts
files are specified.
Here is an example of viewRegistry._['extensions-updated']
in the Developer
Console:
Through these parameters, you can access leafs, parent methods, functions, files, etc. Keep in mind, the view registry's subdata state can already be "ejected."
Important
While "extensions-updated" provides ample information on the view registry, other data types ("view-registered" and "view-unregistered") often contain empty objects.
-
Accessing "Dictionaries" in the View Registry:
The view registry has two main dictionaries: one maps the view by type (name) with keys for view type and values as factory functions (or delegates).
The second is a dictionary where the key is a file extension, and the value is the assigned view type as a string:
{ "md": "markdown", "bmp": "image", "png": "image", "jpg": "image", "jpeg": "image", "gif": "image", "svg": "image", "webp": "image", "avif": "image", "mp3": "audio", "wav": "audio", "m4a": "audio", "3gp": "audio", "flac": "audio", "ogg": "audio", "oga": "audio", "opus": "audio", "mp4": "video", "webm": "video", "ogv": "video", "mov": "video", "mkv": "video", "pdf": "pdf", "canvas": "canvas" }
-
Various Methods:
The view registry provides methods to register extensions for views, register views by extension, check if an extension is registered, and more.
Views are essential to Obsidian’s ecosystem, defining how content is displayed. Examples include the file explorer, graph view, and Markdown view. You can also create custom views for displaying plugin content.
Each view has a unique identifier. Using a constant, VIEW_TYPE_EXAMPLE
, to identify
the view is recommended:
-
getViewType()
returns a unique view identifier. -
getDisplayText()
returns a human-friendly view name. -
onOpen()
is triggered when the view is opened in a new leaf and builds the view’s content. -
onClose()
cleans up resources when the view closes.
Register and clean up custom views in the plugin lifecycle:
import { Plugin, WorkspaceLeaf } from 'obsidian';
import { ExampleView, VIEW_TYPE_EXAMPLE } from './view';
export default class ExamplePlugin extends Plugin {
async onload() {
this.registerView(
VIEW_TYPE_EXAMPLE,
(leaf) => new ExampleView(leaf)
);
this.addRibbonIcon('dice', 'Activate view', () => {
this.activateView();
});
}
async onunload() {}
async activateView() {
const { workspace } = this.app;
let leaf: WorkspaceLeaf | null = null;
const leaves = workspace.getLeavesOfType(VIEW_TYPE_EXAMPLE);
if (leaves.length > 0) {
leaf = leaves[0];
} else {
leaf = workspace.getRightLeaf(false);
await leaf.setViewState({ type: VIEW_TYPE_EXAMPLE, active: true });
}
workspace.revealLeaf(leaf);
}
}
The second argument to registerView() is a factory function that returns an instance of the view.
Warning
Do not manage view references in your plugin. Obsidian may call the view factory
function multiple times. Avoid side effects, and use getLeavesOfType()
to access
view instances:
this.app.workspace.getLeavesOfType(VIEW_TYPE_EXAMPLE).forEach((leaf) => {
if (leaf.view instanceof ExampleView) {
// Access your view instance.
}
});
Obsidian contains "default views", meaning views assigned in the instance by default, there is a list of this views (with ids):
Note
More context about table of views, their extensions and ids see in specified page: https://github.com/Falcion/UNITADE.md/wiki/Views-and-their-extensions
Assignment (which data represented by view) | ID |
---|---|
Markdown | markdown |
Images | image |
Audio | audio |
Video | video |
pdf |
|
Canvas (Obsidian) | canvas |
For extensions read next chapter, and keep in mind: you can assign custom views, even from different plugins (MAKE.md for example), but it is not guaranteed to work properly with given file.
There is a significant difference between ItemView
in the API example and TextFileView
.
The latter implements a plaintext-based, editable file view that can load and
save with an editor.
Note
By default, this view only saves when closing. For auto-save, call this.requestSave()
when content changes.
For custom text views, using super
calls implements this auto-save.
While TextFileView
is suited to text editing, ItemView
is more abstract, designed
for specialized views like canvases or graphs.
Extensions registry in UNITADE represents rendered and supported file extensions accessed by UNITADE, though unrelated to vanilla Obsidian.
By default, Obsidian recognizes the following file formats:
- Markdown:
md
- Images:
avif
,bmp
,gif
,jpeg
,jpg
,png
,svg
,webp
- Audio:
flac
,m4a
,mp3
,ogg
,wav
,webm
,3gp
- Video:
mkv
,mov
,mp4
,ogv
,webm
- PDF:
pdf
Note
Audio and video format support depends on device codecs.
Obsidian stores notes as Markdown-formatted plain text files in a vault. Enabling “Options > Files and links > Detect all file extensions” allows viewing non-markdown files in the vault.
Two main registry policies are:
- Extensions are case-sensitive: Upper and lower case distinctions matter.
- Unique extensions: Duplicate extensions are not allowed in the registry.
While Obsidian lacks direct classes, registries, or methods representing extensions, they remain essential components of its ecosystem.
Important
In Obsidian, an "extension" is the last part after the dot in a filename. Only the last item is recognized as the true extension.
With UNITADE and Obsidian’s API, entering the full extension is supported, though the file may not render as intended unless the extension is in the registry.