HTML writer: add task-list class to ul if all elements are task list items.

This will allow styling unordered task lists in a way that omits
the bullet.
This commit is contained in:
John MacFarlane 2019-12-05 09:32:40 -08:00
parent 4489283b03
commit 79a10388da
2 changed files with 14 additions and 5 deletions

View file

@ -370,6 +370,13 @@ defList :: PandocMonad m
=> WriterOptions -> [Html] -> StateT WriterState m Html
defList opts items = toList H.dl opts (items ++ [nl opts])
isTaskListItem :: [Block] -> Bool
isTaskListItem (Plain (Str "":Space:_):_) = True
isTaskListItem (Plain (Str "":Space:_):_) = True
isTaskListItem (Para (Str "":Space:_):_) = True
isTaskListItem (Para (Str "":Space:_):_) = True
isTaskListItem _ = False
listItemToHtml :: PandocMonad m
=> WriterOptions -> [Block] -> StateT WriterState m Html
listItemToHtml opts bls
@ -819,7 +826,9 @@ blockToHtml opts (Header level attr@(_,classes,_) lst) = do
_ -> H.p ! A.class_ "heading" $ contents'
blockToHtml opts (BulletList lst) = do
contents <- mapM (listItemToHtml opts) lst
unordList opts contents
let isTaskList = not (null lst) && all isTaskListItem lst
(if isTaskList then (! A.class_ "task-list") else id) <$>
unordList opts contents
blockToHtml opts (OrderedList (startnum, numstyle, _) lst) = do
contents <- mapM (listItemToHtml opts) lst
html5 <- gets stHtml5

View file

@ -5,7 +5,7 @@ tests adapted from <https://github.github.com/gfm/#task-list-items-extension->
- [ ] foo
- [x] bar
^D
<ul>
<ul class="task-list">
<li><input type="checkbox" disabled="" />
foo</li>
<li><input type="checkbox" disabled="" checked="" />
@ -21,9 +21,9 @@ bar</li>
- [x] baz
- [ ] bim
^D
<ul>
<ul class="task-list">
<li><input type="checkbox" disabled="" checked="" />
foo<ul>
foo<ul class="task-list">
<li><input type="checkbox" disabled="" />
bar</li>
<li><input type="checkbox" disabled="" checked="" />
@ -73,7 +73,7 @@ ordered unchecked</li>
ordered checked</li>
</ol>
<p>paragraph</p>
<ul>
<ul class="task-list">
<li><p><input type="checkbox" disabled="" />
list item with a</p><p>second paragraph</p></li>
<li><p><input type="checkbox" disabled="" checked="" />