Skip to content
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
30 changes: 25 additions & 5 deletions lib/plugins/tag/code.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,25 @@ const rLineNumber = /\s*line_number:(\w+)/i;
const rHighlight = /\s*highlight:(\w+)/i;
const rFirstLine = /\s*first_line:(\d+)/i;
const rMark = /\s*mark:([0-9,-]+)/i;
const rWrap = /\s*wrap:(\w+)/i;

/**
* Code block tag
*
* Syntax:
* {% codeblock [title] [lang:language] [url] [link text] [line_number:(true|false)] [highlight:(true|false)] [first_line:number] [mark:#,#-#] %}
* code snippet
* {% endcodeblock %}
* {% codeblock [options] %}
* code snippet
* {% endcodeblock %}
* @param {String} title Caption text
* @param {Object} lang Specify language
* @param {String} url Source link
* @param {String} link_text Text of the link
* @param {Object} line_number Show line number, value must be a boolean
* @param {Object} highlight Enable code highlighting, value must be a boolean
* @param {Object} first_line Specify the first line number, value must be a number
* @param {Object} mark Line highlight specific line(s), each value separated by a comma. Specify number range using a dash
* Example: `mark:1,4-7,10` will mark line 1, 4 to 7 and 10.
* @param {Object} wrap Wrap the code block in <table>, value must be a boolean
* @returns {String} Code snippet with code highlighting
*/

function getHighlightOptions(config, arg) {
Expand Down Expand Up @@ -49,6 +60,14 @@ function getHighlightOptions(config, arg) {
});
}

let wrap = config.wrap;
if (rWrap.test(arg)) {
arg = arg.replace(rWrap, (match, _wrap) => {
wrap = _wrap === 'true';
return '';
});
}

let mark = [];
if (rMark.test(arg)) {
arg = arg.replace(rMark, (match, _mark) => {
Expand Down Expand Up @@ -97,7 +116,8 @@ function getHighlightOptions(config, arg) {
hljs: config.hljs,
mark,
tab: config.tab_replace,
autoDetect: config.auto_detect
autoDetect: config.auto_detect,
wrap
};
}

Expand Down
11 changes: 11 additions & 0 deletions test/scripts/tags/code.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,15 @@ describe('code', () => {
const $ = cheerio.load(result);
$('.gutter .line').length.should.eql(3);
});

it('wrap', () => {
let result = code('wrap:false', fixture);
result.should.eql(highlight(fixture, {
wrap: false
}));
result = code('wrap:true', fixture);
result.should.eql(highlight(fixture, {
wrap: true
}));
});
});