diff --git a/README.md b/README.md index 8b0901e..4c0b559 100644 --- a/README.md +++ b/README.md @@ -143,6 +143,9 @@ that match the given extension pattern. - `{{fileName}}`: My note, My note-1, My note-2 - `{{imageNameKey}}`: foo, foo-1, foo-2 - `{{imageNameKey}}-{{DATE:YYYYMMDD}}`: foo-20220408, foo-20220408-1, foo-20220408-2 + - `/sources/images/{{fileName}}`: /sources/images/My note + - The image will be saved at the specified path, and directories will be created recursively if they do not exist. + - **Duplicate number at start (or end)** If enabled, the duplicate number will be added at the start as prefix for the image name, otherwise, it will be added at the end as suffix for the image name. diff --git a/src/main.ts b/src/main.ts index 2b4459f..f89c4fe 100644 --- a/src/main.ts +++ b/src/main.ts @@ -149,55 +149,70 @@ export default class PasteImageRenamePlugin extends Plugin { async renameFile(file: TFile, inputNewName: string, sourcePath: string, replaceCurrentLine?: boolean) { // deduplicate name - const { name:newName } = await this.deduplicateNewName(inputNewName, file) - debugLog('deduplicated newName:', newName) - const originName = file.name - - // generate linkText using Obsidian API, linkText is either ![](filename.png) or ![[filename.png]] according to the "Use [[Wikilinks]]" setting. - const linkText = this.app.fileManager.generateMarkdownLink(file, sourcePath) - - // file system operation: rename the file - const newPath = path.join(file.parent.path, newName) + const { name: newName } = await this.deduplicateNewName(inputNewName, file); + debugLog('deduplicated newName:', newName); + const originName = file.name; + + // Handle forward slashes in the path and create directories + const newPathParts = newName.split('/'); + const newDir = newPathParts.slice(0, -1).join('/'); + const newFileName = newPathParts[newPathParts.length - 1]; + + if (newDir) { + try { + await this.app.vault.createFolder(newDir); + } catch (err) { + if (!err.message.includes("Folder already exists")) { + new Notice(`Failed to create directory ${newDir}: ${err}`); + throw err; + } + } + } + + const newPath = path.join(newDir, newFileName); + + // Generate linkText using Obsidian API + const linkText = this.app.fileManager.generateMarkdownLink(file, sourcePath); + + // File system operation: rename the file try { - await this.app.fileManager.renameFile(file, newPath) + await this.app.fileManager.renameFile(file, newPath); } catch (err) { - new Notice(`Failed to rename ${newName}: ${err}`) - throw err + new Notice(`Failed to rename ${newName}: ${err}`); + throw err; } - + if (!replaceCurrentLine) { - return + return; } - - // 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) - debugLog('replace text', linkText, newLinkText) - - const editor = this.getActiveEditor() + + // Replace the current line in the editor + const newLinkText = this.app.fileManager.generateMarkdownLink(file, sourcePath); + debugLog('replace text', linkText, newLinkText); + + const editor = this.getActiveEditor(); if (!editor) { - new Notice(`Failed to rename ${newName}: no active editor`) - return + new Notice(`Failed to rename ${newName}: no active editor`); + return; } - - const cursor = editor.getCursor() - const line = editor.getLine(cursor.line) - const replacedLine = line.replace(linkText, newLinkText) - debugLog('current line -> replaced line', line, replacedLine) - // console.log('editor context', cursor, ) + + const cursor = editor.getCursor(); + const line = editor.getLine(cursor.line); + const replacedLine = line.replace(linkText, newLinkText); + debugLog('current line -> replaced line', line, replacedLine); + editor.transaction({ changes: [ { - from: {...cursor, ch: 0}, - to: {...cursor, ch: line.length}, + from: { ...cursor, ch: 0 }, + to: { ...cursor, ch: line.length }, text: replacedLine, - } - ] - }) - + }, + ], + }); + if (!this.settings.disableRenameNotice) { - new Notice(`Renamed ${originName} to ${newName}`) + new Notice(`Renamed ${originName} to ${newName}`); } }