Skip to content

Commit a5e39a6

Browse files
committed
use renderer for task lists. see markedjs#107 markedjs#111.
1 parent 26cef98 commit a5e39a6

File tree

1 file changed

+22
-12
lines changed

1 file changed

+22
-12
lines changed

lib/marked.js

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ Lexer.prototype.token = function(src, top, bq) {
378378
}
379379

380380
// task (gfm)
381-
if (top && (cap = this.rules.task.exec(src)) {
381+
if (top && (cap = this.rules.task.exec(src))) {
382382
src = src.substring(cap[0].length);
383383

384384
this.tokens.push({
@@ -806,6 +806,21 @@ Renderer.prototype.code = function(code, lang, escaped) {
806806
+ '\n</code></pre>\n';
807807
};
808808

809+
Renderer.prototype.tasklist = function(text) {
810+
return '<ul class="task-list">\n' + text + '</ul>\n';
811+
};
812+
813+
Renderer.prototype.taskitem = function(text, checked, disabled, i) {
814+
return '<li class="task-list-item"><label>'
815+
+ '<input type="checkbox"'
816+
+ ' class="task-list-item-checkbox"'
817+
+ ' data-item-index="' + i + '"'
818+
+ ' data-item-complete="' + (checked ? 1 : 0) + '"'
819+
+ (disabled ? ' disabled=""' : '') + '>'
820+
+ ' ' + text
821+
+ '</label></li>';
822+
};
823+
809824
Renderer.prototype.blockquote = function(quote) {
810825
return '<blockquote>\n' + quote + '</blockquote>\n';
811826
};
@@ -1014,19 +1029,14 @@ Parser.prototype.tok = function() {
10141029
, i = 1;
10151030

10161031
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>';
1032+
body += this.renderer.taskitem(
1033+
this.inline.output(this.token.text),
1034+
this.token.checked,
1035+
this.token.disabled,
1036+
i++);
10251037
}
10261038

1027-
return '<ul class="task-list">'
1028-
+ body
1029-
+ '</ul>';
1039+
return this.renderer.tasklist(body);
10301040
}
10311041
case 'table': {
10321042
var header = ''

0 commit comments

Comments
 (0)