feat: support mermaid

feat: codeblock highlight

feat: dark mode for highlight

feat: copy button for code block

feat: support katex math rendering
This commit is contained in:
Xin
2023-07-30 22:18:28 +01:00
parent e99f44319a
commit e71887d00e
12 changed files with 392 additions and 40 deletions

24
assets/js/code-copy.js Normal file
View File

@ -0,0 +1,24 @@
document.querySelectorAll('.code-copy-btn').forEach(function (button) {
button.addEventListener('click', function (e) {
const targetId = e.target.getAttribute('data-clipboard-target');
const target = document.querySelector(targetId);
const codeElements = target.querySelectorAll('code');
// Select the last code element in case line numbers are present
const codeElement = codeElements[codeElements.length - 1];
if (codeElement) {
// Replace double newlines with single newlines in the innerText
// as each line inside <span> has trailing newline '\n'
const code = codeElement.innerText.replace(/\n\n/g, '\n');
navigator.clipboard.writeText(code).then(function () {
button.classList.add('copied');
setTimeout(function () {
button.classList.remove('copied');
}, 500);
}).catch(function (err) {
console.error('Failed to copy text: ', err);
});
} else {
console.error('Target element not found');
}
});
});