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] Merge <code> tags into one whenever possible #136308

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

GuillaumeGomez
Copy link
Member

@GuillaumeGomez GuillaumeGomez commented Jan 30, 2025

Fixes #62867.
Fixes #83997.

When we have multiple <code> tags following each others, we never tried merging them into one, especially when a <code> is inside a <a>. This PR merges them into one <code>, making the generated HTML shorter. And it has the nice side-effect of also making it look nicer.

Before:

image

After:

image

You can test it here.

r? @notriddle

@GuillaumeGomez GuillaumeGomez marked this pull request as ready for review January 30, 2025 16:48
@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. labels Jan 30, 2025
@hkBst
Copy link
Contributor

hkBst commented Jan 30, 2025

The "A <Stream a> stuff." does not seem to get rid of extra spacing yet... for me on firefox if that matters.

  • <Stream> vs <Stream a>
  • <code>&lt;<a href="[struct.Foo.html](view-source:https://rustdoc.crud.net/imperio/merge-code-tags/foo/struct.Foo.html)" title="struct foo::Foo">Stream</a>&gt;</code> vs
  • <code>&lt;</code><a href="[struct.Foo.html](view-source:https://rustdoc.crud.net/imperio/merge-code-tags/foo/struct.Foo.html)" title="struct foo::Foo"><code>Stream</code> a</a><code>&gt;</code>

The < is still in its own code block...

@notriddle
Copy link
Contributor

notriddle commented Jan 30, 2025

I'm not convinced that this is a good solution?

Other markdown implementations don't merge consecutive code blocks, the resulting source is noisy with ` marks, and you can already fix this with <code> tags like in this example from #88343:

Additionally, the return value of this function is [`fmt::Result`] which is a
type alias of <code>[Result]<(), [std::fmt::Error]></code>. Formatting implementations

The alternative, [`Result`]`<(), `[`std::fmt::Error`]`>`, has a bunch more formatting characters inside the intended text, which seems worse than the HTML-based solution.

@GuillaumeGomez
Copy link
Member Author

The "A <Stream a> stuff." does not seem to get rid of extra spacing yet... for me on firefox if that matters.

* `<Stream>` vs `<``Stream` a`>`

* `<code>&lt;<a href="[struct.Foo.html](view-source:https://rustdoc.crud.net/imperio/merge-code-tags/foo/struct.Foo.html)" title="struct foo::Foo">Stream</a>&gt;</code>` vs

* `<code>&lt;</code><a href="[struct.Foo.html](view-source:https://rustdoc.crud.net/imperio/merge-code-tags/foo/struct.Foo.html)" title="struct foo::Foo"><code>Stream</code> a</a><code>&gt;</code>`

The < is still in its own code block...

Obviously yes. A whitespace is a character on its own and should not be merged if not part of a code. And in the case you displayed, it's even worse: it's a non-<code> in a <a>. We can improve this situation with CSS but it'll be part of a follow-up.

I'm not convinced that this is a good solution?

Other markdown implementations don't merge consecutive code blocks, the resulting source is noisy with ` marks, and you can already fix this with <code> tags like in this example from #88343:

Additionally, the return value of this function is [`fmt::Result`] which is a
type alias of **<code>[Result]<(), [std::fmt::Error]></code>**. Formatting implementations

The alternative, [`Result`]`<(), `[`std::fmt::Error`]`>`, has a bunch more formatting characters inside the intended text, which seems worse than the HTML-based solution.

It's a case common enough when intra-doc links are used and people are generally not aware that you can simply use HTML with markdown. I agree that it's less good than using <code>, but it's worth noting that even in the std, there are cases like that.

An alternative would be writing a rustdoc lint. I tend to prefer the current approach as it's invisible to the user though.

notriddle added a commit to notriddle/rust-clippy that referenced this pull request Jan 30, 2025
This is the lint described at
rust-lang/rust#136308 (comment)
that recommends using HTML to nest links inside code.
notriddle added a commit to notriddle/rust-clippy that referenced this pull request Jan 30, 2025
This is the lint described at
rust-lang/rust#136308 (comment)
that recommends using HTML to nest links inside code.
notriddle added a commit to notriddle/rust-clippy that referenced this pull request Jan 30, 2025
This is the lint described at
rust-lang/rust#136308 (comment)
that recommends using HTML to nest links inside code.
notriddle added a commit to notriddle/rust-clippy that referenced this pull request Jan 30, 2025
This is the lint described at
rust-lang/rust#136308 (comment)
that recommends using HTML to nest links inside code.
notriddle added a commit to notriddle/rust-clippy that referenced this pull request Feb 4, 2025
This is the lint described at
rust-lang/rust#136308 (comment)
that recommends using HTML to nest links inside code.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue.
Projects
None yet
4 participants