Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: markedjs/marked
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: master
Choose a base ref
...
head repository: zubeio/marked
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: master
Choose a head ref
Can’t automatically merge. Don’t worry, you can still create the pull request.
  • 1 commit
  • 4 files changed
  • 1 contributor

Commits on Aug 12, 2015

  1. Add task lists support

    jendewalt committed Aug 12, 2015
    Copy the full SHA
    6af2b32 View commit details
Showing with 61 additions and 11 deletions.
  1. +43 −10 lib/marked.js
  2. +1 −1 marked.min.js
  3. +11 −0 test/tests/gfm_task_list.html
  4. +6 −0 test/tests/gfm_task_list.text
53 changes: 43 additions & 10 deletions lib/marked.js
Original file line number Diff line number Diff line change
@@ -27,6 +27,8 @@ var block = {
text: /^[^\n]+/
};

block.checkbox = /^\[([ x])\] +/; // Task list addition

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

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

// Task list addition
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 ')) {
@@ -333,6 +347,7 @@ Lexer.prototype.token = function(src, top, bq) {
}

this.tokens.push({
checked: checked,
type: loose
? 'loose_item_start'
: 'list_item_start'
@@ -809,14 +824,26 @@ Renderer.prototype.hr = function() {
return this.options.xhtml ? '<hr/>\n' : '<hr>\n';
};

Renderer.prototype.list = function(body, ordered) {
// Task list alterations
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';
};
// Task list alterations
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) {
return '<p>' + text + '</p>\n';
@@ -1037,24 +1064,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 = '';
@@ -1063,7 +1096,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
Loading