Repository: lepture/mistune

Issue:
Task lists cannot be nested
Task lists apparently cannot be nested (tested with master and 2.0.0rc1):

```py
>>> mistune.create_markdown(plugins=['task_lists'])('* [ ] task\n  * [ ] subtask')
'<ul>\n<li class="task-list-item"><input class="task-list-item-checkbox" type="checkbox" disabled/>task<ul>\n<li>[ ] subtask</li>\n</ul>\n</li>\n</ul>\n'
```

Note that the checkbox in the sublist isn't recognized. Since GitHub [allows task lists to be nested arbitrarily](https://github.github.com/gfm/#task-list-items-extension-), Mistune should probably also support this.

* [ ] nested task lists in mistune
  * [ ] agree that this should be fixed
  * [ ] fix it



Discussion:
502E532E: One example for a list embedded in another block is a list inside a blockquote like the following:
```markdown
> There is a list here:
> - My list item
```

> There is a list here:
> - My list item

This behaviour is already supported by mistune for normal lists, so it should probably apply to task list items as well. Thus, the simplest solution to rewrite all list items is probably to iterate over all tokens and filter out the list items. This is implemented in e7b86a9.


502E532E: As far as I understand it, the plugin tests can consist of just one test case currently. Therefore I skipped them for now and just added some HTML tests (see 2873b51)

Patch:
diff --git a/mistune/plugins/task_lists.py b/mistune/plugins/task_lists.py
index 8f7997c..3094ea7 100644
--- a/mistune/plugins/task_lists.py
+++ b/mistune/plugins/task_lists.py
@@ -7,11 +7,7 @@ TASK_LIST_ITEM = re.compile(r'^(\[[ xX]\])\s+')
 
 
 def task_lists_hook(md, tokens, state):
-    for tok in tokens:
-        if tok['type'] == 'list':
-            for item in tok['children']:
-                _rewrite_list_item(item)
-    return tokens
+    return _rewrite_all_list_items(tokens)
 
 
 def render_ast_task_list_item(children, level, checked):
@@ -50,10 +46,16 @@ def plugin_task_lists(md):
         md.renderer.register('task_list_item', render_ast_task_list_item)
 
 
-def _rewrite_list_item(item):
-    if item['type'] != 'list_item':
-        return
+def _rewrite_all_list_items(tokens):
+    for tok in tokens:
+        if tok['type'] == 'list_item':
+            _rewrite_list_item(tok)
+        if 'children' in tok.keys():
+            _rewrite_all_list_items(tok['children'])
+    return tokens
+
 
+def _rewrite_list_item(item):
     children = item['children']
     if children:
         first_child = children[0]


Test patch:
diff --git a/tests/fixtures/task_lists.txt b/tests/fixtures/task_lists.txt
index e8c1c7b..c1760ac 100644
--- a/tests/fixtures/task_lists.txt
+++ b/tests/fixtures/task_lists.txt
@@ -62,3 +62,47 @@
 over two lines</li>
 </ul>
 ````````````````````````````````
+
+```````````````````````````````` example
+- [ ] foo
+  - [x] bar
+  - [ ] baz
+.
+<ul>
+<li class="task-list-item"><input class="task-list-item-checkbox" type="checkbox" disabled/>foo<ul>
+<li class="task-list-item"><input class="task-list-item-checkbox" type="checkbox" disabled checked/>bar</li>
+<li class="task-list-item"><input class="task-list-item-checkbox" type="checkbox" disabled/>baz</li>
+</ul>
+</li>
+</ul>
+````````````````````````````````
+
+```````````````````````````````` example
+1. [ ] foo
+   1. [ ] bar
+   2. [ ] baz
+.
+<ol>
+<li class="task-list-item"><input class="task-list-item-checkbox" type="checkbox" disabled/>foo<ol>
+<li class="task-list-item"><input class="task-list-item-checkbox" type="checkbox" disabled/>bar</li>
+<li class="task-list-item"><input class="task-list-item-checkbox" type="checkbox" disabled/>baz</li>
+</ol>
+</li>
+</ol>
+````````````````````````````````
+
+```````````````````````````````` example
+> - [ ] foo
+>   - [x] bar
+>   - [ ] baz
+.
+<blockquote>
+<ul>
+<li class="task-list-item"><input class="task-list-item-checkbox" type="checkbox" disabled/>foo<ul>
+<li class="task-list-item"><input class="task-list-item-checkbox" type="checkbox" disabled checked/>bar</li>
+<li class="task-list-item"><input class="task-list-item-checkbox" type="checkbox" disabled/>baz</li>
+</ul>
+</li>
+</ul>
+</blockquote>
+````````````````````````````````
\ No newline at end of file