Skip to content

Commit

Permalink
feat: download pdf in attachment folder and expose a variable {{{pdfA…
Browse files Browse the repository at this point in the history
…ttachment}}} in the template
  • Loading branch information
sywhb committed Apr 3, 2023
1 parent d43029a commit bed8835
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 16 deletions.
87 changes: 71 additions & 16 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
Notice,
Plugin,
PluginSettingTab,
requestUrl,
Setting,
stringifyYaml,
TFile,
Expand Down Expand Up @@ -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",
Expand All @@ -59,7 +61,7 @@ const DEFAULT_SETTINGS: Settings = {
syncAt: "",
customQuery: "",
template: `---
id: {{{id}}}
id: {{id}}
title: {{{title}}}
{{#author}}
author: {{{author}}}
Expand All @@ -70,7 +72,6 @@ tags:
{{/labels}}
{{/labels.length}}
date_saved: {{{dateSaved}}}
{{#datePublished}}
date_published: {{{datePublished}}}
{{/datePublished}}
---
Expand All @@ -85,7 +86,7 @@ date_published: {{{datePublished}}}
## Highlights
{{#highlights}}
> {{{text}}} [⤴️]({{{highlightUrl}}}) {{#labels}} #{{name}} {{/labels}}
> {{{text}}} [⤴️]({{{highlightUrl}}}) {{#labels}} #{{{name}}} {{/labels}}
{{#note}}
{{{note}}}
Expand All @@ -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 {
Expand Down Expand Up @@ -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<string> {
// 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,
Expand Down Expand Up @@ -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
Expand All @@ -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}`,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.",
Expand All @@ -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);
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export interface Article {
pageType: PageType;
content?: string;
publishedAt: string;
url: string;
}

export interface Label {
Expand Down

0 comments on commit bed8835

Please sign in to comment.