diff --git a/README.md b/README.md index 8b0901e..15d296d 100644 --- a/README.md +++ b/README.md @@ -74,6 +74,14 @@ If we paste the third image without editing the "New name" input, its name will This feature is especially powerful if you enable "Auto rename" in settings, you can just add new images without thinking, and they will be renamed sequentially by the pattern and `imageNameKey` set. +### Exclude Paths + +> New in 1.7.0 + +Add a new setting `Exclude path pattern` which allows you to exclude renaming if the file being being pasted into's path matches a regular expression pattern. + +For example, suppose that you don't want to rename attachments to files in `My Special Folder`. If you set the `Exclude path pattern` setting to `^My Special Folder\/` renaming will not be invoked on any files in that folder. Note the leading `^` and ending `\/` in the regular expression. These ensure the pattern matches at the beginning of the path and includes the `/` character at the end. Without the latter the regular expression would match a file named `My Special Folder` in addition to any files in that folder. + ### Batch renaming process > New in 1.3.0 @@ -160,6 +168,9 @@ that match the given extension pattern. This option is only useful when "Handle all attachments" is enabled. Write a Regex pattern to exclude certain extensions from being handled. Only the first line will be used. +- **Exclude path pattern** + + Write a Regex pattern to exclude certain paths from being handled. Only the first line will be used. - **Disable rename notice** Turn off this option if you don't want to see the notice when renaming images. diff --git a/manifest.json b/manifest.json index 152d913..b928cbd 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.7.0", "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..90115a6 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "obsidian-paste-image-rename", - "version": "1.5.0", + "version": "1.7.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "obsidian-paste-image-rename", - "version": "1.5.0", + "version": "1.7.0", "license": "MIT", "dependencies": { "cash-dom": "^8.1.2" diff --git a/package.json b/package.json index c904e4a..5783716 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "obsidian-paste-image-rename", - "version": "1.6.1", + "version": "1.7.0", "main": "main.js", "scripts": { "start": "node esbuild.config.mjs", diff --git a/src/main.ts b/src/main.ts index 2b4459f..9c64862 100644 --- a/src/main.ts +++ b/src/main.ts @@ -44,6 +44,7 @@ interface PluginSettings { autoRename: boolean handleAllAttachments: boolean excludeExtensionPattern: string + excludePathPattern: string disableRenameNotice: boolean } @@ -55,6 +56,7 @@ const DEFAULT_SETTINGS: PluginSettings = { autoRename: false, handleAllAttachments: false, excludeExtensionPattern: '', + excludePathPattern: '', disableRenameNotice: false, } @@ -86,6 +88,11 @@ export default class PasteImageRenamePlugin extends Plugin { return if (isPastedImage(file)) { debugLog('pasted image created', file) + const activeFile = this.getActiveFile() + if (this.testExcludePath(activeFile)) { + debugLog('excluded file by path', activeFile) + return + } this.startRenameProcess(file, this.settings.autoRename) } else { if (this.settings.handleAllAttachments) { @@ -94,6 +101,11 @@ export default class PasteImageRenamePlugin extends Plugin { debugLog('excluded file by ext', file) return } + const activeFile = this.getActiveFile() + if (this.testExcludePath(activeFile)) { + debugLog('excluded file by path', activeFile) + return + } this.startRenameProcess(file, this.settings.autoRename) } } @@ -372,6 +384,12 @@ export default class PasteImageRenamePlugin extends Plugin { if (!pattern) return false return new RegExp(pattern).test(file.extension) } + testExcludePath(file: TFile): boolean { + const pattern = this.settings.excludePathPattern + if (!pattern) return false + debugLog('file path', file.path) + return new RegExp(pattern).test(file.path) + } async loadSettings() { this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData()); @@ -664,6 +682,19 @@ class SettingTab extends PluginSettingTab { } )); + new Setting(containerEl) + .setName('Exclude path pattern') + .setDesc(`Write a Regex pattern to exclude certain paths from being handled. Only the first line will be used.`) + .setClass('single-line-textarea') + .addTextArea(text => text + .setPlaceholder('^Journal\/|^Zotero\/') + .setValue(this.plugin.settings.excludePathPattern) + .onChange(async (value) => { + this.plugin.settings.excludePathPattern = value; + await this.plugin.saveSettings(); + } + )); + new Setting(containerEl) .setName('Disable rename notice') .setDesc(`Turn off this option if you don't want to see the notice when renaming images. diff --git a/versions.json b/versions.json index f843727..1206e98 100644 --- a/versions.json +++ b/versions.json @@ -12,5 +12,6 @@ "1.5.1": "0.12.0", "1.5.2": "0.12.0", "1.6.0": "0.12.0", - "1.6.1": "0.12.0" + "1.6.1": "0.12.0", + "1.7.0": "0.12.0" } \ No newline at end of file