Skip to content

Use appropriate heading levels in docblocks #89309

Closed
@jsha

Description

@jsha

Following up on #87059 (big thanks @ahicks92 for the detailed feedback!):

[...] Rustdoc didn't do h tags right. It's not exactly systemic, but it's something like (level + 3) % 6, where what should be heading levels 4-6 (examples, etc) become heading level 1 and 2. This is confusing, but once you learn things like impl blocks tend to be heading level 3 and functions are usually level 4, you effectively have a "jump to next function" keystroke and so on.

Here's what's happening: in Markdown # generally represents an h1, ## represents h2, and so on. That means each docblock often has h1s and h2s in them, even though they are nested under higher-up h1s and h2s. For instance, https://doc.rust-lang.org/1.55.0/std/io/trait.Read.html has an <h1>Trait std::io::Read</h1> and an <h1>Examples</h1>. The latter should be <h2>. Similarly, there's an <h4>fn read...</h4>, with an <h1>Errors</h1> nested under it. The latter should be <h5>.

Per https://developer.mozilla.org/en-US/docs/Web/HTML/Element/Heading_Elements#usage_notes:

Avoid skipping heading levels: always start from <h1>, followed by <h2> and so on.
Use only one <h1> per page or view. It should concisely describe the overall purpose of the content.

Fortunately, we already control the <h{level}> tag that's emitted based on Markdown. The code is here:

let start_tags = format!(
"<h{level} id=\"{id}\" class=\"section-header\">\
<a href=\"#{id}\">",
id = id,
level = level
);
. What we additionally need:

  • Add a way to keep track of what heading level we're generating Markdown into: are we nested under an <h1> or under an <h4>?
  • Adjust the {level} interpolation to add the number of levels appropriate. This should be capped at 6, since h6 is the highest defined in HTML.
  • Adjust CSS styles appropriately.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-rustdoc-uiArea: Rustdoc UI (generated HTML)T-rustdocRelevant to the rustdoc team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions