mirror of
https://github.com/Steffo99/obsidian-file-index.git
synced 2024-11-25 14:34:23 +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
|
# 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.
|
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
|
```json
|
||||||
{
|
{
|
||||||
"paths": [
|
"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]
|
> [#WARNING]
|
||||||
>
|
>
|
||||||
|
|
94
main.ts
94
main.ts
|
@ -8,6 +8,38 @@ export interface SteffoFileIndex {
|
||||||
|
|
||||||
|
|
||||||
export default class SteffoFileIndexPlugin extends Plugin {
|
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"
|
static FILE_INDEX_PATH = "steffo-file-index.json"
|
||||||
|
|
||||||
async recreateFileIndex() {
|
async recreateFileIndex() {
|
||||||
|
@ -17,6 +49,17 @@ export default class SteffoFileIndexPlugin extends Plugin {
|
||||||
const paths = []
|
const paths = []
|
||||||
|
|
||||||
for(const file of files) {
|
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)
|
paths.push(file.path)
|
||||||
basenames[file.basename] = file.path
|
basenames[file.basename] = file.path
|
||||||
}
|
}
|
||||||
|
@ -24,40 +67,65 @@ export default class SteffoFileIndexPlugin extends Plugin {
|
||||||
paths.sort()
|
paths.sort()
|
||||||
|
|
||||||
const index: SteffoFileIndex = {basenames, paths}
|
const index: SteffoFileIndex = {basenames, paths}
|
||||||
|
console.debug("[SteffoFileIndexPlugin] Determined index to be:", index)
|
||||||
|
|
||||||
const indexContents = JSON.stringify(index, null, "\t")
|
const indexContents = JSON.stringify(index, null, "\t")
|
||||||
|
|
||||||
const indexFile = this.app.vault.getAbstractFileByPath(SteffoFileIndexPlugin.FILE_INDEX_PATH)
|
const indexFile = this.app.vault.getAbstractFileByPath(SteffoFileIndexPlugin.FILE_INDEX_PATH)
|
||||||
if(indexFile instanceof TFolder) {
|
if(indexFile === null) {
|
||||||
console.error("Cannot create file index, as there's a folder at:", SteffoFileIndexPlugin.FILE_INDEX_PATH)
|
console.debug("[SteffoFileIndexPlugin] File index does not exist, creating it right now at:", SteffoFileIndexPlugin.FILE_INDEX_PATH)
|
||||||
return
|
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) {
|
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)
|
await this.app.vault.modify(indexFile, indexContents)
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
console.info("File index does not exist, creating it right now at:", SteffoFileIndexPlugin.FILE_INDEX_PATH)
|
console.error("[SteffoFileIndexPlugin] File index is of an unknown type, not doing anything:", SteffoFileIndexPlugin.FILE_INDEX_PATH)
|
||||||
await this.app.vault.create(SteffoFileIndexPlugin.FILE_INDEX_PATH, indexContents)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
recreateFileIndexBinding = this.recreateFileIndex.bind(this)
|
#reloadIgnoreRegExpsIfIgnoreFileChangedBinding = this.reloadIgnoreRegExpsIfIgnoreFileChanged.bind(this)
|
||||||
|
#recreateFileIndexBinding = this.recreateFileIndex.bind(this)
|
||||||
|
|
||||||
async onload() {
|
async onload() {
|
||||||
|
|
||||||
this.addCommand({
|
this.addCommand({
|
||||||
id: 'steffo-file-index-recreate',
|
id: 'steffo-file-index-recreate',
|
||||||
name: 'Force file index recreation',
|
name: 'Force file index recreation',
|
||||||
callback: this.recreateFileIndex.bind(this)
|
callback: this.recreateFileIndex.bind(this)
|
||||||
})
|
})
|
||||||
|
|
||||||
this.app.vault.on("create", this.recreateFileIndexBinding)
|
this.app.workspace.onLayoutReady(async () => {
|
||||||
this.app.vault.on("delete", this.recreateFileIndexBinding)
|
await this.reloadIgnoreRegExps()
|
||||||
this.app.vault.on("rename", this.recreateFileIndexBinding)
|
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() {
|
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==",
|
"integrity": "sha512-dsKNQNdj6xA3T+QlADDA7mOSlX0qiMINjn0cgr+eGHGsbSHzTabcIogz2+p/iqP1Xs6EP/sS2SbqH+brGTbq0g==",
|
||||||
"dev": true
|
"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": {
|
"node_modules/glob-parent": {
|
||||||
"version": "6.0.2",
|
"version": "6.0.2",
|
||||||
"resolved": "https://registry.npmjs.org/glob-parent/-/glob-parent-6.0.2.tgz",
|
"resolved": "https://registry.npmjs.org/glob-parent/-/glob-parent-6.0.2.tgz",
|
||||||
|
@ -1965,6 +1944,27 @@
|
||||||
"url": "https://github.com/sponsors/isaacs"
|
"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": {
|
"node_modules/run-parallel": {
|
||||||
"version": "1.2.0",
|
"version": "1.2.0",
|
||||||
"resolved": "https://registry.npmjs.org/run-parallel/-/run-parallel-1.2.0.tgz",
|
"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",
|
"build": "tsc -noEmit -skipLibCheck && node esbuild.config.mjs production",
|
||||||
"version": "node version-bump.mjs && git add manifest.json versions.json"
|
"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>",
|
"author": "Stefano Pigozzi <me@steffo.eu>",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
|
Loading…
Reference in a new issue