Skip to content

Commit 3d08672

Browse files
committed
Added obsidian plugin for name highlight
1 parent 8265acc commit 3d08672

File tree

3 files changed

+145
-0
lines changed

3 files changed

+145
-0
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
const obsidian = require('obsidian');
2+
3+
class TitleAppenderPlugin extends obsidian.Plugin {
4+
async onload() {
5+
console.log('Loading TitleAppender plugin');
6+
7+
// Create stylesheet element only once during initialization
8+
try {
9+
this.styleEl = document.head.createEl('style');
10+
this.styleEl.id = 'title-appender-styles';
11+
} catch (error) {
12+
console.error('Error creating style element:', error);
13+
return;
14+
}
15+
16+
// Wait for layout to be ready
17+
this.app.workspace.onLayoutReady(() => {
18+
this.registerEvents();
19+
this.updateAllTitles();
20+
});
21+
}
22+
23+
registerEvents() {
24+
// Update styles when files change
25+
this.registerEvent(
26+
this.app.metadataCache.on('changed', () => {
27+
this.updateAllTitles();
28+
})
29+
);
30+
31+
// Update on file explorer changes
32+
this.registerEvent(
33+
this.app.workspace.on('file-menu', () => {
34+
this.updateAllTitles();
35+
})
36+
);
37+
38+
// Update when layout changes
39+
this.registerEvent(
40+
this.app.workspace.on('layout-change', () => {
41+
this.updateAllTitles();
42+
})
43+
);
44+
}
45+
46+
updateAllTitles() {
47+
try {
48+
// Get all markdown files
49+
const files = this.app.vault.getMarkdownFiles();
50+
let cssRules = [];
51+
52+
// Create CSS rules for each file with a frontmatter title
53+
files.forEach(file => {
54+
try {
55+
const metadata = this.app.metadataCache.getFileCache(file);
56+
const title = metadata?.frontmatter?.title;
57+
58+
if (title) {
59+
const escapedPath = CSS.escape(file.path);
60+
const escapedTitle = title.replace(/"/g, '\\"');
61+
62+
cssRules.push(`
63+
.nav-file-title[data-path="${escapedPath}"] .nav-file-title-content::after {
64+
content: " (${escapedTitle})";
65+
color: var(--color-orange);
66+
font-size: 0.85em;
67+
opacity: 0.8;
68+
}
69+
`);
70+
}
71+
} catch (e) {
72+
console.error('Error processing file:', file?.path, e);
73+
}
74+
});
75+
76+
// Update stylesheet content
77+
if (this.styleEl) {
78+
this.styleEl.textContent = cssRules.join("\n");
79+
}
80+
} catch (error) {
81+
console.error('Error updating titles:', error);
82+
}
83+
}
84+
85+
onunload() {
86+
console.log('Unloading TitleAppender plugin');
87+
if (this.styleEl) {
88+
this.styleEl.remove();
89+
}
90+
}
91+
}
92+
93+
module.exports = TitleAppenderPlugin;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"id": "docs-viewer",
3+
"name": "docs-viewer",
4+
"version": "2.9.2",
5+
"minAppVersion": "1.1.6",
6+
"description": "An Obsidian plugin to edit and view Docs-Viewer metadata",
7+
"author": "Litruv",
8+
"authorUrl": "https://lit.ruv.wtf",
9+
"fundingUrl": "https://ko-fi.com/zsolt",
10+
"helpUrl": "https://github.com/zsviczian/obsidian-excalidraw-plugin#readme",
11+
"isDesktopOnly": false
12+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
.title-appended {
2+
color: var(--text-normal);
3+
display: inline;
4+
}
5+
6+
.title-metadata {
7+
color: var(--color-orange);
8+
font-size: 0.85em;
9+
opacity: 0.8;
10+
margin-left: 0;
11+
}
12+
13+
/* Icon and title styling */
14+
.title-icon {
15+
margin-right: 5px;
16+
font-size: 0.9em;
17+
color: var(--color-orange);
18+
flex-shrink: 0;
19+
}
20+
21+
.has-icon {
22+
display: flex;
23+
align-items: center;
24+
}
25+
26+
27+
28+
.nav-file-title-content {
29+
padding: 0;
30+
line-height: 1.1;
31+
white-space: nowrap;
32+
overflow: hidden;
33+
text-overflow: ellipsis;
34+
}
35+
36+
/* Reduce indent for child items */
37+
.nav-folder .nav-folder .nav-file-title,
38+
.nav-folder .nav-folder .nav-folder-title {
39+
padding-left: 12px !important;
40+
}

0 commit comments

Comments
 (0)