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

Commit

Permalink
fix: fix broken bug in 2.1.7
Browse files Browse the repository at this point in the history
fix #35
  • Loading branch information
JYC333 committed Dec 28, 2023
1 parent b41902d commit 076ee69
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 45 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
## [2.1.8](https://github.com/JYC333/obsidian-attachment-name-formatting/compare/2.1.7...2.1.8) (2023-12-28)


### Bug Fixes

* fix broken bug in 2.1.7 ([adbe414](https://github.com/JYC333/obsidian-attachment-name-formatting/commit/adbe414b4997f14c720a32d1b9754e1a85f1d9ae)), closes [#35](https://github.com/JYC333/obsidian-attachment-name-formatting/issues/35)
* no active editor won't break plugin now, set a minimum time for plugin run the second time ([c11e44a](https://github.com/JYC333/obsidian-attachment-name-formatting/commit/c11e44ab14d3f5ab0eac50f568322e5f1bf86b3e)), closes [#29](https://github.com/JYC333/obsidian-attachment-name-formatting/issues/29)



## [2.1.7](https://github.com/JYC333/obsidian-file-structure-format/compare/2.1.6...2.1.7) (2023-12-26)


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": "2.1.7",
"version": "2.1.8",
"minAppVersion": "0.12.0",
"description": "Obsidian plugin for formatting attachments name (filename attachmentType indexNumber.xxx)",
"author": "JYC333",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "obsidian-attachment-name-formattion",
"version": "2.1.7",
"version": "2.1.8",
"description": "Obsidian plugin for formatting attachments name (filename attachmentType indexNumber.xxx)",
"main": "main.js",
"scripts": {
Expand Down
51 changes: 27 additions & 24 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,8 @@ export default class AttachmentNameFormatting extends Plugin {
for (const fileIndex in fileList) {
progress.empty();
progress.createEl("span", {
text: `Attachment renaming: ${
fileIndex + 1
}/${fileList.length}`,
text: `Attachment renaming: ${fileIndex + 1
}/${fileList.length}`,
});

await this.handleAttachmentNameFormatting(
Expand Down Expand Up @@ -305,7 +304,7 @@ export default class AttachmentNameFormatting extends Plugin {
item.link,
file.basename,
this.settings[
fileType as keyof ANFSettings
fileType as keyof ANFSettings
] as string,
this.settings
) &&
Expand All @@ -331,7 +330,7 @@ export default class AttachmentNameFormatting extends Plugin {
attachmentFile.path.replace(
path.extname(attachmentFile.path),
"_copy" +
path.extname(attachmentFile.path)
path.extname(attachmentFile.path)
);

await this.app.vault.adapter.copy(
Expand Down Expand Up @@ -385,6 +384,10 @@ export default class AttachmentNameFormatting extends Plugin {
// Fetch attachment folder path setting
let parent_path = this.vaultAttachmentFolderPath;

if (parent_path === undefined) {
parent_path = '/'
}

if (parent_path.startsWith("./")) {
parent_path = path.join(
file.parent.path,
Expand All @@ -395,7 +398,7 @@ export default class AttachmentNameFormatting extends Plugin {
// Fetch subfolder setting
const subfolder =
this.settings.subfolders[
ATTACHMENT_TYPE.indexOf(fileType)
ATTACHMENT_TYPE.indexOf(fileType)
];

const baseNameComponent = [
Expand Down Expand Up @@ -513,7 +516,7 @@ export default class AttachmentNameFormatting extends Plugin {
destinationFile.path.substring(
0,
destinationFile.path.length -
destinationFile.name.length
destinationFile.name.length
);
const tmpName =
"tmp" + Date.now() + "_" + destinationFile.name;
Expand All @@ -522,11 +525,11 @@ export default class AttachmentNameFormatting extends Plugin {
);
console.log(
'Rename attachment "' +
destinationFile.name +
'" to "' +
destinationFile_path +
tmpName +
'"'
destinationFile.name +
'" to "' +
destinationFile_path +
tmpName +
'"'
);
await this.app.fileManager.renameFile(
destinationFile,
Expand All @@ -539,10 +542,10 @@ export default class AttachmentNameFormatting extends Plugin {
);
console.log(
'Rename attachment "' +
attachmentFile.path +
'" to "' +
fullName +
'"'
attachmentFile.path +
'" to "' +
fullName +
'"'
);

const oldName = attachmentFile.name;
Expand Down Expand Up @@ -596,8 +599,8 @@ export default class AttachmentNameFormatting extends Plugin {
const file_path = normalizePath(
// @ts-ignore
this.app.vault.adapter.basePath +
"\\" +
attachement.path
"\\" +
attachement.path
);
console.log("Get attachment", file_path);
// Get the attachment and write into JSZip instance
Expand All @@ -620,9 +623,9 @@ export default class AttachmentNameFormatting extends Plugin {
normalizePath(
// @ts-ignore
this.app.vault.adapter.basePath +
"/" +
file.basename +
"_Attachments.zip"
"/" +
file.basename +
"_Attachments.zip"
)
)
)
Expand Down Expand Up @@ -692,8 +695,8 @@ export default class AttachmentNameFormatting extends Plugin {
const file_path = normalizePath(
// @ts-ignore
this.app.vault.adapter.basePath +
"\\" +
parseLinktext(file.path).path
"\\" +
parseLinktext(file.path).path
);
await FileSystemAdapter.readLocalFile(file_path).then((data) =>
zip.file(normalizePath(file.name), data)
Expand All @@ -708,7 +711,7 @@ export default class AttachmentNameFormatting extends Plugin {
normalizePath(
// @ts-ignore
this.app.vault.adapter.basePath +
"/Unused_Attachments.zip"
"/Unused_Attachments.zip"
)
)
)
Expand Down
44 changes: 26 additions & 18 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { App, Editor, TAbstractFile, TFile, parseLinktext } from "obsidian";
import * as path from "path";
import { ANFSettings } from "src/types";
import AttachmentNameFormatting from "src/main";

/**
* Get attachment file
Expand Down Expand Up @@ -112,7 +111,7 @@ export function checkAlreadyRenamed(
if (settings.enablePathHash) {
const connector =
settings.multipleConnectors[
settings.multipleConnectors.length - 1
settings.multipleConnectors.length - 1
];

const nameSplits = name.split(".");
Expand All @@ -131,23 +130,32 @@ export function checkAlreadyRenamed(
components = [...name.split(settings.connector)];
}

const dateCheck2 = new Date(
+components[2].slice(0, 4),
+components[2].slice(4, 6) - 1,
+components[2].slice(6, 8),
+components[2].slice(8, 10),
+components[2].slice(10, 12),
+components[2].slice(12, 14)
);
let dateCheck2, dateCheck3
if (components.length >= 3) {
dateCheck2 = new Date(
+components[2].slice(0, 4),
+components[2].slice(4, 6) - 1,
+components[2].slice(6, 8),
+components[2].slice(8, 10),
+components[2].slice(10, 12),
+components[2].slice(12, 14)
);
} else {
dateCheck2 = new Date("Invalid");
}

const dateCheck3 = new Date(
+components[3].slice(0, 4),
+components[3].slice(4, 6) - 1,
+components[3].slice(6, 8),
+components[3].slice(8, 10),
+components[3].slice(10, 12),
+components[3].slice(12, 14)
);
if (components.length >= 4) {
dateCheck3 = new Date(
+components[3].slice(0, 4),
+components[3].slice(4, 6) - 1,
+components[3].slice(6, 8),
+components[3].slice(8, 10),
+components[3].slice(10, 12),
+components[3].slice(12, 14)
);
} else {
dateCheck3 = new Date("Invalid");
}

// Check whether the compents are followed the renaming pattern
if (components.length === 3) {
Expand Down
3 changes: 2 additions & 1 deletion versions.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,6 @@
"2.1.3": "0.12.0",
"2.1.4": "0.12.0",
"2.1.5": "0.12.0",
"2.1.7": "0.12.0"
"2.1.7": "0.12.0",
"2.1.8": "0.12.0"
}

0 comments on commit 076ee69

Please sign in to comment.