Skip to content

Commit

Permalink
feat: allow dataPublished and dateSaved to be used as folder name wit…
Browse files Browse the repository at this point in the history
…h folder date format
  • Loading branch information
sywhb committed Jun 19, 2023
1 parent 4f7e370 commit 3f8fb56
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 27 deletions.
18 changes: 6 additions & 12 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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
Expand All @@ -652,7 +646,7 @@ class OmnivoreSettingTab extends PluginSettingTab {
await this.plugin.saveSettings();
})
);

new Setting(containerEl)
.setName("Filename Date Format")
.setDesc(
Expand Down
28 changes: 13 additions & 15 deletions src/settings/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
});
};

Expand Down

0 comments on commit 3f8fb56

Please sign in to comment.