|
| 1 | +// Custom JavaScript for torchTextClassifiers documentation |
| 2 | + |
| 3 | +document.addEventListener('DOMContentLoaded', function() { |
| 4 | + // Add copy buttons to code blocks |
| 5 | + addCopyButtons(); |
| 6 | + |
| 7 | + // Improve navigation |
| 8 | + improveNavigation(); |
| 9 | + |
| 10 | + // Add search enhancements |
| 11 | + enhanceSearch(); |
| 12 | +}); |
| 13 | + |
| 14 | +function addCopyButtons() { |
| 15 | + const codeBlocks = document.querySelectorAll('pre'); |
| 16 | + |
| 17 | + codeBlocks.forEach(function(block) { |
| 18 | + const button = document.createElement('button'); |
| 19 | + button.className = 'copy-button'; |
| 20 | + button.textContent = 'Copy'; |
| 21 | + button.style.cssText = ` |
| 22 | + position: absolute; |
| 23 | + top: 8px; |
| 24 | + right: 8px; |
| 25 | + background: #007bff; |
| 26 | + color: white; |
| 27 | + border: none; |
| 28 | + border-radius: 4px; |
| 29 | + padding: 4px 8px; |
| 30 | + font-size: 12px; |
| 31 | + cursor: pointer; |
| 32 | + opacity: 0.7; |
| 33 | + transition: opacity 0.2s; |
| 34 | + `; |
| 35 | + |
| 36 | + button.addEventListener('click', function() { |
| 37 | + const code = block.querySelector('code'); |
| 38 | + const text = code ? code.textContent : block.textContent; |
| 39 | + |
| 40 | + navigator.clipboard.writeText(text).then(function() { |
| 41 | + button.textContent = 'Copied!'; |
| 42 | + setTimeout(function() { |
| 43 | + button.textContent = 'Copy'; |
| 44 | + }, 2000); |
| 45 | + }); |
| 46 | + }); |
| 47 | + |
| 48 | + button.addEventListener('mouseenter', function() { |
| 49 | + button.style.opacity = '1'; |
| 50 | + }); |
| 51 | + |
| 52 | + button.addEventListener('mouseleave', function() { |
| 53 | + button.style.opacity = '0.7'; |
| 54 | + }); |
| 55 | + |
| 56 | + block.style.position = 'relative'; |
| 57 | + block.appendChild(button); |
| 58 | + }); |
| 59 | +} |
| 60 | + |
| 61 | +function improveNavigation() { |
| 62 | + // Add smooth scrolling to anchor links |
| 63 | + const anchorLinks = document.querySelectorAll('a[href^="#"]'); |
| 64 | + |
| 65 | + anchorLinks.forEach(function(link) { |
| 66 | + link.addEventListener('click', function(e) { |
| 67 | + e.preventDefault(); |
| 68 | + const target = document.querySelector(this.getAttribute('href')); |
| 69 | + if (target) { |
| 70 | + target.scrollIntoView({ |
| 71 | + behavior: 'smooth', |
| 72 | + block: 'start' |
| 73 | + }); |
| 74 | + } |
| 75 | + }); |
| 76 | + }); |
| 77 | + |
| 78 | + // Highlight current section in navigation |
| 79 | + highlightCurrentSection(); |
| 80 | +} |
| 81 | + |
| 82 | +function highlightCurrentSection() { |
| 83 | + const sections = document.querySelectorAll('h1, h2, h3'); |
| 84 | + const navLinks = document.querySelectorAll('.wy-menu-vertical a'); |
| 85 | + |
| 86 | + function updateActiveLink() { |
| 87 | + let current = ''; |
| 88 | + |
| 89 | + sections.forEach(function(section) { |
| 90 | + const rect = section.getBoundingClientRect(); |
| 91 | + if (rect.top <= 100) { |
| 92 | + current = section.id; |
| 93 | + } |
| 94 | + }); |
| 95 | + |
| 96 | + navLinks.forEach(function(link) { |
| 97 | + link.classList.remove('current'); |
| 98 | + if (link.getAttribute('href') === '#' + current) { |
| 99 | + link.classList.add('current'); |
| 100 | + } |
| 101 | + }); |
| 102 | + } |
| 103 | + |
| 104 | + window.addEventListener('scroll', updateActiveLink); |
| 105 | + updateActiveLink(); |
| 106 | +} |
| 107 | + |
| 108 | +function enhanceSearch() { |
| 109 | + // Add keyboard shortcuts for search |
| 110 | + document.addEventListener('keydown', function(e) { |
| 111 | + // Ctrl+K or Cmd+K to focus search |
| 112 | + if ((e.ctrlKey || e.metaKey) && e.key === 'k') { |
| 113 | + e.preventDefault(); |
| 114 | + const searchInput = document.querySelector('input[name="q"]'); |
| 115 | + if (searchInput) { |
| 116 | + searchInput.focus(); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + // Escape to clear search |
| 121 | + if (e.key === 'Escape') { |
| 122 | + const searchInput = document.querySelector('input[name="q"]'); |
| 123 | + if (searchInput && searchInput === document.activeElement) { |
| 124 | + searchInput.value = ''; |
| 125 | + searchInput.blur(); |
| 126 | + } |
| 127 | + } |
| 128 | + }); |
| 129 | +} |
| 130 | + |
| 131 | +// Add version info to footer |
| 132 | +function addVersionInfo() { |
| 133 | + const footer = document.querySelector('.rst-footer-buttons'); |
| 134 | + if (footer) { |
| 135 | + const versionInfo = document.createElement('div'); |
| 136 | + versionInfo.innerHTML = ` |
| 137 | + <div class="footer"> |
| 138 | + Built with ❤️ by the torchTextClassifiers team | |
| 139 | + <a href="https://github.com/your-org/torch-fastText">View on GitHub</a> |
| 140 | + </div> |
| 141 | + `; |
| 142 | + footer.parentNode.insertBefore(versionInfo, footer.nextSibling); |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | +// Initialize additional features |
| 147 | +document.addEventListener('DOMContentLoaded', function() { |
| 148 | + addVersionInfo(); |
| 149 | +}); |
0 commit comments