From fcd48c1eed201a32bd8e49b3e17a07dcbdd2eebe Mon Sep 17 00:00:00 2001 From: Mimi <1119186082@qq.com> Date: Wed, 24 Mar 2021 13:58:08 +0800 Subject: [PATCH 1/8] Update dependency highlight.js --- lib/highlight.js | 10 +++++++--- package.json | 2 +- test/highlight.spec.js | 24 ++++++++++++++++++------ 3 files changed, 26 insertions(+), 10 deletions(-) diff --git a/lib/highlight.js b/lib/highlight.js index f0856e03..7e3bc01c 100644 --- a/lib/highlight.js +++ b/lib/highlight.js @@ -127,15 +127,19 @@ function highlight(str, options) { return tryHighlight(str, result.language) || result; } -function tryHighlight(str, lang) { +function tryHighlight(str, language) { try { const matching = str.match(/(\r?\n)/); const separator = matching ? matching[1] : ''; const lines = matching ? str.split(separator) : [str]; - let result = hljs.highlight(lang, lines.shift()); + let result = hljs.highlight(lines.shift(), { language }); let html = result.value; while (lines.length > 0) { - result = hljs.highlight(lang, lines.shift(), false, result.top); + result = hljs.highlight(lines.shift(), { + language, + ignoreIllegals: false, + continuation: result.top + }); html += separator + result.value; } diff --git a/package.json b/package.json index 84a91a6b..c28dee13 100644 --- a/package.json +++ b/package.json @@ -47,7 +47,7 @@ "camel-case": "^4.0.0", "cross-spawn": "^7.0.0", "deepmerge": "^4.2.2", - "highlight.js": "^10.0.0", + "highlight.js": "^10.7.1", "htmlparser2": "^6.0.0", "prismjs": "^1.17.1", "strip-indent": "^3.0.0" diff --git a/test/highlight.spec.js b/test/highlight.spec.js index 5990e0f6..66d565e5 100644 --- a/test/highlight.spec.js +++ b/test/highlight.spec.js @@ -37,7 +37,9 @@ function code(str, lang) { let data; if (lang) { - data = hljs.highlight(lang.toLowerCase(), str); + data = hljs.highlight(str, { + language: lang.toLowerCase() + }); } else if (lang === null) { data = {value: str}; } else { @@ -115,7 +117,9 @@ describe('highlight', () => { hljs.configure({classPrefix: ''}); result.should.eql([ '
',
- hljs.highlight('json', testString).value,
+ hljs.highlight(testString, {
+ language: 'json'
+ }).value,
'
'
].join(''));
validateHtmlAsync(result, done);
@@ -126,7 +130,9 @@ describe('highlight', () => {
hljs.configure({classPrefix: 'hljs-'});
result.should.eql([
'',
- hljs.highlight('json', testString).value,
+ hljs.highlight(testString, {
+ language: 'json'
+ }).value,
'
'
].join(''));
validateHtmlAsync(result, done);
@@ -137,7 +143,9 @@ describe('highlight', () => {
hljs.configure({classPrefix: 'hljs-'});
result.should.eql([
'',
- hljs.highlight('json', testString).value.replace('{', '{'),
+ hljs.highlight(testString, {
+ language: 'json'
+ }).value.replace('{', '{'),
'
'
].join(''));
validateHtmlAsync(result, done);
@@ -148,7 +156,9 @@ describe('highlight', () => {
hljs.configure({classPrefix: 'hljs-'});
result.should.eql([
'',
- hljs.highlight('json', testString).value,
+ hljs.highlight(testString, {
+ language: 'json'
+ }).value,
'\n
'
].join(''));
validateHtmlAsync(result, done);
@@ -252,7 +262,9 @@ describe('highlight', () => {
result.should.eql([
'',
spaces,
- hljs.highlight('json', testString).value,
+ hljs.highlight(testString, {
+ language: 'json'
+ }).value,
'
'
].join(''));
validateHtmlAsync(result, done);
From ee489391e4683cebf8fdc2aeb36489aa80f26b96 Mon Sep 17 00:00:00 2001
From: Mimi <1119186082@qq.com>
Date: Thu, 25 Mar 2021 10:32:02 +0800
Subject: [PATCH 2/8] Fix tryHighlight
---
lib/highlight.js | 40 ++++++++++++++++++++++------------------
1 file changed, 22 insertions(+), 18 deletions(-)
diff --git a/lib/highlight.js b/lib/highlight.js
index 7e3bc01c..f3263bf5 100644
--- a/lib/highlight.js
+++ b/lib/highlight.js
@@ -128,26 +128,30 @@ function highlight(str, options) {
}
function tryHighlight(str, language) {
- try {
- const matching = str.match(/(\r?\n)/);
- const separator = matching ? matching[1] : '';
- const lines = matching ? str.split(separator) : [str];
- let result = hljs.highlight(lines.shift(), { language });
- let html = result.value;
- while (lines.length > 0) {
- result = hljs.highlight(lines.shift(), {
- language,
- ignoreIllegals: false,
- continuation: result.top
- });
- html += separator + result.value;
- }
+ const res = hljs.highlight(str, {
+ language,
+ ignoreIllegals: false
+ });
- result.value = html;
- return result;
- } catch (err) {
+ const tokenStack = [];
- }
+ res.value = res.value.split('\n').map(line => {
+ let prepend = '';
+ let append = '';
+ if (tokenStack.length > 0) {
+ prepend = tokenStack.map(token => ``).join('');
+ }
+ const matches = line.matchAll(/(|<\/span>)/g);
+ for (const match of matches) {
+ if (match[0] === '') tokenStack.shift();
+ else tokenStack.unshift(match[2]);
+ }
+ if (tokenStack.length > 0) {
+ append = tokenStack.map(() => ``).join('');
+ }
+ return prepend + line + append;
+ }).join('\n');
+ return res;
}
module.exports = highlightUtil;
From 66310752f2279eab6a6f2cbb28613f5f64a0feb6 Mon Sep 17 00:00:00 2001
From: Mimi <1119186082@qq.com>
Date: Thu, 25 Mar 2021 11:33:52 +0800
Subject: [PATCH 3/8] Performance improvement
---
lib/highlight.js | 34 ++++++++++------------------------
lib/prism.js | 2 +-
test/highlight.spec.js | 10 +++++-----
3 files changed, 16 insertions(+), 30 deletions(-)
diff --git a/lib/highlight.js b/lib/highlight.js
index f3263bf5..49cc3bf2 100644
--- a/lib/highlight.js
+++ b/lib/highlight.js
@@ -3,7 +3,6 @@
const hljs = require('highlight.js');
const stripIndent = require('strip-indent');
const alias = require('../highlight_alias.json');
-const escapeHTML = require('./escape_html');
function highlightUtil(str, options = {}) {
if (typeof str !== 'string') throw new TypeError('str must be a string!');
@@ -103,36 +102,23 @@ function highlight(str, options) {
if (!lang && autoDetect) {
const result = hljs.highlightAuto(str);
- if (result.relevance > 0 && result.language) lang = result.language;
+ return closeTags(result);
}
- if (!lang) {
- lang = 'plain';
+ if (!lang || !alias.aliases[lang]) {
+ lang = 'plaintext';
}
- const result = {
- value: escapeHTML(str),
- language: lang.toLowerCase()
- };
-
- if (result.language === 'plain') {
- return result;
- }
-
- if (!alias.aliases[result.language]) {
- result.language = 'plain';
- return result;
- }
-
- return tryHighlight(str, result.language) || result;
-}
-
-function tryHighlight(str, language) {
const res = hljs.highlight(str, {
- language,
- ignoreIllegals: false
+ language: lang.toLowerCase(),
+ ignoreIllegals: true
});
+ return closeTags(res);
+}
+
+// https://github.com/hexojs/hexo-util/issues/10
+function closeTags(res) {
const tokenStack = [];
res.value = res.value.split('\n').map(line => {
diff --git a/lib/prism.js b/lib/prism.js
index 9bac50e1..7201f2ca 100644
--- a/lib/prism.js
+++ b/lib/prism.js
@@ -67,7 +67,7 @@ function PrismUtil(str, options = {}) {
} = options;
// To be consistent with highlight.js
- const language = lang === 'plain' || lang === 'none' ? 'none' : lang;
+ const language = lang === 'plaintext' || lang === 'none' ? 'none' : lang;
const preTagClassArr = [];
const preTagAttrArr = [];
diff --git a/test/highlight.spec.js b/test/highlight.spec.js
index 66d565e5..86442389 100644
--- a/test/highlight.spec.js
+++ b/test/highlight.spec.js
@@ -12,7 +12,7 @@ const testJson = {
const testString = JSON.stringify(testJson, null, ' ');
-const start = ''; @@ -95,7 +95,7 @@ describe('highlight', () => { it('wrap: false (without hljs, without lang)', done => { const result = highlight(testString, {gutter: false, wrap: false}); result.should.eql([ - '', + '
' ].join('')); @@ -105,7 +105,7 @@ describe('highlight', () => { it('wrap: false (with hljs, without lang)', done => { const result = highlight(testString, {gutter: false, wrap: false, hljs: true}); result.should.eql([ - '', entities.encode(testString), '
', + '
' ].join('')); @@ -209,7 +209,7 @@ describe('highlight', () => { }); result.should.eql([ - `', entities.encode(testString), '
${caption}
`, + ` ${caption}
`, gutter(1, 4), code(testString), end @@ -228,7 +228,7 @@ describe('highlight', () => { result.should.eql([ ' ', `' ].join('')); From c3acdf8bd4773a32fea9480a05410af2e606b70e Mon Sep 17 00:00:00 2001 From: Mimi <1119186082@qq.com> Date: Thu, 25 Mar 2021 14:01:27 +0800 Subject: [PATCH 4/8] Run toLowerCase first --- lib/highlight.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/highlight.js b/lib/highlight.js index 49cc3bf2..68f9fa6f 100644 --- a/lib/highlight.js +++ b/lib/highlight.js @@ -100,7 +100,9 @@ function highlight(str, options) { let { lang } = options; const { autoDetect = false } = options; - if (!lang && autoDetect) { + if (lang) { + lang = lang.toLowerCase(); + } else if (autoDetect) { const result = hljs.highlightAuto(str); return closeTags(result); } @@ -110,7 +112,7 @@ function highlight(str, options) { } const res = hljs.highlight(str, { - language: lang.toLowerCase(), + language: lang, ignoreIllegals: true }); From e3fe2754c4c2806a0d1dc4ea4e7560871d1302fe Mon Sep 17 00:00:00 2001 From: Mimi <1119186082@qq.com> Date: Thu, 25 Mar 2021 14:37:36 +0800 Subject: [PATCH 5/8] Code style update --- lib/highlight.js | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/lib/highlight.js b/lib/highlight.js index 68f9fa6f..16354652 100644 --- a/lib/highlight.js +++ b/lib/highlight.js @@ -124,19 +124,13 @@ function closeTags(res) { const tokenStack = []; res.value = res.value.split('\n').map(line => { - let prepend = ''; - let append = ''; - if (tokenStack.length > 0) { - prepend = tokenStack.map(token => ` `, - '`).join(''); - } + let prepend = tokenStack.map(token => ``).join(''); const matches = line.matchAll(/(|<\/span>)/g); for (const match of matches) { if (match[0] === '') tokenStack.shift(); else tokenStack.unshift(match[2]); } - if (tokenStack.length > 0) { - append = tokenStack.map(() => ``).join(''); - } + let append = "".repeat(tokenStack.length) return prepend + line + append; }).join('\n'); return res; From db5edd869b072066aafd079a42af7ea20b282ffa Mon Sep 17 00:00:00 2001 From: Mimi <1119186082@qq.com> Date: Thu, 25 Mar 2021 14:37:12 +0800 Subject: [PATCH 6/8] Prefer const --- lib/highlight.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/highlight.js b/lib/highlight.js index 16354652..4851b11a 100644 --- a/lib/highlight.js +++ b/lib/highlight.js @@ -124,13 +124,13 @@ function closeTags(res) { const tokenStack = []; res.value = res.value.split('\n').map(line => { - let prepend = tokenStack.map(token => ``).join(''); + const prepend = tokenStack.map(token => ``).join(''); const matches = line.matchAll(/(|<\/span>)/g); for (const match of matches) { if (match[0] === '') tokenStack.shift(); else tokenStack.unshift(match[2]); } - let append = "".repeat(tokenStack.length) + const append = "".repeat(tokenStack.length); return prepend + line + append; }).join('\n'); return res; From 70d7b6caf9b7b453245d6de0d1c682a7325b16af Mon Sep 17 00:00:00 2001 From: Mimi <1119186082@qq.com> Date: Fri, 26 Mar 2021 12:54:50 +0800 Subject: [PATCH 7/8] Single quote --- lib/highlight.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/highlight.js b/lib/highlight.js index 4851b11a..132004d3 100644 --- a/lib/highlight.js +++ b/lib/highlight.js @@ -130,7 +130,7 @@ function closeTags(res) { if (match[0] === '') tokenStack.shift(); else tokenStack.unshift(match[2]); } - const append = "".repeat(tokenStack.length); + const append = ''.repeat(tokenStack.length); return prepend + line + append; }).join('\n'); return res; From 979873b63a725377c2bd6ad834d790023496130d Mon Sep 17 00:00:00 2001 From: Mimi <1119186082@qq.com> Date: Sun, 28 Mar 2021 09:40:39 +0800 Subject: [PATCH 8/8] Update package.json --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index c28dee13..25a94cac 100644 --- a/package.json +++ b/package.json @@ -53,6 +53,6 @@ "strip-indent": "^3.0.0" }, "engines": { - "node": ">=10.13.0" + "node": ">=12.4.0" } }', + '
', entities.encode(testString), '