Skip to content

Commit f260938

Browse files
committed
feat: support ${var[:]}
1 parent 57ec4a1 commit f260938

File tree

11 files changed

+181
-146
lines changed

11 files changed

+181
-146
lines changed

src/arrange.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,16 @@ export class ArrangeHandler {
8080
debugLog("rearrangeAttachment - original name:", originalName);
8181

8282
// create attachment path if it's not exists
83-
const attachPath = metadata.getAttachmentPath(setting, this.settings.dateFormat);
83+
const attachPath = await metadata.getAttachmentPath(setting, this.settings.dateFormat, this.app.vault.adapter);
8484
if (!(await this.app.vault.adapter.exists(attachPath, true))) {
8585
// process the case where rename the filename to uppercase or lowercase
8686
if (oldPath != undefined && (await this.app.vault.adapter.exists(attachPath, false))) {
8787
const mdOld = getMetadata(oldPath);
88-
const attachPathOld = mdOld.getAttachmentPath(setting, this.settings.dateFormat);
88+
const attachPathOld = await mdOld.getAttachmentPath(
89+
setting,
90+
this.settings.dateFormat,
91+
this.app.vault.adapter
92+
);
8993
// this will trigger the rename event and cause the path of attachment change
9094
this.app.vault.adapter.rename(attachPathOld, attachPath);
9195
} else {
@@ -335,15 +339,15 @@ export class ArrangeHandler {
335339
if (attachPath !== linkPath) {
336340
return true;
337341
} else {
338-
if (settings.attachFormat.includes(SETTINGS_VARIABLES_NOTENAME) && !linkName.includes(noteName)) {
342+
if (settings.attachFormat.includes(`\${${SETTINGS_VARIABLES_NOTENAME}}`) && !linkName.includes(noteName)) {
339343
return true;
340344
}
341345
// suppose the ${notename} was in format
342-
const noNoteNameAttachFormat = settings.attachFormat.split(SETTINGS_VARIABLES_NOTENAME);
343-
if (settings.attachFormat.includes(SETTINGS_VARIABLES_DATES)) {
346+
const noNoteNameAttachFormat = settings.attachFormat.split(`\${${SETTINGS_VARIABLES_NOTENAME}}`);
347+
if (settings.attachFormat.includes(`\${${SETTINGS_VARIABLES_DATES}}`)) {
344348
for (const formatPart in noNoteNameAttachFormat) {
345349
// suppose the ${date} was in format, split each part and search in linkName
346-
const splited = formatPart.split(SETTINGS_VARIABLES_DATES);
350+
const splited = formatPart.split(`\${${SETTINGS_VARIABLES_DATES}}`);
347351
for (const part in splited) {
348352
if (!linkName.includes(part)) {
349353
return true;

src/create.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export class CreateHandler {
2727
* @param source - the notes file that linked to attach
2828
* @returns - none
2929
*/
30-
processAttach(attach: TFile, source: TFile) {
30+
async processAttach(attach: TFile, source: TFile) {
3131
// ignore if the path of notes file has been excluded.
3232
if (source.parent && isExcluded(source.parent.path, this.settings)) {
3333
debugLog("processAttach - not a file or exclude path:", source.path);
@@ -47,7 +47,7 @@ export class CreateHandler {
4747
const metadata = getMetadata(source.path, attach);
4848
debugLog("processAttach - metadata:", metadata);
4949

50-
const attachPath = metadata.getAttachmentPath(setting, this.settings.dateFormat);
50+
const attachPath = await metadata.getAttachmentPath(setting, this.settings.dateFormat, this.app.vault.adapter);
5151
metadata
5252
.getAttachFileName(setting, this.settings.dateFormat, attach.basename, this.app.vault.adapter)
5353
.then((attachName) => {

src/i18n/locales/en.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export const en: TranslationMap = {
3939
},
4040
attachmentFormat: {
4141
name: 'Attachment format',
42-
desc: 'Define how to name the attachment file, available variables {{dates}}, {{notename}}, {{md5}} and {{originalname}}.'
42+
desc: 'Define how to name the attachment file, available variables {{date}}, {{notename}}, {{md5}} and {{originalname}}.'
4343
},
4444
dateFormat: {
4545
name: 'Date format',
@@ -186,4 +186,4 @@ export const en: TranslationMap = {
186186
duplicateExtension: 'Duplicate extension override.',
187187
excludedExtension: 'Extension override cannot be an excluded extension.'
188188
}
189-
};
189+
};

src/i18n/locales/zh-cn.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export const zhCn: TranslationMap = {
3939
},
4040
attachmentFormat: {
4141
name: '附件格式',
42-
desc: '定义如何命名附件文件,可用变量 {{dates}}、{{notename}}、{{md5}} 和 {{originalname}}。'
42+
desc: '定义如何命名附件文件,可用变量 {{date}}、{{notename}}、{{md5}} 和 {{originalname}}。'
4343
},
4444
dateFormat: {
4545
name: '日期格式',
@@ -193,4 +193,4 @@ export const zhCn: TranslationMap = {
193193
duplicateExtension: '重复的扩展覆盖。',
194194
excludedExtension: '扩展覆盖不能是被排除的扩展。'
195195
}
196-
};
196+
};

src/lib/constant.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1-
export const SETTINGS_VARIABLES_DATES = "${date}";
2-
export const SETTINGS_VARIABLES_NOTEPATH = "${notepath}";
3-
export const SETTINGS_VARIABLES_NOTENAME = "${notename}";
4-
export const SETTINGS_VARIABLES_NOTEPARENT = "${parent}";
5-
export const SETTINGS_VARIABLES_ORIGINALNAME = "${originalname}";
6-
export const SETTINGS_VARIABLES_EXTENSION = "${extension}";
7-
export const SETTINGS_VARIABLES_MD5 = "${md5}";
1+
export const SETTINGS_VARIABLES_DATES = "date";
2+
export const SETTINGS_VARIABLES_YEAR = "year";
3+
export const SETTINGS_VARIABLES_MONTH = "month";
4+
export const SETTINGS_VARIABLES_DAY = "day";
5+
export const SETTINGS_VARIABLES_NOTEPATH = "notepath";
6+
export const SETTINGS_VARIABLES_NOTENAME = "notename";
7+
export const SETTINGS_VARIABLES_NOTEPARENT = "parent";
8+
export const SETTINGS_VARIABLES_ORIGINALNAME = "originalname";
9+
export const SETTINGS_VARIABLES_EXTENSION = "extension";
10+
export const SETTINGS_VARIABLES_MD5 = "md5";
811
export const SETTINGS_ROOT_OBSFOLDER = "obsFolder";
912
export const SETTINGS_ROOT_INFOLDER = "inFolderBelow";
1013
export const SETTINGS_ROOT_NEXTTONOTE = "nextToNote";

src/lib/originalStorage.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import { SETTINGS_VARIABLES_ORIGINALNAME } from "./constant";
55
export function containOriginalNameVariable(setting: AttachmentPathSettings, ext: string): boolean {
66
const { extSetting } = getExtensionOverrideSetting(ext, setting);
77
if (
8-
(extSetting !== undefined && extSetting.attachFormat.contains(SETTINGS_VARIABLES_ORIGINALNAME)) ||
9-
setting.attachFormat.contains(SETTINGS_VARIABLES_ORIGINALNAME)
8+
(extSetting !== undefined && extSetting.attachFormat.contains(`\${${SETTINGS_VARIABLES_ORIGINALNAME}`)) ||
9+
setting.attachFormat.contains(`\${${SETTINGS_VARIABLES_ORIGINALNAME}`)
1010
) {
1111
return true;
1212
}

src/main.ts

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export default class AttachmentManagementPlugin extends Plugin {
3030

3131
// 初始化国际化系统
3232
loadAllTranslations();
33-
const savedLanguage = this.settings.language as SupportedLanguage || detectLanguage();
33+
const savedLanguage = (this.settings.language as SupportedLanguage) || detectLanguage();
3434
setLanguage(savedLanguage);
3535
initI18n();
3636

@@ -46,7 +46,7 @@ export default class AttachmentManagementPlugin extends Plugin {
4646
}
4747
menu.addItem((item) => {
4848
item
49-
.setTitle(t('override.menuTitle'))
49+
.setTitle(t("override.menuTitle"))
5050
.setIcon("image-plus")
5151
.onClick(async () => {
5252
const { setting } = getOverrideSetting(this.settings, file);
@@ -142,7 +142,7 @@ export default class AttachmentManagementPlugin extends Plugin {
142142
if (file instanceof TFile) {
143143
if (file.parent && isExcluded(file.parent.path, this.settings)) {
144144
debugLog("rename - exclude path:", file.parent.path);
145-
new Notice(t('notifications.fileExcluded', { path: file.path }));
145+
new Notice(t("notifications.fileExcluded", { path: file.path }));
146146
return;
147147
}
148148

@@ -160,7 +160,11 @@ export default class AttachmentManagementPlugin extends Plugin {
160160

161161
const oldMetadata = getMetadata(oldPath);
162162
// if the user have used the ${date} in `Attachment path` this could be not working, since the date will be change.
163-
const oldAttachPath = oldMetadata.getAttachmentPath(setting, this.settings.dateFormat);
163+
const oldAttachPath = await oldMetadata.getAttachmentPath(
164+
setting,
165+
this.settings.dateFormat,
166+
this.app.vault.adapter
167+
);
164168
this.app.vault.adapter.exists(oldAttachPath, true).then((exists) => {
165169
if (exists) {
166170
checkEmptyFolder(this.app.vault.adapter, oldAttachPath).then((empty) => {
@@ -194,7 +198,11 @@ export default class AttachmentManagementPlugin extends Plugin {
194198
if (file instanceof TFile) {
195199
const oldMetadata = getMetadata(file.path);
196200
const { setting } = getOverrideSetting(this.settings, file);
197-
const oldAttachPath = oldMetadata.getAttachmentPath(setting, this.settings.dateFormat);
201+
const oldAttachPath = await oldMetadata.getAttachmentPath(
202+
setting,
203+
this.settings.dateFormat,
204+
this.app.vault.adapter
205+
);
198206
this.app.vault.adapter.exists(oldAttachPath, true).then((exists) => {
199207
if (exists) {
200208
checkEmptyFolder(this.app.vault.adapter, oldAttachPath).then((empty) => {
@@ -231,18 +239,18 @@ export default class AttachmentManagementPlugin extends Plugin {
231239
initCommands() {
232240
this.addCommand({
233241
id: "attachment-management-rearrange-all-links",
234-
name: t('commands.rearrangeAllLinks'),
242+
name: t("commands.rearrangeAllLinks"),
235243
callback: async () => {
236244
new ConfirmModal(this).open();
237245
},
238246
});
239247

240248
this.addCommand({
241249
id: "attachment-management-rearrange-active-links",
242-
name: t('commands.rearrangeActiveLinks'),
250+
name: t("commands.rearrangeActiveLinks"),
243251
callback: async () => {
244252
new ArrangeHandler(this.settings, this.app, this).rearrangeAttachment(RearrangeType.ACTIVE).finally(() => {
245-
new Notice(t('notifications.arrangeCompleted'));
253+
new Notice(t("notifications.arrangeCompleted"));
246254
});
247255
},
248256
});
@@ -275,7 +283,7 @@ export default class AttachmentManagementPlugin extends Plugin {
275283

276284
this.addCommand({
277285
id: "attachment-management-reset-override-setting",
278-
name: t('commands.resetOverrideSetting'),
286+
name: t("commands.resetOverrideSetting"),
279287
checkCallback: (checking: boolean) => {
280288
const file = getActiveFile(this.app);
281289
if (file) {
@@ -290,7 +298,7 @@ export default class AttachmentManagementPlugin extends Plugin {
290298
}
291299
delete this.settings.overridePath[file.path];
292300
this.saveSettings().finally(() => {
293-
new Notice(t('notifications.resetAttachmentSetting', { path: file.path }));
301+
new Notice(t("notifications.resetAttachmentSetting", { path: file.path }));
294302
});
295303
}
296304
return true;
@@ -301,7 +309,7 @@ export default class AttachmentManagementPlugin extends Plugin {
301309

302310
this.addCommand({
303311
id: "attachment-management-clear-unused-originalname-storage",
304-
name: t('commands.clearUnusedStorage'),
312+
name: t("commands.clearUnusedStorage"),
305313
callback: async () => {
306314
const attachments = await new ArrangeHandler(this.settings, this.app, this).getAttachmentsInVault(
307315
this.settings,

src/model/extensionOverride.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,6 @@ import {
44
SETTINGS_ROOT_INFOLDER,
55
SETTINGS_ROOT_NEXTTONOTE,
66
SETTINGS_ROOT_OBSFOLDER,
7-
SETTINGS_VARIABLES_DATES,
8-
SETTINGS_VARIABLES_MD5,
9-
SETTINGS_VARIABLES_NOTENAME,
10-
SETTINGS_VARIABLES_NOTEPARENT,
11-
SETTINGS_VARIABLES_NOTEPATH,
12-
SETTINGS_VARIABLES_ORIGINALNAME,
137
} from "../lib/constant";
148
import AttachmentManagementPlugin from "../main";
159
import { AttachmentPathSettings, DEFAULT_SETTINGS, ExtensionOverrideSettings } from "../settings/settings";

src/model/override.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,6 @@ import {
44
SETTINGS_ROOT_OBSFOLDER,
55
SETTINGS_ROOT_INFOLDER,
66
SETTINGS_ROOT_NEXTTONOTE,
7-
SETTINGS_VARIABLES_NOTEPATH,
8-
SETTINGS_VARIABLES_NOTENAME,
9-
SETTINGS_VARIABLES_DATES,
10-
SETTINGS_VARIABLES_NOTEPARENT,
11-
SETTINGS_VARIABLES_ORIGINALNAME,
12-
SETTINGS_VARIABLES_MD5,
137
} from "../lib/constant";
148
import AttachmentManagementPlugin from "../main";
159
import { OverrideExtensionModal } from "./extensionOverride";

0 commit comments

Comments
 (0)