Skip to content
This repository has been archived by the owner on Sep 1, 2024. It is now read-only.

Commit

Permalink
customize the connector, update support file type
Browse files Browse the repository at this point in the history
  • Loading branch information
JYC333 committed Aug 9, 2022
1 parent a0b6a77 commit 2d20c88
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 9 deletions.
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
## Obsidian Attachment Name Formatting
This plugin will format all attachments in the format: "filename attachmentFormat indexNumber.xxx"

The attachmentFormat are image, audio, video and pdf. IndexNumber is ascending number from 1 based on the attchmentFormat.
The attachmentFormat are image, audio, video and pdf. IndexNumber is ascending number from 1 based on the attchmentFormat.You can change the default format for different type of attachments and the connector in the setting.

### Features
- Format attachments in active file, such as "filename image 1.png"
- Add new attachment -> will rename the attachment based on type and index
- Change order -> will rename attachments with new order
- Delete attachment -> will rename attachments with new order

### Supported attachment format
1. Image files: png, jpg, jpeg, gif, bmp, svg
2. Audio files: mp3, wav, m4a, ogg, 3gp, flac
3. Video files: mp4, ogv, mov, mkv
4. PDF files: pdf
`webm` file type doesn't support right now.

### Known Issues
- When delete an attachment which is already renamed, it will not rename and will occupy the indexNmuber for this note. But will be renamed to tmp_xxx if there is a conflict later.
Expand All @@ -23,6 +29,11 @@ The attachmentFormat are image, audio, video and pdf. IndexNumber is ascending n
- Support the situation that attachments have same name but under different path.
- When change the setting "Files & Links -> Default location for new attachments", sync all attachments' location with this setting. (Could be disable)

### Change in 1.4.4
- Add setting for connector, now you can customize the connector.
- Update Readme and the description in the setting.
- Update the support file type for video files (mov, mkv).

### Change in 1.4.3
- Using "_" instead of space for the attachment naming

Expand Down
55 changes: 48 additions & 7 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ interface AttachmentNameFormattingSettings {
audio: string;
video: string;
pdf: string;
connector: string;
exportCurrentDeletion: boolean;
exportUnusedDeletion: boolean;
copyPath: boolean;
Expand All @@ -29,6 +30,7 @@ const DEFAULT_SETTINGS: AttachmentNameFormattingSettings = {
audio: "audio",
video: "video",
pdf: "pdf",
connector: "_",
exportCurrentDeletion: false,
exportUnusedDeletion: false,
copyPath: false,
Expand All @@ -38,7 +40,7 @@ const DEFAULT_SETTINGS: AttachmentNameFormattingSettings = {
const extensions = {
image: ["png", "jpg", "jpeg", "gif", "bmp", "svg"],
audio: ["mp3", "wav", "m4a", "ogg", "3gp", "flac"], // "webm"
video: ["map", "ogv"], // "webm"
video: ["mp4", "ogv", "mov", "mkv"], // "webm"
pdf: ["pdf"],
};

Expand Down Expand Up @@ -154,9 +156,9 @@ export default class AttachmentNameFormatting extends Plugin {
if (attachmentFile instanceof TFile) {
// Create the new full name with path
let parent_path = attachmentFile.path.substring(0, attachmentFile.path.length - attachmentFile.name.length);
let newName = [file.basename, this.settings[fileType], num].join("_") + "." + attachmentFile.extension;
let newName = [file.basename, this.settings[fileType], num].join(this.settings.connector) + "." + attachmentFile.extension;
let fullName = parent_path + newName;

// Check wether destination is existed, if existed, rename the destination file to a tmp name
let destinationFile = this.app.vault.getAbstractFileByPath(fullName);
if (destinationFile && destinationFile !== attachmentFile) {
Expand Down Expand Up @@ -388,9 +390,32 @@ class AttachmentNameFormattingSettingTab extends PluginSettingTab {
containerEl.createEl('p', { text: 'This plugin will format all attachments in the format: "filename attachmentType indexNumber.xxx".' });
containerEl.createEl('p', { text: 'Each type of attachment will have individual index.' });
containerEl.createEl('p', { text: 'Only recognize the file type that can be recognized by Obsidian.' });
containerEl.createEl('p', { text: '(Do not have "webm" extension in audio and video right now)' });
containerEl.createEl('h3', { text: 'Supported file formats' });
containerEl.createEl('p', { text: 'Image files: png, jpg, jpeg, gif, bmp, svg' });
containerEl.createEl('p', { text: 'Audio files: mp3, wav, m4a, ogg, 3gp, flac' });
containerEl.createEl('p', { text: 'Video files: mp4, ogv, mov, mkv' });
containerEl.createEl('p', { text: 'PDF files: pdf' });
containerEl.createEl('p', { text: 'Do not have "webm" extension in audio and video right now' });
containerEl.createEl('h2', { text: 'Attachments Format Setting' });

new Setting(containerEl)
.setName('Format for connector')
.setDesc(
'Set the format for connector between file name and attachment name.'
)
.addText(text => text
.setPlaceholder('_')
.setValue(this.plugin.settings.connector === "_" ? "" : this.plugin.settings.connector)
.onChange(async (value) => {
let fileNamepatn = /\||<|>|\?|\*|:|\/|\\|"/;
if (fileNamepatn.test(value) || value === "") {
new FilenameWarningModal(this.app).open();
value = "_";
}
this.plugin.settings.connector = value;
await this.plugin.saveSettings();
}));

new Setting(containerEl)
.setName('Format for image')
.setDesc(
Expand Down Expand Up @@ -467,7 +492,7 @@ class AttachmentNameFormattingSettingTab extends PluginSettingTab {
.onChange(async (value) => {
this.plugin.settings.exportCurrentDeletion = value;
if (value) {
new WarningModal(this.app).open();
new DeletionWarningModal(this.app).open();
}
await this.plugin.saveSettings();
})
Expand Down Expand Up @@ -496,7 +521,7 @@ class AttachmentNameFormattingSettingTab extends PluginSettingTab {
.onChange(async (value) => {
this.plugin.settings.exportUnusedDeletion = value;
if (value) {
new WarningModal(this.app).open();
new DeletionWarningModal(this.app).open();
}
await this.plugin.saveSettings();
})
Expand Down Expand Up @@ -533,7 +558,7 @@ class AttachmentNameFormattingSettingTab extends PluginSettingTab {
}
}

class WarningModal extends Modal {
class DeletionWarningModal extends Modal {
constructor(app: App) {
super(app);
}
Expand All @@ -543,6 +568,22 @@ class WarningModal extends Modal {
contentEl.setText('Will delete the attachments and content after export!');
}

onClose() {
const { contentEl } = this;
contentEl.empty();
}
}

class FilenameWarningModal extends Modal {
constructor(app: App) {
super(app);
}

onOpen() {
const { contentEl } = this;
contentEl.setText('Invalid/No connector for filename, will use "_" as connector!');
}

onClose() {
const { contentEl } = this;
contentEl.empty();
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-attachment-name-formatting",
"name": "Attachment Name Formatting",
"version": "1.4.3",
"version": "1.4.4",
"minAppVersion": "0.12.0",
"description": "Obsidian plugin for formatting attachments name (filename attachmentType indexNumber.xxx)",
"author": "JYC333",
Expand Down

0 comments on commit 2d20c88

Please sign in to comment.