mirror of
https://github.com/Steffo99/obsidian-file-index.git
synced 2024-11-21 20:54:18 +00:00
Add ignore file feature
This commit is contained in:
parent
21d236e9ba
commit
d330c83a78
4 changed files with 127 additions and 36 deletions
20
README.md
20
README.md
|
@ -1,7 +1,11 @@
|
|||
# Obsidian File Index
|
||||
|
||||
## About
|
||||
|
||||
This [Obsidian] plugin creates and keeps updated a `steffo-file-index.json` file at the root of your Vault, containing a list of all file paths and a map of all basenames to file paths.
|
||||
|
||||
Useful to externally render Wikilinks with no knowledge of the file structure of the vault, for example in [Obsiview].
|
||||
|
||||
```json
|
||||
{
|
||||
"paths": [
|
||||
|
@ -16,7 +20,21 @@ This [Obsidian] plugin creates and keeps updated a `steffo-file-index.json` file
|
|||
}
|
||||
```
|
||||
|
||||
Useful to externally render Wikilinks with no knowledge of the file structure of the vault, for example in [Obsiview].
|
||||
[Obsidian]: https://obsidian.md/
|
||||
[Obsiview]: https://github.com/Steffo99/obsiview
|
||||
|
||||
## Ignore
|
||||
|
||||
Files can be excluded by the index by adding a `steffo-file-index-ignore.json` file at the root of your Vault, containing a JSON array of regular expressions that will prevent matching files from being added to the list:
|
||||
|
||||
```json
|
||||
[
|
||||
"^Garas",
|
||||
".*HelloWorld.*"
|
||||
]
|
||||
```
|
||||
|
||||
## Known issues
|
||||
|
||||
> [#WARNING]
|
||||
>
|
||||
|
|
94
main.ts
94
main.ts
|
@ -8,6 +8,38 @@ export interface SteffoFileIndex {
|
|||
|
||||
|
||||
export default class SteffoFileIndexPlugin extends Plugin {
|
||||
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()
|
||||
}
|
||||
}
|
||||
|
||||
static FILE_INDEX_PATH = "steffo-file-index.json"
|
||||
|
||||
async recreateFileIndex() {
|
||||
|
@ -17,6 +49,17 @@ export default class SteffoFileIndexPlugin extends Plugin {
|
|||
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)
|
||||
basenames[file.basename] = file.path
|
||||
}
|
||||
|
@ -24,40 +67,65 @@ export default class SteffoFileIndexPlugin extends Plugin {
|
|||
paths.sort()
|
||||
|
||||
const index: SteffoFileIndex = {basenames, paths}
|
||||
console.debug("[SteffoFileIndexPlugin] Determined index to be:", index)
|
||||
|
||||
const indexContents = JSON.stringify(index, null, "\t")
|
||||
|
||||
const indexFile = this.app.vault.getAbstractFileByPath(SteffoFileIndexPlugin.FILE_INDEX_PATH)
|
||||
if(indexFile instanceof TFolder) {
|
||||
console.error("Cannot create file index, as there's a folder at:", SteffoFileIndexPlugin.FILE_INDEX_PATH)
|
||||
return
|
||||
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)
|
||||
}
|
||||
else if(indexFile instanceof TFile) {
|
||||
console.info("File index already exists, overwriting contents of:", SteffoFileIndexPlugin.FILE_INDEX_PATH)
|
||||
console.debug("[SteffoFileIndexPlugin] File index already exists, overwriting contents of:", SteffoFileIndexPlugin.FILE_INDEX_PATH)
|
||||
await this.app.vault.modify(indexFile, indexContents)
|
||||
}
|
||||
else {
|
||||
console.info("File index does not exist, creating it right now at:", SteffoFileIndexPlugin.FILE_INDEX_PATH)
|
||||
await this.app.vault.create(SteffoFileIndexPlugin.FILE_INDEX_PATH, indexContents)
|
||||
console.error("[SteffoFileIndexPlugin] File index is of an unknown type, not doing anything:", SteffoFileIndexPlugin.FILE_INDEX_PATH)
|
||||
}
|
||||
}
|
||||
|
||||
recreateFileIndexBinding = this.recreateFileIndex.bind(this)
|
||||
#reloadIgnoreRegExpsIfIgnoreFileChangedBinding = this.reloadIgnoreRegExpsIfIgnoreFileChanged.bind(this)
|
||||
#recreateFileIndexBinding = this.recreateFileIndex.bind(this)
|
||||
|
||||
async onload() {
|
||||
|
||||
this.addCommand({
|
||||
id: 'steffo-file-index-recreate',
|
||||
name: 'Force file index recreation',
|
||||
callback: this.recreateFileIndex.bind(this)
|
||||
})
|
||||
|
||||
this.app.vault.on("create", this.recreateFileIndexBinding)
|
||||
this.app.vault.on("delete", this.recreateFileIndexBinding)
|
||||
this.app.vault.on("rename", this.recreateFileIndexBinding)
|
||||
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() {
|
||||
this.app.vault.off("create", this.recreateFileIndexBinding)
|
||||
this.app.vault.off("delete", this.recreateFileIndexBinding)
|
||||
this.app.vault.off("rename", this.recreateFileIndexBinding)
|
||||
|
||||
}
|
||||
}
|
||||
|
|
42
package-lock.json
generated
42
package-lock.json
generated
|
@ -1401,27 +1401,6 @@
|
|||
"integrity": "sha512-dsKNQNdj6xA3T+QlADDA7mOSlX0qiMINjn0cgr+eGHGsbSHzTabcIogz2+p/iqP1Xs6EP/sS2SbqH+brGTbq0g==",
|
||||
"dev": true
|
||||
},
|
||||
"node_modules/glob": {
|
||||
"version": "7.2.3",
|
||||
"resolved": "https://registry.npmjs.org/glob/-/glob-7.2.3.tgz",
|
||||
"integrity": "sha512-nFR0zLpU2YCaRxwoCJvL6UvCH2JFyFVIvwTLsIf21AuHlMskA1hhTdk+LlYJtOlYt9v6dvszD2BGRqBL+iQK9Q==",
|
||||
"dev": true,
|
||||
"peer": true,
|
||||
"dependencies": {
|
||||
"fs.realpath": "^1.0.0",
|
||||
"inflight": "^1.0.4",
|
||||
"inherits": "2",
|
||||
"minimatch": "^3.1.1",
|
||||
"once": "^1.3.0",
|
||||
"path-is-absolute": "^1.0.0"
|
||||
},
|
||||
"engines": {
|
||||
"node": "*"
|
||||
},
|
||||
"funding": {
|
||||
"url": "https://github.com/sponsors/isaacs"
|
||||
}
|
||||
},
|
||||
"node_modules/glob-parent": {
|
||||
"version": "6.0.2",
|
||||
"resolved": "https://registry.npmjs.org/glob-parent/-/glob-parent-6.0.2.tgz",
|
||||
|
@ -1965,6 +1944,27 @@
|
|||
"url": "https://github.com/sponsors/isaacs"
|
||||
}
|
||||
},
|
||||
"node_modules/rimraf/node_modules/glob": {
|
||||
"version": "7.2.3",
|
||||
"resolved": "https://registry.npmjs.org/glob/-/glob-7.2.3.tgz",
|
||||
"integrity": "sha512-nFR0zLpU2YCaRxwoCJvL6UvCH2JFyFVIvwTLsIf21AuHlMskA1hhTdk+LlYJtOlYt9v6dvszD2BGRqBL+iQK9Q==",
|
||||
"dev": true,
|
||||
"peer": true,
|
||||
"dependencies": {
|
||||
"fs.realpath": "^1.0.0",
|
||||
"inflight": "^1.0.4",
|
||||
"inherits": "2",
|
||||
"minimatch": "^3.1.1",
|
||||
"once": "^1.3.0",
|
||||
"path-is-absolute": "^1.0.0"
|
||||
},
|
||||
"engines": {
|
||||
"node": "*"
|
||||
},
|
||||
"funding": {
|
||||
"url": "https://github.com/sponsors/isaacs"
|
||||
}
|
||||
},
|
||||
"node_modules/run-parallel": {
|
||||
"version": "1.2.0",
|
||||
"resolved": "https://registry.npmjs.org/run-parallel/-/run-parallel-1.2.0.tgz",
|
||||
|
|
|
@ -8,7 +8,12 @@
|
|||
"build": "tsc -noEmit -skipLibCheck && node esbuild.config.mjs production",
|
||||
"version": "node version-bump.mjs && git add manifest.json versions.json"
|
||||
},
|
||||
"keywords": ["obsidian", "file", "obsiview", "obsidian-plugin"],
|
||||
"keywords": [
|
||||
"obsidian",
|
||||
"file",
|
||||
"obsiview",
|
||||
"obsidian-plugin"
|
||||
],
|
||||
"author": "Stefano Pigozzi <me@steffo.eu>",
|
||||
"license": "MIT",
|
||||
"devDependencies": {
|
||||
|
|
Loading…
Reference in a new issue