Blog / Build with AI /
Building a heading outline extractor with AI
Headings are the skeleton of a page. Readers skim them, screen reader users jump between them, and search engines use them to understand structure. Yet on a lot of pages the skeleton is a mess: three H1s, an H4 straight after an H2, or headings chosen because the font looked right.
Seeing the outline is the first step to fixing it. And it’s a lovely small project to build with an AI assistant.
The finished thing
A tool that takes a page (pasted HTML or a URL) and prints its headings as an indented outline, with warnings for a missing or repeated H1 and for skipped levels.
Decide where it runs
You have three realistic choices, and the right one depends on how you’ll use it.
- A bookmarklet or browser console snippet. Runs on the page you’re looking at. The simplest by far, and it sees the page after JavaScript has run.
- A Chrome extension. The same logic with a button and a nicer display. Good if you’ll use it daily. The structure of that kind of project is in building a Chrome extension with AI: the complete beginner’s guide.
- A web page where you paste HTML. Works anywhere, but can’t fetch other sites’ pages directly from the browser, because of cross-origin rules. You’d need a small server-side step for URLs.
Start with the console snippet. It teaches you the logic with no setup, and everything else is packaging.
Step 1: the logic in plain words
Before the prompt, be clear on what you want:
- Find every
h1toh6element in the page’s main content. - For each one, record the level (1 to 6) and its text, trimmed and with extra whitespace collapsed.
- Print them in order, indented by level.
- Warn if there’s no H1, or more than one.
- Warn when a heading jumps down by more than one level (an H2 followed by an H4).
You’ll notice that “main content” is in there. Navigation menus and footers often contain headings too, and you may not want them in the outline. Ask for that explicitly.
Step 2: the prompt
Write a JavaScript function I can paste in the browser console. It should find all h1-h6 elements inside the main element (or the body if there is no main), print an indented outline using console.log with two spaces per level, and list warnings: no H1, more than one H1, and any heading that skips a level compared with the previous heading. Trim text, collapse whitespace, and show “(empty)” for empty headings. Don’t use any libraries.
The result should be short. A correct core looks like this:
const headings = [...document.querySelectorAll('main h1, main h2, main h3, main h4, main h5, main h6')];
let prev = 0;
headings.forEach(h => {
const level = Number(h.tagName[1]);
const text = h.textContent.trim().replace(/\s+/g, ' ') || '(empty)';
console.log(' '.repeat(level - 1) + 'H' + level + ' ' + text);
if (prev && level > prev + 1) console.warn('Skipped level before: ' + text);
prev = level;
});
If your page has no main element, this prints nothing, which is exactly the sort of edge case to test. Ask the assistant to fall back to the body.
Step 3: test it on messy pages
Try it on:
- A page of your own that you think is fine.
- A long article, to check the indentation reads well.
- A page built with a visual builder, where headings are often styled divs. You’ll find the heading tags aren’t where you expect.
- A page with headings inside collapsible sections or tabs. Hidden content is still in the page; decide whether you want it listed.
When it misbehaves, give the assistant the exact symptom (“on this page it lists the menu items as H3s”) and the relevant HTML snippet, not a general complaint.
Step 4: add the things that make it useful
Once the basic outline works, add features one at a time:
- Mark hidden headings. Check
offsetParent === nullor computed style to flag headings not visible to users. - Count words under each heading. It highlights sections that are very thin or very long.
- Copy to clipboard as plain text or as a Markdown list, handy for content briefs.
- Highlight headings on the page with a temporary outline, so you can see where each one sits.
- Check lengths. Very long headings are often whole sentences stuffed with keywords.
What the outline is telling you
Read it like a table of contents. Ask:
- Would a visitor understand the page from the headings alone?
- Is there one H1 that matches the page’s purpose?
- Do the H2s represent the main sections, and the H3s their parts?
- Are any headings there only for looks? If you wanted large text, use CSS, not an H2.
- Are there headings that say nothing (“Welcome”, “More info”)?
Heading order isn’t a ranking switch. A page with a slightly odd outline won’t vanish from search. But a clear structure helps readers and assistive technology, and it tends to make you notice thin or muddled content. If you want the accessibility side, website accessibility basics explains why.
What goes wrong
Page builders and themes. Many themes print the site name or a logo as an H1 on every page, and then the post title as another. Your tool will show you this, and the fix is in the theme, not in the content.
Headings inserted by JavaScript. If the tool runs against the raw HTML rather than the finished page, it will miss these. Check which one you’re measuring.
Over-trusting the warnings. A skipped level in a sidebar widget is not the same as one in the article. Treat the warnings as prompts to look, not verdicts.
Key takeaways
- Start as a console snippet; package it later.
- Limit the search to the main content, and handle pages without a
mainelement. - Warn on missing or multiple H1s and skipped levels, but judge in context.
- Test on messy, builder-made pages.
- Use the outline as a readability check, not only a rules check.
Related reading
Run it on your five most important pages. Which outline surprised you most?