Skip to content

Commit

Permalink
fix: allow frontmatter to be an array
Browse files Browse the repository at this point in the history
  • Loading branch information
sywhb committed May 16, 2023
1 parent 4c04f8e commit 2b3b0f3
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 19 deletions.
12 changes: 6 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"jest": "^29.4.3",
"jest-cli": "^29.4.3",
"jest-junit-reporter": "^1.1.0",
"obsidian": "^1.1.1",
"obsidian": "^1.2.8",
"prettier": "^2.8.1",
"semantic-release": "^19.0.5",
"ts-jest": "^29.0.5",
Expand Down
36 changes: 31 additions & 5 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
App,
normalizePath,
Notice,
parseYaml,
Plugin,
PluginSettingTab,
requestUrl,
Expand Down Expand Up @@ -135,6 +136,25 @@ export default class OmnivorePlugin extends Plugin {
return file.path;
}

isArticleInFile(frontMatter: unknown, id: string): boolean {
// check if frontMatter is an array
if (!Array.isArray(frontMatter)) {
return false;
}
// check if id is in frontMatter
return frontMatter.some((f: { id: string }) => f.id === id);
}

getFrontMatterFromContent(content: string): unknown | undefined {
// get front matter yaml from content
const frontMatter = content.match(/^---\n(.*)\n---\n/);
if (!frontMatter) {
return undefined;
}
// parse yaml
return parseYaml(frontMatter[1]);
}

async fetchOmnivore() {
const {
syncAt,
Expand Down Expand Up @@ -212,6 +232,7 @@ export default class OmnivorePlugin extends Plugin {
highlightOrder,
this.settings.dateHighlightedFormat,
this.settings.dateSavedFormat,
isSingleFile,
fileAttachment
);
// use the custom filename
Expand All @@ -231,13 +252,12 @@ export default class OmnivorePlugin extends Plugin {
await this.app.fileManager.processFrontMatter(
omnivoreFile,
async (frontMatter) => {
const id = frontMatter.id as string[];
// we need to remove the front matter
const contentWithoutFrontmatter = content.replace(
/^---\n.*\n---\n/,
""
);
if (id && id.includes(article.id)) {
if (this.isArticleInFile(frontMatter, article.id)) {
// this article already exists in the file
// we need to locate the article and update it
const existingContent = await this.app.vault.read(
Expand All @@ -259,8 +279,15 @@ export default class OmnivorePlugin extends Plugin {
omnivoreFile,
contentWithoutFrontmatter
);
// append id to front matter
frontMatter.id = id ? [...id, article.id] : [article.id];
// append generated front matter
const newFrontMatter =
this.getFrontMatterFromContent(content);
frontMatter.push(
Array.isArray(newFrontMatter)
? newFrontMatter[0]
: newFrontMatter
);
console.log(frontMatter);
}
);
} else {
Expand Down Expand Up @@ -587,7 +614,6 @@ class OmnivoreSettingTab extends PluginSettingTab {
.setPlaceholder("API endpoint")
.setValue(this.plugin.settings.endpoint)
.onChange(async (value) => {
console.log("endpoint: " + value);
this.plugin.settings.endpoint = value;
await this.plugin.saveSettings();
})
Expand Down
27 changes: 20 additions & 7 deletions src/settings/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ export const renderArticleContnet = async (
highlightOrder: string,
dateHighlightedFormat: string,
dateSavedFormat: string,
isSingleFile: boolean,
fileAttachment?: string
) => {
// filter out notes and redactions
Expand Down Expand Up @@ -255,20 +256,23 @@ export const renderArticleContnet = async (
// Build content string based on template
let content = Mustache.render(template, articleView);

const frontmatterRegex = /^(---[\s\S]*?---)/gm;
// get the frontmatter from the content
const frontmatter = content.match(frontmatterRegex);
const frontmatter = getFrontMatterStr(content);
if (frontmatter) {
// frontmatter is an array for single file notes
const replacement = `${isSingleFile ? "- " : ""}id: ${article.id}`;
// replace the id in the frontmatter
content = content.replace(
frontmatter[0],
frontmatter[0].replace('id: ""', `id: ${article.id}`)
frontmatter,
frontmatter.replace(/id:\s*\S+/, replacement)
);
} else {
// if the content doesn't have frontmatter, add it
const frontmatter = {
id: article.id,
};
const frontmatter = isSingleFile
? [{ id: article.id }]
: {
id: article.id,
};
const frontmatterYaml = stringifyYaml(frontmatter);
const frontmatterString = `---\n${frontmatterYaml}---`;
content = `${frontmatterString}\n\n${content}`;
Expand All @@ -285,3 +289,12 @@ export const renderFolderName = (folder: string, folderDate: string) => {
export const preParseTemplate = (template: string) => {
Mustache.parse(template);
};

export const getFrontMatterStr = (content: string) => {
const frontmatterRegex = /^(---[\s\S]*?---)/gm;
const frontmatter = content.match(frontmatterRegex);
if (frontmatter) {
return frontmatter[0];
}
return undefined;
};

0 comments on commit 2b3b0f3

Please sign in to comment.