Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def toc_structure_from_headers(headers, toc = [])
- if headers.empty?
- return []
- end
- depth = headers[0][:depth]
- while header = headers[0]
- if header[:depth] == depth
- toc << { id: header[:id], text: header[:text], children: [] }
- headers.shift
- elsif header[:depth] < depth
- break;
- else
- toc_structure_from_headers(headers, toc[-1][:children])
- end
- end
- toc
- end
Advertisement
Add Comment
Please, Sign In to add comment