Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(tag/code): add language_attr option (hexojs/hexo-util#278) #5017

Merged
merged 1 commit into from
Sep 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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