diff --git a/src/main.ts b/src/main.ts index 1060bef..9260a2f 100644 --- a/src/main.ts +++ b/src/main.ts @@ -486,7 +486,15 @@ class OmnivoreSettingTab extends PluginSettingTab { new Setting(containerEl) .setName("Front Matter Variables") .setDesc( - "Enter the front matter variables to be used in the template separated by commas. Available variables are title, author, tags, date_saved, date_published. You can also use custom aliases in the format of variable::alias, e.g. date_saved::date" + createFragment((fragment) => { + fragment.append( + "Enter the front matter variables separated by commas. You can also use custom aliases in the format of variable::alias, e.g. date_saved::date. ", + fragment.createEl("a", { + text: "Reference", + href: "https://docs.omnivore.app/integrations/obsidian.html#controlling-the-layout-of-the-data-imported-to-obsidian", + }) + ); + }) ) .addTextArea((text) => { text diff --git a/src/settings/index.ts b/src/settings/index.ts index d15eb40..64bd9bf 100644 --- a/src/settings/index.ts +++ b/src/settings/index.ts @@ -6,6 +6,17 @@ export const FRONT_MATTER_VARIABLES = [ "tags", "date_saved", "date_published", + "omnivore_url", + "site_name", + "original_url", + "description", + "note", + "type", + "date_read", + "words_count", + "read_length", + "state", + "date_archived", ]; export const DEFAULT_SETTINGS: OmnivoreSettings = { @@ -27,7 +38,7 @@ export const DEFAULT_SETTINGS: OmnivoreSettings = { isSingleFile: false, frequency: 0, intervalId: 0, - frontMatterVariables: FRONT_MATTER_VARIABLES, + frontMatterVariables: [], }; export enum Filter { diff --git a/src/settings/template.ts b/src/settings/template.ts index 4998084..8534c64 100644 --- a/src/settings/template.ts +++ b/src/settings/template.ts @@ -9,6 +9,7 @@ import { getHighlightLocation, removeFrontMatterFromContent, siteNameFromUrl, + snakeToCamelCase, } from "../util"; type FunctionMap = { @@ -246,32 +247,28 @@ export const renderArticleContnet = async ( }; for (const item of frontMatterVariables) { + // split the item into variable and alias const aliasedVariables = item.split("::"); const variable = aliasedVariables[0]; + // we use snake case for variables in the front matter + const articleVariable = snakeToCamelCase(variable); + // use alias if available, otherwise use variable const key = aliasedVariables[1] || variable; - switch (variable) { - case "title": - frontMatter[key] = articleView.title; - break; - case "author": - if (articleView.author) { - frontMatter[key] = articleView.author; - } - break; - case "tags": - if (articleView.labels && articleView.labels.length > 0) { - // use label names as tags - frontMatter[key] = articleView.labels.map((l) => l.name); - } - break; - case "date_saved": - frontMatter[key] = dateSaved; - break; - case "date_published": - if (datePublished) { - frontMatter[key] = datePublished; - } - break; + if ( + variable === "tags" && + articleView.labels && + articleView.labels.length > 0 + ) { + // tags are handled separately + // use label names as tags + frontMatter[key] = articleView.labels.map((l) => l.name); + continue; + } + + const value = (articleView as any)[articleVariable]; + if (value) { + // if variable is in article, use it + frontMatter[key] = value; } } diff --git a/src/util.ts b/src/util.ts index ba5a81a..2bd9cc5 100644 --- a/src/util.ts +++ b/src/util.ts @@ -161,3 +161,6 @@ export const removeFrontMatterFromContent = (content: string): string => { return content.replace(frontMatterRegex, ""); }; + +export const snakeToCamelCase = (str: string) => + str.replace(/(_[a-z])/g, (group) => group.toUpperCase().replace("_", ""));