From 74829901310b9f9c518c532e249e21662c0bd79d Mon Sep 17 00:00:00 2001 From: Tom Theisen Date: Wed, 2 May 2018 20:17:13 -0700 Subject: [PATCH 1/3] GFM compliance for tasks --- lib/marked.js | 28 ++++++++++++++++++++++++++-- test/specs/gfm/gfm-spec.js | 5 +++-- test/specs/gfm/gfm.0.28.json | 6 ++++-- 3 files changed, 33 insertions(+), 6 deletions(-) diff --git a/lib/marked.js b/lib/marked.js index 2b40cfaf41..c4fb908921 100644 --- a/lib/marked.js +++ b/lib/marked.js @@ -195,7 +195,9 @@ Lexer.prototype.token = function(src, top) { i, tag, l, - isordered; + isordered, + istask, + ischecked; while (src) { // newline @@ -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. @@ -927,6 +939,14 @@ Renderer.prototype.listitem = function(text) { return '
  • ' + text + '
  • \n'; }; +Renderer.prototype.checkbox = function(checked) { + return ''; +} + Renderer.prototype.paragraph = function(text) { return '

    ' + text + '

    \n'; }; @@ -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() diff --git a/test/specs/gfm/gfm-spec.js b/test/specs/gfm/gfm-spec.js index bf768bda8c..067f40f5ad 100644 --- a/test/specs/gfm/gfm-spec.js +++ b/test/specs/gfm/gfm-spec.js @@ -15,7 +15,8 @@ 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 usexhtml = typeof spec.xhtml === 'boolean' ? spec.xhtml : true; + var actual = marked(spec.markdown, { headerIds: false, xhtml: usexhtml }); since(messenger.message(spec, expected, actual)).expect( htmlDiffer.isEqual(expected, actual) ).toEqual(!shouldFail); @@ -42,7 +43,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 = []; diff --git a/test/specs/gfm/gfm.0.28.json b/test/specs/gfm/gfm.0.28.json index d045f8af62..eee52da306 100644 --- a/test/specs/gfm/gfm.0.28.json +++ b/test/specs/gfm/gfm.0.28.json @@ -51,13 +51,15 @@ "section": "Task list items", "html": "", "markdown": "- [ ] foo\n- [x] bar", - "example": 272 + "example": 272, + "xhtml": false }, { "section": "Task list items", "html": "", "markdown": "- [x] foo\n - [ ] bar\n - [x] baz\n- [ ] bim", - "example": 273 + "example": 273, + "xhtml": false }, { "section": "Strikethrough", From aa73bb6ea74eab64941ee9d48935d472453f07fc Mon Sep 17 00:00:00 2001 From: Tom Theisen Date: Thu, 3 May 2018 19:51:31 -0700 Subject: [PATCH 2/3] add space to task regex and disable xhtml for gfm testing --- lib/marked.js | 4 ++-- test/specs/gfm/gfm-spec.js | 3 +-- test/specs/gfm/gfm.0.28.json | 6 ++---- 3 files changed, 5 insertions(+), 8 deletions(-) diff --git a/lib/marked.js b/lib/marked.js index c4fb908921..0ab0b3299b 100644 --- a/lib/marked.js +++ b/lib/marked.js @@ -366,11 +366,11 @@ Lexer.prototype.token = function(src, top) { } // Check for task list items - istask = /^\[[ xX]\]/.test(item); + istask = /^\[[ xX]\] /.test(item); ischecked = undefined; if (istask) { ischecked = item[1] !== ' '; - item = item.replace(/^\[[ xX]\] */, ''); + item = item.replace(/^\[[ xX]\] +/, ''); } this.tokens.push({ diff --git a/test/specs/gfm/gfm-spec.js b/test/specs/gfm/gfm-spec.js index 067f40f5ad..a8b43a2e2b 100644 --- a/test/specs/gfm/gfm-spec.js +++ b/test/specs/gfm/gfm-spec.js @@ -15,8 +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 usexhtml = typeof spec.xhtml === 'boolean' ? spec.xhtml : true; - var actual = marked(spec.markdown, { headerIds: false, xhtml: usexhtml }); + var actual = marked(spec.markdown, { headerIds: false, xhtml: false }); since(messenger.message(spec, expected, actual)).expect( htmlDiffer.isEqual(expected, actual) ).toEqual(!shouldFail); diff --git a/test/specs/gfm/gfm.0.28.json b/test/specs/gfm/gfm.0.28.json index eee52da306..d045f8af62 100644 --- a/test/specs/gfm/gfm.0.28.json +++ b/test/specs/gfm/gfm.0.28.json @@ -51,15 +51,13 @@ "section": "Task list items", "html": "", "markdown": "- [ ] foo\n- [x] bar", - "example": 272, - "xhtml": false + "example": 272 }, { "section": "Task list items", "html": "", "markdown": "- [x] foo\n - [ ] bar\n - [x] baz\n- [ ] bim", - "example": 273, - "xhtml": false + "example": 273 }, { "section": "Strikethrough", From b083a1a3071f24b154fe8abb2c5b5d665f4525c9 Mon Sep 17 00:00:00 2001 From: Tom Theisen Date: Thu, 3 May 2018 19:52:57 -0700 Subject: [PATCH 3/3] space following task checkbox --- lib/marked.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/marked.js b/lib/marked.js index 0ab0b3299b..a88b7d2dbe 100644 --- a/lib/marked.js +++ b/lib/marked.js @@ -944,7 +944,7 @@ Renderer.prototype.checkbox = function(checked) { + (checked ? 'checked="" ' : '') + 'disabled="" type="checkbox"' + (this.options.xhtml ? ' /' : '') - + '>'; + + '> '; } Renderer.prototype.paragraph = function(text) {