How do I process a nested list?

后端 未结 3 1062
情深已故
情深已故 2021-01-20 12:35

Suppose I have a bulleted list like this:

* list item 1
* list item 2 (a parent)
** list item 3 (a child of list item 2)
** list item 4 (a child of list item         


        
3条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-20 13:15

    Keep track of the current "depth" you're parsing at.

    • If the depth of the next line is more than the current depth, recursively call the parser with the new depth, then add the result from that call to the current list.
    • If the depth of the next line is equal to the current depth, add it to the current list.
    • If the depth of the next line is less than the current depth, return the current list.

提交回复
热议问题