Table of Contents (Vanilla JS)

Generate a hierarchical table of contents from a HTML document.

Asked in Google frontend interviews