Skip to content

Commit

Permalink
fix: invalid yaml error when generating the front matter
Browse files Browse the repository at this point in the history
  • Loading branch information
sywhb committed May 22, 2023
1 parent d9bd3a9 commit 10a9b96
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 32 deletions.
19 changes: 19 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ export default class OmnivorePlugin extends Plugin {
filename,
folderDateFormat,
isSingleFile,
frontMatterVariables,
} = this.settings;

if (syncing) {
Expand Down Expand Up @@ -237,6 +238,7 @@ export default class OmnivorePlugin extends Plugin {
this.settings.dateHighlightedFormat,
this.settings.dateSavedFormat,
isSingleFile,
frontMatterVariables,
fileAttachment
);
// use the custom filename
Expand Down Expand Up @@ -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(
Expand Down
10 changes: 10 additions & 0 deletions src/settings/index.ts
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -19,6 +27,7 @@ export const DEFAULT_SETTINGS: OmnivoreSettings = {
isSingleFile: false,
frequency: 0,
intervalId: 0,
frontMatterVariables: FRONT_MATTER_VARIABLES,
};

export enum Filter {
Expand Down Expand Up @@ -51,4 +60,5 @@ export interface OmnivoreSettings {
isSingleFile: boolean;
frequency: number;
intervalId: number;
frontMatterVariables: string[];
}
60 changes: 28 additions & 32 deletions src/settings/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
formatDate,
formatHighlightQuote,
getHighlightLocation,
parseFrontMatterFromContent,
removeFrontMatterFromContent,
siteNameFromUrl,
} from "../util";
Expand All @@ -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}}})
Expand Down Expand Up @@ -176,6 +156,7 @@ export const renderArticleContnet = async (
dateHighlightedFormat: string,
dateSavedFormat: string,
isSingleFile: boolean,
frontMatterVariables: string[],
fileAttachment?: string
) => {
// filter out notes and redactions
Expand Down Expand Up @@ -257,30 +238,45 @@ 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%%`;
const sectionEnd = `%%${article.id}_end%%`;
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}`;
Expand Down

0 comments on commit 10a9b96

Please sign in to comment.