|
4 | 4 | // https://github.com/blog/1825-task-lists-in-all-markdown-documents |
5 | 5 |
|
6 | 6 | module.exports = function(md, options) { |
7 | | - md.core.ruler.after('inline', 'github-task-lists', function(state) { |
8 | | - var tokens = state.tokens; |
9 | | - for (var index = 2; index < tokens.length; index++) { |
10 | | - var token = tokens[index]; |
11 | | - if (isTodoItem(tokens, index)) { |
12 | | - todoify(token, state.Token); |
13 | | - tokens[index - 2].attrSet('class', 'task-list-item'); |
14 | | - tokens[parentToken(tokens, index - 2)].attrSet('class', 'task-list'); |
15 | | - } |
16 | | - } |
17 | | - }); |
| 7 | +md.core.ruler.after('inline', 'github-task-lists', function(state) { |
| 8 | +var tokens = state.tokens; |
| 9 | +for (var i = 2; i < tokens.length; i++) { |
| 10 | +if (isTodoItem(tokens, i)) { |
| 11 | +todoify(tokens[i], state.Token); |
| 12 | +tokens[i-2].attrSet('class', 'task-list-item'); |
| 13 | +tokens[parentToken(tokens, i-2)].attrSet('class', 'task-list'); |
| 14 | +} |
| 15 | +} |
| 16 | +}); |
18 | 17 | }; |
19 | 18 |
|
20 | 19 | function parentToken(tokens, index) { |
21 | | - var targetLevel = tokens[index].level - 1; |
22 | | - for (var i = index - 1; i >= 0; i--) { |
23 | | - if (tokens[i].level === targetLevel) { |
24 | | - return i; |
25 | | - } |
26 | | - } |
27 | | - return -1; |
| 20 | +var targetLevel = tokens[index].level - 1; |
| 21 | +for (var i = index - 1; i >= 0; i--) { |
| 22 | +if (tokens[i].level === targetLevel) { |
| 23 | +return i; |
| 24 | +} |
| 25 | +} |
| 26 | +return -1; |
28 | 27 | } |
29 | 28 |
|
30 | 29 | function isTodoItem(tokens, index) { |
31 | | - return isInline(tokens[index]) && |
32 | | - isParagraph(tokens[index - 1]) && |
33 | | - isListItem(tokens[index - 2]) && |
34 | | - startsWithTodoMarkdown(tokens[index]); |
| 30 | +return isInline(tokens[index]) && |
| 31 | + isParagraph(tokens[index - 1]) && |
| 32 | + isListItem(tokens[index - 2]) && |
| 33 | + startsWithTodoMarkdown(tokens[index]); |
35 | 34 | } |
36 | 35 |
|
37 | 36 | function todoify(token, TokenConstructor) { |
38 | | - token.children.unshift(makeCheckbox(token, TokenConstructor)); |
39 | | - token.children[1].content = token.children[1].content.slice(3); |
40 | | - token.content = token.content.slice(3); |
| 37 | +token.children.unshift(makeCheckbox(token, TokenConstructor)); |
| 38 | +token.children[1].content = token.children[1].content.slice(3); |
| 39 | +token.content = token.content.slice(3); |
41 | 40 | } |
42 | 41 |
|
43 | 42 | function makeCheckbox(token, TokenConstructor) { |
44 | | - var checkbox = new TokenConstructor('html_inline', '', 0); |
45 | | - if (token.content.indexOf('[ ]') === 0) { |
46 | | - checkbox.content = '<input class="task-list-item-checkbox" disabled="" type="checkbox">'; |
47 | | - } else if (token.content.indexOf('[x]') === 0) { |
48 | | - checkbox.content = '<input class="task-list-item-checkbox" checked="" disabled="" type="checkbox">'; |
49 | | - } |
50 | | - return checkbox; |
| 43 | +var checkbox = new TokenConstructor('html_inline', '', 0); |
| 44 | +if (token.content.indexOf('[ ]') === 0) { |
| 45 | +checkbox.content = '<input class="task-list-item-checkbox" disabled="" type="checkbox">'; |
| 46 | +} else if (token.content.indexOf('[x]') === 0) { |
| 47 | +checkbox.content = '<input class="task-list-item-checkbox" checked="" disabled="" type="checkbox">'; |
| 48 | +} |
| 49 | +return checkbox; |
51 | 50 | } |
52 | 51 |
|
53 | 52 | function isInline(token) { return token.type === 'inline'; } |
54 | 53 | function isParagraph(token) { return token.type === 'paragraph_open'; } |
55 | 54 | function isListItem(token) { return token.type === 'list_item_open'; } |
56 | 55 |
|
57 | 56 | function startsWithTodoMarkdown(token) { |
58 | | - return token.content.indexOf('[ ]') === 0 || token.content.indexOf('[x]') === 0; |
| 57 | +// leading whitespace in a list item is already trimmed off by markdown-it |
| 58 | +return token.content.indexOf('[ ]') === 0 || token.content.indexOf('[x]') === 0; |
59 | 59 | } |
0 commit comments