Skip to content

Commit

Permalink
feat: add template suffix to image renaming
Browse files Browse the repository at this point in the history
  • Loading branch information
BadWinniePooh committed Oct 13, 2023
1 parent c187bf6 commit 4ccf8a0
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ interface PluginSettings {
handleAllAttachments: boolean
excludeExtensionPattern: string
disableRenameNotice: boolean
templateSuffix: string
}

const DEFAULT_SETTINGS: PluginSettings = {
Expand All @@ -56,6 +57,7 @@ const DEFAULT_SETTINGS: PluginSettings = {
handleAllAttachments: false,
excludeExtensionPattern: '',
disableRenameNotice: false,
templateSuffix: '',
}

const PASTED_IMAGE_PREFIX = 'Pasted image '
Expand Down Expand Up @@ -172,7 +174,7 @@ export default class PasteImageRenamePlugin extends Plugin {
// in case fileManager.renameFile may not update the internal link in the active file,
// we manually replace the current line by manipulating the editor

const newLinkText = this.app.fileManager.generateMarkdownLink(file, sourcePath)
var newLinkText = this.app.fileManager.generateMarkdownLink(file, sourcePath)
debugLog('replace text', linkText, newLinkText)

const editor = this.getActiveEditor()
Expand All @@ -181,6 +183,13 @@ export default class PasteImageRenamePlugin extends Plugin {
return
}

// insert a template Suffix into the new link text if settings.templateSuffix is populated
// example output: [[image.png|some suffix]]
if(this.settings.templateSuffix){
var position = newLinkText.length - 2
newLinkText = [newLinkText.slice(0,position), "|", this.settings.templateSuffix, newLinkText.slice(position)].join('')
}

const cursor = editor.getCursor()
const line = editor.getLine(cursor.line)
const replacedLine = line.replace(linkText, newLinkText)
Expand Down Expand Up @@ -675,5 +684,18 @@ class SettingTab extends PluginSettingTab {
await this.plugin.saveSettings();
}
));

new Setting(containerEl)
.setName('Template Suffix')
.setDesc(`This option provides the opportunity to add a suffix to the new link text.
This can be used for defining the width of an image. It provides an output like ![[image.png|300]]`)
.addText(text => text
.setPlaceholder('300')
.setValue(this.plugin.settings.templateSuffix)
.onChange(async (value) => {
this.plugin.settings.templateSuffix = value;
await this.plugin.saveSettings();
}
));
}
}

0 comments on commit 4ccf8a0

Please sign in to comment.