Skip to content
Open
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
89 changes: 52 additions & 37 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
}
}

Expand Down