-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
89 lines (87 loc) · 7.79 KB
/
main.js
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
/*
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);
// main.ts
var main_exports = {};
__export(main_exports, {
default: () => HeadingDisplayPlugin
});
module.exports = __toCommonJS(main_exports);
var import_obsidian = require("obsidian");
var HeadingDisplayPlugin = class extends import_obsidian.Plugin {
async onload() {
console.log("Heading Display Plugin loaded!");
this.registerEvent(
this.app.vault.on("modify", this.updateFileExplorerDisplay.bind(this))
);
this.app.workspace.onLayoutReady(() => {
this.updateFileExplorerDisplay();
});
}
onunload() {
console.log("Heading Display Plugin unloaded!");
}
/**
* Updates the File Explorer to show the first heading, or falls back to the filename.
*/
async updateFileExplorerDisplay() {
const files = this.app.vault.getFiles();
for (const file of files) {
if (!(file instanceof import_obsidian.TFile))
continue;
const content = await this.app.vault.cachedRead(file);
const contentWithoutYaml = this.removeYamlHeader(content);
const firstHeading = this.getFirstHeading(contentWithoutYaml);
const fileExplorerElement = document.querySelector(
`[data-path="${file.path}"] .nav-file-title-content`
);
if (fileExplorerElement) {
fileExplorerElement.textContent = firstHeading || file.basename;
}
}
}
/**
* Removes the YAML header from the file content.
*
* @param {string} content - The content of the file.
* @returns {string} - The content without the YAML header.
*/
removeYamlHeader(content) {
if (content.startsWith("---")) {
const yamlEnd = content.indexOf("\n---", 3);
if (yamlEnd !== -1) {
return content.slice(yamlEnd + 4).trim();
}
}
return content;
}
/**
* Extracts the first heading from the file content.
*
* @param {string} content - The content of the file.
* @returns {string | null} - The first heading found, or null if none exists.
*/
getFirstHeading(content) {
const match = content.match(/^\s*#+\s+(.*)/m);
return match ? match[1].trim() : null;
}
};
//# sourceMappingURL=data:application/json;base64,ewogICJ2ZXJzaW9uIjogMywKICAic291cmNlcyI6IFsibWFpbi50cyJdLAogICJzb3VyY2VzQ29udGVudCI6IFsiaW1wb3J0IHsgUGx1Z2luLCBURmlsZSB9IGZyb20gXCJvYnNpZGlhblwiO1xuXG5leHBvcnQgZGVmYXVsdCBjbGFzcyBIZWFkaW5nRGlzcGxheVBsdWdpbiBleHRlbmRzIFBsdWdpbiB7XG4gIGFzeW5jIG9ubG9hZCgpIHtcbiAgICBjb25zb2xlLmxvZyhcIkhlYWRpbmcgRGlzcGxheSBQbHVnaW4gbG9hZGVkIVwiKTtcblxuICAgIC8vIFVwZGF0ZSB0aGUgRmlsZSBFeHBsb3JlciB3aGVuIGEgZmlsZSBpcyBtb2RpZmllZFxuICAgIHRoaXMucmVnaXN0ZXJFdmVudChcbiAgICAgIHRoaXMuYXBwLnZhdWx0Lm9uKFwibW9kaWZ5XCIsIHRoaXMudXBkYXRlRmlsZUV4cGxvcmVyRGlzcGxheS5iaW5kKHRoaXMpKVxuICAgICk7XG5cbiAgICAvLyBQZXJmb3JtIGluaXRpYWwgdXBkYXRlcyB3aGVuIHRoZSB3b3Jrc3BhY2UgbGF5b3V0IGlzIHJlYWR5XG4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