Fix Obsidian write conflicts

This commit is contained in:
Andras Schmelczer 2025-03-27 21:40:52 +00:00
commit ba060cf770
No known key found for this signature in database
GPG key ID: FC8F2C3D3D1A718C

View file

@ -13,19 +13,26 @@ export class ObsidianFileSystemOperations implements FileSystemOperations {
} }
public async read(path: RelativePath): Promise<Uint8Array> { public async read(path: RelativePath): Promise<Uint8Array> {
path = normalizePath(path);
const view = this.workspace.getActiveViewOfType(MarkdownView); const view = this.workspace.getActiveViewOfType(MarkdownView);
if (view?.file?.path === path) { if (view?.file?.path === path) {
return new TextEncoder().encode(view.editor.getValue()); return new TextEncoder().encode(view.editor.getValue());
} }
return new Uint8Array( return new Uint8Array(await this.vault.adapter.readBinary(path));
await this.vault.adapter.readBinary(normalizePath(path))
);
} }
public async write(path: RelativePath, content: Uint8Array): Promise<void> { public async write(path: RelativePath, content: Uint8Array): Promise<void> {
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( return this.vault.adapter.writeBinary(
normalizePath(path), path,
// eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
content.buffer as ArrayBuffer content.buffer as ArrayBuffer
); );
@ -35,7 +42,16 @@ export class ObsidianFileSystemOperations implements FileSystemOperations {
path: RelativePath, path: RelativePath,
updater: (currentContent: string) => string updater: (currentContent: string) => string
): Promise<string> { ): Promise<string> {
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<number> { public async getFileSize(path: RelativePath): Promise<number> {