Skip to content

Commit 73c75bc

Browse files
committed
normalize indentation, rename loop index var to i for brevity
1 parent dde2d6e commit 73c75bc

File tree

1 file changed

+33
-33
lines changed

1 file changed

+33
-33
lines changed

index.js

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,56 +4,56 @@
44
// https://github.com/blog/1825-task-lists-in-all-markdown-documents
55

66
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+
});
1817
};
1918

2019
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;
2827
}
2928

3029
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]);
3534
}
3635

3736
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);
4140
}
4241

4342
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;
5150
}
5251

5352
function isInline(token) { return token.type === 'inline'; }
5453
function isParagraph(token) { return token.type === 'paragraph_open'; }
5554
function isListItem(token) { return token.type === 'list_item_open'; }
5655

5756
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;
5959
}

0 commit comments

Comments
 (0)