Skip to content

Commit

Permalink
support handling all attachments, add an option to exclude by extension
Browse files Browse the repository at this point in the history
- rename handleAllImages to handleAllAttachments
- add excludeExtensionPattern
  • Loading branch information
reorx committed May 8, 2022
1 parent 3b701d2 commit c489b93
Showing 1 changed file with 40 additions and 10 deletions.
50 changes: 40 additions & 10 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ interface PluginSettings {
dupNumberAtStart: boolean
dupNumberDelimiter: string
autoRename: boolean
handleAllImages: boolean
handleAllAttachments: boolean
excludeExtensionPattern: string
disableRenameNotice: boolean
}

Expand All @@ -35,7 +36,8 @@ const DEFAULT_SETTINGS: PluginSettings = {
dupNumberAtStart: false,
dupNumberDelimiter: '-',
autoRename: false,
handleAllImages: false,
handleAllAttachments: false,
excludeExtensionPattern: '',
disableRenameNotice: false,
}

Expand All @@ -45,6 +47,7 @@ const PASTED_IMAGE_PREFIX = 'Pasted image '
export default class PasteImageRenamePlugin extends Plugin {
settings: PluginSettings
modals: Modal[] = []
excludeExtensionRegex: RegExp

async onload() {
const pkg = require('../package.json')
Expand All @@ -63,9 +66,12 @@ export default class PasteImageRenamePlugin extends Plugin {
debugLog('pasted image created', file)
this.startRenameProcess(file, this.settings.autoRename)
} else {
// handle drop images
if (isImage(file) && this.settings.handleAllImages) {
debugLog('image created', file)
if (this.settings.handleAllAttachments) {
debugLog('file created', file)
if (this.testExcludeExtensionRegex(file)) {
debugLog('excluded file by ext', file)
return
}
this.startRenameProcess(file, this.settings.autoRename)
}
}
Expand Down Expand Up @@ -299,6 +305,16 @@ export default class PasteImageRenamePlugin extends Plugin {
this.modals.map(modal => modal.close())
}

buildExcludeExtensionRegex() {
this.excludeExtensionRegex = new RegExp(this.settings.excludeExtensionPattern)
}
testExcludeExtensionRegex(file: TFile): boolean {
if (!this.excludeExtensionRegex) {
this.buildExcludeExtensionRegex()
}
return this.excludeExtensionRegex.test(file.extension)
}

async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
Expand Down Expand Up @@ -533,16 +549,30 @@ class SettingTab extends PluginSettingTab {
));

new Setting(containerEl)
.setName('Handle all images')
.setName('Handle all attachments')
.setDesc(`By default, the plugin only handles images that starts with "Pasted image " in name,
which is the prefix Obsidian uses to create images from pasted content.
If this option is set, the plugin will handle all images. This includes drag'n drop image,
or any other image that is created in the valut.`)
If this option is set, the plugin will handle all attachments that are created in the valut.`)
.addToggle(toggle => toggle
.setValue(this.plugin.settings.handleAllImages)
.setValue(this.plugin.settings.handleAllAttachments)
.onChange(async (value) => {
this.plugin.settings.handleAllAttachments = value;
await this.plugin.saveSettings();
}
));

new Setting(containerEl)
.setName('Exclude extension pattern')
.setDesc(`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.`)
.setClass('single-line-textarea')
.addTextArea(text => text
.setPlaceholder('docx?|xlsx?|pptx?|zip|rar')
.setValue(this.plugin.settings.excludeExtensionPattern)
.onChange(async (value) => {
this.plugin.settings.handleAllImages = value;
this.plugin.settings.excludeExtensionPattern = value;
await this.plugin.saveSettings();
this.plugin.buildExcludeExtensionRegex()
}
));

Expand Down

0 comments on commit c489b93

Please sign in to comment.