Skip to content

Commit 26cef98

Browse files
committed
rough implementation of task lists. see #107 #111.
1 parent bcf206e commit 26cef98

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

lib/marked.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ var block = {
2222
list: /^( *)(bull) [\s\S]+?(?:hr|def|\n{2,}(?! )(?!\1bull )\n*|\s*$)/,
2323
html: /^ *(?:comment|closed|closing) *(?:\n{2,}|\s*$)/,
2424
def: /^ *\[([^\]]+)\]: *<?([^\s>]+)>?(?: +["(]([^\n]+)[")])? *(?:\n+|$)/,
25+
task: noop,
2526
table: noop,
2627
paragraph: /^((?:[^\n]+\n?(?!hr|heading|lheading|blockquote|tag|def))+)\n*/,
2728
text: /^[^\n]+/
@@ -75,6 +76,7 @@ block.normal = merge({}, block);
7576
*/
7677

7778
block.gfm = merge({}, block.normal, {
79+
task: /^(\s*\[[x ]\][^\n]+\n)+\n*/,
7880
fences: /^ *(`{3,}|~{3,}) *(\S+)? *\n([\s\S]+?)\s*\1 *(?:\n+|$)/,
7981
paragraph: /^/
8082
});
@@ -375,6 +377,33 @@ Lexer.prototype.token = function(src, top, bq) {
375377
continue;
376378
}
377379

380+
// task (gfm)
381+
if (top && (cap = this.rules.task.exec(src)) {
382+
src = src.substring(cap[0].length);
383+
384+
this.tokens.push({
385+
type: 'task_list_start'
386+
});
387+
388+
cap[0] = cap[0].replace(/^\s+|\s+$/g, '');
389+
cap = cap[0].split(/\n+/);
390+
391+
i = 0;
392+
l = cap.length;
393+
394+
for (; i < l; i++) {
395+
this.tokens.push({
396+
type: 'task_item',
397+
checked: /^\s*\[x\]/.test(cap[i]),
398+
text: cap[i].replace(/^\s*\[[x ]\]\s*/, '')
399+
});
400+
}
401+
402+
this.tokens.push({
403+
type: 'task_list_end'
404+
});
405+
}
406+
378407
// table (gfm)
379408
if (top && (cap = this.rules.table.exec(src))) {
380409
src = src.substring(cap[0].length);
@@ -980,6 +1009,25 @@ Parser.prototype.tok = function() {
9801009
this.token.lang,
9811010
this.token.escaped);
9821011
}
1012+
case 'task_list_start': {
1013+
var body = ''
1014+
, i = 1;
1015+
1016+
while (this.next().type !== 'task_list_end') {
1017+
body += '<li class="task-list-item"><label>'
1018+
+ '<input type="checkbox"'
1019+
+ ' class="task-list-item-checkbox"
1020+
+ ' data-item-index="' + (i++) + '"'
1021+
+ ' data-item-complete="' + (this.token.checked ? 1 : 0) + '"'
1022+
+ ' ' + (this.token.disabled ? 'disabled=""' + '') + '>'
1023+
+ ' ' + this.inline.output(this.token.text)
1024+
+ '</label></li>';
1025+
}
1026+
1027+
return '<ul class="task-list">'
1028+
+ body
1029+
+ '</ul>';
1030+
}
9831031
case 'table': {
9841032
var header = ''
9851033
, body = ''

0 commit comments

Comments
 (0)