@@ -4,15 +4,6 @@ class TitleAppenderPlugin extends obsidian.Plugin {
44 async onload ( ) {
55 console . log ( 'Loading TitleAppender plugin' ) ;
66
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-
167 // Wait for layout to be ready
178 this . app . workspace . onLayoutReady ( ( ) => {
189 this . registerEvents ( ) ;
@@ -47,46 +38,58 @@ class TitleAppenderPlugin extends obsidian.Plugin {
4738 try {
4839 // Get all markdown files
4940 const files = this . app . vault . getMarkdownFiles ( ) ;
50- let cssRules = [ ] ;
5141
52- // Create CSS rules for each file with a frontmatter title
42+ // First, reset all previously set classes and attributes
43+ document . querySelectorAll ( '.has-sort-value, .has-title' ) . forEach ( el => {
44+ el . classList . remove ( 'has-sort-value' , 'has-title' ) ;
45+ el . removeAttribute ( 'data-sort-value' ) ;
46+ el . removeAttribute ( 'data-title' ) ;
47+ } ) ;
48+
49+ // Apply classes and attributes for each file with frontmatter
5350 files . forEach ( file => {
5451 try {
5552 const metadata = this . app . metadataCache . getFileCache ( file ) ;
56- const title = metadata ?. frontmatter ?. title ;
53+ const frontmatter = metadata ?. frontmatter ;
54+ const title = frontmatter ?. title ;
55+ const sortValue = frontmatter ?. sort ;
5756
58- if ( title ) {
57+ if ( title || sortValue ) {
5958 const escapedPath = CSS . escape ( file . path ) ;
60- const escapedTitle = title . replace ( / " / g , '\\"' ) ;
59+ const fileElement = document . querySelector ( `.nav-file-title[data-path=" ${ escapedPath } "] .nav-file-title-content` ) ;
6160
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;
61+ if ( fileElement ) {
62+ // Add sort value if available
63+ if ( sortValue !== undefined ) {
64+ fileElement . classList . add ( 'has-sort-value' ) ;
65+ fileElement . setAttribute ( 'data-sort-value' , `[${ sortValue } ] ` ) ;
6866 }
69- ` ) ;
67+
68+ // Add title if available
69+ if ( title ) {
70+ fileElement . classList . add ( 'has-title' ) ;
71+ fileElement . setAttribute ( 'data-title' , ` (${ title } )` ) ;
72+ }
73+ }
7074 }
7175 } catch ( e ) {
7276 console . error ( 'Error processing file:' , file ?. path , e ) ;
7377 }
7478 } ) ;
75-
76- // Update stylesheet content
77- if ( this . styleEl ) {
78- this . styleEl . textContent = cssRules . join ( "\n" ) ;
79- }
8079 } catch ( error ) {
8180 console . error ( 'Error updating titles:' , error ) ;
8281 }
8382 }
8483
8584 onunload ( ) {
8685 console . log ( 'Unloading TitleAppender plugin' ) ;
87- if ( this . styleEl ) {
88- this . styleEl . remove ( ) ;
89- }
86+
87+ // Clean up any added classes when unloading
88+ document . querySelectorAll ( '.has-sort-value, .has-title' ) . forEach ( el => {
89+ el . classList . remove ( 'has-sort-value' , 'has-title' ) ;
90+ el . removeAttribute ( 'data-sort-value' ) ;
91+ el . removeAttribute ( 'data-title' ) ;
92+ } ) ;
9093 }
9194}
9295
0 commit comments