From be1d7e5316871e8c740afc8e2c19dbff4f1eeef8 Mon Sep 17 00:00:00 2001 From: Hongbo Wu Date: Tue, 30 May 2023 15:54:54 +0800 Subject: [PATCH] fix: allow adding aliases to the variables in the front matter --- src/main.ts | 5 +++-- src/settings/template.ts | 15 +++++++++------ 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/src/main.ts b/src/main.ts index 1296be4..1060bef 100644 --- a/src/main.ts +++ b/src/main.ts @@ -486,7 +486,7 @@ 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" + "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" ) .addTextArea((text) => { text @@ -499,7 +499,8 @@ class OmnivoreSettingTab extends PluginSettingTab { .map((v) => v.trim()) .filter( (v, i, a) => - FRONT_MATTER_VARIABLES.includes(v) && a.indexOf(v) === i + FRONT_MATTER_VARIABLES.includes(v.split("::")[0]) && + a.indexOf(v) === i ); await this.plugin.saveSettings(); }); diff --git a/src/settings/template.ts b/src/settings/template.ts index 0610cf5..4998084 100644 --- a/src/settings/template.ts +++ b/src/settings/template.ts @@ -246,27 +246,30 @@ export const renderArticleContnet = async ( }; for (const item of frontMatterVariables) { - switch (item) { + const aliasedVariables = item.split("::"); + const variable = aliasedVariables[0]; + const key = aliasedVariables[1] || variable; + switch (variable) { case "title": - frontMatter[item] = articleView.title; + frontMatter[key] = articleView.title; break; case "author": if (articleView.author) { - frontMatter[item] = articleView.author; + frontMatter[key] = articleView.author; } break; case "tags": if (articleView.labels && articleView.labels.length > 0) { // use label names as tags - frontMatter[item] = articleView.labels.map((l) => l.name); + frontMatter[key] = articleView.labels.map((l) => l.name); } break; case "date_saved": - frontMatter[item] = dateSaved; + frontMatter[key] = dateSaved; break; case "date_published": if (datePublished) { - frontMatter[item] = datePublished; + frontMatter[key] = datePublished; } break; }