|
1 | 1 | const obsidian = require('obsidian'); |
2 | 2 |
|
3 | | -class TitleAppenderPlugin extends obsidian.Plugin { |
| 3 | +/** |
| 4 | + * @class DocsViewerPlugin |
| 5 | + * @description An Obsidian plugin that enhances document viewing by adding titles from frontmatter to file and folder elements |
| 6 | + * @extends {obsidian.Plugin} |
| 7 | + */ |
| 8 | +class DocsViewerPlugin extends obsidian.Plugin { |
| 9 | + /** |
| 10 | + * @description Plugin initialization method called when the plugin is loaded |
| 11 | + * @returns {Promise<void>} |
| 12 | + */ |
4 | 13 | async onload() { |
5 | | - console.log('Loading TitleAppender plugin'); |
| 14 | + console.log('Loading DocsViewer plugin'); |
6 | 15 |
|
7 | | - // Initial update attempt |
8 | 16 | this.updateAllTitles(); |
9 | 17 |
|
10 | | - // Wait for layout to be ready before registering events |
11 | 18 | this.app.workspace.onLayoutReady(() => { |
12 | 19 | this.registerEvents(); |
13 | 20 |
|
14 | | - // Force another update after layout is ready |
15 | 21 | setTimeout(() => { |
16 | 22 | this.updateAllTitles(); |
17 | 23 | }, 500); |
18 | 24 | }); |
19 | 25 |
|
20 | | - // Schedule periodic updates to catch any missed files |
21 | 26 | this.registerInterval( |
22 | 27 | window.setInterval(() => this.updateAllTitles(), 5000) |
23 | 28 | ); |
24 | 29 | } |
25 | 30 |
|
| 31 | + /** |
| 32 | + * @description Registers event listeners for file and layout changes |
| 33 | + * @returns {void} |
| 34 | + */ |
26 | 35 | registerEvents() { |
27 | | - // Update styles when files change |
28 | 36 | this.registerEvent( |
29 | 37 | this.app.metadataCache.on('changed', () => { |
30 | 38 | this.updateAllTitles(); |
31 | 39 | }) |
32 | 40 | ); |
33 | 41 |
|
34 | | - // Update on file explorer changes |
35 | 42 | this.registerEvent( |
36 | 43 | this.app.workspace.on('file-menu', () => { |
37 | 44 | this.updateAllTitles(); |
38 | 45 | }) |
39 | 46 | ); |
40 | 47 |
|
41 | | - // Update when layout changes |
42 | 48 | this.registerEvent( |
43 | 49 | this.app.workspace.on('layout-change', () => { |
44 | 50 | this.updateAllTitles(); |
45 | 51 | }) |
46 | 52 | ); |
47 | 53 | } |
48 | 54 |
|
| 55 | + /** |
| 56 | + * @description Updates display titles for files and folders based on frontmatter data |
| 57 | + * @details Processes all markdown files to extract frontmatter and update the UI with proper titles and sorting |
| 58 | + * @returns {void} |
| 59 | + */ |
49 | 60 | updateAllTitles() { |
50 | 61 | try { |
51 | | - // Get all markdown files |
52 | 62 | const files = this.app.vault.getMarkdownFiles(); |
53 | | - |
54 | | - // First, clean up any existing elements |
55 | 63 | this.cleanupExistingElements(); |
56 | | - |
57 | | - // Apply classes and attributes for each file with frontmatter |
58 | | - files.forEach(file => { |
59 | | - try { |
60 | | - const metadata = this.app.metadataCache.getFileCache(file); |
61 | | - const frontmatter = metadata?.frontmatter; |
62 | | - const title = frontmatter?.title; |
63 | | - const sortValue = frontmatter?.sort; |
| 64 | + |
| 65 | + this.ensureCustomStyles(); |
| 66 | + |
| 67 | + document.querySelectorAll('.tree-item.nav-file, .nav-file, .tree-item.nav-folder, .nav-folder').forEach(item => { |
| 68 | + let path, isFolder; |
| 69 | + |
| 70 | + const folderTitleEl = item.querySelector('.tree-item-self[data-path]'); |
| 71 | + if (folderTitleEl && item.classList.contains('nav-folder')) { |
| 72 | + path = folderTitleEl.getAttribute('data-path'); |
| 73 | + isFolder = true; |
| 74 | + } else { |
| 75 | + const fileEl = item.querySelector('.tree-item-self, .nav-file-title'); |
| 76 | + if (fileEl) { |
| 77 | + path = fileEl.getAttribute('data-path'); |
| 78 | + isFolder = false; |
| 79 | + } else { |
| 80 | + return; |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + if (!path) return; |
| 85 | + |
| 86 | + let file; |
| 87 | + if (isFolder) { |
| 88 | + const folderName = path.split('/').pop(); |
| 89 | + file = files.find(f => f.path === `${path}/${folderName}.md`); |
64 | 90 |
|
65 | | - if (title || sortValue) { |
66 | | - const escapedPath = CSS.escape(file.path); |
67 | | - const fileElements = document.querySelectorAll(`.nav-file-title[data-path="${escapedPath}"] .nav-file-title-content`); |
68 | | - |
69 | | - fileElements.forEach(fileElement => { |
70 | | - if (fileElement) { |
71 | | - // Add title if available |
72 | | - if (title) { |
73 | | - fileElement.classList.add('has-title'); |
74 | | - fileElement.setAttribute('data-title', ` (${title})`); |
75 | | - } |
76 | | - |
77 | | - // Add sort value if available |
78 | | - if (sortValue !== undefined) { |
79 | | - fileElement.classList.add('has-sort-value'); |
80 | | - |
81 | | - // If both title and sort exist, create a span for the sort value |
82 | | - if (title) { |
83 | | - // Create a span for the sort value to apply different color |
84 | | - const sortSpan = document.createElement('span'); |
85 | | - sortSpan.className = 'sort-suffix'; |
86 | | - sortSpan.textContent = `[${sortValue}]`; |
87 | | - fileElement.appendChild(sortSpan); |
88 | | - } else { |
89 | | - // If only sort exists, use the attribute |
90 | | - fileElement.setAttribute('data-sort-value', `[${sortValue}]`); |
91 | | - } |
92 | | - } |
| 91 | + if (!file) return; |
| 92 | + } else { |
| 93 | + file = files.find(f => f.path === path); |
| 94 | + } |
| 95 | + |
| 96 | + if (!file) return; |
| 97 | + |
| 98 | + const frontmatter = this.app.metadataCache.getFileCache(file)?.frontmatter; |
| 99 | + |
| 100 | + const parentContainer = item.parentElement; |
| 101 | + if (parentContainer) { |
| 102 | + parentContainer.classList.add('docs-viewer-flex-container'); |
| 103 | + |
| 104 | + let sortValue; |
| 105 | + |
| 106 | + if (!isFolder) { |
| 107 | + const filePath = file.path; |
| 108 | + const folderName = filePath.substring(0, filePath.lastIndexOf('/')).split('/').pop(); |
| 109 | + if (folderName && file.basename === folderName) { |
| 110 | + sortValue = -9999999; |
| 111 | + } else { |
| 112 | + sortValue = frontmatter?.sort !== undefined ? parseInt(frontmatter.sort) : 9999; |
| 113 | + } |
| 114 | + } else { |
| 115 | + sortValue = frontmatter?.sort !== undefined ? parseInt(frontmatter.sort) : 9999; |
| 116 | + } |
| 117 | + |
| 118 | + item.style.order = sortValue; |
| 119 | + } |
| 120 | + |
| 121 | + if (!isFolder) { |
| 122 | + const titleEl = item.querySelector('.tree-item-inner, .nav-file-title-content'); |
| 123 | + if (titleEl) { |
| 124 | + const frontTitle = frontmatter?.title; |
| 125 | + if (frontTitle && frontTitle !== file.basename) { |
| 126 | + const frontSort = frontmatter?.sort; |
| 127 | + let displayTitle = frontTitle; |
| 128 | + if (frontSort !== undefined) { |
| 129 | + titleEl.setAttribute('data-sort', frontSort); |
93 | 130 | } |
94 | | - }); |
| 131 | + |
| 132 | + titleEl.classList.add('has-title'); |
| 133 | + titleEl.setAttribute('data-title', ` (${displayTitle})`); |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | + else { |
| 138 | + const folderTitleEl = item.querySelector('.tree-item-inner') || |
| 139 | + item.querySelector('.nav-folder-title-content'); |
| 140 | + |
| 141 | + if (folderTitleEl && frontmatter && frontmatter.title) { |
| 142 | + const frontTitle = frontmatter.title; |
| 143 | + const frontSort = frontmatter.sort; |
| 144 | + |
| 145 | + folderTitleEl.classList.add('has-title'); |
| 146 | + folderTitleEl.setAttribute('data-title', ` (${frontTitle})`); |
| 147 | + |
| 148 | + if (frontSort !== undefined) { |
| 149 | + folderTitleEl.setAttribute('data-sort', frontSort); |
| 150 | + } |
95 | 151 | } |
96 | | - } catch (e) { |
97 | | - console.error('Error processing file:', file?.path, e); |
98 | 152 | } |
99 | 153 | }); |
| 154 | + |
100 | 155 | } catch (error) { |
101 | 156 | console.error('Error updating titles:', error); |
102 | 157 | } |
103 | 158 | } |
104 | | - |
| 159 | + |
| 160 | + /** |
| 161 | + * @description Ensures the plugin's custom CSS styles are loaded in the document |
| 162 | + * @returns {void} |
| 163 | + */ |
| 164 | + ensureCustomStyles() { |
| 165 | + const styleId = 'docs-viewer-style'; |
| 166 | + // Check if our stylesheet is already in the document |
| 167 | + if (!document.getElementById(styleId)) { |
| 168 | + const link = document.createElement('link'); |
| 169 | + link.id = styleId; |
| 170 | + link.rel = 'stylesheet'; |
| 171 | + link.type = 'text/css'; |
| 172 | + link.href = 'obsidian://css-theme-plugins/docs-viewer/styles.css'; |
| 173 | + document.head.appendChild(link); |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + /** |
| 178 | + * @description Removes all custom classes and attributes added by this plugin |
| 179 | + * @details Called during cleanup and before applying new changes to prevent duplication |
| 180 | + * @returns {void} |
| 181 | + */ |
105 | 182 | cleanupExistingElements() { |
106 | | - // Remove all classes and attributes |
107 | | - document.querySelectorAll('.has-sort-value, .has-title').forEach(el => { |
108 | | - el.classList.remove('has-sort-value', 'has-title'); |
109 | | - el.removeAttribute('data-sort-value'); |
| 183 | + // Remove custom classes and attributes from elements |
| 184 | + document.querySelectorAll('.has-title, .folder-has-title').forEach(el => { |
| 185 | + el.classList.remove('has-title', 'folder-has-title'); |
110 | 186 | el.removeAttribute('data-title'); |
111 | | - |
112 | | - // Remove any added elements |
113 | | - const sortSuffix = el.querySelector('.sort-suffix'); |
114 | | - if (sortSuffix) sortSuffix.remove(); |
| 187 | + el.removeAttribute('data-sort'); |
| 188 | + el.removeAttribute('data-folder-title'); |
| 189 | + }); |
| 190 | + |
| 191 | + // Reset order styles on tree items and files |
| 192 | + document.querySelectorAll('.tree-item, .nav-file').forEach(el => { |
| 193 | + el.style.order = ''; |
115 | 194 | }); |
116 | 195 | } |
117 | 196 |
|
| 197 | + /** |
| 198 | + * @description Plugin lifecycle method called when the plugin is disabled or Obsidian is closed |
| 199 | + * @details Performs cleanup to remove all plugin-added elements and styles |
| 200 | + * @returns {void} |
| 201 | + */ |
118 | 202 | onunload() { |
119 | | - console.log('Unloading TitleAppender plugin'); |
| 203 | + console.log('Unloading DocsViewer plugin'); |
| 204 | + // Remove all custom attributes and classes |
120 | 205 | this.cleanupExistingElements(); |
| 206 | + |
| 207 | + // Remove flex container class from parent elements |
| 208 | + document.querySelectorAll('.docs-viewer-flex-container').forEach(el => { |
| 209 | + el.classList.remove('docs-viewer-flex-container'); |
| 210 | + }); |
| 211 | + |
| 212 | + // Ensure order styles are cleared from navigation elements |
| 213 | + document.querySelectorAll('.nav-file, .nav-file-title').forEach(el => { |
| 214 | + el.style.order = ''; |
| 215 | + }); |
121 | 216 | } |
122 | 217 | } |
123 | 218 |
|
124 | | -module.exports = TitleAppenderPlugin; |
| 219 | +module.exports = DocsViewerPlugin; |
0 commit comments