Skip to content

Commit

Permalink
replace title or date in the filename
Browse files Browse the repository at this point in the history
  • Loading branch information
sywhb committed Mar 7, 2023
1 parent 650a9ad commit e18df0b
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,15 @@ export default class OmnivorePlugin extends Plugin {
await this.saveData(this.settings);
}

getFilename(article: Article) {
const { filename, folderDateFormat } = this.settings;
const date = formatDate(article.savedAt, folderDateFormat);
return Mustache.render(filename, {
...article,
date,
});
}

async fetchOmnivore() {
const {
syncAt,
Expand Down Expand Up @@ -280,10 +289,9 @@ export default class OmnivorePlugin extends Plugin {
if (!content.match(/^(---[\s\S]*?---)/gm)) {
updatedContent = `${frontmatterString}\n\n${content}`;
}
// use the title as the filename
const pageName = `${folderName}/${replaceIllegalChars(
article.title
)}.md`;
// use the custom filename
const filename = replaceIllegalChars(this.getFilename(article));
const pageName = `${folderName}/${filename}.md`;
const normalizedPath = normalizePath(pageName);
const omnivoreFile = app.vault.getAbstractFileByPath(normalizedPath);
try {
Expand All @@ -294,9 +302,7 @@ export default class OmnivorePlugin extends Plugin {
const id = frontMatter.id;
if (id && id !== article.id) {
// this article has the same name but different id
const newPageName = `${folderName}/${replaceIllegalChars(
article.title
)}-${article.id}.md`;
const newPageName = `${folderName}/${filename}-${article.id}.md`;
const newNormalizedPath = normalizePath(newPageName);
const newOmnivoreFile =
app.vault.getAbstractFileByPath(newNormalizedPath);
Expand Down

0 comments on commit e18df0b

Please sign in to comment.