diff --git a/highlight-worker.js b/highlight-worker.js index 5d5f492..77c2348 100644 --- a/highlight-worker.js +++ b/highlight-worker.js @@ -8,7 +8,11 @@ importScripts('https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@9.16.2/build/ onmessage = function (event) { var data = JSON.parse(event.data); - var result = self.hljs.highlightAuto(data.code); + var result = self.hljs.highlight(data.lang, data.code); - postMessage(JSON.stringify({result: result.value, index: data.index})); + postMessage(JSON.stringify({ + result: { + value: result.value, + }, index: data.index + })); }; diff --git a/mac-style.css b/mac-style.css index 7bb90f8..34612dc 100644 --- a/mac-style.css +++ b/mac-style.css @@ -98,6 +98,11 @@ pre.highlight-wrap code { border-radius: 10px } + +.highlight-wrap > code > table { + color: #fff; +} + .copy-code { color: #fff; position: absolute; diff --git a/wp-heightlight.js b/wp-heightlight.js index 01e1972..1a959e1 100644 --- a/wp-heightlight.js +++ b/wp-heightlight.js @@ -5,6 +5,22 @@ (function ($) { function initHighlight() { + function getLang(codeCls) { + var result = codeCls.match(/language-(\w+)/); + + var lang = "plaintext"; + + if (result && result.length) { + lang = result[1]; + } + + if (lang === "text") { + lang = "plaintext"; + } + + return lang; + } + function createBar(i) { var attributes = { 'autocomplete': 'off', @@ -15,34 +31,14 @@ }; var codeCls = $('pre:eq(' + i + ')')[0].children[0].className; - var result = codeCls.match(/language-(\w+)/); - - var lang = "text"; - - if (result && result.length) { - lang = result[1]; - } - $('pre:eq(' + i + ')').addClass('highlight-wrap'); for (var t in attributes) { $('pre:eq(' + i + ')').attr(t, attributes[t]); } - $('pre:eq(' + i + ') code').attr('data-rel', lang.toUpperCase()).attr({id: "highlight-code-" + i}) + $('pre:eq(' + i + ') code').addClass('hljs').attr('data-rel', getLang(codeCls).toUpperCase()).attr({id: "highlight-code-" + i}) .after('复制'); } - function highlight_code_worker_function() { - importScripts('https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@9.16.2/build/highlight.min.js'); - - onmessage = function (event) { - var data = JSON.parse(event.data); - - var result = self.hljs.highlightAuto(data.code); - - postMessage(JSON.stringify({result: result.value, index: data.index})); - }; - } - // 使用 web worker function highlightWorker() { var codeBlocks = document.querySelectorAll('pre code'); @@ -56,11 +52,9 @@ worker.onmessage = (event) => { var highlightData = JSON.parse(event.data); - codeBlocks[highlightData.index].innerHTML = highlightData.result; + codeBlocks[highlightData.index].innerHTML = highlightData.result.value; createBar(highlightData.index); - new ClipboardJS('.copy-code'); - if (highlightData.index === codeBlocks.length - 1) { // 结束关闭worker worker.terminate(); @@ -73,6 +67,7 @@ codeBlocks.forEach(function (block, index) { worker.postMessage(JSON.stringify({ code: block.textContent, + lang: getLang(block.className), index, total: codeBlocks.length, })); @@ -99,6 +94,8 @@ } else { highlightWorker(); } + + new ClipboardJS('.copy-code'); } $(document).ready(function () {