Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GFM compliance for tasks lists #1250

Merged
merged 4 commits into from
May 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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