Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rustdoc: Add anchors to section headers #12687

Closed
wants to merge 1 commit into from

Conversation

alexcrichton
Copy link
Member

This commit adds a appear-on-over link to all section headers to generated
documentation. Each header also receives an id now, even those generated through
markdown. The purpose of this is to provide easy to link to sections.

This modifies the default header markdown generation because the default id
added looks like "toc_NN" which is difficult to reconcile among all sections (by
default each section gets a "toc_0" id), and it's also not very descriptive of
where you're going.

This chooses to adopt the github-style anchors by taking the contents of the
title and hyphen-separating them (after lower casing).

Closes #12681

@alexcrichton
Copy link
Member Author

@adrientetar
Copy link
Contributor

Is the yellow highlight necessary? I don't think it serves any purpose and it adds visual noise imo. Looks good otherwise.

@alexcrichton
Copy link
Member Author

It was initially implemented for jumping to methods and such because it's quite a forest once you get into the nitty gritty. That may be a little different from this, but I kinda like the highlights (not that I have a designer license any more)

@huonw
Copy link
Member

huonw commented Mar 5, 2014

What happens with something like

# General Topic 1

## Example

# General Topic 2

## Example

Do both Examples get the example anchor?

@alexcrichton
Copy link
Member Author

@huonw, good catch. I think I'll again adopt what github does and name the second one example-2 whereas the first one is just example

@huonw
Copy link
Member

huonw commented Mar 5, 2014

Expanding on that example above, AFAICT each markdown chunk is rendered in isolation via a Show impl, but there are some pages that show multiple independent chunks (e.g. trait pages, and pages for types with docs on each method).

These should have unique anchors too: I guess just adding the current method/function/whatever to the subsection would work (e.g. #method.foo-example for the Example heading in the docs of a fn foo(&self))... but this presumably requires external state which rendering via Show doesn't support easily.

@huonw
Copy link
Member

huonw commented Mar 5, 2014

Oh, I guess one could just put the parent information into the markdown struct itself via a parent: Option<&str> field. Breaks the neatness of Markdown<'a>(&'a str) though. :(

@alexcrichton
Copy link
Member Author

Updated with a task-local map which is reset across page boundaries.

// Make sure our hyphenated ID is unique for this page
let id = local_data::get_mut(used_header_map, |map| {
let map = map.unwrap();
match map.find(&id) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couldn't this be

match map.find_mut(&id) {
    None => {}
    Some(a) => { *a += 1; format!("{}-{}", id, a - 1); return }
}
map.insert(id.clone(), 1);

?

(Not that performance matters much here.)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks cleaner to me!

@huonw
Copy link
Member

huonw commented Mar 6, 2014

I'm not keen on having the implicit almost-global (especially if this part of rustdoc is ever parallised, but I guess this is unlikely to happen, since markdown rendering is now really fast since we're not shelling out to pandoc, right?), but it is certainly a neat solution to this.

@alexcrichton
Copy link
Member Author

I think this strategy would work with parallelism because we'll only ever parallelize on the granularity of rendering pages, not rendering within a page itself (we generate a huge number of pages).

@alexcrichton
Copy link
Member Author

Updated with comments

This commit adds a appear-on-over link to all section headers to generated
documentation. Each header also receives an id now, even those generated through
markdown. The purpose of this is to provide easy to link to sections.

This modifies the default header markdown generation because the default id
added looks like "toc_NN" which is difficult to reconcile among all sections (by
default each section gets a "toc_0" id), and it's also not very descriptive of
where you're going.

This chooses to adopt the github-style anchors by taking the contents of the
title and hyphen-separating them (after lower casing).

Closes rust-lang#12681
bors added a commit that referenced this pull request Mar 6, 2014
This commit adds a appear-on-over link to all section headers to generated
documentation. Each header also receives an id now, even those generated through
markdown. The purpose of this is to provide easy to link to sections.

This modifies the default header markdown generation because the default id
added looks like "toc_NN" which is difficult to reconcile among all sections (by
default each section gets a "toc_0" id), and it's also not very descriptive of
where you're going.

This chooses to adopt the github-style anchors by taking the contents of the
title and hyphen-separating them (after lower casing).

Closes #12681
@bors bors closed this Mar 6, 2014
@alexcrichton alexcrichton deleted the issue-12681 branch March 6, 2014 20:02
bors added a commit to rust-lang-ci/rust that referenced this pull request Jul 25, 2022
Improve documentation for `buildScripts.overrideCommand` / `checkOnSave.overrideCommand`
flip1995 pushed a commit to flip1995/rust that referenced this pull request May 2, 2024
…Frednet

Don't suggest `Box::default()` in functions with differing generics

Fixes rust-lang#12684

changelog: none
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Headings in rustdoc markdown chunks should have anchors & with copyable links
4 participants