(function() {
var contentEl = document.getElementById('content');
var loadingBar = document.getElementById('loading-bar');
var sidebar = document.getElementById('sidebar');
var currentMd = '1-INDEX.md';
marked.setOptions({
highlight: false, gfm: true,
breaks: false,
langPrefix: 'language-',
});
hljs.configure({
ignoreUnescapedHTML: true
});
window.navigate = function(mdFile, el) {
loadPage(mdFile);
if (el) return false; };
function loadPage(mdFile) {
if (mdFile === currentMd) return;
currentMd = mdFile;
loadingBar.style.opacity = '1';
loadingBar.style.width = '30%';
var xhr = new XMLHttpRequest();
xhr.open('GET', mdFile, true);
xhr.onprogress = function(e) {
if (e.lengthComputable) {
var pct = Math.min(70, 30 + (e.loaded / e.total) * 40);
loadingBar.style.width = pct + '%';
}
};
xhr.onload = function() {
if (xhr.status === 200 || xhr.status === 0) {
var md = xhr.responseText;
var html = marked.parse(md);
contentEl.innerHTML = html;
contentEl.querySelectorAll('pre code').forEach(function(block) {
hljs.highlightElement(block);
});
contentEl.querySelectorAll('p code, li code, td code').forEach(function(el) {
el.style.background = '#e8e8ee';
el.style.color = '#c7254e';
});
addCopyButtons();
updateSidebar(mdFile);
var titleEl = contentEl.querySelector('h1');
document.title = (titleEl ? titleEl.textContent + ' - ' : '') + 'rslog 中文文档';
loadingBar.style.width = '100%';
setTimeout(function() {
loadingBar.style.opacity = '0';
loadingBar.style.width = '0';
}, 300);
window.scrollTo({ top: 0, behavior: 'smooth' });
if (window.innerWidth <= 768) {
sidebar.classList.remove('open');
document.getElementById('sidebar-overlay').classList.remove('active');
}
}
};
xhr.onerror = function() {
contentEl.innerHTML = '<h1>加载失败</h1><p>无法加载文档内容,请检查网络连接后刷新页面。</p>';
loadingBar.style.opacity = '0';
loadingBar.style.width = '0';
};
xhr.send();
history.replaceState({ md: mdFile }, '', '#' + mdFile);
}
function addCopyButtons() {
contentEl.querySelectorAll('pre').forEach(function(pre) {
if (pre.querySelector('.copy-btn')) return;
var btn = document.createElement('button');
btn.className = 'copy-btn';
btn.textContent = 'Copy';
pre.appendChild(btn);
btn.addEventListener('click', function(e) {
e.stopPropagation();
var code = pre.querySelector('code');
var text = code ? (code.textContent || '') : '';
if (navigator.clipboard && navigator.clipboard.writeText) {
navigator.clipboard.writeText(text).then(function() {
showCopied(btn);
}).catch(function() {
fallbackCopy(text, btn);
});
} else {
fallbackCopy(text, btn);
}
});
});
}
function fallbackCopy(text, btn) {
var ta = document.createElement('textarea');
ta.value = text;
ta.style.position = 'fixed';
ta.style.opacity = '0';
document.body.appendChild(ta);
ta.select();
try {
document.execCommand('copy');
showCopied(btn);
} catch (e) {}
document.body.removeChild(ta);
}
function showCopied(btn) {
var orig = btn.textContent;
btn.textContent = 'Copied!';
btn.classList.add('copied');
setTimeout(function() {
btn.textContent = orig;
btn.classList.remove('copied');
}, 2000);
}
function updateSidebar(mdFile) {
sidebar.querySelectorAll('.sidebar-link').forEach(function(link) {
link.classList.remove('active');
if (link.getAttribute('data-md') === mdFile) {
link.classList.add('active');
}
});
}
sidebar.querySelectorAll('.sidebar-link[data-md]').forEach(function(link) {
link.addEventListener('click', function(e) {
e.preventDefault();
var md = this.getAttribute('data-md');
if (md) loadPage(md);
});
});
var menuToggle = document.getElementById('menu-toggle');
var overlay = document.getElementById('sidebar-overlay');
if (menuToggle && sidebar && overlay) {
function toggleMenu() {
sidebar.classList.toggle('open');
overlay.classList.toggle('active');
}
menuToggle.addEventListener('click', toggleMenu);
overlay.addEventListener('click', toggleMenu);
}
var initialMd = location.hash.replace('#', '') || currentMd;
if (initialMd && initialMd.match(/\.md$/)) {
currentMd = ''; loadPage(initialMd);
} else {
loadPage(currentMd);
}
window.addEventListener('popstate', function(e) {
if (e.state && e.state.md) {
var md = e.state.md;
var prev = currentMd;
currentMd = md + '_force'; loadPage(md);
}
});
})();