Understanding Obsidian’s core architecture and how major modules interact
Obsidian’s architecture is organized into major modules that work together to provide a complete note-taking experience. Understanding these modules and how they interact is essential for effective plugin development.
The Vault interface lets you interact with files and folders in the vault:
// Read a fileconst content = await this.app.vault.read(file);// Create a fileconst newFile = await this.app.vault.create('path/to/file.md', 'content');// Modify a fileawait this.app.vault.modify(file, 'new content');// Delete a fileawait this.app.vault.delete(file);// Get all markdown filesconst markdownFiles = this.app.vault.getMarkdownFiles();// Get file by pathconst file = this.app.vault.getAbstractFileByPath('path/to/file.md');
File Operations
Create, read, update, and delete files in the vault
The Workspace interface manages the UI layout and panes:
// Get the active fileconst activeFile = this.app.workspace.getActiveFile();// Get the active viewconst activeView = this.app.workspace.getActiveViewOfType(MarkdownView);// Open a fileawait this.app.workspace.openLinkText('file.md', '', false);// Get a leaf (pane)const leaf = this.app.workspace.getLeaf(false);// Listen to workspace eventsthis.registerEvent( this.app.workspace.on('active-leaf-change', (leaf) => { console.log('Active leaf changed'); }));
Pane Management
Create, manage, and navigate between workspace panes
async processAllNotes() { // Use Vault to get all files const files = this.app.vault.getMarkdownFiles(); for (const file of files) { // Use MetadataCache to get metadata const cache = this.app.metadataCache.getFileCache(file); if (cache?.frontmatter?.tags?.includes('process')) { // Use Vault to read content const content = await this.app.vault.read(file); // Process content... const newContent = this.processContent(content); // Use Vault to write back await this.app.vault.modify(file, newContent); } }}
Example: Smart File Creation
async createNote(title: string) { // Use FileManager to determine location const folder = this.app.fileManager.getNewFileParent('', title + '.md'); // Use Vault to create the file const file = await this.app.vault.create( folder.path + '/' + title + '.md', '# ' + title ); // Use Workspace to open the file await this.app.workspace.openLinkText(file.path, '', false); return file;}
Example: Updating Links
async renameAndUpdateLinks(file: TFile, newName: string) { // FileManager handles renaming and link updates automatically await this.app.fileManager.renameFile(file, newName); // Workspace will update open views // MetadataCache will update cached metadata // No manual intervention needed!}
// Check if modifier key is pressedconst isCtrlPressed = Keymap.isModifier(evt, 'Mod');// Check what type of pane to openconst paneType = Keymap.isModEvent(evt);