Skip to content

Commit b9b0354

Browse files
asommer70revin
authored andcommitted
Add Option for Label After Checkbox (#21)
* Add option for placing label after checkbox. * Added a id property to the checkbox and a for property to the label using a timestamp. * Changed id from timestamp to large random number because multiple checkboxes can end up having the same timestamp. Also, added task-list-item-label class to trailing label which makes it easier for CSS styling. * Updated documentation for the labelAfter option. * Added task-item- to the id attribute to make it unique to the project.
1 parent fbfa63c commit b9b0354

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,14 @@ purposes, pass a truthy value to the `label` property of the plugin options:
4949
var parser = md().use(taskLists, {label: true});
5050
```
5151

52+
To add the label after the checkbox pass a truthy value to `labelAfter` property:
53+
54+
```js
55+
var parser = md().use(taskLists, {label: true, labelAfter: true});
56+
```
57+
58+
**Note:** This option does require the `label` option to be truthy.
59+
5260
The options can be combined, of course.
5361

5462
### Browser Usage

index.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55

66
var disableCheckboxes = true;
77
var useLabelWrapper = false;
8+
var useLabelAfter = false;
89

910
module.exports = function(md, options) {
1011
if (options) {
1112
disableCheckboxes = !options.enabled;
1213
useLabelWrapper = !!options.label;
14+
useLabelAfter = !!options.labelAfter;
1315
}
1416

1517
md.core.ruler.after('inline', 'github-task-lists', function(state) {
@@ -58,8 +60,17 @@ function todoify(token, TokenConstructor) {
5860
token.content = token.content.slice(3);
5961

6062
if (useLabelWrapper) {
61-
token.children.unshift(beginLabel(TokenConstructor));
62-
token.children.push(endLabel(TokenConstructor));
63+
if (useLabelAfter) {
64+
token.children.pop();
65+
66+
// Use large random number as id property of the checkbox.
67+
var id = 'task-item-' + Math.ceil(Math.random() * (10000 * 1000) - 1000);
68+
token.children[0].content = token.children[0].content.slice(0, -1) + ' id="' + id + '">';
69+
token.children.push(afterLabel(token.content, id, TokenConstructor));
70+
} else {
71+
token.children.unshift(beginLabel(TokenConstructor));
72+
token.children.push(endLabel(TokenConstructor));
73+
}
6374
}
6475
}
6576

@@ -88,6 +99,13 @@ function endLabel(TokenConstructor) {
8899
return token;
89100
}
90101

102+
function afterLabel(content, id, TokenConstructor) {
103+
var token = new TokenConstructor('html_inline', '', 0);
104+
token.content = '<label class="task-list-item-label" for="' + id + '">' + content + '</label>';
105+
token.attrs = [{for: id}];
106+
return token;
107+
}
108+
91109
function isInline(token) { return token.type === 'inline'; }
92110
function isParagraph(token) { return token.type === 'paragraph_open'; }
93111
function isListItem(token) { return token.type === 'list_item_open'; }

test/index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,12 @@ describe('markdown-it-task-lists', function() {
8787
assert($$('.task-list-item > label > input[type=checkbox].task-list-item-checkbox:not([disabled])').length > 0);
8888
});
8989

90+
it('adds label after items when options.label and options.labelAfter are truthy', function() {
91+
var enabledLabeledParser = md().use(taskLists, {enabled: true, label: true, labelAfter: true});
92+
var $$ = cheerio.load(enabledLabeledParser.render(fixtures.ordered));
93+
assert( $$('.task-list-item > input[type=checkbox].task-list-item-checkbox:not([disabled])').next().is('label'));
94+
});
95+
9096
it('does NOT render [ ], "[ ]" (no space after closing bracket), [ x], [x ], or [ x ] as checkboxes', function () {
9197
var html = $.dirty.html();
9298
assert(~html.indexOf('<li>[ ]'));

0 commit comments

Comments
 (0)