Skip to content

Commit

Permalink
Merge pull request #1250 from tomtheisen/gfm-tasks
Browse files Browse the repository at this point in the history
GFM compliance for tasks lists
  • Loading branch information
styfle authored May 8, 2018
2 parents e0434b1 + b083a1a commit 42c3915
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
28 changes: 26 additions & 2 deletions lib/marked.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,9 @@ Lexer.prototype.token = function(src, top) {
i,
tag,
l,
isordered;
isordered,
istask,
ischecked;

while (src) {
// newline
Expand Down Expand Up @@ -363,10 +365,20 @@ Lexer.prototype.token = function(src, top) {
if (!loose) loose = next;
}

// Check for task list items
istask = /^\[[ xX]\] /.test(item);
ischecked = undefined;
if (istask) {
ischecked = item[1] !== ' ';
item = item.replace(/^\[[ xX]\] +/, '');
}

this.tokens.push({
type: loose
? 'loose_item_start'
: 'list_item_start'
: 'list_item_start',
task: istask,
checked: ischecked
});

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

Renderer.prototype.checkbox = function(checked) {
return '<input '
+ (checked ? 'checked="" ' : '')
+ 'disabled="" type="checkbox"'
+ (this.options.xhtml ? ' /' : '')
+ '> ';
}

Renderer.prototype.paragraph = function(text) {
return '<p>' + text + '</p>\n';
};
Expand Down Expand Up @@ -1198,6 +1218,10 @@ Parser.prototype.tok = function() {
case 'list_item_start': {
body = '';

if (this.token.task) {
body += this.renderer.checkbox(this.token.checked);
}

while (this.next().type !== 'list_item_end') {
body += this.token.type === 'text'
? this.parseText()
Expand Down
4 changes: 2 additions & 2 deletions test/specs/gfm/gfm-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Messenger.prototype.test = function(spec, section, ignore) {
var shouldFail = ~ignore.indexOf(spec.example);
it('should ' + (shouldFail ? 'fail' : 'pass') + ' example ' + spec.example, function() {
var expected = spec.html;
var actual = marked(spec.markdown, { headerIds: false, xhtml: true });
var actual = marked(spec.markdown, { headerIds: false, xhtml: false });
since(messenger.message(spec, expected, actual)).expect(
htmlDiffer.isEqual(expected, actual)
).toEqual(!shouldFail);
Expand All @@ -42,7 +42,7 @@ describe('GFM 0.28 Tables', function() {
describe('GFM 0.28 Task list items', function() {
var section = 'Task list items';

var shouldPassButFails = [272, 273];
var shouldPassButFails = [];

var willNotBeAttemptedByCoreTeam = [];

Expand Down

0 comments on commit 42c3915

Please sign in to comment.