Skip to content

Github Task Lists #2

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

Merged
merged 1 commit into from
Apr 24, 2016
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
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,8 @@ This code will output the following HTML:
- html(*string* html)
- heading(*string* text, *number* level)
- hr()
- list(*string* body, *boolean* ordered)
- list(*string* body, *boolean* ordered, *boolean* taskList)
- `taskList` true when `gfm` is `true` and there is a list item with a check box
- listitem(*string* text)
- paragraph(*string* text)
- table(*string* header, *string* body)
Expand All @@ -211,7 +212,8 @@ This code will output the following HTML:
- codespan(*string* code)
- br()
- del(*string* text)
- link(*string* href, *string* title, *string* text)
- link(*string* href, *string* title, *string* text, [*boolean* checked]).
- `checked` only defined when `gfm` is `true` and there is a check box at the start of the list item (e.g. `* [ ] foo`).
- image(*string* href, *string* title, *string* text)

### gfm
Expand Down
48 changes: 39 additions & 9 deletions lib/marked.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ var block = {
text: /^[^\n]+/
};

block.checkbox = /^\[([ x])\] +/;
block.bullet = /(?:[*+-]|\d+\.)/;
block.item = /^( *)(bull) [^\n]*(?:\n(?!\1bull )[^\n]*)*/;
block.item = replace(block.item, 'gm')
Expand Down Expand Up @@ -156,7 +157,8 @@ Lexer.prototype.token = function(src, top, bq) {
, item
, space
, i
, l;
, l
, checked;

while (src) {
// newline
Expand Down Expand Up @@ -303,6 +305,17 @@ Lexer.prototype.token = function(src, top, bq) {
space = item.length;
item = item.replace(/^ *([*+-]|\d+\.) +/, '');

if (this.options.gfm) {
checked = block.checkbox.exec(item);

if (checked) {
checked = checked[1] === 'x';
item = item.replace(block.checkbox, '');
} else {
checked = undefined;
}
}

// Outdent whatever the
// list item contains. Hacky.
if (~item.indexOf('\n ')) {
Expand Down Expand Up @@ -332,6 +345,7 @@ Lexer.prototype.token = function(src, top, bq) {
}

this.tokens.push({
checked: checked,
type: loose
? 'loose_item_start'
: 'list_item_start'
Expand Down Expand Up @@ -878,13 +892,23 @@ Renderer.prototype.hr = function() {
return this.options.xhtml ? '<hr/>\n' : '<hr>\n';
};

Renderer.prototype.list = function(body, ordered) {
Renderer.prototype.list = function(body, ordered, taskList) {
var type = ordered ? 'ol' : 'ul';
return '<' + type + '>\n' + body + '</' + type + '>\n';
var classes = taskList ? ' class="task-list"' : '';
return '<' + type + classes + '>\n' + body + '</' + type + '>\n';
};

Renderer.prototype.listitem = function(text) {
return '<li>' + text + '</li>\n';
Renderer.prototype.listitem = function(text, checked) {
if (checked === undefined) {
return '<li>' + text + '</li>\n';
}

return '<li class="task-list-item">'
+ '<input type="checkbox" class="task-list-item-checkbox"'
+ (checked ? ' checked' : '')
+ '> '
+ text
+ '</li>\n';
};

Renderer.prototype.paragraph = function(text) {
Expand Down Expand Up @@ -1108,24 +1132,30 @@ Parser.prototype.tok = function() {
}
case 'list_start': {
var body = ''
, taskList = false
, ordered = this.token.ordered;

while (this.next().type !== 'list_end') {
if (this.token.checked !== undefined) {
taskList = true;
}

body += this.tok();
}

return this.renderer.list(body, ordered);
return this.renderer.list(body, ordered, taskList);
}
case 'list_item_start': {
var body = '';
var body = ''
, checked = this.token.checked;

while (this.next().type !== 'list_item_end') {
body += this.token.type === 'text'
? this.parseText()
: this.tok();
}

return this.renderer.listitem(body);
return this.renderer.listitem(body, checked);
}
case 'loose_item_start': {
var body = '';
Expand All @@ -1134,7 +1164,7 @@ Parser.prototype.tok = function() {
body += this.tok();
}

return this.renderer.listitem(body);
return this.renderer.listitem(body, checked);
}
case 'html': {
var html = !this.token.pre && !this.options.pedantic
Expand Down
11 changes: 11 additions & 0 deletions test/tests/gfm_task_list.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<ul class="task-list">
<li class="task-list-item"><input type="checkbox" class="task-list-item-checkbox"> foo</li>
<li>bar</li>
<li class="task-list-item"><input type="checkbox" class="task-list-item-checkbox" checked> baz</li>
<li>[] bam
<ul class="task-list">
<li class="task-list-item"><input type="checkbox" class="task-list-item-checkbox"> bim</li>
<li class="task-list-item"><input type="checkbox" class="task-list-item-checkbox"> lim</li>
</ul>
</li>
</ul>
6 changes: 6 additions & 0 deletions test/tests/gfm_task_list.text
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
* [ ] foo
* bar
* [x] baz
* [] bam
* [ ] bim
* [ ] lim