From 12e3f59b97cd901d3c084089821118ea957b3941 Mon Sep 17 00:00:00 2001 From: jeffreytse Date: Tue, 18 Aug 2020 14:02:07 +0800 Subject: fix: wrong lang type of code block --- assets/js/main.js | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/assets/js/main.js b/assets/js/main.js index 0a11d07..bb295f4 100644 --- a/assets/js/main.js +++ b/assets/js/main.js @@ -59,13 +59,25 @@ function smoothScrollTo(y, time) { // Init highlight js document.addEventListener('DOMContentLoaded', function(event) { document.querySelectorAll('pre code').forEach((block) => { - hljs.highlightBlock(block); + var outer = block.parentElement.parentElement.parentElement; var lang = block.getAttribute('data-lang'); - if (!lang) { + for (var cls of outer.classList) { + if (cls.startsWith('language-')) { + lang = cls; + break; + } + } + if (lang) { + block.setAttribute('class', 'hljs ' + lang); + } else { lang = block .getAttribute('class') .replace('hljs ', ''); } + if (lang.startsWith('language-')) { + lang = lang.substr(9); + } block.parentNode.setAttribute('data-lang', lang); + hljs.highlightBlock(block); }); }); -- cgit v1.2.3