Skip to content

Commit

Permalink
Merge branch 'mark' of git://github.com/greg-js/hexo into greg-js-mark
Browse files Browse the repository at this point in the history
  • Loading branch information
leesei committed Feb 25, 2016
2 parents 3e9fb4f + 3d179d1 commit b4825d9
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
27 changes: 26 additions & 1 deletion lib/plugins/tag/code.js
Expand Up @@ -13,12 +13,13 @@ var rLang = /\s*lang:(\w+)/i;
var rLineNumber = /\s*line_number:(\w+)/i;
var rHighlight = /\s*highlight:(\w+)/i;
var rFirstLine = /\s*first_line:(\d+)/i;
var rMark = /\s*mark:([0-9,\-]+)/i;

/**
* Code block tag
*
* Syntax:
* {% codeblock [title] [lang:language] [url] [link text] [line_number:(true|false)] [highlight:(true|false)] [first_line:number] %}
* {% codeblock [title] [lang:language] [url] [link text] [line_number:(true|false)] [highlight:(true|false)] [first_line:number] [mark:#,#-#] %}
* code snippet
* {% endcodeblock %}
*/
Expand All @@ -44,6 +45,7 @@ module.exports = function(ctx) {
var lang = '';
var line_number = config.line_number;
var first_line = 1;
var mark = [];
var match;

if (rLang.test(arg)) {
Expand All @@ -67,6 +69,28 @@ module.exports = function(ctx) {
});
}

if (rMark.test(arg)) {
arg = arg.replace(rMark, function() {
mark = arguments[1].split(',').reduce(function getMarkedLines(prev, cur) {
var a, b, temp;
if (/\-/.test(cur)) {
a = Number(cur.substr(0, cur.indexOf('-')));
b = Number(cur.substr(cur.indexOf('-') + 1));
if (b < a) { // switch a & b
temp = a; a = b; b = temp;
}
for (; a <= b; a++) {
prev.push(a);
}
return prev;
}
prev.push(Number(cur));
return prev;
}, []);
return '';
});
}

if (rCaptionUrlTitle.test(arg)) {
match = arg.match(rCaptionUrlTitle);
caption = '<span>' + match[1] + '</span><a href="' + match[2] + match[3] + '">' + match[4] + '</a>';
Expand All @@ -85,6 +109,7 @@ module.exports = function(ctx) {
firstLine: first_line,
caption: caption,
gutter: line_number,
mark: mark,
tab: config.tab_replace,
autoDetect: config.auto_detect
});
Expand Down
7 changes: 7 additions & 0 deletions test/scripts/tags/code.js
Expand Up @@ -111,6 +111,13 @@ describe('code', function() {
}));
});

it('mark', function() {
var result = code('mark:2,4,6-8', fixture);
result.should.eql(highlight(fixture, {
mark: [2, 4, 6, 7, 8]
}));
});

it('# lines', function() {
var result = code('', fixture);
var $ = cheerio.load(result);
Expand Down

0 comments on commit b4825d9

Please sign in to comment.