feat: 优化highlight判断结果
This commit is contained in:
parent
58380da0ae
commit
1555c24441
|
@ -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
|
||||
}));
|
||||
};
|
||||
|
|
|
@ -98,6 +98,11 @@ pre.highlight-wrap code {
|
|||
border-radius: 10px
|
||||
}
|
||||
|
||||
|
||||
.highlight-wrap > code > table {
|
||||
color: #fff;
|
||||
}
|
||||
|
||||
.copy-code {
|
||||
color: #fff;
|
||||
position: absolute;
|
||||
|
|
|
@ -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('<a class="copy-code" data-clipboard-target="#highlight-code-' + i + '" title="拷贝代码">复制</a>');
|
||||
}
|
||||
|
||||
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 () {
|
||||
|
|
Loading…
Reference in New Issue