From 6db22cccd87f8a51b37b642c2572bd669eb2cbe1 Mon Sep 17 00:00:00 2001 From: D-Sketon <2055272094@qq.com> Date: Sat, 1 Apr 2023 21:55:58 +0800 Subject: [PATCH 1/2] feat: add exclude_languages feature to prismjs --- lib/hexo/default_config.ts | 3 ++- lib/plugins/highlight/prism.ts | 7 ++++++- test/scripts/filters/backtick_code_block.js | 15 +++++++++++++++ 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/lib/hexo/default_config.ts b/lib/hexo/default_config.ts index 379f80737e..5e01f5781d 100644 --- a/lib/hexo/default_config.ts +++ b/lib/hexo/default_config.ts @@ -51,7 +51,8 @@ export = { prismjs: { preprocess: true, line_number: true, - tab_replace: '' + tab_replace: '', + exclude_languages: [] }, // Category & Tag default_category: 'uncategorized', diff --git a/lib/plugins/highlight/prism.ts b/lib/plugins/highlight/prism.ts index 1e37674fd0..ec5200758d 100644 --- a/lib/plugins/highlight/prism.ts +++ b/lib/plugins/highlight/prism.ts @@ -20,5 +20,10 @@ module.exports = function(code, options) { if (!prismHighlight) prismHighlight = require('hexo-util').prismHighlight; - return prismHighlight(code, prismjsOptions); + if (Array.isArray(prismjsCfg.exclude_languages) && prismjsCfg.exclude_languages.includes(prismjsOptions.lang)) { + // Only wrap with
+ return `
${require('hexo-util').escapeHTML(code)}
`; + } else { + return prismHighlight(code, prismjsOptions); + } }; diff --git a/test/scripts/filters/backtick_code_block.js b/test/scripts/filters/backtick_code_block.js index 4b7c932f52..5c75bbccdb 100644 --- a/test/scripts/filters/backtick_code_block.js +++ b/test/scripts/filters/backtick_code_block.js @@ -635,5 +635,20 @@ describe('Backtick code block', () => { codeBlock(data); data.content.should.eql('' + expected + ''); }); + + 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, '{').replace(/}/g, '}'); + const expected = `
${escapeSwigTag(util.escapeHTML(code))}
`; + codeBlock(data); + data.content.should.eql('' + expected + ''); + }); }); }); From 522b6d99c86fa274092beb07c3c785ac4af97d09 Mon Sep 17 00:00:00 2001 From: D-Sketon <2055272094@qq.com> Date: Sat, 1 Apr 2023 22:09:43 +0800 Subject: [PATCH 2/2] feat: add exclude_languages feature to prismjs --- lib/plugins/highlight/prism.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/plugins/highlight/prism.ts b/lib/plugins/highlight/prism.ts index ec5200758d..33304068d4 100644 --- a/lib/plugins/highlight/prism.ts +++ b/lib/plugins/highlight/prism.ts @@ -23,7 +23,6 @@ module.exports = function(code, options) { if (Array.isArray(prismjsCfg.exclude_languages) && prismjsCfg.exclude_languages.includes(prismjsOptions.lang)) { // Only wrap with
return `
${require('hexo-util').escapeHTML(code)}
`; - } else { - return prismHighlight(code, prismjsOptions); } + return prismHighlight(code, prismjsOptions); };