Skip to content

Commit

Permalink
add GFM style checkbox in list. see chjj#111 for detail
Browse files Browse the repository at this point in the history
  • Loading branch information
kindy committed Nov 5, 2013
1 parent ab84e8c commit f8108c2
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
21 changes: 21 additions & 0 deletions lib/marked.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ Lexer.prototype.token = function(src, top) {
, bull
, b
, item
, gtr = this.options.gfm && this.options.gfmTodoRule
, space
, i
, l;
Expand Down Expand Up @@ -332,6 +333,14 @@ Lexer.prototype.token = function(src, top) {
: 'list_item_start'
});

if (gtr && (b = gtr.exec(item))) {
this.tokens.push({
type: 'text',
text: b[0]
});

item = item.substring(b[0].length);
}
// Recurse.
this.token(item, false);

Expand Down Expand Up @@ -948,6 +957,17 @@ Parser.prototype.tok = function() {
}
case 'list_item_start': {
var body = '';
var gtr = this.options.gfm && this.options.gfmTodoRule;
var peek,
m;

if (gtr && (peek = this.peek()) && peek.type === 'text') {
if ((m = gtr.exec(peek.text)) && m[0].length === peek.text.length) {
this.next();
body += '<input type="checkbox" disabled' +
(m[1] === ' ' ? '' : ' checked') + ' /> '
}
}

while (this.next().type !== 'list_item_end') {
body += this.token.type === 'text'
Expand Down Expand Up @@ -1127,6 +1147,7 @@ marked.setOptions = function(opt) {

marked.defaults = {
gfm: true,
gfmTodoRule: /^\[([- ovx*+%#@~?])\] +/,
tables: true,
breaks: false,
pedantic: false,
Expand Down
5 changes: 5 additions & 0 deletions test/tests/gfm_todo_list.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<ul>
<li><input type="checkbox" disabled /> abc
[ ] bcd</li>
<li><input type="checkbox" disabled checked /> cde</li>
</ul>
3 changes: 3 additions & 0 deletions test/tests/gfm_todo_list.text
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
* [ ] abc
[ ] bcd
* [x] cde

2 comments on commit f8108c2

@evanwon
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kindy I haven't tested this fork yet, but if the task list feature is complete, do you plan to issue a pull request?

@kindy
Copy link
Owner Author

@kindy kindy commented on f8108c2 Jan 21, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@evanwon it seems they don't like this feature, so I have no plan to issue a pull request.

Please sign in to comment.