Use PageOutline.Create to list headings and regions like main content or navigation.
dotnet add package RecursiveAI.OneHtml.AccessibilityRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using RecursiveAI.OneHtml; using RecursiveAI.OneHtml.Css; using RecursiveAI.OneHtml.Accessibility; var doc = Document.Parse(""" <main><h1>Account</h1> <h2 hidden>Draft</h2> <nav aria-label="Settings"></nav></main> """); var styles = DocumentStyles.Create(doc); var outline = PageOutline.Create(doc, styles); foreach (var h in outline.Headings) Console.WriteLine($"H{h.Level}: {h.Name}"); foreach (var l in outline.Landmarks) { var label = l.Name.Length == 0 ? l.Role : $"{l.Role}: {l.Name}"; Console.WriteLine( $"{label} (depth {l.Depth})"); }
Paste the code above into Program.cs of a new dotnet new console project and dotnet run, or unzip the example and dotnet run.
Prints the accessible heading and landmarks. The hidden heading Draft is omitted:
H1: Account main (depth 0) navigation: Settings (depth 1)
If the lists are empty, check for hidden elements and for generic elements such as div that lack a heading or landmark role.
Variations
Keep heading levels but skip computing their names
Replace the outline declaration. Heading names are empty, but landmark names are still computed:
var outline = PageOutline.Create(doc, styles, includeHeadingNames: false);
Related recipes
- Build a screen-reader reading order
- Build an accessibility tree
- Resolve accessible names and descriptions
The short answer
Parse the HTML, create DocumentStyles, then pass both to PageOutline.Create. Its Headings and Landmarks are separate lists. Each heading gives a level and accessible name; each landmark gives a role, name and nesting depth. Each entry also points to its original DOM node, so you can tie a row in your outline back to the element that produced it.
What is the difference between headings and landmarks in HTML?
Headings and landmarks serve different navigation needs. Headings describe the document’s sections and their levels. Landmarks describe regions by purpose, such as the main content or a navigation area. A navigation region can be useful without a heading, and an article can have multiple heading levels inside one main landmark. In the sample, Account is the level-one heading and Settings is the accessible name of a navigation landmark, not a second heading.
OneHtml returns two ordered lists, not a tree grouping headings under landmarks. If you’re building a table of contents, use the heading levels to express hierarchy. For a landmark report, use Depth to show nesting; it counts listed ancestor landmarks, not all ancestor HTML elements.
How do I extract only accessible headings from HTML?
A CSS query such as h1, h2, h3, h4, h5, h6 finds tags. PageOutline works from the accessible roles, names and heading levels instead. It takes hidden state into account, including hidden, display: none and aria-hidden. It also accounts for aria-owns, which can affect the list order and landmark depth. That’s why it’s useful when reviewing navigation exposed by the page, rather than when counting every heading tag in the source.
Use the same style inputs you want the outline to reflect. As the OneHtml reading-view guide explains, the outline uses the HTML and CSS you pass in; it doesn’t launch a browser or use browser layout data. It doesn’t return a reading sequence, focus information or all accessible descriptions and states. Use a full accessibility tree when you need those additional details; use the two lists when you only need page navigation.