Skip to content

Commit

Permalink
fix: fix frontmatter update breaking in Obsidian 1.4.1
Browse files Browse the repository at this point in the history
  • Loading branch information
chhoumann committed Jul 30, 2023
1 parent 57883f6 commit 4bf3ceb
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"id": "metaedit",
"name": "MetaEdit",
"version": "1.8.0",
"minAppVersion": "0.12.0",
"minAppVersion": "1.4.1",
"description": "MetaEdit helps you manage your metadata.",
"author": "Christian B. B. Houmann",
"authorUrl": "https://bagerbach.com",
Expand Down
26 changes: 25 additions & 1 deletion src/metaController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {ADD_FIRST_ELEMENT, ADD_TO_BEGINNING, ADD_TO_END} from "./constants";
import type {ProgressProperty} from "./Types/progressProperty";
import {ProgressPropertyOptions} from "./Types/progressPropertyOptions";
import {MetaType} from "./Types/metaType";
import {Notice} from "obsidian";
import {Notice, stringifyYaml} from "obsidian";
import {log} from "./logger/logManager";

export default class MetaController {
Expand Down Expand Up @@ -304,7 +304,31 @@ export default class MetaController {
return null;
}

private updateYamlProperty(property: Partial<Property>, newValue: string, file: TFile): string {
const fileCache = this.app.metadataCache.getFileCache(file);
const frontMatter = fileCache.frontmatter;
frontMatter[property.key] = newValue;
return stringifyYaml(frontMatter);
}

public async updatePropertyInFile(property: Partial<Property>, newValue: string, file: TFile): Promise<void> {
// 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) {
const updatedMetaData = `---\n${this.updateYamlProperty(property, newValue, file)}\n---`;
//@ts-ignore
const frontmatterPosition = this.app.metadataCache.getFileCache(file).frontmatterPosition;
const fileContents = await this.app.vault.read(file);

const deleteFrom = frontmatterPosition.start.offset;
const deleteTo = frontmatterPosition.end.offset;

const newFileContents = fileContents.substring(0, deleteFrom) + updatedMetaData + fileContents.substring(deleteTo);

await this.app.vault.modify(file, newFileContents);
return;
}

const fileContent = await this.app.vault.read(file);

const newFileContent = fileContent.split("\n").map(line => {
Expand Down
7 changes: 5 additions & 2 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,12 @@ export default class MetaEditParser {
}

public async parseFrontmatter(file: TFile): Promise<Property[]> {
const frontmatter = this.app.metadataCache.getFileCache(file)?.frontmatter;
const fileCache = this.app.metadataCache.getFileCache(file);
const frontmatter = fileCache?.frontmatter;
if (!frontmatter) return [];
const {position: {start, end}} = frontmatter;

//@ts-ignore - this is part of the new Obsidian API as of v1.4.1
const {start, end} = fileCache?.frontmatterPosition;
const filecontent = await this.app.vault.cachedRead(file);

const yamlContent: string = filecontent.split("\n").slice(start.line, end.line).join("\n");
Expand Down

0 comments on commit 4bf3ceb

Please sign in to comment.