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

Commit

Permalink
small fix
Browse files Browse the repository at this point in the history
  • Loading branch information
JYC333 committed May 9, 2022
1 parent 84dd87d commit 2b15de1
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ This plugin will format all attachments in the format: "filename attachmentForma

The attachmentFormat are image, audio, video and pdf. IndexNumber is ascending number from 1 based on the attchmentFormat.

### New in 1.4.1
- Add some console outputs for debug.
- Add rescan command for manual rescan.

### New Features in 1.4.0
- Add copy attachment path option when right-click on the attachment link.
- Provide two options, absolute path and relative path (according to the vault path), default with relative path.
Expand Down
38 changes: 38 additions & 0 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,15 @@ export default class AttachmentNameFormatting extends Plugin {
this.app.metadataCache.on('changed', (file) => this.handleAttachmentNameFormatting(file)),
);

this.addCommand({
id: 'attachments-rescan-command',
name: 'Rescan Attachments in Current File',
callback: () => {
let file = this.app.workspace.getActiveFile();
this.handleAttachmentNameFormatting(file);
}
});

this.registerEvent(
this.app.workspace.on('editor-menu', (menu, editor, view) => {
let cursorPosition = editor.getCursor();
Expand Down Expand Up @@ -172,9 +181,12 @@ export default class AttachmentNameFormatting extends Plugin {
return;
}

console.log("Formatting attachments...");

// Get the metadata of the active file
const attachments = this.app.metadataCache.getFileCache(file);
// Check whether the file has attachments
console.log("Getting attachments list...");
if (attachments.hasOwnProperty("embeds")) {
// Create a list of attachments, classified by types
let attachmentList: AttachmentList = {};
Expand All @@ -198,7 +210,9 @@ export default class AttachmentNameFormatting extends Plugin {
}
}
}
console.log("Attachment list:", attachmentList);
// Rename the attachments
console.log("Renaming attachments...");
for (let [fileType, attachmentFiles] of Object.entries(attachmentList)) {
// Check if it exists and is of the correct type
let num = 1;
Expand All @@ -212,32 +226,41 @@ export default class AttachmentNameFormatting extends Plugin {
let destinationFile = this.app.vault.getAbstractFileByPath(fullName);
if (destinationFile && destinationFile !== attachmentFile) {
await this.app.fileManager.renameFile(attachmentFile, parent_path + "tmp_" + newName);
console.log("Rename attachment \"" + attachmentFile.name + "\" to \"" + newName + "\"");
} else {
await this.app.fileManager.renameFile(attachmentFile, fullName);
console.log("Rename attachment \"" + attachmentFile.name + "\" to \"" + newName + "\"");
}
num++;
}
}
}
} else {
console.log("No attachments found...");
}
};

/*
* Export the attachments in the active file when it has
*/
async handleAttachmentExport() {
console.log("Exporting attachments...");

// Create new JSZip instance
let zip = new JSZip();

// Get the active file
let file = this.app.workspace.getActiveFile()
const attachments = this.app.metadataCache.getFileCache(file);
console.log("Getting attachments list...");
if (attachments.hasOwnProperty("embeds")) {
for (let item of attachments.embeds) {
for (let [fileType, fileExtensions] of Object.entries(extensions)) {
let attachmentExtension = item.link.split(".").pop();
console.log("Collecting attachments...");
if (fileExtensions.contains(attachmentExtension)) {
let file_path = normalizePath(this.app.vault.adapter.basePath + '\\' + parseLinktext(item.link).path);
console.log("Get attachment", file_path);
// Get the attachment and write into JSZip instance
await FileSystemAdapter.readLocalFile(file_path)
.then(data => zip.file(normalizePath(fileType + '\\' + item.link), data))
Expand All @@ -246,35 +269,44 @@ export default class AttachmentNameFormatting extends Plugin {
}

// Save zip file to the root folder
console.log("Saving attachemnts...");
zip.generateNodeStream({ type: 'nodebuffer', streamFiles: true })
.pipe(fs.createWriteStream(normalizePath(this.app.vault.adapter.basePath + '/' + file.basename + '_Attachments.zip')))
.on('finish', function () {
// Send the finish message
new Notice(file.basename + ' attachments exported.');
});
console.log("Saving Done...");

let content = '';
await this.app.vault.cachedRead(file).then(data => content = data);

if (this.settings.exportCurrentDeletion) {
console.log("Deleting attachments...");
for (let item of attachments.embeds) {
let file_path = parseLinktext(item.link).path;
let attachmentFile = this.app.vault.getAbstractFileByPath(file_path);
if (!attachmentFile) {
attachmentFile = this.app.metadataCache.getFirstLinkpathDest(file_path, file_path);
}
content = content.replace(item.original, '');
console.log("Delete attachment", attachmentFile.name);
await this.app.vault.delete(attachmentFile);
}
await this.app.vault.modify(file, content);
console.log("Deleting Done...");
}
} else {
console.log("No attachments found...");
}
};

/*
* Export the unused attachments in all files
*/
async handleUnusedAttachmentExport() {
console.log("Exporting unused attachments...");

let files = this.app.vault.getFiles();
let mdFiles = this.app.vault.getMarkdownFiles();
let attachmentFiles = files.filter(file => !mdFiles.contains(file));
Expand All @@ -284,6 +316,7 @@ export default class AttachmentNameFormatting extends Plugin {
attachmentFiles = attachmentFiles.filter(file => allExtensions.contains(file.extension));

// Get all Unused attachments
console.log("Getting all unused attachments...");
for (let mdfile of mdFiles) {
let attachments = this.app.metadataCache.getFileCache(mdfile);
if (attachments.hasOwnProperty("embeds")) {
Expand All @@ -306,17 +339,22 @@ export default class AttachmentNameFormatting extends Plugin {
}

// Save zip file to the root folder
console.log("Saving attachemnts...");
zip.generateNodeStream({ type: 'nodebuffer', streamFiles: true })
.pipe(fs.createWriteStream(normalizePath(this.app.vault.adapter.basePath + '/Unused_Attachments.zip')))
.on('finish', function () {
// Send the finish message
new Notice('Unused attachments exported.');
});
console.log("Saving Done...");

if (this.settings.exportCurrentDeletion) {
console.log("Deleting attachments...");
for (let file of attachmentFiles) {
console.log("Delete attachment", file.name);
await this.app.vault.delete(file);
}
console.log("Deleting Done...");
}
}
}
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.3.0",
"version": "1.4.1",
"minAppVersion": "0.12.0",
"description": "Obsidian plugin for formatting attachments name (filename attachmentType indexNumber.xxx)",
"author": "JYC333",
Expand Down

0 comments on commit 2b15de1

Please sign in to comment.