Files
readur/docs/javascripts/extra.js

97 lines
3.3 KiB
JavaScript

// Custom JavaScript for Readur documentation
// Add copy button to code blocks
document.addEventListener('DOMContentLoaded', function() {
// Initialize copy buttons for code blocks (if not already handled by theme)
const codeBlocks = document.querySelectorAll('pre > code');
codeBlocks.forEach(function(codeBlock) {
// Check if copy button already exists
if (codeBlock.parentElement.querySelector('.copy-button')) {
return;
}
const button = document.createElement('button');
button.className = 'copy-button';
button.textContent = 'Copy';
button.setAttribute('aria-label', 'Copy code to clipboard');
button.addEventListener('click', function() {
const code = codeBlock.textContent;
navigator.clipboard.writeText(code).then(function() {
button.textContent = 'Copied!';
setTimeout(function() {
button.textContent = 'Copy';
}, 2000);
}).catch(function(err) {
console.error('Failed to copy code: ', err);
});
});
codeBlock.parentElement.style.position = 'relative';
codeBlock.parentElement.appendChild(button);
});
// Smooth scroll for anchor links
document.querySelectorAll('a[href^="#"]').forEach(anchor => {
anchor.addEventListener('click', function(e) {
const href = this.getAttribute('href');
if (href !== '#' && href !== '#!') {
e.preventDefault();
const target = document.querySelector(href);
if (target) {
target.scrollIntoView({
behavior: 'smooth',
block: 'start'
});
}
}
});
});
// Add external link indicators (without forcing new tab)
const externalLinks = document.querySelectorAll('a[href^="http"]:not([href*="readur.app"])');
externalLinks.forEach(link => {
// Only add rel attribute for security, don't force target="_blank"
link.setAttribute('rel', 'noopener noreferrer');
link.classList.add('external-link');
});
// Track documentation page views (if analytics enabled)
if (typeof gtag !== 'undefined') {
gtag('event', 'page_view', {
page_title: document.title,
page_location: window.location.href,
page_path: window.location.pathname
});
}
});
// Add keyboard shortcuts
document.addEventListener('keydown', function(e) {
// Ctrl/Cmd + K for search
if ((e.ctrlKey || e.metaKey) && e.key === 'k') {
e.preventDefault();
const searchInput = document.querySelector('.md-search__input');
if (searchInput) {
searchInput.focus();
}
}
// Escape to close search
if (e.key === 'Escape') {
const searchInput = document.querySelector('.md-search__input');
if (searchInput && document.activeElement === searchInput) {
searchInput.blur();
}
}
});
// Custom console message
console.log(
'%c Welcome to Readur Documentation! ',
'background: #4051b5; color: white; padding: 5px 10px; border-radius: 3px;'
);
console.log(
'Found an issue? Report it at https://github.com/readur/readur/issues'
);