Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exclude rename for regex matching paths #90

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
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
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
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.7.0",
"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.7.0",
"main": "main.js",
"scripts": {
"start": "node esbuild.config.mjs",
Expand Down
31 changes: 31 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ interface PluginSettings {
autoRename: boolean
handleAllAttachments: boolean
excludeExtensionPattern: string
excludePathPattern: string
disableRenameNotice: boolean
}

Expand All @@ -55,6 +56,7 @@ const DEFAULT_SETTINGS: PluginSettings = {
autoRename: false,
handleAllAttachments: false,
excludeExtensionPattern: '',
excludePathPattern: '',
disableRenameNotice: false,
}

Expand Down Expand Up @@ -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) {
Expand All @@ -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)
}
}
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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.
Expand Down
3 changes: 2 additions & 1 deletion versions.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}