Skip to content

Commit

Permalink
fix: use existing empty frontmatter key for uid
Browse files Browse the repository at this point in the history
close #144
  • Loading branch information
Vinzent03 committed Aug 30, 2023
1 parent 978acbb commit f07c035
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions src/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,20 @@ export default class Tools {
(!frontmatter || frontmatter.length === 0) &&
!fileContent.match(/^-{3}\s*\n*\r*-{3}/);
let splitContent = fileContent.split("\n");
const key = `${this.plugin.settings.idField}:`;
if (isYamlEmpty) {
splitContent.unshift("---");
splitContent.unshift(`${this.plugin.settings.idField}: ${uid}`);
splitContent.unshift(`${key} ${uid}`);
splitContent.unshift("---");
} else {
splitContent.splice(
1,
0,
`${this.plugin.settings.idField}: ${uid}`
const lineIndexOfKey = splitContent.findIndex((line) =>
line.startsWith(key)
);
if (lineIndexOfKey != -1) {
splitContent[lineIndexOfKey] = `${key} ${uid}`;
} else {
splitContent.splice(1, 0, `${key} ${uid}`);
}
}

const newFileContent = splitContent.join("\n");
Expand Down

0 comments on commit f07c035

Please sign in to comment.