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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Obsidian paste image rename

> Forked from `reorx/obsidian-paste-image-rename`. Fixed a bug, which may throw an error when target folder does not exist.

---------

> :loudspeaker: Starting from 1.4.0, Paste image rename becomes a general-purpose renaming plugin
> that can handle all attachments added to the vault.

Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "obsidian-paste-image-rename",
"name": "Paste image rename",
"version": "1.6.1",
"version": "1.6.2",
"minAppVersion": "0.12.0",
"description": "Rename pasted images and all the other attchments added to the vault",
"author": "Reorx",
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "obsidian-paste-image-rename",
"version": "1.6.1",
"version": "1.6.2",
"main": "main.js",
"scripts": {
"start": "node esbuild.config.mjs",
Expand Down
6 changes: 6 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,12 @@ export default class PasteImageRenamePlugin extends Plugin {

// file system operation: rename the file
const newPath = path.join(file.parent.path, newName)
const dirPath = path.dirname(newPath)

if (dirPath && !await this.app.vault.adapter.exists(dirPath)) {
await this.app.vault.createFolder(dirPath)
}

try {
await this.app.fileManager.renameFile(file, newPath)
} catch (err) {
Expand Down
6 changes: 6 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ export const path = {
const positions = [...fullpath.matchAll(new RegExp('\\.', 'gi'))].map(a => a.index)
return fullpath.slice(positions[positions.length - 1] + 1)
},

// return directory name of the path
dirname(fullpath: string): string {
const index = fullpath.lastIndexOf('/')
return fullpath.substring(0, index + 1)
}
}

const filenameNotAllowedChars = /[^\p{L}0-9~`!@$&*()\-_=+{};'",<.>? ]/ug
Expand Down