Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support new and old frontmatter api #100

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/metaController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,10 +314,11 @@ export default class MetaController {
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) {
//@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---`;
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const updatedMetaData = `---\n${this.updateYamlProperty(property, newValue, file)}\n---`;
const updatedMetaData = `---\n${this.updateYamlProperty(property, newValue, file)}---`;

On both desktop & mobile an extra blank line is being added to the frontmatter because of this \n. I removed it in my local copy but wasn't sure if it should be part of this PR.

//@ts-ignore
const frontmatterPosition = this.app.metadataCache.getFileCache(file).frontmatterPosition;
const fileContents = await this.app.vault.read(file);

const deleteFrom = frontmatterPosition.start.offset;
Expand Down
2 changes: 1 addition & 1 deletion src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Copy link
Author

@xt0rted xt0rted Sep 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't ideal because if anything's null/undefined then destructuring will throw an exception, but that would happen even before this change.

FYI: v1.4.0 of the obsidian types includes both of these properties now obsidianmd/obsidian-api@90517f4

const filecontent = await this.app.vault.cachedRead(file);

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