Skip to curriculum
Jump to the editor

16.12. Build a table of contents from headings

Medium
Topics

This question is in

16. DOM: finding elements
25–40 min

Documentation pages, blog posts and long settings screens show a table of contents: a nested list of links to the page's headings. Building one in the browser means finding the headings, nesting them by level (an h3 under the h2 before it), and making sure every heading has an id the links can point to: readable, and unique in the page.

Write two functions in toc.js.

Signature

JAVASCRIPT
function buildToc(root)   // returns an array of entries
function renderToc(items) // returns a <ul> element
  • buildToc(root): reads the h1–h6 headings inside root and returns the top-level entries. Each entry is { id, text, level, children }, where level is the heading's number (2 for h2) and children holds the entries under it. Headings without an id get one.
  • renderToc(items): turns entries into a <ul> with one <li><a href="#id">text</a></li> per entry, and a nested <ul> inside the <li> when the entry has children.

Requirements

  1. Every h1–h6 inside root, at any depth, in document order, becomes an entry. Other elements (header, hr, role="heading") do not, and nothing outside root does.
  2. A heading goes under the nearest earlier heading with a smaller level, even if levels are skipped (h2 then h4). With no such heading, it is a top-level entry.
  3. text is the heading's text content with every run of whitespace turned into one space and the ends trimmed. Headings whose text is empty are left out and get no id.
  4. A heading that has an id keeps it. One without gets an id made from its text: lower case, every run of characters that are not letters or digits (of any language) replaced by -, dashes trimmed from both ends. If nothing is left, the id is section.
  5. A new id never repeats an id already used anywhere in the page or in root, including ids of later headings: it gets -2, -3… (the first free number). The id is set on the heading itself.
  6. Running buildToc again on the same article gives the same entries and ids. Other elements' ids are never changed.
  7. renderToc builds the nested lists described above; an entry without children has no nested <ul>, and no entries give an empty <ul>.
  8. renderToc writes the text as text: a heading that contains < or & never turns into markup.

Examples

JAVASCRIPT
// <article id="post">
//   <h1>Guide</h1>
//   <h2>Install &amp; Run</h2>
//   <h3>On macOS</h3>
//   <h2 id="faq">Questions</h2>
//   <h2>Install &amp; Run</h2>
// </article>
const entries = buildToc(document.getElementById("post"));
// [{ id: "guide", text: "Guide", level: 1, children: [
//     { id: "install-run", text: "Install & Run", level: 2, children: [
//         { id: "on-macos", text: "On macOS", level: 3, children: [] }] },
//     { id: "faq", text: "Questions", level: 2, children: [] },
//     { id: "install-run-2", text: "Install & Run", level: 2, children: [] }] }]
JAVASCRIPT
renderToc([{ id: "faq", text: "Questions", level: 2, children: [] }]).outerHTML;
// '<ul><li><a href="#faq">Questions</a></li></ul>'
JAVASCRIPT
// <h3>Intro</h3><h2>Main</h2>   →  both at the top: nothing higher comes before the h3

Environment

  • Plain JavaScript in a browser page; the tests call the globals buildToc and renderToc from toc.js.
  • Every DOM method is allowed, querySelectorAll included.
  • root is an element in the page.

Hints

  1. While reading headings in order, which earlier entries can still receive children? How does that set change when a higher heading comes along?
  2. Where do the ids you must avoid come from, besides the ones you make yourself?

The explanation comes with the course

A walkthrough of every line of a solution, the mistakes the tests catch, other ways to write it, and links to the lessons that teach each idea.

  • Every line explained
  • The mistakes each test catches
  • Links to the lessons behind it
Back to the question
Loading the editor…