Unrelated change but why not:
`build_output(doc_block.children, output_file, depth + 1)`

Instead of incrementing and storing the new depth?