Skip to content

Commit

Permalink
add batch rename all images command
Browse files Browse the repository at this point in the history
  • Loading branch information
reorx committed May 12, 2022
1 parent bc3eca9 commit 38b2484
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,18 @@ export default class PasteImageRenamePlugin extends Plugin {
this.addRibbonIcon('wand-glyph', 'Batch rename embeded files', startBatchRenameProcess)
}

const batchRenameAllImages = () => {
this.batchRenameAllImages()
}
this.addCommand({
id: 'batch-rename-all-images',
name: 'Batch rename all images instantly (in the current file)',
callback: batchRenameAllImages,
})
if (DEBUG) {
this.addRibbonIcon('wand-glyph', 'Batch rename all images instantly (in the current file)', batchRenameAllImages)
}

// add settings tab
this.addSettingTab(new SettingTab(this.app, this));

Expand Down Expand Up @@ -216,6 +228,34 @@ export default class PasteImageRenamePlugin extends Plugin {
modal.open()
}

async batchRenameAllImages() {
const activeFile = this.getActiveFile()
const fileCache = this.app.metadataCache.getFileCache(activeFile)
if (!fileCache || !fileCache.embeds) return
const extPatternRegex = /jpe?g|png|gif|tiff|webp/i

for (const embed of fileCache.embeds) {
const file = this.app.metadataCache.getFirstLinkpathDest(embed.link, activeFile.path)
if (!file) {
console.warn('file not found', embed.link)
return
}
// match ext
const m0 = extPatternRegex.exec(file.extension)
if (!m0) return

// rename
const { newName, isMeaningful }= this.generateNewName(file, activeFile)
debugLog('generated newName:', newName, isMeaningful)
if (!isMeaningful) {
new Notice('Failed to batch rename images: the generated name is not meaningful')
break;
}

await this.renameFile(file, newName, activeFile.path, false)
}
}

// returns a new name for the input file, with extension
generateNewName(file: TFile, activeFile: TFile) {
let imageNameKey = ''
Expand Down

0 comments on commit 38b2484

Please sign in to comment.