From 0617cfa46878949bf42e92950812c0ff9ce06c17 Mon Sep 17 00:00:00 2001 From: Mimi <1119186082@qq.com> Date: Fri, 3 Sep 2021 17:20:32 +0800 Subject: [PATCH] fix(highlight): highlight sublanguages with tab (#260) * fix(highlight): highlight sublanguages with tab * refactor(prism): replaceTabs --- lib/highlight.js | 10 +--------- lib/prism.js | 10 +--------- test/highlight.spec.js | 19 ++++++++++++++----- 3 files changed, 16 insertions(+), 23 deletions(-) diff --git a/lib/highlight.js b/lib/highlight.js index 132004d3..b267fbaa 100644 --- a/lib/highlight.js +++ b/lib/highlight.js @@ -85,15 +85,7 @@ function formatLine(line, lineno, marked, options, wrap) { } function replaceTabs(str, tab) { - return str.replace(/^\t+/, match => { - let result = ''; - - for (let i = 0, len = match.length; i < len; i++) { - result += tab; - } - - return result; - }); + return str.replace(/\t+/, match => tab.repeat(match.length)); } function highlight(str, options) { diff --git a/lib/prism.js b/lib/prism.js index 7201f2ca..c6ed45ce 100644 --- a/lib/prism.js +++ b/lib/prism.js @@ -41,15 +41,7 @@ function lineNumberUtil(code) { } function replaceTabs(str, tab) { - return str.replace(/^\t+/gm, match => { - let result = ''; - - for (let i = 0, len = match.length; i < len; i++) { - result += tab; - } - - return result; - }); + return str.replace(/^\t+/gm, match => tab.repeat(match.length)); } function PrismUtil(str, options = {}) { diff --git a/test/highlight.spec.js b/test/highlight.spec.js index 86442389..6e215c0e 100644 --- a/test/highlight.spec.js +++ b/test/highlight.spec.js @@ -145,7 +145,7 @@ describe('highlight', () => { '
',
hljs.highlight(testString, {
language: 'json'
- }).value.replace('{', '{'),
+ }).value.replace('{', '{'),
'
'
].join(''));
validateHtmlAsync(result, done);
@@ -290,12 +290,21 @@ describe('highlight', () => {
result.should.eql([
'