hugo-theme-zzo/layouts/partials/script/codeblock-script.html

73 lines
2.4 KiB
HTML

<script>
'use strict';
// ====================== markdown code block ======================
function wrap(el, wrapper) {
el.parentNode.insertBefore(wrapper, el);
wrapper.appendChild(el);
}
(function () {
var singleContentsElem = document.querySelector('.single__contents');
singleContentsElem ?
singleContentsElem.querySelectorAll('pre > code').forEach(function(elem) {
var dataLang = elem.getAttribute('data-lang');
var dataLangWrapper = document.createElement('div');
var code = null;
var codeTitle = null;
if (dataLang && dataLang.includes(':')) {
code = dataLang.split(':')[0];
codeTitle = dataLang.split(':')[1];
dataLangWrapper.className = 'language-' + code;
dataLangWrapper.setAttribute('data-lang', codeTitle);
elem.className = 'language-' + code;
elem.setAttribute('data-lang', codeTitle);
elem.setAttribute('id', codeTitle);
} else if (!dataLang) {
dataLangWrapper.setAttribute('data-lang', 'Code');
dataLangWrapper.className = 'language-code';
}
if (!dataLang || codeTitle) {
wrap(elem.parentNode, dataLangWrapper);
}
}) : null;
})();
var langCodeElem = document.querySelectorAll('.language-code');
langCodeElem ? langCodeElem.forEach(function (elem) {
var newElem = document.createElement('span');
newElem.className = 'copy-to-clipboard';
newElem.setAttribute('title', 'Copy to clipboard');
elem.append(newElem);
}) : null;
// =================================================================
// ================ codeblock line number to symbol ================
var dollarCodeElem = document.querySelectorAll('div.language-\\$');
var gtCodeElem = document.querySelectorAll('div.language-\\>');
dollarCodeElem ?
dollarCodeElem.forEach(function(elem) {
var lnts = elem.parentNode.parentNode ? elem.parentNode.parentNode.querySelectorAll('.lnt') : null;
lnts ?
lnts.forEach(function(lnt) {
lnt.innerHTML = '$<br/>';
}) : null;
}) : null;
gtCodeElem ?
gtCodeElem.forEach(function(elem) {
var lnts = elem.parentNode.parentNode ? elem.parentNode.parentNode.querySelectorAll('.lnt') : null;
lnts ?
lnts.forEach(function(lnt) {
lnt.innerHTML = '><br/>';
}) : null;
}) : null;
// =================================================================
</script>