Update.
This commit is contained in:
@@ -0,0 +1,112 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
(function() {
|
||||
'use strict';
|
||||
|
||||
function initSidebar() {
|
||||
var toggle = document.querySelector('.mobile-menu-toggle');
|
||||
var sidebar = document.querySelector('.sidebar');
|
||||
var overlay = document.createElement('div');
|
||||
overlay.className = 'sidebar-overlay';
|
||||
document.body.appendChild(overlay);
|
||||
|
||||
if (toggle) {
|
||||
toggle.addEventListener('click', function() {
|
||||
sidebar.classList.toggle('open');
|
||||
overlay.classList.toggle('visible');
|
||||
});
|
||||
}
|
||||
|
||||
overlay.addEventListener('click', function() {
|
||||
sidebar.classList.remove('open');
|
||||
overlay.classList.remove('visible');
|
||||
});
|
||||
|
||||
var currentPath = window.location.pathname;
|
||||
var links = document.querySelectorAll('.sidebar-nav a');
|
||||
links.forEach(function(link) {
|
||||
var href = link.getAttribute('href');
|
||||
if (href && currentPath.endsWith(href.replace(/^\.\.\//, '').replace(/^\.\//, ''))) {
|
||||
link.classList.add('active');
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function initCopyButtons() {
|
||||
var codeBlocks = document.querySelectorAll('pre');
|
||||
codeBlocks.forEach(function(pre) {
|
||||
var wrapper = document.createElement('div');
|
||||
wrapper.className = 'code-block';
|
||||
pre.parentNode.insertBefore(wrapper, pre);
|
||||
wrapper.appendChild(pre);
|
||||
|
||||
var btn = document.createElement('button');
|
||||
btn.className = 'copy-btn';
|
||||
btn.textContent = 'Copy';
|
||||
wrapper.appendChild(btn);
|
||||
|
||||
btn.addEventListener('click', function() {
|
||||
var code = pre.querySelector('code');
|
||||
var text = code ? code.textContent : pre.textContent;
|
||||
|
||||
navigator.clipboard.writeText(text).then(function() {
|
||||
btn.textContent = 'Copied!';
|
||||
btn.classList.add('copied');
|
||||
setTimeout(function() {
|
||||
btn.textContent = 'Copy';
|
||||
btn.classList.remove('copied');
|
||||
}, 2000);
|
||||
}).catch(function() {
|
||||
btn.textContent = 'Failed';
|
||||
setTimeout(function() {
|
||||
btn.textContent = 'Copy';
|
||||
}, 2000);
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function initSmoothScroll() {
|
||||
document.querySelectorAll('a[href^="#"]').forEach(function(anchor) {
|
||||
anchor.addEventListener('click', function(e) {
|
||||
var targetId = this.getAttribute('href').slice(1);
|
||||
var target = document.getElementById(targetId);
|
||||
if (target) {
|
||||
e.preventDefault();
|
||||
target.scrollIntoView({ behavior: 'smooth' });
|
||||
history.pushState(null, null, '#' + targetId);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function highlightCode() {
|
||||
var keywords = ['import', 'for', 'class', 'static', 'var', 'if', 'else', 'while',
|
||||
'return', 'true', 'false', 'null', 'this', 'super', 'is', 'new',
|
||||
'foreign', 'construct', 'break', 'continue', 'in'];
|
||||
|
||||
document.querySelectorAll('pre code').forEach(function(block) {
|
||||
if (block.classList.contains('highlighted')) return;
|
||||
block.classList.add('highlighted');
|
||||
|
||||
var html = block.innerHTML;
|
||||
|
||||
html = html.replace(/(\/\/[^\n]*)/g, '<span style="color:#6a9955">$1</span>');
|
||||
html = html.replace(/("(?:[^"\\]|\\.)*")/g, '<span style="color:#ce9178">$1</span>');
|
||||
|
||||
keywords.forEach(function(kw) {
|
||||
var regex = new RegExp('\\b(' + kw + ')\\b', 'g');
|
||||
html = html.replace(regex, '<span style="color:#569cd6">$1</span>');
|
||||
});
|
||||
|
||||
block.innerHTML = html;
|
||||
});
|
||||
}
|
||||
|
||||
document.addEventListener('DOMContentLoaded', function() {
|
||||
initSidebar();
|
||||
initCopyButtons();
|
||||
initSmoothScroll();
|
||||
highlightCode();
|
||||
});
|
||||
})();
|
||||
Reference in New Issue
Block a user