feat: 优化highlight判断结果

This commit is contained in:
neo 2019-11-18 18:12:37 +08:00
parent 58380da0ae
commit 1555c24441
3 changed files with 32 additions and 26 deletions

View File

@ -8,7 +8,11 @@ importScripts('https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@9.16.2/build/
onmessage = function (event) { onmessage = function (event) {
var data = JSON.parse(event.data); 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
}));
}; };

View File

@ -98,6 +98,11 @@ pre.highlight-wrap code {
border-radius: 10px border-radius: 10px
} }
.highlight-wrap > code > table {
color: #fff;
}
.copy-code { .copy-code {
color: #fff; color: #fff;
position: absolute; position: absolute;

View File

@ -5,6 +5,22 @@
(function ($) { (function ($) {
function initHighlight() { 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) { function createBar(i) {
var attributes = { var attributes = {
'autocomplete': 'off', 'autocomplete': 'off',
@ -15,34 +31,14 @@
}; };
var codeCls = $('pre:eq(' + i + ')')[0].children[0].className; 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'); $('pre:eq(' + i + ')').addClass('highlight-wrap');
for (var t in attributes) { for (var t in attributes) {
$('pre:eq(' + i + ')').attr(t, attributes[t]); $('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>'); .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 // 使用 web worker
function highlightWorker() { function highlightWorker() {
var codeBlocks = document.querySelectorAll('pre code'); var codeBlocks = document.querySelectorAll('pre code');
@ -56,11 +52,9 @@
worker.onmessage = (event) => { worker.onmessage = (event) => {
var highlightData = JSON.parse(event.data); var highlightData = JSON.parse(event.data);
codeBlocks[highlightData.index].innerHTML = highlightData.result; codeBlocks[highlightData.index].innerHTML = highlightData.result.value;
createBar(highlightData.index); createBar(highlightData.index);
new ClipboardJS('.copy-code');
if (highlightData.index === codeBlocks.length - 1) { if (highlightData.index === codeBlocks.length - 1) {
// 结束关闭worker // 结束关闭worker
worker.terminate(); worker.terminate();
@ -73,6 +67,7 @@
codeBlocks.forEach(function (block, index) { codeBlocks.forEach(function (block, index) {
worker.postMessage(JSON.stringify({ worker.postMessage(JSON.stringify({
code: block.textContent, code: block.textContent,
lang: getLang(block.className),
index, index,
total: codeBlocks.length, total: codeBlocks.length,
})); }));
@ -99,6 +94,8 @@
} else { } else {
highlightWorker(); highlightWorker();
} }
new ClipboardJS('.copy-code');
} }
$(document).ready(function () { $(document).ready(function () {