1
Fork 0
mirror of https://github.com/Steffo99/appunti-magistrali.git synced 2024-11-24 19:24:19 +00:00
appunti-steffo/.obsidian/plugins/file-index/main.js

155 lines
6.4 KiB
JavaScript
Raw Normal View History

2023-10-29 01:31:29 +00:00
/*
THIS IS A GENERATED/BUNDLED FILE BY ESBUILD
if you want to view the source, please visit the github repository of this plugin
*/
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var __accessCheck = (obj, member, msg) => {
if (!member.has(obj))
throw TypeError("Cannot " + msg);
};
var __privateGet = (obj, member, getter) => {
__accessCheck(obj, member, "read from private field");
return getter ? getter.call(obj) : member.get(obj);
};
var __privateAdd = (obj, member, value) => {
if (member.has(obj))
throw TypeError("Cannot add the same private member more than once");
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
};
// main.ts
var main_exports = {};
__export(main_exports, {
2023-11-17 14:23:05 +00:00
default: () => FileIndexPlugin
2023-10-29 01:31:29 +00:00
});
module.exports = __toCommonJS(main_exports);
var import_obsidian = require("obsidian");
var _reloadIgnoreRegExpsIfIgnoreFileChangedBinding, _recreateFileIndexBinding;
2023-11-17 14:23:05 +00:00
var _FileIndexPlugin = class extends import_obsidian.Plugin {
2023-10-29 01:31:29 +00:00
constructor() {
super(...arguments);
this.ignoreRegExps = [];
__privateAdd(this, _reloadIgnoreRegExpsIfIgnoreFileChangedBinding, this.reloadIgnoreRegExpsIfIgnoreFileChanged.bind(this));
__privateAdd(this, _recreateFileIndexBinding, this.recreateFileIndex.bind(this));
}
async reloadIgnoreRegExps() {
2023-11-17 14:23:05 +00:00
const ignoreFile = this.app.vault.getAbstractFileByPath(_FileIndexPlugin.FILE_IGNORE_PATH);
2023-10-29 01:31:29 +00:00
if (ignoreFile === null) {
2023-11-17 14:23:05 +00:00
console.debug("[FileIndexPlugin] Ignore file does not exist, not ignoring anything:", _FileIndexPlugin.FILE_IGNORE_PATH);
2023-10-29 01:31:29 +00:00
this.ignoreRegExps = [];
} else if (ignoreFile instanceof import_obsidian.TFolder) {
2023-11-17 14:23:05 +00:00
console.debug("[FileIndexPlugin] Ignore file is actually a folder, not ignoring anything:", _FileIndexPlugin.FILE_IGNORE_PATH);
2023-10-29 01:31:29 +00:00
this.ignoreRegExps = [];
} else if (ignoreFile instanceof import_obsidian.TFile) {
const ignoreJSON = await this.app.vault.cachedRead(ignoreFile);
const ignoreContents = JSON.parse(ignoreJSON);
this.ignoreRegExps = ignoreContents.map((re) => new RegExp(re));
2023-11-17 14:23:05 +00:00
console.debug("[FileIndexPlugin] Determined ignore list to be:", this.ignoreRegExps);
2023-10-29 01:31:29 +00:00
} else {
2023-11-17 14:23:05 +00:00
console.error("[FileIndexPlugin] Ignore file is of an unknown type, not doing anything:", _FileIndexPlugin.FILE_IGNORE_PATH);
2023-10-29 01:31:29 +00:00
}
}
async reloadIgnoreRegExpsIfIgnoreFileChanged(file) {
2023-11-17 14:23:05 +00:00
if (file.path === _FileIndexPlugin.FILE_IGNORE_PATH) {
2023-10-29 01:31:29 +00:00
await this.reloadIgnoreRegExps();
}
}
async recreateFileIndex() {
const files = this.app.vault.getFiles();
const basenames = {};
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);
2023-11-17 14:23:05 +00:00
let basename;
if (file.extension === "md") {
basename = file.basename.toLocaleLowerCase();
} else {
basename = file.basename.toLocaleLowerCase() + "." + file.extension.toLocaleLowerCase();
}
if (basenames.hasOwnProperty(basename)) {
2023-11-17 14:23:05 +00:00
console.warn("[FileIndexPlugin] Multiple files with the same basename detected:", basenames[basename], file.path);
}
basenames[basename] = file.path;
2023-10-29 01:31:29 +00:00
}
paths.sort();
const index = { basenames, paths };
2023-11-17 14:23:05 +00:00
console.debug("[FileIndexPlugin] Determined index to be:", index);
2023-10-29 01:31:29 +00:00
const indexContents = JSON.stringify(index, null, " ");
2023-11-17 14:23:05 +00:00
const indexFile = this.app.vault.getAbstractFileByPath(_FileIndexPlugin.FILE_INDEX_PATH);
2023-10-29 01:31:29 +00:00
if (indexFile === null) {
2023-11-17 14:23:05 +00:00
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);
2023-10-29 01:31:29 +00:00
} else if (indexFile instanceof import_obsidian.TFolder) {
2023-11-17 14:23:05 +00:00
console.debug("[FileIndexPlugin] Cannot create file index, as there's a folder at:", _FileIndexPlugin.FILE_INDEX_PATH);
2023-10-29 01:31:29 +00:00
} else if (indexFile instanceof import_obsidian.TFile) {
2023-11-17 14:23:05 +00:00
console.debug("[FileIndexPlugin] File index already exists, overwriting contents of:", _FileIndexPlugin.FILE_INDEX_PATH);
2023-10-29 01:31:29 +00:00
await this.app.vault.modify(indexFile, indexContents);
} else {
2023-11-17 14:23:05 +00:00
console.error("[FileIndexPlugin] File index is of an unknown type, not doing anything:", _FileIndexPlugin.FILE_INDEX_PATH);
2023-10-29 01:31:29 +00:00
}
}
async onload() {
this.addCommand({
2023-11-17 14:23:05 +00:00
id: "recreate",
2023-10-29 01:31:29 +00:00
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", __privateGet(this, _reloadIgnoreRegExpsIfIgnoreFileChangedBinding))
);
this.registerEvent(
this.app.vault.on("delete", __privateGet(this, _reloadIgnoreRegExpsIfIgnoreFileChangedBinding))
);
this.registerEvent(
this.app.vault.on("rename", __privateGet(this, _reloadIgnoreRegExpsIfIgnoreFileChangedBinding))
);
this.registerEvent(
this.app.vault.on("create", __privateGet(this, _recreateFileIndexBinding))
);
this.registerEvent(
this.app.vault.on("delete", __privateGet(this, _recreateFileIndexBinding))
);
this.registerEvent(
this.app.vault.on("rename", __privateGet(this, _recreateFileIndexBinding))
);
});
}
onunload() {
}
};
2023-11-17 14:23:05 +00:00
var FileIndexPlugin = _FileIndexPlugin;
2023-10-29 01:31:29 +00:00
_reloadIgnoreRegExpsIfIgnoreFileChangedBinding = new WeakMap();
_recreateFileIndexBinding = new WeakMap();
2023-11-17 14:23:05 +00:00
FileIndexPlugin.FILE_IGNORE_PATH = "file-index-ignore.json";
FileIndexPlugin.FILE_INDEX_PATH = "file-index.json";