Table of contents
Sections
Pseudo code steps, operating during parse
of standard mdast
.
- With a stack initialised with root node (with children removed) ie
[root]
- Iterate over all removed root chilren (in reverse? TBD) as
node
- define
section
as top of stack - if
section
isroot
- create section with
node
depth or 0. - add
node
as child of section - push section to children of node at the top of stack
- push section to top of stack
- create section with
- else if
node
is aheading
- with deeper depth?
- create section with
node
depth - add
node
as child of section - push section to children of node at the top of stack
- push section to top of stack
- create section with
- else
- for each section in stack until top of stack has depth equal to or lower (shallower) than
node
depth.- pop stack and call process handler on popped section
- create section with
node
depth - add
node
as child of section - push section to children of node at the top of stack
- push section to top of stack
- for each section in stack until top of stack has depth equal to or lower (shallower) than
- with deeper depth?
- else add
node
tosection
- if no next node
- for each section in stack until but not including root
- pop stack and call process handler on popped section
- for each section in stack until but not including root
- define
Cells
- given a set of nodes (generally children of a
section
) - define an empty stack
[]
- for each node in nodes
- if node type is section push to top of stack
- else if node type is a code
- if previous cell is a code cell and node is attached
- push node as child of code cell at top of stack
- else
- create new cell with node as first child
- push cell to top of stack
- if previous cell is a code cell and node is attached
- else if stack is empty or top of stack is a section or code cell
- create new cell with node as first child
- push cell to top of stack
- else push node to children of cell at top of stack
Potential improvements & optimisations
- TODO perform cell grouping internal to section node collection, preventing unnecessary additional loops.
-
list
nodes (with no position? had assumed only toc) being grouped into prior code cells