diff --git a/lib/hexo/default_config.js b/lib/hexo/default_config.js index ecf688faf8..3ef8cdd87d 100644 --- a/lib/hexo/default_config.js +++ b/lib/hexo/default_config.js @@ -47,6 +47,7 @@ module.exports = { tab_replace: '', wrap: true, exclude_languages: [], + language_attr: false, hljs: false }, prismjs: { diff --git a/lib/plugins/filter/before_post_render/backtick_code_block.js b/lib/plugins/filter/before_post_render/backtick_code_block.js index 6d3b7ed1ee..871146c42e 100644 --- a/lib/plugins/filter/before_post_render/backtick_code_block.js +++ b/lib/plugins/filter/before_post_render/backtick_code_block.js @@ -73,6 +73,7 @@ function backtickCodeBlock(data) { tab: hljsCfg.tab_replace, wrap: hljsCfg.wrap, lang, + languageAttr: hljsCfg.language_attr, caption }; diff --git a/lib/plugins/tag/code.js b/lib/plugins/tag/code.js index 4b2fdd07f0..63d16743c3 100644 --- a/lib/plugins/tag/code.js +++ b/lib/plugins/tag/code.js @@ -33,7 +33,7 @@ const rCaption = /\S[\S\s]*/; function parseArgs(args) { const _else = []; const len = args.length; - let lang, + let lang, language_attr, line_number, line_threshold, wrap; let firstLine = 1; const mark = []; @@ -85,6 +85,10 @@ function parseArgs(args) { } break; } + case 'language_attr': { + language_attr = value === 'true'; + break; + } default: { _else.push(args[i]); } @@ -105,6 +109,7 @@ function parseArgs(args) { return { lang, + language_attr, firstLine, caption, line_number, @@ -138,7 +143,7 @@ module.exports = ctx => function codeTag(args, content) { return `
${escapeHTML(content)}
`;
}
- const { lang, firstLine, caption, line_number, line_threshold, mark, wrap } = parseArgs(args);
+ const { lang, language_attr, firstLine, caption, line_number, line_threshold, mark, wrap } = parseArgs(args);
if (prismjsCfg.enable) {
const shouldUseLineNumbers = typeof line_number !== 'undefined' ? line_number : prismjsCfg.line_number;
@@ -185,7 +190,8 @@ module.exports = ctx => function codeTag(args, content) {
mark,
tab: hljsCfg.tab_replace,
autoDetect: hljsCfg.auto_detect,
- wrap: typeof wrap === 'boolean' ? wrap : hljsCfg.wrap
+ wrap: typeof wrap === 'boolean' ? wrap : hljsCfg.wrap,
+ languageAttr: typeof language_attr === 'boolean' ? language_attr : hljsCfg.language_attr
};
if (!highlight) highlight = require('hexo-util').highlight;
diff --git a/lib/plugins/tag/include_code.js b/lib/plugins/tag/include_code.js
index a7cd9f6c3a..a024d81ac2 100644
--- a/lib/plugins/tag/include_code.js
+++ b/lib/plugins/tag/include_code.js
@@ -89,6 +89,7 @@ module.exports = ctx => function includeCodeTag(args) {
const hljsOptions = {
lang,
+ languageAttr: hljsCfg.language_attr,
caption,
gutter: hljsCfg.line_number && lines.length > line_threshold,
hljs: hljsCfg.hljs,
diff --git a/test/scripts/tags/code.js b/test/scripts/tags/code.js
index acf954815b..4a1220f9c1 100644
--- a/test/scripts/tags/code.js
+++ b/test/scripts/tags/code.js
@@ -173,6 +173,14 @@ describe('code', () => {
wrap: true
}));
});
+
+ it('language_attr', () => {
+ const result = code('lang:js language_attr:true', fixture);
+ result.should.eql(highlight(fixture, {
+ lang: 'js',
+ languageAttr: true
+ }));
+ });
});
describe('prismjs', () => {
diff --git a/test/scripts/tags/include_code.js b/test/scripts/tags/include_code.js
index 040b172098..48af4a33c2 100644
--- a/test/scripts/tags/include_code.js
+++ b/test/scripts/tags/include_code.js
@@ -62,6 +62,22 @@ describe('include_code', () => {
result.should.eql(expected);
});
+ it('language_attr', async () => {
+ const original = hexo.config.highlight.language_attr;
+ hexo.config.highlight.language_attr = true;
+
+ const expected = highlight(fixture, {
+ lang: 'js',
+ caption: 'Hello worldview raw',
+ languageAttr: true
+ });
+
+ const result = await code('Hello world lang:js test.js');
+ result.should.eql(expected);
+
+ hexo.config.highlight.language_attr = original;
+ });
+
it('from', async () => {
const fixture = [
'}'