Skip to content

Commit

Permalink
fix: allow adding aliases to the variables in the front matter
Browse files Browse the repository at this point in the history
  • Loading branch information
sywhb committed May 30, 2023
1 parent dc92a38 commit be1d7e5
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
5 changes: 3 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();
});
Expand Down
15 changes: 9 additions & 6 deletions src/settings/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit be1d7e5

Please sign in to comment.