From 3f8fb565804db5fb81500b3cfffe32bff736e62a Mon Sep 17 00:00:00 2001 From: Hongbo Wu Date: Mon, 19 Jun 2023 19:08:34 +0800 Subject: [PATCH] feat: allow dataPublished and dateSaved to be used as folder name with folder date format --- src/main.ts | 18 ++++++------------ src/settings/template.ts | 28 +++++++++++++--------------- 2 files changed, 19 insertions(+), 27 deletions(-) diff --git a/src/main.ts b/src/main.ts index 2d1480f..7bb593f 100644 --- a/src/main.ts +++ b/src/main.ts @@ -24,14 +24,12 @@ import { FolderSuggest } from "./settings/file-suggest"; import { preParseTemplate, renderArticleContnet, - renderAttachmentFolder, renderFilename, renderFolderName, } from "./settings/template"; import { DATE_FORMAT, findFrontMatterIndex, - formatDate, getQueryFromFilter, parseDateTime, parseFrontMatterFromContent, @@ -139,7 +137,7 @@ export default class OmnivorePlugin extends Plugin { contentType: "application/pdf", }); const folderName = normalizePath( - renderAttachmentFolder( + renderFolderName( article, this.settings.attachmentFolder, this.settings.folderDateFormat @@ -224,12 +222,8 @@ export default class OmnivorePlugin extends Plugin { ); for (const article of articles) { - const folderDate = formatDate( - article.savedAt, - this.settings.folderDateFormat - ); const folderName = normalizePath( - renderFolderName(folder, folderDate) + renderFolderName(article, folder, this.settings.folderDateFormat) ); const omnivoreFolder = this.app.vault.getAbstractFileByPath(folderName); @@ -596,7 +590,7 @@ class OmnivoreSettingTab extends PluginSettingTab { new Setting(containerEl) .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. {{{title}}}, {{{dateSaved}}} and {{{datePublished}}} could be used in the folder name" ) .addSearch((search) => { new FolderSuggest(this.app, search.inputEl); @@ -611,7 +605,7 @@ class OmnivoreSettingTab extends PluginSettingTab { new Setting(containerEl) .setName("Attachment Folder") .setDesc( - "Enter the folder where the attachment will be downloaded to. {{{date}}} could be used in the folder name" + "Enter the folder where the attachment will be downloaded to. {{{title}}}, {{{dateSaved}}} and {{{datePublished}}} could be used in the folder name" ) .addSearch((search) => { new FolderSuggest(this.app, search.inputEl); @@ -641,7 +635,7 @@ class OmnivoreSettingTab extends PluginSettingTab { new Setting(containerEl) .setName("Filename") .setDesc( - "Enter the filename where the data will be stored. {{{title}}}, {{{date}}} and {{{datePublished}}} could be used in the filename" + "Enter the filename where the data will be stored. {{{title}}}, {{{dateSaved}}} and {{{datePublished}}} could be used in the filename" ) .addText((text) => text @@ -652,7 +646,7 @@ class OmnivoreSettingTab extends PluginSettingTab { await this.plugin.saveSettings(); }) ); - + new Setting(containerEl) .setName("Filename Date Format") .setDesc( diff --git a/src/settings/template.ts b/src/settings/template.ts index 6b17f04..9804be9 100644 --- a/src/settings/template.ts +++ b/src/settings/template.ts @@ -143,18 +143,6 @@ export const renderFilename = ( }); }; -export const renderAttachmentFolder = ( - article: Article, - attachmentFolder: string, - folderDateFormat: string -) => { - const date = formatDate(article.savedAt, folderDateFormat); - return Mustache.render(attachmentFolder, { - ...article, - date, - }); -}; - export const renderLabels = (labels?: LabelView[]) => { return labels?.map((l) => ({ // replace spaces with underscores because Obsidian doesn't allow spaces in tags @@ -326,9 +314,19 @@ export const renderArticleContnet = async ( return `${frontMatterStr}\n\n${contentWithoutFrontMatter}`; }; -export const renderFolderName = (folder: string, folderDate: string) => { - return Mustache.render(folder, { - date: folderDate, +export const renderFolderName = ( + article: Article, + template: string, + dateFormat: string +) => { + const date = formatDate(article.savedAt, dateFormat); + const datePublished = article.publishedAt + ? formatDate(article.publishedAt, dateFormat).trim() + : undefined; + return Mustache.render(template, { + date, + dateSaved: date, + datePublished, }); };