From 7c814b95d678c1bc3063b4067231d828a71fe5e7 Mon Sep 17 00:00:00 2001 From: yfzhao20 Date: Sat, 4 Mar 2023 01:06:08 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix=20error=20when=20target=20fo?= =?UTF-8?q?lder=20doesn't=20exist?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 4 ++++ manifest.json | 2 +- package-lock.json | 4 ++-- package.json | 2 +- src/main.ts | 6 ++++++ src/utils.ts | 6 ++++++ 6 files changed, 20 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 8b0901e..24acc4b 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/manifest.json b/manifest.json index 152d913..c0812d4 100644 --- a/manifest.json +++ b/manifest.json @@ -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", diff --git a/package-lock.json b/package-lock.json index 69d5854..78bf060 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "obsidian-paste-image-rename", - "version": "1.5.0", + "version": "1.6.1", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "obsidian-paste-image-rename", - "version": "1.5.0", + "version": "1.6.1", "license": "MIT", "dependencies": { "cash-dom": "^8.1.2" diff --git a/package.json b/package.json index c904e4a..6b72098 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/main.ts b/src/main.ts index 2b4459f..45d9fb4 100644 --- a/src/main.ts +++ b/src/main.ts @@ -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) { diff --git a/src/utils.ts b/src/utils.ts index 50c22b1..c8d51f6 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -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