This commit is contained in:
2025-03-25 00:02:52 +11:00
parent 3d08672ec8
commit 45648e7111
2 changed files with 50 additions and 42 deletions

View File

@@ -4,15 +4,6 @@ class TitleAppenderPlugin extends obsidian.Plugin {
async onload() { async onload() {
console.log('Loading TitleAppender plugin'); console.log('Loading TitleAppender plugin');
// Create stylesheet element only once during initialization
try {
this.styleEl = document.head.createEl('style');
this.styleEl.id = 'title-appender-styles';
} catch (error) {
console.error('Error creating style element:', error);
return;
}
// Wait for layout to be ready // Wait for layout to be ready
this.app.workspace.onLayoutReady(() => { this.app.workspace.onLayoutReady(() => {
this.registerEvents(); this.registerEvents();
@@ -47,36 +38,44 @@ class TitleAppenderPlugin extends obsidian.Plugin {
try { try {
// Get all markdown files // Get all markdown files
const files = this.app.vault.getMarkdownFiles(); const files = this.app.vault.getMarkdownFiles();
let cssRules = [];
// Create CSS rules for each file with a frontmatter title // First, reset all previously set classes and attributes
document.querySelectorAll('.has-sort-value, .has-title').forEach(el => {
el.classList.remove('has-sort-value', 'has-title');
el.removeAttribute('data-sort-value');
el.removeAttribute('data-title');
});
// Apply classes and attributes for each file with frontmatter
files.forEach(file => { files.forEach(file => {
try { try {
const metadata = this.app.metadataCache.getFileCache(file); const metadata = this.app.metadataCache.getFileCache(file);
const title = metadata?.frontmatter?.title; const frontmatter = metadata?.frontmatter;
const title = frontmatter?.title;
const sortValue = frontmatter?.sort;
if (title) { if (title || sortValue) {
const escapedPath = CSS.escape(file.path); const escapedPath = CSS.escape(file.path);
const escapedTitle = title.replace(/"/g, '\\"'); const fileElement = document.querySelector(`.nav-file-title[data-path="${escapedPath}"] .nav-file-title-content`);
cssRules.push(` if (fileElement) {
.nav-file-title[data-path="${escapedPath}"] .nav-file-title-content::after { // Add sort value if available
content: " (${escapedTitle})"; if (sortValue !== undefined) {
color: var(--color-orange); fileElement.classList.add('has-sort-value');
font-size: 0.85em; fileElement.setAttribute('data-sort-value', `[${sortValue}] `);
opacity: 0.8;
} }
`);
// Add title if available
if (title) {
fileElement.classList.add('has-title');
fileElement.setAttribute('data-title', ` (${title})`);
}
}
} }
} catch (e) { } catch (e) {
console.error('Error processing file:', file?.path, e); console.error('Error processing file:', file?.path, e);
} }
}); });
// Update stylesheet content
if (this.styleEl) {
this.styleEl.textContent = cssRules.join("\n");
}
} catch (error) { } catch (error) {
console.error('Error updating titles:', error); console.error('Error updating titles:', error);
} }
@@ -84,9 +83,13 @@ class TitleAppenderPlugin extends obsidian.Plugin {
onunload() { onunload() {
console.log('Unloading TitleAppender plugin'); console.log('Unloading TitleAppender plugin');
if (this.styleEl) {
this.styleEl.remove(); // Clean up any added classes when unloading
} document.querySelectorAll('.has-sort-value, .has-title').forEach(el => {
el.classList.remove('has-sort-value', 'has-title');
el.removeAttribute('data-sort-value');
el.removeAttribute('data-title');
});
} }
} }

View File

@@ -10,21 +10,11 @@
margin-left: 0; margin-left: 0;
} }
/* Icon and title styling */ /* Sort value styling */
.title-icon { .nav-file-title-content::before {
margin-right: 5px; margin-right: 4px;
font-size: 0.9em;
color: var(--color-orange);
flex-shrink: 0;
} }
.has-icon {
display: flex;
align-items: center;
}
.nav-file-title-content { .nav-file-title-content {
padding: 0; padding: 0;
line-height: 1.1; line-height: 1.1;
@@ -38,3 +28,18 @@
.nav-folder .nav-folder .nav-folder-title { .nav-folder .nav-folder .nav-folder-title {
padding-left: 12px !important; padding-left: 12px !important;
} }
/* Sort value styling */
.has-sort-value::before {
content: attr(data-sort-value);
color: var(--color-yellow);
margin-right: 2px;
}
/* Title styling */
.has-title::after {
content: attr(data-title);
color: var(--color-orange);
font-size: 0.85em;
opacity: 0.8;
}