1
Fork 0
mirror of https://github.com/Steffo99/obsidian-file-index.git synced 2024-11-25 14:34:23 +00:00
obsidian-file-index/main.ts

132 lines
4.1 KiB
TypeScript
Raw Normal View History

2023-10-28 17:37:02 +00:00
import {Plugin, TFile, TFolder} from "obsidian"
2023-10-28 16:05:49 +00:00
2023-10-28 17:37:02 +00:00
export interface SteffoFileIndex {
basenames: {[basename: string]: string},
paths: string[],
2023-10-28 16:05:49 +00:00
}
2023-10-28 17:37:02 +00:00
export default class SteffoFileIndexPlugin extends Plugin {
2023-10-29 00:55:41 +00:00
static FILE_IGNORE_PATH = "steffo-file-index-ignore.json"
ignoreRegExps: RegExp[] = []
async reloadIgnoreRegExps() {
const ignoreFile = this.app.vault.getAbstractFileByPath(SteffoFileIndexPlugin.FILE_IGNORE_PATH)
if(ignoreFile === null) {
console.debug("[SteffoFileIndexPlugin] Ignore file does not exist, not ignoring anything:", SteffoFileIndexPlugin.FILE_IGNORE_PATH)
this.ignoreRegExps = []
}
else if(ignoreFile instanceof TFolder) {
console.debug("[SteffoFileIndexPlugin] Ignore file is actually a folder, not ignoring anything:", SteffoFileIndexPlugin.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("[SteffoFileIndexPlugin] Determined ignore list to be:", this.ignoreRegExps)
}
else {
console.error("[SteffoFileIndexPlugin] Ignore file is of an unknown type, not doing anything:", SteffoFileIndexPlugin.FILE_IGNORE_PATH)
}
}
async reloadIgnoreRegExpsIfIgnoreFileChanged(file: TFile) {
if(file.path === SteffoFileIndexPlugin.FILE_IGNORE_PATH) {
await this.reloadIgnoreRegExps()
}
}
2023-10-28 17:37:02 +00:00
static FILE_INDEX_PATH = "steffo-file-index.json"
2023-10-28 16:05:49 +00:00
2023-10-28 17:37:02 +00:00
async recreateFileIndex() {
const files = this.app.vault.getFiles()
2023-10-28 16:05:49 +00:00
2023-10-28 17:37:02 +00:00
const basenames: {[basename: string]: string} = {}
const paths = []
2023-10-28 16:05:49 +00:00
2023-10-28 17:37:02 +00:00
for(const file of files) {
2023-10-29 00:55:41 +00:00
let ignored = false
for(const regexp of this.ignoreRegExps) {
if(file.path.match(regexp)) {
ignored = true
break
}
}
if(ignored) {
continue
}
2023-10-28 17:37:02 +00:00
paths.push(file.path)
basenames[file.basename] = file.path
}
2023-10-28 16:05:49 +00:00
2023-10-28 17:37:02 +00:00
paths.sort()
2023-10-28 16:05:49 +00:00
2023-10-28 17:37:02 +00:00
const index: SteffoFileIndex = {basenames, paths}
2023-10-29 00:55:41 +00:00
console.debug("[SteffoFileIndexPlugin] Determined index to be:", index)
2023-10-28 17:37:02 +00:00
const indexContents = JSON.stringify(index, null, "\t")
2023-10-28 16:05:49 +00:00
2023-10-28 17:37:02 +00:00
const indexFile = this.app.vault.getAbstractFileByPath(SteffoFileIndexPlugin.FILE_INDEX_PATH)
2023-10-29 00:55:41 +00:00
if(indexFile === null) {
console.debug("[SteffoFileIndexPlugin] File index does not exist, creating it right now at:", SteffoFileIndexPlugin.FILE_INDEX_PATH)
await this.app.vault.create(SteffoFileIndexPlugin.FILE_INDEX_PATH, indexContents)
}
else if(indexFile instanceof TFolder) {
console.debug("[SteffoFileIndexPlugin] Cannot create file index, as there's a folder at:", SteffoFileIndexPlugin.FILE_INDEX_PATH)
2023-10-28 17:37:02 +00:00
}
else if(indexFile instanceof TFile) {
2023-10-29 00:55:41 +00:00
console.debug("[SteffoFileIndexPlugin] File index already exists, overwriting contents of:", SteffoFileIndexPlugin.FILE_INDEX_PATH)
2023-10-28 17:37:02 +00:00
await this.app.vault.modify(indexFile, indexContents)
}
else {
2023-10-29 00:55:41 +00:00
console.error("[SteffoFileIndexPlugin] File index is of an unknown type, not doing anything:", SteffoFileIndexPlugin.FILE_INDEX_PATH)
2023-10-28 17:37:02 +00:00
}
2023-10-28 16:05:49 +00:00
}
2023-10-29 00:55:41 +00:00
#reloadIgnoreRegExpsIfIgnoreFileChangedBinding = this.reloadIgnoreRegExpsIfIgnoreFileChanged.bind(this)
#recreateFileIndexBinding = this.recreateFileIndex.bind(this)
2023-10-28 16:05:49 +00:00
2023-10-28 17:37:02 +00:00
async onload() {
2023-10-29 00:55:41 +00:00
2023-10-28 17:37:02 +00:00
this.addCommand({
id: 'steffo-file-index-recreate',
name: 'Force file index recreation',
callback: this.recreateFileIndex.bind(this)
})
2023-10-28 16:05:49 +00:00
2023-10-29 00:55:41 +00:00
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)
)
})
2023-10-28 16:05:49 +00:00
}
2023-10-28 17:37:02 +00:00
onunload() {
2023-10-29 00:55:41 +00:00
2023-10-28 16:05:49 +00:00
}
}