Skip to content

Commit

Permalink
Add line threshold option for codeblocks
Browse files Browse the repository at this point in the history
  • Loading branch information
dbelokon committed Nov 22, 2021
1 parent 902cd70 commit 85dbb32
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 20 deletions.
33 changes: 29 additions & 4 deletions lib/plugins/tag/code.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ function parseArgs(args) {
const _else = [];
const len = args.length;
let lang,
line_number, wrap;
line_number, line_threshold, wrap;
let firstLine = 1;
const mark = [];
for (let i = 0; i < len; i++) {
Expand All @@ -55,6 +55,9 @@ function parseArgs(args) {
case 'line_number':
line_number = value === 'true';
break;
case 'line_threshold':
if (!isNaN(value)) line_threshold = +value;
break;
case 'first_line':
if (!isNaN(value)) firstLine = +value;
break;
Expand Down Expand Up @@ -105,6 +108,7 @@ function parseArgs(args) {
firstLine,
caption,
line_number,
line_threshold,
mark,
wrap
};
Expand Down Expand Up @@ -134,14 +138,23 @@ module.exports = ctx => function codeTag(args, content) {
return `<pre><code>${escapeHTML(content)}</code></pre>`;
}

const { lang, firstLine, caption, line_number, mark, wrap } = parseArgs(args);
const { lang, firstLine, caption, line_number, line_threshold, mark, wrap } = parseArgs(args);

if (prismjsCfg.enable) {
const shouldUseLineNumbers = typeof line_number !== 'undefined' ? line_number : prismjsCfg.line_number;
let surpassesLineThreshold;

if (typeof line_threshold !== 'undefined') {
surpassesLineThreshold = content.split('\n').length > line_threshold;
} else {
surpassesLineThreshold = content.split('\n').length > (prismjsCfg.line_threshold || 0);
}

const prismjsOption = {
lang,
firstLine,
caption,
lineNumber: typeof line_number !== 'undefined' ? line_number : prismjsCfg.line_number,
lineNumber: shouldUseLineNumbers && surpassesLineThreshold,
mark,
tab: prismjsCfg.tab_replace,
isPreprocess: prismjsCfg.preprocess
Expand All @@ -151,11 +164,23 @@ module.exports = ctx => function codeTag(args, content) {

content = prismHighlight(content, prismjsOption);
} else {
const shouldUseLineNumbers = typeof line_number !== 'undefined'
? line_number : hljsCfg.line_number;
let surpassesLineThreshold;

if (typeof line_threshold !== 'undefined') {
surpassesLineThreshold
= content.split('\n').length > line_threshold;
} else {
surpassesLineThreshold
= content.split('\n').length > (hljsCfg.line_threshold || 0);
}

const hljsOption = {
lang: typeof lang !== 'undefined' ? lang : '',
firstLine,
caption,
gutter: typeof line_number !== 'undefined' ? line_number : hljsCfg.line_number,
gutter: shouldUseLineNumbers && surpassesLineThreshold,
hljs: hljsCfg.hljs,
mark,
tab: hljsCfg.tab_replace,
Expand Down
38 changes: 22 additions & 16 deletions lib/plugins/tag/include_code.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,22 +60,6 @@ module.exports = ctx => function includeCodeTag(args) {
const hljsCfg = ctx.config.highlight || {};
const prismjsCfg = ctx.config.prismjs || {};

const hljsOptions = {
lang,
caption,
gutter: hljsCfg.line_number,
hljs: hljsCfg.hljs,
tab: hljsCfg.tab_replace
};

const prismjsOptions = {
lang,
caption,
lineNumber: prismjsCfg.line_number,
tab: prismjsCfg.tab_replace,
isPreprocess: prismjsCfg.preprocess
};

return exists(src).then(exist => {
if (exist) return readFile(src);
}).then(code => {
Expand All @@ -85,10 +69,32 @@ module.exports = ctx => function includeCodeTag(args) {
code = lines.slice(from, to).join('\n').trim();

if (prismjsCfg.enable) {
const line_threshold = prismjsCfg.line_threshold
? prismjsCfg.line_threshold : 0;

const prismjsOptions = {
lang,
caption,
lineNumber: prismjsCfg.line_number && lines.length > line_threshold,
tab: prismjsCfg.tab_replace,
isPreprocess: prismjsCfg.preprocess
};

if (!prismHighlight) prismHighlight = require('hexo-util').prismHighlight;

return prismHighlight(code, prismjsOptions);
} else if (hljsCfg.enable) {
const line_threshold = hljsCfg.line_threshold
? hljsCfg.line_threshold : 0;

const hljsOptions = {
lang,
caption,
gutter: hljsCfg.line_number && lines.length > line_threshold,
hljs: hljsCfg.hljs,
tab: hljsCfg.tab_replace
};

if (!highlight) highlight = require('hexo-util').highlight;

return highlight(code, hljsOptions);
Expand Down
30 changes: 30 additions & 0 deletions test/scripts/tags/code.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,21 @@ describe('code', () => {
}));
});

it('line_threshold', () => {
let result = code('line_number:false line_threshold:1', fixture);
result.should.eql(highlight(fixture, {
gutter: false
}));
result = code('line_number:true line_threshold:1', fixture);
result.should.eql(highlight(fixture, {
gutter: true
}));
result = code('line_number:true line_threshold:3', fixture);
result.should.eql(highlight(fixture, {
gutter: false
}));
});

it('highlight disable', () => {
const result = code('highlight:false', fixture);
result.should.eql('<pre><code>' + escapeHTML(fixture) + '</code></pre>');
Expand Down Expand Up @@ -202,6 +217,21 @@ describe('code', () => {
}));
});

it('line_threshold', () => {
let result = code('line_number:false line_threshold:1', fixture);
result.should.eql(prism(fixture, {
lineNumber: false
}));
result = code('line_number:true line_threshold:1', fixture);
result.should.eql(prism(fixture, {
lineNumber: true
}));
result = code('line_number:true line_threshold:3', fixture);
result.should.eql(prism(fixture, {
lineNumber: false
}));
});

it('highlight disable', () => {
const result = code('highlight:false', fixture);
result.should.eql('<pre><code>' + escapeHTML(fixture) + '</code></pre>');
Expand Down

0 comments on commit 85dbb32

Please sign in to comment.