Skip to content

Commit

Permalink
add {{fileName}} variable for imageNamePattern
Browse files Browse the repository at this point in the history
  • Loading branch information
reorx committed Apr 10, 2022
1 parent a3130f2 commit 795dff6
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
18 changes: 11 additions & 7 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ interface PluginSettings {
}

const DEFAULT_SETTINGS: PluginSettings = {
imageNamePattern: '{{imageNameKey}}',
imageNamePattern: '{{fileName}}',
dupNumberAtStart: false,
dupNumberDelimiter: '-',
autoRename: false,
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
5 changes: 4 additions & 1 deletion src/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -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
}

0 comments on commit 795dff6

Please sign in to comment.