Skip to content

Commit

Permalink
Add class to lists with tasks
Browse files Browse the repository at this point in the history
* Add `contains-task-list` class to list if list contains at least one task list item.

Fixes #28 

Reviewed-by: Merlijn Vos <merlijn@soverin.net>
  • Loading branch information
detj authored and Murderlon committed Feb 15, 2019
1 parent 51bc203 commit cb12c28
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
19 changes: 18 additions & 1 deletion lib/handlers/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,27 @@ var all = require('../all')
function list(h, node) {
var props = {}
var name = node.ordered ? 'ol' : 'ul'
var items
var index = -1
var length

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

return h(node, name, props, wrap(all(h, node), true))
items = all(h, node)
length = items.length

// Like GitHub, add a class for custom styling
while (++index < length) {
if (
items[index].properties.className &&
items[index].properties.className.indexOf('task-list-item') !== -1
) {
props.className = ['contains-task-list']
break
}
}

return h(node, name, props, wrap(items, true))
}
33 changes: 33 additions & 0 deletions test/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,39 @@ test('List', function(t) {
'should transform unordered lists to `ul`'
)

t.deepEqual(
to(
u('list', {ordered: false}, [
u('listItem', {checked: true}, [u('paragraph', [u('text', 'todo')])])
])
),
u(
'element',
{tagName: 'ul', properties: {className: ['contains-task-list']}},
[
u('text', '\n'),
u(
'element',
{tagName: 'li', properties: {className: ['task-list-item']}},
[
u(
'element',
{
tagName: 'input',
properties: {type: 'checkbox', checked: true, disabled: true}
},
[]
),
u('text', ' '),
u('text', 'todo')
]
),
u('text', '\n')
]
),
'should identify task list items in unordered lists'
)

t.deepEqual(
to(
u('list', {ordered: true, start: 3}, [
Expand Down

0 comments on commit cb12c28

Please sign in to comment.