From 30bc4b18995e3ff0a706b21aef2a055081f8d747 Mon Sep 17 00:00:00 2001 From: Brian Surowiec Date: Sun, 3 Sep 2023 16:45:40 -0400 Subject: [PATCH 1/2] Support new and old frontmatter api --- src/metaController.ts | 3 ++- src/parser.ts | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/metaController.ts b/src/metaController.ts index 7d87a80..e2659fe 100644 --- a/src/metaController.ts +++ b/src/metaController.ts @@ -316,8 +316,9 @@ export default class MetaController { // This uses the new frontmatter API to update the frontmatter. Later TODO: rewrite old logic to just do this & clean. if (property.type === MetaType.YAML) { const updatedMetaData = `---\n${this.updateYamlProperty(property, newValue, file)}\n---`; + const fileCache = this.app.metadataCache.getFileCache(file); //@ts-ignore - const frontmatterPosition = this.app.metadataCache.getFileCache(file).frontmatterPosition; + const frontmatterPosition = fileCache.frontmatterPosition ?? fileCache.frontmatter.position; const fileContents = await this.app.vault.read(file); const deleteFrom = frontmatterPosition.start.offset; diff --git a/src/parser.ts b/src/parser.ts index 2403f14..a165b14 100644 --- a/src/parser.ts +++ b/src/parser.ts @@ -28,7 +28,7 @@ export default class MetaEditParser { if (!frontmatter) return []; //@ts-ignore - this is part of the new Obsidian API as of v1.4.1 - const {start, end} = fileCache?.frontmatterPosition; + const {start, end} = fileCache?.frontmatterPosition ?? fileCache?.frontmatter?.position; const filecontent = await this.app.vault.cachedRead(file); const yamlContent: string = filecontent.split("\n").slice(start.line, end.line).join("\n"); From d501b521e03cd9b981a5769c13fc977c239d02e6 Mon Sep 17 00:00:00 2001 From: Brian Surowiec Date: Sun, 3 Sep 2023 18:37:52 -0400 Subject: [PATCH 2/2] Use old code path for mobile --- src/metaController.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/metaController.ts b/src/metaController.ts index e2659fe..40d9776 100644 --- a/src/metaController.ts +++ b/src/metaController.ts @@ -314,11 +314,11 @@ export default class MetaController { public async updatePropertyInFile(property: Partial, newValue: string, file: TFile): Promise { // I'm aware this is hacky. Didn't want to spend a bunch of time rewriting old logic. // This uses the new frontmatter API to update the frontmatter. Later TODO: rewrite old logic to just do this & clean. - if (property.type === MetaType.YAML) { + //@ts-ignore + const frontmatterPosition = this.app.metadataCache.getFileCache(file).frontmatterPosition; + + if (property.type === MetaType.YAML && frontmatterPosition) { const updatedMetaData = `---\n${this.updateYamlProperty(property, newValue, file)}\n---`; - const fileCache = this.app.metadataCache.getFileCache(file); - //@ts-ignore - const frontmatterPosition = fileCache.frontmatterPosition ?? fileCache.frontmatter.position; const fileContents = await this.app.vault.read(file); const deleteFrom = frontmatterPosition.start.offset;