Skip to content

Commit

Permalink
feat: add exclude_languages feature to prismjs (hexojs#5182)
Browse files Browse the repository at this point in the history
* feat: add exclude_languages feature to prismjs

* feat: add exclude_languages feature to prismjs
  • Loading branch information
D-Sketon authored Apr 20, 2023
1 parent 46fbdcf commit 8251df3
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/hexo/default_config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ export = {
prismjs: {
preprocess: true,
line_number: true,
tab_replace: ''
tab_replace: '',
exclude_languages: []
},
// Category & Tag
default_category: 'uncategorized',
Expand Down
4 changes: 4 additions & 0 deletions lib/plugins/highlight/prism.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,9 @@ module.exports = function(code, options) {

if (!prismHighlight) prismHighlight = require('hexo-util').prismHighlight;

if (Array.isArray(prismjsCfg.exclude_languages) && prismjsCfg.exclude_languages.includes(prismjsOptions.lang)) {
// Only wrap with <pre><code class="lang"></code></pre>
return `<pre><code class="${prismjsOptions.lang}">${require('hexo-util').escapeHTML(code)}</code></pre>`;
}
return prismHighlight(code, prismjsOptions);
};
15 changes: 15 additions & 0 deletions test/scripts/filters/backtick_code_block.js
Original file line number Diff line number Diff line change
Expand Up @@ -635,5 +635,20 @@ describe('Backtick code block', () => {
codeBlock(data);
data.content.should.eql('<hexoPostRenderCodeBlock>' + expected + '</hexoPostRenderCodeBlock>');
});

it('prism only wrap with pre and code', () => {
hexo.config.prismjs.exclude_languages = ['js'];
const data = {
content: [
'``` js',
code,
'```'
].join('\n')
};
const escapeSwigTag = str => str.replace(/{/g, '&#123;').replace(/}/g, '&#125;');
const expected = `<pre><code class="js">${escapeSwigTag(util.escapeHTML(code))}</code></pre>`;
codeBlock(data);
data.content.should.eql('<hexoPostRenderCodeBlock>' + expected + '</hexoPostRenderCodeBlock>');
});
});
});

0 comments on commit 8251df3

Please sign in to comment.