Skip to content

Commit 42c3915

Browse files
authored
Merge pull request #1250 from tomtheisen/gfm-tasks
GFM compliance for tasks lists
2 parents e0434b1 + b083a1a commit 42c3915

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

lib/marked.js

+26-2
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,9 @@ Lexer.prototype.token = function(src, top) {
195195
i,
196196
tag,
197197
l,
198-
isordered;
198+
isordered,
199+
istask,
200+
ischecked;
199201

200202
while (src) {
201203
// newline
@@ -363,10 +365,20 @@ Lexer.prototype.token = function(src, top) {
363365
if (!loose) loose = next;
364366
}
365367

368+
// Check for task list items
369+
istask = /^\[[ xX]\] /.test(item);
370+
ischecked = undefined;
371+
if (istask) {
372+
ischecked = item[1] !== ' ';
373+
item = item.replace(/^\[[ xX]\] +/, '');
374+
}
375+
366376
this.tokens.push({
367377
type: loose
368378
? 'loose_item_start'
369-
: 'list_item_start'
379+
: 'list_item_start',
380+
task: istask,
381+
checked: ischecked
370382
});
371383

372384
// Recurse.
@@ -927,6 +939,14 @@ Renderer.prototype.listitem = function(text) {
927939
return '<li>' + text + '</li>\n';
928940
};
929941

942+
Renderer.prototype.checkbox = function(checked) {
943+
return '<input '
944+
+ (checked ? 'checked="" ' : '')
945+
+ 'disabled="" type="checkbox"'
946+
+ (this.options.xhtml ? ' /' : '')
947+
+ '> ';
948+
}
949+
930950
Renderer.prototype.paragraph = function(text) {
931951
return '<p>' + text + '</p>\n';
932952
};
@@ -1198,6 +1218,10 @@ Parser.prototype.tok = function() {
11981218
case 'list_item_start': {
11991219
body = '';
12001220

1221+
if (this.token.task) {
1222+
body += this.renderer.checkbox(this.token.checked);
1223+
}
1224+
12011225
while (this.next().type !== 'list_item_end') {
12021226
body += this.token.type === 'text'
12031227
? this.parseText()

test/specs/gfm/gfm-spec.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Messenger.prototype.test = function(spec, section, ignore) {
1515
var shouldFail = ~ignore.indexOf(spec.example);
1616
it('should ' + (shouldFail ? 'fail' : 'pass') + ' example ' + spec.example, function() {
1717
var expected = spec.html;
18-
var actual = marked(spec.markdown, { headerIds: false, xhtml: true });
18+
var actual = marked(spec.markdown, { headerIds: false, xhtml: false });
1919
since(messenger.message(spec, expected, actual)).expect(
2020
htmlDiffer.isEqual(expected, actual)
2121
).toEqual(!shouldFail);
@@ -42,7 +42,7 @@ describe('GFM 0.28 Tables', function() {
4242
describe('GFM 0.28 Task list items', function() {
4343
var section = 'Task list items';
4444

45-
var shouldPassButFails = [272, 273];
45+
var shouldPassButFails = [];
4646

4747
var willNotBeAttemptedByCoreTeam = [];
4848

0 commit comments

Comments
 (0)