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('${escapeSwigTag(util.escapeHTML(code))}
`;
+ codeBlock(data);
+ data.content.should.eql('
return `${require('hexo-util').escapeHTML(code)}
`;
- } else {
- return prismHighlight(code, prismjsOptions);
}
+ return prismHighlight(code, prismjsOptions);
};