generated from obsidianmd/obsidian-sample-plugin
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.ts
More file actions
143 lines (116 loc) · 4.29 KB
/
main.ts
File metadata and controls
143 lines (116 loc) · 4.29 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
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import {Plugin, TFile, TFolder} from "obsidian"
export interface FileIndex {
basenames: {[basename: string]: string},
paths: string[],
}
export default class FileIndexPlugin extends Plugin {
static FILE_IGNORE_PATH = "file-index-ignore.json"
ignoreRegExps: RegExp[] = []
async reloadIgnoreRegExps() {
const ignoreFile = this.app.vault.getAbstractFileByPath(FileIndexPlugin.FILE_IGNORE_PATH)
if(ignoreFile === null) {
console.debug("[FileIndexPlugin] Ignore file does not exist, not ignoring anything:", FileIndexPlugin.FILE_IGNORE_PATH)
this.ignoreRegExps = []
}
else if(ignoreFile instanceof TFolder) {
console.debug("[FileIndexPlugin] Ignore file is actually a folder, not ignoring anything:", FileIndexPlugin.FILE_IGNORE_PATH)
this.ignoreRegExps = []
}
else if(ignoreFile instanceof TFile) {
const ignoreJSON = await this.app.vault.cachedRead(ignoreFile)
const ignoreContents: string[] = JSON.parse(ignoreJSON)
this.ignoreRegExps = ignoreContents.map((re) => new RegExp(re))
console.debug("[FileIndexPlugin] Determined ignore list to be:", this.ignoreRegExps)
}
else {
console.error("[FileIndexPlugin] Ignore file is of an unknown type, not doing anything:", FileIndexPlugin.FILE_IGNORE_PATH)
}
}
async reloadIgnoreRegExpsIfIgnoreFileChanged(file: TFile) {
if(file.path === FileIndexPlugin.FILE_IGNORE_PATH) {
await this.reloadIgnoreRegExps()
}
}
static FILE_INDEX_PATH = "file-index.json"
async recreateFileIndex() {
const files = this.app.vault.getFiles()
const basenames: {[basename: string]: string} = {}
const paths = []
for(const file of files) {
let ignored = false
for(const regexp of this.ignoreRegExps) {
if(file.path.match(regexp)) {
ignored = true
break
}
}
if(ignored) {
continue
}
paths.push(file.path)
let basename
if(file.extension === "md") {
basename = file.basename.toLocaleLowerCase()
}
else {
basename = file.basename.toLocaleLowerCase() + "." + file.extension.toLocaleLowerCase()
}
if(basenames.hasOwnProperty(basename)) {
console.warn("[FileIndexPlugin] Multiple files with the same basename detected:", basenames[basename], file.path)
}
basenames[basename] = file.path
}
paths.sort()
const index: FileIndex = {basenames, paths}
console.debug("[FileIndexPlugin] Determined index to be:", index)
const indexContents = JSON.stringify(index, null, "\t")
const indexFile = this.app.vault.getAbstractFileByPath(FileIndexPlugin.FILE_INDEX_PATH)
if(indexFile === null) {
console.debug("[FileIndexPlugin] File index does not exist, creating it right now at:", FileIndexPlugin.FILE_INDEX_PATH)
await this.app.vault.create(FileIndexPlugin.FILE_INDEX_PATH, indexContents)
}
else if(indexFile instanceof TFolder) {
console.debug("[FileIndexPlugin] Cannot create file index, as there's a folder at:", FileIndexPlugin.FILE_INDEX_PATH)
}
else if(indexFile instanceof TFile) {
console.debug("[FileIndexPlugin] File index already exists, overwriting contents of:", FileIndexPlugin.FILE_INDEX_PATH)
await this.app.vault.modify(indexFile, indexContents)
}
else {
console.error("[FileIndexPlugin] File index is of an unknown type, not doing anything:", FileIndexPlugin.FILE_INDEX_PATH)
}
}
#reloadIgnoreRegExpsIfIgnoreFileChangedBinding = this.reloadIgnoreRegExpsIfIgnoreFileChanged.bind(this)
#recreateFileIndexBinding = this.recreateFileIndex.bind(this)
async onload() {
this.addCommand({
id: 'recreate',
name: 'Force file index recreation',
callback: this.recreateFileIndex.bind(this)
})
this.app.workspace.onLayoutReady(async () => {
await this.reloadIgnoreRegExps()
await this.recreateFileIndex()
this.registerEvent(
this.app.vault.on("create", this.#reloadIgnoreRegExpsIfIgnoreFileChangedBinding)
)
this.registerEvent(
this.app.vault.on("delete", this.#reloadIgnoreRegExpsIfIgnoreFileChangedBinding)
)
this.registerEvent(
this.app.vault.on("rename", this.#reloadIgnoreRegExpsIfIgnoreFileChangedBinding)
)
this.registerEvent(
this.app.vault.on("create", this.#recreateFileIndexBinding)
)
this.registerEvent(
this.app.vault.on("delete", this.#recreateFileIndexBinding)
)
this.registerEvent(
this.app.vault.on("rename", this.#recreateFileIndexBinding)
)
})
}
onunload() {
}
}