Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add template suffix to image renaming #75

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 31 additions & 2 deletions 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 @@ -147,6 +149,17 @@ export default class PasteImageRenamePlugin extends Plugin {
this.renameFile(file, newName, activeFile.path, true)
}

applySuffix(input: string){
let result = '';

if(this.settings.templateSuffix){
var position = input.length - 2
result = [input.slice(0,position), "|", this.settings.templateSuffix, input.slice(position)].join('')
}

return result;
}

async renameFile(file: TFile, inputNewName: string, sourcePath: string, replaceCurrentLine?: boolean) {
// deduplicate name
const { name:newName } = await this.deduplicateNewName(inputNewName, file)
Expand All @@ -171,8 +184,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)
const newLinkText = this.applySuffix(linkText);
debugLog('replace text', linkText, newLinkText)

const editor = this.getActiveEditor()
Expand All @@ -181,6 +193,10 @@ 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]]


const cursor = editor.getCursor()
const line = editor.getLine(cursor.line)
const replacedLine = line.replace(linkText, newLinkText)
Expand Down Expand Up @@ -675,5 +691,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();
}
));
}
}