Skip to content

Commit

Permalink
Exclude some languages in code highlghting. (#3865)
Browse files Browse the repository at this point in the history
Signed-off-by: corvofeng <corvofeng@gmail.com>
  • Loading branch information
corvofeng authored Feb 27, 2022
1 parent 035c73f commit c73dbed
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/hexo/default_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ module.exports = {
line_number: true,
tab_replace: '',
wrap: true,
exclude_languages: [],
hljs: false
},
prismjs: {
Expand Down
7 changes: 7 additions & 0 deletions lib/plugins/filter/before_post_render/backtick_code_block.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,13 @@ function backtickCodeBlock(data) {
}
}

if (Array.isArray(hljsCfg.exclude_languages) && hljsCfg.exclude_languages.includes(options.lang)) {
// Only wrap with <pre><code class="lang"></code></pre>
options.wrap = false;
options.gutter = false;
options.autoDetect = false;
}

content = highlight(content, options);
}

Expand Down
20 changes: 20 additions & 0 deletions test/scripts/filters/backtick_code_block.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,26 @@ describe('Backtick code block', () => {
data.content.should.eql('<hexoPostRenderCodeBlock>' + expected + '</hexoPostRenderCodeBlock>');
});

it('only wrap with pre and code', () => {
hexo.config.highlight.exclude_languages = ['js'];
hexo.config.highlight.hljs = true;
const data = {
content: [
'``` js',
code,
'```'
].join('\n')
};
const expected = highlight(code, {
lang: 'js',
gutter: false,
hljs: true,
wrap: false
});
codeBlock(data);
data.content.should.eql('<hexoPostRenderCodeBlock>' + expected + '</hexoPostRenderCodeBlock>');
});

it('line number false, don`t care first_line_number inilne', () => {
hexo.config.highlight.line_number = false;
hexo.config.highlight.first_line_number = 'inilne';
Expand Down

0 comments on commit c73dbed

Please sign in to comment.