|
| 1 | +(function() { |
| 2 | + console.log("Beancount header script loaded"); |
| 3 | + |
| 4 | + function applyLinks() { |
| 5 | + const logoLinks = document.querySelectorAll('[data-md-component="logo"]'); |
| 6 | + if (logoLinks.length === 0) return false; |
| 7 | + |
| 8 | + let docRoot = ''; |
| 9 | + // Find the original documentation root link. |
| 10 | + // It's usually "" (on index) or contains ".." or is "/docs/" |
| 11 | + for (let logo of logoLinks) { |
| 12 | + const href = logo.getAttribute('href'); |
| 13 | + if (href !== null && href !== 'https://beancount.github.io') { |
| 14 | + docRoot = logo.href; // Get resolved absolute URL |
| 15 | + break; |
| 16 | + } |
| 17 | + } |
| 18 | + |
| 19 | + // If we can't find it clearly, fallback to a sensible default. |
| 20 | + if (!docRoot) docRoot = window.location.origin + '/docs/'; |
| 21 | + |
| 22 | + console.log("Determined docRoot:", docRoot); |
| 23 | + |
| 24 | + // Update all logo links to point to organization root. |
| 25 | + logoLinks.forEach(function(logo) { |
| 26 | + logo.href = 'https://beancount.github.io'; |
| 27 | + }); |
| 28 | + |
| 29 | + // Update the site title in the header. |
| 30 | + const titleTopics = document.querySelectorAll('.md-header__topic'); |
| 31 | + if (titleTopics.length > 0) { |
| 32 | + const titleSpan = titleTopics[0].querySelector('.md-ellipsis'); |
| 33 | + if (titleSpan && !titleSpan.querySelector('a')) { |
| 34 | + const titleText = titleSpan.textContent.trim(); |
| 35 | + if (titleText === 'Beancount Documentation') { |
| 36 | + titleSpan.innerHTML = `<a href="${docRoot}" style="color: inherit; text-decoration: none;">${titleText}</a>`; |
| 37 | + console.log("Updated title link to:", docRoot); |
| 38 | + } |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + // Also update the sidebar title if it exists. |
| 43 | + const navTitle = document.querySelector('.md-nav__title'); |
| 44 | + if (navTitle) { |
| 45 | + for (let node of navTitle.childNodes) { |
| 46 | + if (node.nodeType === Node.TEXT_NODE && node.textContent.trim() === 'Beancount Documentation') { |
| 47 | + const span = document.createElement('span'); |
| 48 | + span.innerHTML = `<a href="${docRoot}" style="color: inherit; text-decoration: none;">${node.textContent}</a>`; |
| 49 | + navTitle.replaceChild(span, node); |
| 50 | + console.log("Updated sidebar title link"); |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + return true; |
| 56 | + } |
| 57 | + |
| 58 | + // Try multiple times to handle potential dynamic loading or race conditions. |
| 59 | + let attempts = 0; |
| 60 | + const interval = setInterval(function() { |
| 61 | + attempts++; |
| 62 | + const success = applyLinks(); |
| 63 | + if (success || attempts > 20) { |
| 64 | + clearInterval(interval); |
| 65 | + } |
| 66 | + }, 250); |
| 67 | + |
| 68 | + // Also hook into Material for MkDocs navigation if available. |
| 69 | + if (typeof location$ !== 'undefined') { |
| 70 | + location$.subscribe(function() { |
| 71 | + setTimeout(applyLinks, 100); |
| 72 | + setTimeout(applyLinks, 500); |
| 73 | + }); |
| 74 | + } |
| 75 | +})(); |
0 commit comments