Skip to content

Commit

Permalink
fix(highlight): highlight sublanguages with tab (hexojs#260)
Browse files Browse the repository at this point in the history
* fix(highlight): highlight sublanguages with tab

* refactor(prism): replaceTabs
  • Loading branch information
stevenjoezhang authored and nevilm-lt committed Apr 22, 2022
1 parent 9703a1e commit d4cff75
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 23 deletions.
10 changes: 1 addition & 9 deletions lib/highlight.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
10 changes: 1 addition & 9 deletions lib/prism.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}) {
Expand Down
19 changes: 14 additions & 5 deletions test/highlight.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ describe('highlight', () => {
'<pre><code class="hljs json">',
hljs.highlight(testString, {
language: 'json'
}).value.replace('{', '<mark>{</mark>'),
}).value.replace('<span class="hljs-punctuation">{</span>', '<mark><span class="hljs-punctuation">{</span></mark>'),
'</code></pre>'
].join(''));
validateHtmlAsync(result, done);
Expand Down Expand Up @@ -290,12 +290,21 @@ describe('highlight', () => {
result.should.eql([
'<figure class="highlight php-template"><table><tr>',
gutter(1, 1),
code('<span class="xml"><span class="tag">&lt;<span class="name">node</span>&gt;</span></span><span class="php"><span class="meta">&lt;?php</span> <span class="keyword">echo</span> <span class="string">&quot;foo&quot;</span>; <span class="meta">?&gt;</span></span><span class="xml"><span class="tag">&lt;/<span class="name">node</span>&gt;</span></span>', null),
code('<span class="language-xml"><span class="tag">&lt;<span class="name">node</span>&gt;</span></span><span class="language-php"><span class="meta">&lt;?php</span> <span class="keyword">echo</span> <span class="string">&quot;foo&quot;</span>; <span class="meta">?&gt;</span></span><span class="language-xml"><span class="tag">&lt;/<span class="name">node</span>&gt;</span></span>', null),
end
].join(''));
validateHtmlAsync(result, done);
});

// https://github.com/hexojs/hexo/issues/4726
it('highlight sublanguages with tab', done => {
const spaces = ' ';
const str = '<script>\n\tfunction a() {\n\t\treturn;\n\t}\n</script>';
const result = highlight(str, { tab: spaces, autoDetect: true });
result.should.not.include('\t');
validateHtmlAsync(result, done);
});

it('parse multi-line strings correctly', done => {
const str = [
'var string = `',
Expand Down Expand Up @@ -383,7 +392,7 @@ describe('highlight', () => {
result.should.include(gutterStart);
result.should.include(codeStart);
result.should.include('code class="hljs javascript"');
result.should.include('class="hljs-function"');
result.should.include('class="hljs-keyword"');
result.should.include(gutter(1, 5));
validateHtmlAsync(result, done);
});
Expand All @@ -400,7 +409,7 @@ describe('highlight', () => {
result.should.not.include(gutterStart);
result.should.not.include(codeStart);
result.should.include('code class="hljs javascript"');
result.should.include('class="hljs-function"');
result.should.include('class="hljs-keyword"');
validateHtmlAsync(result, done);
});

Expand All @@ -416,7 +425,7 @@ describe('highlight', () => {
result.should.not.include(gutterStart);
result.should.include(codeStart);
result.should.include('code class="hljs javascript"');
result.should.include('class="hljs-function"');
result.should.include('class="hljs-keyword"');
validateHtmlAsync(result, done);
});
});

0 comments on commit d4cff75

Please sign in to comment.