-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patharchive.ts
More file actions
52 lines (37 loc) · 1.45 KB
/
archive.ts
File metadata and controls
52 lines (37 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import { Notice, moment, normalizePath, TFile, type App } from 'obsidian';
import { ensureFolder, getUniquePath } from './vault-utils';
import { updateFrontmatter } from './frontmatter';
export async function archiveArticle(app: App, file: TFile) {
const cache = app.metadataCache.getFileCache(file);
const frontmatter = cache?.frontmatter as Record<string, unknown> | undefined;
const archivedValue = frontmatter?.archived;
if (archivedValue) {
new Notice('Article already archived.');
return false;
}
const parent = file.parent?.path ?? '';
const archivePath = normalizePath(parent ? `${parent}/Archive` : 'Archive');
try {
await ensureFolder(app, archivePath);
} catch (error) {
console.error('Failed to create archive folder', error);
new Notice('Failed to create archive folder.');
return false;
}
const targetPath = getUniquePath(app, normalizePath(`${archivePath}/${file.name}`));
try {
await app.vault.rename(file, targetPath);
} catch (error) {
console.error('Failed to move file to archive', error);
new Notice('Failed to archive article.');
return false;
}
const movedFile = app.vault.getAbstractFileByPath(targetPath);
if (movedFile instanceof TFile) {
await updateFrontmatter(app, movedFile, {
archived: moment().toDate(),
});
}
new Notice('Article archived.');
return true;
}