From bed8835c25b7566f1cad43433f94b1cc83a90c3f Mon Sep 17 00:00:00 2001 From: Hongbo Wu Date: Mon, 3 Apr 2023 22:20:11 +0800 Subject: [PATCH] feat: download pdf in attachment folder and expose a variable {{{pdfAttachment}}} in the template --- src/main.ts | 87 +++++++++++++++++++++++++++++++++++++++++++---------- src/util.ts | 1 + 2 files changed, 72 insertions(+), 16 deletions(-) diff --git a/src/main.ts b/src/main.ts index 1600796..009ba4b 100644 --- a/src/main.ts +++ b/src/main.ts @@ -7,6 +7,7 @@ import { Notice, Plugin, PluginSettingTab, + requestUrl, Setting, stringifyYaml, TFile, @@ -50,6 +51,7 @@ interface Settings { dateHighlightedFormat: string; dateSavedFormat: string; filename: string; + attachmentFolder: string; } const DEFAULT_SETTINGS: Settings = { dateHighlightedFormat: "yyyy-MM-dd HH:mm:ss", @@ -59,7 +61,7 @@ const DEFAULT_SETTINGS: Settings = { syncAt: "", customQuery: "", template: `--- -id: {{{id}}} +id: {{id}} title: {{{title}}} {{#author}} author: {{{author}}} @@ -70,7 +72,6 @@ tags: {{/labels}} {{/labels.length}} date_saved: {{{dateSaved}}} -{{#datePublished}} date_published: {{{datePublished}}} {{/datePublished}} --- @@ -85,7 +86,7 @@ date_published: {{{datePublished}}} ## Highlights {{#highlights}} -> {{{text}}} [⤴️]({{{highlightUrl}}}) {{#labels}} #{{name}} {{/labels}} +> {{{text}}} [⤴️]({{{highlightUrl}}}) {{#labels}} #{{{name}}} {{/labels}} {{#note}} {{{note}}} @@ -95,10 +96,11 @@ date_published: {{{datePublished}}} {{/highlights.length}}`, highlightOrder: "LOCATION", syncing: false, - folder: "Omnivore/{{date}}", + folder: "Omnivore/{{{date}}}", folderDateFormat: "yyyy-MM-dd", endpoint: "https://api-prod.omnivore.app/api/graphql", filename: "{{{title}}}", + attachmentFolder: "Omnivore/attachments", }; export default class OmnivorePlugin extends Plugin { @@ -166,6 +168,41 @@ export default class OmnivorePlugin extends Plugin { }); } + getAttachmentFolder(article: Article) { + const { attachmentFolder, folderDateFormat } = this.settings; + const date = formatDate(article.savedAt, folderDateFormat); + return Mustache.render(attachmentFolder, { + ...article, + date, + }); + } + + async downloadPDF(article: Article): Promise { + // download pdf from the URL to the attachment folder + const url = article.url; + const response = await requestUrl({ + url, + contentType: "application/pdf", + }); + const folderName = normalizePath(this.getAttachmentFolder(article)); + const folder = app.vault.getAbstractFileByPath(folderName); + if (!(folder instanceof TFolder)) { + await this.app.vault.createFolder(folderName); + } + const fileName = normalizePath( + `${folderName}/${article.title}-${article.id}.pdf` + ); + const file = app.vault.getAbstractFileByPath(fileName); + if (!(file instanceof TFile)) { + const newFile = await this.app.vault.createBinary( + fileName, + response.arrayBuffer + ); + return newFile.path; + } + return file.path; + } + async fetchOmnivore() { const { syncAt, @@ -219,19 +256,19 @@ export default class OmnivorePlugin extends Plugin { article.savedAt, this.settings.folderDateFormat ); - const folderName = Mustache.render(folder, { - date: folderDate, - }); - const omnivoreFolder = app.vault.getAbstractFileByPath( - normalizePath(folderName) + const folderName = normalizePath( + Mustache.render(folder, { + date: folderDate, + }) ); + const omnivoreFolder = app.vault.getAbstractFileByPath(folderName); if (!(omnivoreFolder instanceof TFolder)) { await this.app.vault.createFolder(folderName); } - + const articleHighlights = article.highlights || []; // sort highlights by location if selected in options highlightOrder === "LOCATION" && - article.highlights?.sort((a, b) => { + articleHighlights.sort((a, b) => { try { if (article.pageType === PageType.File) { // sort by location in file @@ -246,7 +283,7 @@ export default class OmnivorePlugin extends Plugin { return compareHighlightsInFile(a, b); } }); - const highlights = article.highlights?.map((highlight) => { + const highlights = articleHighlights.map((highlight) => { return { text: highlight.quote, highlightUrl: `https://omnivore.app/me/${article.slug}#${highlight.id}`, @@ -286,6 +323,10 @@ export default class OmnivorePlugin extends Plugin { highlights, content: article.content, datePublished, + pdfAttachment: + article.pageType === PageType.File + ? await this.downloadPDF(article) + : undefined, }); const frontmatterRegex = /^(---[\s\S]*?---)/gm; // get the frontmatter from the content @@ -518,7 +559,7 @@ class OmnivoreSettingTab extends PluginSettingTab { href: "https://github.com/janl/mustache.js/#templates", }), fragment.createEl("p", { - text: "Available variables: id, title, omnivoreUrl, siteName, originalUrl, author, content, dateSaved, labels.name, highlights.text, highlights.highlightUrl, highlights.note, highlights.dateHighlighted, highlights.labels.name", + text: "Available variables: id, title, omnivoreUrl, siteName, originalUrl, author, content, dateSaved, pdfAttachment, labels.name, highlights.text, highlights.highlightUrl, highlights.note, highlights.dateHighlighted, highlights.labels.name", }), fragment.createEl("p", { text: "Please note that id in the frontmatter is required for the plugin to work properly.", @@ -545,7 +586,7 @@ class OmnivoreSettingTab extends PluginSettingTab { new Setting(generalSettings) .setName("Folder") .setDesc( - "Enter the folder where the data will be stored. {{date}} could be used in the folder name" + "Enter the folder where the data will be stored. {{{date}}} could be used in the folder name" ) .addSearch((search) => { new FolderSuggest(this.app, search.inputEl); @@ -557,11 +598,25 @@ class OmnivoreSettingTab extends PluginSettingTab { await this.plugin.saveSettings(); }); }); - + new Setting(generalSettings) + .setName("Attachment Folder") + .setDesc( + "Enter the folder where the attachment will be downloaded to. {{{date}}} could be used in the folder name" + ) + .addSearch((search) => { + new FolderSuggest(this.app, search.inputEl); + search + .setPlaceholder("Enter the attachment folder") + .setValue(this.plugin.settings.attachmentFolder) + .onChange(async (value) => { + this.plugin.settings.attachmentFolder = value; + await this.plugin.saveSettings(); + }); + }); new Setting(generalSettings) .setName("Filename") .setDesc( - "Enter the filename where the data will be stored. {{{title}}} and {{date}} could be used in the filename" + "Enter the filename where the data will be stored. {{{title}}} and {{{date}}} could be used in the filename" ) .addText((text) => text diff --git a/src/util.ts b/src/util.ts index 89ae509..49a0334 100644 --- a/src/util.ts +++ b/src/util.ts @@ -67,6 +67,7 @@ export interface Article { pageType: PageType; content?: string; publishedAt: string; + url: string; } export interface Label {