Skip to content

Commit cb12c28

Browse files
detjMurderlon
authored andcommitted
Add class to lists with tasks
* Add `contains-task-list` class to list if list contains at least one task list item. Fixes #28 Reviewed-by: Merlijn Vos <[email protected]>
1 parent 51bc203 commit cb12c28

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

lib/handlers/list.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,27 @@ var all = require('../all')
88
function list(h, node) {
99
var props = {}
1010
var name = node.ordered ? 'ol' : 'ul'
11+
var items
12+
var index = -1
13+
var length
1114

1215
if (typeof node.start === 'number' && node.start !== 1) {
1316
props.start = node.start
1417
}
1518

16-
return h(node, name, props, wrap(all(h, node), true))
19+
items = all(h, node)
20+
length = items.length
21+
22+
// Like GitHub, add a class for custom styling
23+
while (++index < length) {
24+
if (
25+
items[index].properties.className &&
26+
items[index].properties.className.indexOf('task-list-item') !== -1
27+
) {
28+
props.className = ['contains-task-list']
29+
break
30+
}
31+
}
32+
33+
return h(node, name, props, wrap(items, true))
1734
}

test/list.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,39 @@ test('List', function(t) {
3333
'should transform unordered lists to `ul`'
3434
)
3535

36+
t.deepEqual(
37+
to(
38+
u('list', {ordered: false}, [
39+
u('listItem', {checked: true}, [u('paragraph', [u('text', 'todo')])])
40+
])
41+
),
42+
u(
43+
'element',
44+
{tagName: 'ul', properties: {className: ['contains-task-list']}},
45+
[
46+
u('text', '\n'),
47+
u(
48+
'element',
49+
{tagName: 'li', properties: {className: ['task-list-item']}},
50+
[
51+
u(
52+
'element',
53+
{
54+
tagName: 'input',
55+
properties: {type: 'checkbox', checked: true, disabled: true}
56+
},
57+
[]
58+
),
59+
u('text', ' '),
60+
u('text', 'todo')
61+
]
62+
),
63+
u('text', '\n')
64+
]
65+
),
66+
'should identify task list items in unordered lists'
67+
)
68+
3669
t.deepEqual(
3770
to(
3871
u('list', {ordered: true, start: 3}, [

0 commit comments

Comments
 (0)