generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.ts
99 lines (91 loc) · 2.49 KB
/
main.ts
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import { Notice, Plugin } from 'obsidian';
const FILENAME = "meta.json";
interface Item {
basename: string,
extension: string,
name: string,
path: string,
ctime: number,
mtime: number
}
export default class Dumper extends Plugin {
normalizeItem(item: Object) {
const {
basename,
extension,
name,
path,
stat: {
ctime,
mtime
}
} = item as any
const backlinks = (this.app.metadataCache as any).getBacklinksForFile(item).data
const backlinkFiles = Object.keys(backlinks)
return {
basename,
extension,
name,
path,
ctime,
mtime,
referencedBy: backlinkFiles
} as Item
}
async dumpMetadata() {
console.log("dumping...")
// TODO: assert this is happening only once concurrently
const input = (this.app.vault as any).fileMap
let ret : Record<string, Item> = {}
const promises = Object.keys(input).map((key) => {
return new Promise<void>((res) => {
setTimeout(() => { // WORKAROUND: don't freeze the main thread while dumping stuff. Good for big (>1k notes) vaults
try { // folders does not provide stat so normalizeItem will fail
if (key === "meta.json") {
throw null // just to finally
}
const value = input[key]
const normalizedValue = this.normalizeItem(value)
let shortKey = normalizedValue.basename
let longKey = normalizedValue.path
if (normalizedValue.extension === "md") {
longKey = longKey.slice(0, longKey.length - normalizedValue.extension.length - 1)
}
ret[longKey] = normalizedValue
if (ret[shortKey] === undefined || ret[shortKey].path.split("/").length > normalizedValue.path.split("/").length) {
ret[shortKey] = normalizedValue
}
}
catch {}
finally {
res()
}
}, 1)
})
})
try {
await Promise.all(promises)
const data = JSON.stringify(ret, null, 2)
await this.app.vault.adapter.write(FILENAME, data)
} catch(e) {
new Notice("Failed to dump metadata. Press Ctrl+Shift+i for details.")
console.error(e)
} finally {
console.log("metadump success")
}
}
async onload() {
console.log('started dumping metadata');
this.addCommand({
id: 'dump-metadata',
name: 'Dump metadata file',
callback: () => {
this.dumpMetadata()
},
});
this.registerInterval(window.setInterval(() => this.dumpMetadata(), 1000 * 300)) // 5 minutes
}
onunload() {
console.log('stopped dumping metadata');
}
}