diff --git a/src/main.ts b/src/main.ts index e7f4b2a..d6a1bc3 100644 --- a/src/main.ts +++ b/src/main.ts @@ -25,7 +25,7 @@ interface PluginSettings { } const DEFAULT_SETTINGS: PluginSettings = { - imageNamePattern: '{{imageNameKey}}', + imageNamePattern: '{{fileName}}', dupNumberAtStart: false, dupNumberDelimiter: '-', autoRename: false, @@ -155,7 +155,10 @@ export default class PasteImageRenamePlugin extends Plugin { } } - const stem = renderTemplate(this.settings.imageNamePattern, { imageNameKey }) + const stem = renderTemplate(this.settings.imageNamePattern, { + imageNameKey, + fileName: activeFile.basename, + }) const meaninglessRegex = new RegExp(`[${this.settings.dupNumberDelimiter}\s]`, 'gm') return { @@ -378,12 +381,13 @@ The pattern indicates how the new name should be generated. Available variables: - {{imageNameKey}}: this variable is read from the markdown file's frontmatter, from the same key "imageNameKey". -- {{DATE:$FORMAT}}: use "$FORMAT" to format the current date, "$FORMAT" must be a Moment.js format string, e.g. {{DATE:YYYY-MM-DD}} +- {{fileName}}: name of the active file, without ".md" extension. +- {{DATE:$FORMAT}}: use "$FORMAT" to format the current date, "$FORMAT" must be a Moment.js format string, e.g. {{DATE:YYYY-MM-DD}}. -Examples (imageNameKey = "foo"): -- {{imageNameKey}}-: foo- -- {{imageNameKey}}-{{DATE:YYYYMMDDHHmm}}: foo-202204081652 -- Pasted Image {{DATE:YYYYMMDDHHmm}}: Pasted Image 202204081652 +Here are some examples from pattern to image names (repeat in sequence), variables: imageNameKey = "foo", fileName = "My note": +- {{imageNameKey}}: foo, foo-1, foo-2 +- {{imageNameKey}}-{{DATE:YYYYMMDD}}: foo-20220408, foo-20220408-1, foo-20220408-2 +- {{fileName}}: My note, My note-1, My note-2 ` class SettingTab extends PluginSettingTab { diff --git a/src/template.ts b/src/template.ts index f216949..8e8d73e 100644 --- a/src/template.ts +++ b/src/template.ts @@ -8,6 +8,7 @@ const replaceDateVar = (s: string, date: moment.Moment): string => { interface TemplateData { imageNameKey: string + fileName: string } export const renderTemplate = (tmpl: string, data: TemplateData) => { @@ -18,6 +19,8 @@ export const renderTemplate = (tmpl: string, data: TemplateData) => { text = newtext } - text = text.replace(/{{imageNameKey}}/gm, data.imageNameKey) + text = text + .replace(/{{imageNameKey}}/gm, data.imageNameKey) + .replace(/{{fileName}}/gm, data.fileName) return text }