diff --git a/frontend/obsidian-plugin/src/obsidian-file-system.ts b/frontend/obsidian-plugin/src/obsidian-file-system.ts index a487d91e..55388e05 100644 --- a/frontend/obsidian-plugin/src/obsidian-file-system.ts +++ b/frontend/obsidian-plugin/src/obsidian-file-system.ts @@ -13,19 +13,26 @@ export class ObsidianFileSystemOperations implements FileSystemOperations { } public async read(path: RelativePath): Promise { + path = normalizePath(path); const view = this.workspace.getActiveViewOfType(MarkdownView); if (view?.file?.path === path) { return new TextEncoder().encode(view.editor.getValue()); } - return new Uint8Array( - await this.vault.adapter.readBinary(normalizePath(path)) - ); + return new Uint8Array(await this.vault.adapter.readBinary(path)); } public async write(path: RelativePath, content: Uint8Array): Promise { + path = normalizePath(path); + + const view = this.workspace.getActiveViewOfType(MarkdownView); + if (view?.file?.path === path) { + view.editor.setValue(new TextDecoder().decode(content)); + return; + } + return this.vault.adapter.writeBinary( - normalizePath(path), + path, // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion content.buffer as ArrayBuffer ); @@ -35,7 +42,16 @@ export class ObsidianFileSystemOperations implements FileSystemOperations { path: RelativePath, updater: (currentContent: string) => string ): Promise { - return this.vault.adapter.process(normalizePath(path), updater); + path = normalizePath(path); + + const view = this.workspace.getActiveViewOfType(MarkdownView); + if (view?.file?.path === path) { + const result = updater(view.editor.getValue()); + view.editor.setValue(result); + return result; + } + + return this.vault.adapter.process(path, updater); } public async getFileSize(path: RelativePath): Promise {