import hljs from 'highlight.js';
import md from 'markdown-it';
document.querySelectorAll('.markdown').forEach((e: HTMLElement) => {
e.innerHTML = md({
html: true,
highlight: function (str, lang) {
if (lang && hljs.getLanguage(lang)) {
try {
return '
' +
hljs.highlight(str, {language: lang, ignoreIllegals: true}).value +
'
';
} catch (__) {
}
}
return '' + md().utils.escapeHtml(str) + '
';
}
}).render(e.textContent);
});
document.querySelectorAll('.table-code').forEach((el) => {
const ext = el.dataset.filename?.split('.').pop() || '';
if (hljs.getLanguage(ext) && ext !== 'txt') {
el.querySelectorAll('td.line-code').forEach((ell) => {
ell.classList.add('language-' + ext);
hljs.highlightElement(ell);
});
}
el.addEventListener('click', event => {
if (event.target && (event.target as HTMLElement).matches('.line-num')) {
Array.from(document.querySelectorAll('.table-code .selected')).forEach((el) => el.classList.remove('selected'));
const nextSibling = (event.target as HTMLElement).nextSibling;
if (nextSibling instanceof HTMLElement) {
nextSibling.classList.add('selected');
}
const filename = el.dataset.filenameSlug;
const line = (event.target as HTMLElement).textContent;
const url = location.protocol + '//' + location.host + location.pathname;
const hash = '#file-' + filename + '-' + line;
window.history.pushState(null, null, url + hash);
location.hash = hash;
}
});
});