diff --git a/src/main.ts b/src/main.ts index de2d3be..16801a8 100644 --- a/src/main.ts +++ b/src/main.ts @@ -173,6 +173,7 @@ export default class OmnivorePlugin extends Plugin { filename, folderDateFormat, isSingleFile, + frontMatterVariables, } = this.settings; if (syncing) { @@ -237,6 +238,7 @@ export default class OmnivorePlugin extends Plugin { this.settings.dateHighlightedFormat, this.settings.dateSavedFormat, isSingleFile, + frontMatterVariables, fileAttachment ); // use the custom filename @@ -484,6 +486,23 @@ class OmnivoreSettingTab extends PluginSettingTab { }); }); + new Setting(generalSettings) + .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" + ) + .addTextArea((text) => { + text + .setPlaceholder("Enter the front matter variables") + .setValue(this.plugin.settings.frontMatterVariables.join(",")) + .onChange(async (value) => { + this.plugin.settings.frontMatterVariables = JSON.parse(value); + await this.plugin.saveSettings(); + }); + text.inputEl.setAttr("rows", 5); + text.inputEl.setAttr("cols", 60); + }); + new Setting(generalSettings) .setName("Template") .setDesc( diff --git a/src/settings/index.ts b/src/settings/index.ts index 0839fcf..d15eb40 100644 --- a/src/settings/index.ts +++ b/src/settings/index.ts @@ -1,5 +1,13 @@ import { DEFAULT_TEMPLATE } from "./template"; +export const FRONT_MATTER_VARIABLES = [ + "title", + "author", + "tags", + "date_saved", + "date_published", +]; + export const DEFAULT_SETTINGS: OmnivoreSettings = { dateHighlightedFormat: "yyyy-MM-dd HH:mm:ss", dateSavedFormat: "yyyy-MM-dd HH:mm:ss", @@ -19,6 +27,7 @@ export const DEFAULT_SETTINGS: OmnivoreSettings = { isSingleFile: false, frequency: 0, intervalId: 0, + frontMatterVariables: FRONT_MATTER_VARIABLES, }; export enum Filter { @@ -51,4 +60,5 @@ export interface OmnivoreSettings { isSingleFile: boolean; frequency: number; intervalId: number; + frontMatterVariables: string[]; } diff --git a/src/settings/template.ts b/src/settings/template.ts index a82bc73..a8a2a64 100644 --- a/src/settings/template.ts +++ b/src/settings/template.ts @@ -6,7 +6,6 @@ import { formatDate, formatHighlightQuote, getHighlightLocation, - parseFrontMatterFromContent, removeFrontMatterFromContent, siteNameFromUrl, } from "../util"; @@ -18,26 +17,7 @@ type FunctionMap = { ) => string; }; -export const DEFAULT_TEMPLATE = `--- -id: {{{id}}} -title: > - {{{title}}} -{{#author}} -author: > - {{{author}}} -{{/author}} -{{#labels.length}} -tags: -{{#labels}} - {{{name}}} -{{/labels}} -{{/labels.length}} -date_saved: {{{dateSaved}}} -{{#datePublished}} -date_published: {{{datePublished}}} -{{/datePublished}} ---- - -# {{{title}}} +export const DEFAULT_TEMPLATE = `# {{{title}}} #Omnivore [Read on Omnivore]({{{omnivoreUrl}}}) @@ -176,6 +156,7 @@ export const renderArticleContnet = async ( dateHighlightedFormat: string, dateSavedFormat: string, isSingleFile: boolean, + frontMatterVariables: string[], fileAttachment?: string ) => { // filter out notes and redactions @@ -257,19 +238,35 @@ export const renderArticleContnet = async ( dateArchived: article.archivedAt, ...functionMap, }; - // Build content string based on template - const content = Mustache.render(template, articleView); - // get the front matter from the content - let frontMatter = parseFrontMatterFromContent(content); - if (!frontMatter) { - // if no front matter, add the id - frontMatter = { - id: article.id, - }; + const frontMatter: { [id: string]: unknown } = { + id: article.id, // id is required for deduplication + }; + + for (const item of frontMatterVariables) { + switch (item) { + case "title": + frontMatter[item] = articleView.title; + break; + case "author": + frontMatter[item] = articleView.author; + break; + case "tags": + frontMatter[item] = articleView.labels; + break; + case "date_saved": + frontMatter[item] = dateSaved; + break; + case "date_published": + frontMatter[item] = datePublished; + break; + } } + // Build content string based on template + const content = Mustache.render(template, articleView); let contentWithoutFrontMatter = removeFrontMatterFromContent(content); + let frontMatterYaml = stringifyYaml(frontMatter); if (isSingleFile) { // wrap the content without front matter in comments const sectionStart = `%%${article.id}_start%%`; @@ -277,10 +274,9 @@ export const renderArticleContnet = async ( contentWithoutFrontMatter = `${sectionStart}\n${contentWithoutFrontMatter}\n${sectionEnd}`; // if single file, wrap the front matter in an array - frontMatter = [frontMatter]; + frontMatterYaml = stringifyYaml([frontMatter]); } - const frontMatterYaml = stringifyYaml(frontMatter); const frontMatterStr = `---\n${frontMatterYaml}---`; return `${frontMatterStr}\n\n${contentWithoutFrontMatter}`;