Skip to content

Commit

Permalink
feat(tag/code): add languageAttr option (hexojs/hexo-util#278)
Browse files Browse the repository at this point in the history
  • Loading branch information
renbaoshuo committed Aug 19, 2022
1 parent 8a08bb9 commit f935f18
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 3 deletions.
1 change: 1 addition & 0 deletions lib/hexo/default_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ module.exports = {
tab_replace: '',
wrap: true,
exclude_languages: [],
language_attr: false,
hljs: false
},
prismjs: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ function backtickCodeBlock(data) {
tab: hljsCfg.tab_replace,
wrap: hljsCfg.wrap,
lang,
languageAttr: hljsCfg.language_attr,
caption
};

Expand Down
12 changes: 9 additions & 3 deletions lib/plugins/tag/code.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand Down Expand Up @@ -85,6 +85,10 @@ function parseArgs(args) {
}
break;
}
case 'language_attr': {
language_attr = value === 'true';
break;
}
default: {
_else.push(args[i]);
}
Expand All @@ -105,6 +109,7 @@ function parseArgs(args) {

return {
lang,
language_attr,
firstLine,
caption,
line_number,
Expand Down Expand Up @@ -138,7 +143,7 @@ module.exports = ctx => function codeTag(args, content) {
return `<pre><code>${escapeHTML(content)}</code></pre>`;
}

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;
Expand Down Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions lib/plugins/tag/include_code.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
8 changes: 8 additions & 0 deletions test/scripts/tags/code.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
16 changes: 16 additions & 0 deletions test/scripts/tags/include_code.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: '<span>Hello world</span><a href="/downloads/code/test.js">view raw</a>',
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 = [
'}'
Expand Down

0 comments on commit f935f18

Please sign in to comment.