From 2b3b0f34d29ce84e3e35e7ace2a8871314d7a13d Mon Sep 17 00:00:00 2001 From: Hongbo Wu Date: Tue, 16 May 2023 22:31:33 +0800 Subject: [PATCH] fix: allow frontmatter to be an array --- package-lock.json | 12 ++++++------ package.json | 2 +- src/main.ts | 36 +++++++++++++++++++++++++++++++----- src/settings/template.ts | 27 ++++++++++++++++++++------- 4 files changed, 58 insertions(+), 19 deletions(-) diff --git a/package-lock.json b/package-lock.json index c9f48bf..773f9f4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "obsidian-omnivore", - "version": "1.0.6", + "version": "1.2.3", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "obsidian-omnivore", - "version": "1.0.6", + "version": "1.2.3", "license": "MIT", "dependencies": { "diff-match-patch": "^1.0.5", @@ -33,7 +33,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", @@ -8382,9 +8382,9 @@ "license": "ISC" }, "node_modules/obsidian": { - "version": "1.1.1", - "resolved": "https://registry.npmjs.org/obsidian/-/obsidian-1.1.1.tgz", - "integrity": "sha512-GcxhsHNkPEkwHEjeyitfYNBcQuYGeAHFs1pEpZIv0CnzSfui8p8bPLm2YKLgcg20B764770B1sYGtxCvk9ptxg==", + "version": "1.2.8", + "resolved": "https://registry.npmjs.org/obsidian/-/obsidian-1.2.8.tgz", + "integrity": "sha512-HrC+feA8o0tXspj4lEAqxb1btwLwHD2oHXSwbbN+CdRHURqbCkuIDLld+nkuyJ1w1c9uvVDRVk8BoeOnWheOrQ==", "dev": true, "dependencies": { "@types/codemirror": "0.0.108", diff --git a/package.json b/package.json index ed8bc8c..b88e49c 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/main.ts b/src/main.ts index 9a5eed5..45fb7aa 100644 --- a/src/main.ts +++ b/src/main.ts @@ -4,6 +4,7 @@ import { App, normalizePath, Notice, + parseYaml, Plugin, PluginSettingTab, requestUrl, @@ -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, @@ -212,6 +232,7 @@ export default class OmnivorePlugin extends Plugin { highlightOrder, this.settings.dateHighlightedFormat, this.settings.dateSavedFormat, + isSingleFile, fileAttachment ); // use the custom filename @@ -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( @@ -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 { @@ -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(); }) diff --git a/src/settings/template.ts b/src/settings/template.ts index 1d5fc4a..889af23 100644 --- a/src/settings/template.ts +++ b/src/settings/template.ts @@ -172,6 +172,7 @@ export const renderArticleContnet = async ( highlightOrder: string, dateHighlightedFormat: string, dateSavedFormat: string, + isSingleFile: boolean, fileAttachment?: string ) => { // filter out notes and redactions @@ -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}`; @@ -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; +};