Skip to content

Commit

Permalink
rough implementation of task lists. see markedjs#107 markedjs#111.
Browse files Browse the repository at this point in the history
  • Loading branch information
chjj committed Feb 26, 2014
1 parent bcf206e commit 26cef98
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions lib/marked.js
Expand Up @@ -22,6 +22,7 @@ var block = {
list: /^( *)(bull) [\s\S]+?(?:hr|def|\n{2,}(?! )(?!\1bull )\n*|\s*$)/,
html: /^ *(?:comment|closed|closing) *(?:\n{2,}|\s*$)/,
def: /^ *\[([^\]]+)\]: *<?([^\s>]+)>?(?: +["(]([^\n]+)[")])? *(?:\n+|$)/,
task: noop,
table: noop,
paragraph: /^((?:[^\n]+\n?(?!hr|heading|lheading|blockquote|tag|def))+)\n*/,
text: /^[^\n]+/
Expand Down Expand Up @@ -75,6 +76,7 @@ block.normal = merge({}, block);
*/

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

// task (gfm)
if (top && (cap = this.rules.task.exec(src)) {
src = src.substring(cap[0].length);

this.tokens.push({
type: 'task_list_start'
});

cap[0] = cap[0].replace(/^\s+|\s+$/g, '');
cap = cap[0].split(/\n+/);

i = 0;
l = cap.length;

for (; i < l; i++) {
this.tokens.push({
type: 'task_item',
checked: /^\s*\[x\]/.test(cap[i]),
text: cap[i].replace(/^\s*\[[x ]\]\s*/, '')
});
}

this.tokens.push({
type: 'task_list_end'
});
}

// table (gfm)
if (top && (cap = this.rules.table.exec(src))) {
src = src.substring(cap[0].length);
Expand Down Expand Up @@ -980,6 +1009,25 @@ Parser.prototype.tok = function() {
this.token.lang,
this.token.escaped);
}
case 'task_list_start': {
var body = ''
, i = 1;

while (this.next().type !== 'task_list_end') {
body += '<li class="task-list-item"><label>'
+ '<input type="checkbox"'
+ ' class="task-list-item-checkbox"
+ ' data-item-index="' + (i++) + '"'
+ ' data-item-complete="' + (this.token.checked ? 1 : 0) + '"'
+ ' ' + (this.token.disabled ? 'disabled=""' + '') + '>'
+ ' ' + this.inline.output(this.token.text)
+ '</label></li>';
}

return '<ul class="task-list">'
+ body
+ '</ul>';
}
case 'table': {
var header = ''
, body = ''
Expand Down

0 comments on commit 26cef98

Please sign in to comment.