Skip to content

Commit

Permalink
feat: add more variables in the front matter setting
Browse files Browse the repository at this point in the history
  • Loading branch information
sywhb committed May 30, 2023
1 parent be1d7e5 commit 942b93e
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 25 deletions.
10 changes: 9 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 12 additions & 1 deletion src/settings/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -27,7 +38,7 @@ export const DEFAULT_SETTINGS: OmnivoreSettings = {
isSingleFile: false,
frequency: 0,
intervalId: 0,
frontMatterVariables: FRONT_MATTER_VARIABLES,
frontMatterVariables: [],
};

export enum Filter {
Expand Down
43 changes: 20 additions & 23 deletions src/settings/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
getHighlightLocation,
removeFrontMatterFromContent,
siteNameFromUrl,
snakeToCamelCase,
} from "../util";

type FunctionMap = {
Expand Down Expand Up @@ -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;
}
}

Expand Down
3 changes: 3 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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("_", ""));

0 comments on commit 942b93e

Please sign in to comment.