about summary refs log tree commit diff
path: root/src/librustdoc/html/markdown.rs
AgeCommit message (Collapse)AuthorLines
2025-07-19Fix clippy lints in librustdocGuillaume Gomez-2/+2
2025-06-29Lazy-ify some markdown renderingYotam Ofek-18/+13
2025-06-29Don't try to guess how much to pre-allocateYotam Ofek-2/+2
Removing this heuristic doesn't show up as a regression in perf run
2025-06-10Give more information into extracted doctest informationGuillaume Gomez-1/+2
2025-06-07Rollup merge of #140560 - Urgau:test_attr-module-level, r=GuillaumeGomezGuillaume Gomez-1/+0
Allow `#![doc(test(attr(..)))]` everywhere This PR adds the ability to specify [`#![doc(test(attr(..)))]`](https://doc.rust-lang.org/nightly/rustdoc/write-documentation/the-doc-attribute.html#testattr) ~~at module level~~ everywhere in addition to allowing it at crate-root. This is motivated by a recent PR #140323 (by ````@tgross35)```` where we have to duplicate 2 attributes to every single `f16` and `f128` doctests, by allowing `#![doc(test(attr(..)))]` at module level (and everywhere else) we can omit them entirely and just have (in both module): ```rust #![doc(test(attr(feature(cfg_target_has_reliable_f16_f128))))] #![doc(test(attr(expect(internal_features))))] ``` Those new attributes are appended to the one found at crate-root or at a previous module. Those "global" attributes are compatible with merged doctests (they already were before). Given the small addition that this is, I'm proposing to insta-stabilize it, but I can feature-gate it if preferred. Best reviewed commit by commit. r? ````@GuillaumeGomez````
2025-05-30Auto merge of #141573 - nnethercote:rustdoc-alloc-cleanups, r=camelidbors-1/+1
rustdoc: cleanups relating to allocations These commits generally clean up the code a bit and also reduce allocation rates a bit. r? `@camelid`
2025-05-26Avoid some unnecessary cloning.Nicholas Nethercote-1/+1
2025-05-24rustdoc: use descriptive tooltip if doctest is conditionally ignoredbinarycat-2/+4
fixes https://github.com/rust-lang/rust/issues/141092
2025-05-22Collect and use `#![doc(test(attr(..)))]` at module level tooUrgau-1/+0
2025-05-02Create a builder for DocTestBuilder typeGuillaume Gomez-4/+6
2025-05-02Emit a warning if the doctest `main` function will not be runGuillaume Gomez-2/+4
2025-04-25Rollup merge of #137096 - ehuss:stabilize-doctest-xcompile, r=fmeaseMatthias Krüger-22/+8
Stabilize flags for doctest cross compilation This makes the following changes in preparation for supporting doctest cross-compiling in cargo: - Renames `--runtool` and `--runtool-arg` to `--test-runtool` and `--test-runtool-arg` to maintain consistency with other `--test-*` arguments. - Stabilizes the `--test-runtool` and `--test-runtool-arg`. These are needed in order to support cargo's `target.runner` option which specifies a runner to execute a cross-compiled doctest (for example, qemu). - Stabilizes the `--enable-per-target-ignores` flag by removing it and making it unconditionally enabled. This makes it possible to disable a doctest on a per-target basis, which I think will be helpful for rolling out this feature. These changes were suggested in https://rust-lang.zulipchat.com/#narrow/channel/266220-t-rustdoc/topic/stabilizing.20doctest.20xcompile/near/409281127 The intent is to stabilize the doctest-xcompile feature in cargo. This will help ensure that for projects that do cross-compile testing that their doctests are also covered. Currently there is a somewhat surprising behavior that they are ignored. Closes https://github.com/rust-lang/rust/issues/64245 try-job: x86_64-msvc-1
2025-04-09rustdoc: Enable Markdown extensions when looking for doctestsNoah Lev-1/+1
We should enable these to avoid misinterpreting uses of the extended syntax as code blocks. This happens in practice with multi-paragraph footnotes, as discovered in #139064.
2025-03-28Rollup merge of #138678 - durin42:rmeta-stability, r=fmeaseMatthias Krüger-1/+2
rustc_resolve: fix instability in lib.rmeta contents rust-lang/rust@23032f31c91f2 accidentally introduced some nondeterminism in the ordering of lib.rmeta files, which we caught in our bazel-based builds only recently due to being further behind than normal. In my testing, this fixes the issue.
2025-03-27Remove and stabilize --enable-per-target-ignoresEric Huss-22/+8
This removes the `--enable-per-target-ignores` and enables it unconditionally.
2025-03-27librustdoc: also stabilize iteration order hereAugie Fackler-1/+2
2025-03-25ignore doctests only in specified targetsTakayuki Maeda-4/+5
add necessary lines fix ui test error
2025-03-22Rollup merge of #138535 - yotamofek:pr/rustdoc/lang-string-parse-cleanup, ↵Matthias Krüger-44/+41
r=notriddle Cleanup `LangString::parse` Flatten some `if`s into match patterns Use `str::strip_prefix` instead of `starts_with`+indexing Avoid redundant tests for `extra.is_some()`
2025-03-16Suppress must_use in compiler and toolsMichael Goulet-1/+1
2025-03-15Cleanup `LangString::parse`Yotam Ofek-44/+41
Flatten some `if`s into match patterns Use `str::strip_prefix` instead of `starts_with`+indexing Avoid redundant tests for `extra.is_some()`
2025-03-07Rollup merge of #138107 - yotamofek:pr/rustdoc/clippy, r=GuillaumeGomezMatthias Krüger-1/+1
`librustdoc`: clippy fixes First commit is all machine-generated fixes, next two are some more lints fixed by hand/misc. cleanups Inspired by the redundant `.and_then()` added in https://github.com/rust-lang/rust/pull/137320 , and [this comment](https://github.com/rust-lang/rust/pull/138090#discussion_r1983111856) r? ```@GuillaumeGomez```
2025-03-06`x clippy src/librustdoc --fix`Yotam Ofek-1/+1
2025-03-06`librustdoc`: flatten nested ifsYotam Ofek-8/+7
2025-02-15rustdoc: improve refdef handling in the unresolved link lintMichael Howell-3/+67
This commit takes advantage of a feature in pulldown-cmark that makes the list of link definitions available to the consuming application. It produces unresolved link warnings for refdefs that aren't used, and can now produce exact spans for the dest even when it has escapes.
2025-02-12Rollup merge of #136927 - GuillaumeGomez:add-missing-hashtag-escape, r=notriddleJacob Pratt-3/+5
Correctly escape hashtags when running `invalid_rust_codeblocks` lint Fixes #136899. We forgot to use `map_line` when we wrote this lint. r? ``@notriddle``
2025-02-12Correctly escape hashtags when running `invalid_rust_codeblocks` lintGuillaume Gomez-3/+5
2025-02-12Nuke `Buffer` abstraction from `librustdoc` 💣Yotam Ofek-3/+2
2024-12-25Improve rustdoc codeGuillaume Gomez-3/+3
2024-12-17Fix intra doc links not generated inside footnote definitionsGuillaume Gomez-16/+50
2024-12-05Turn `markdown_split_summary_and_content` into a method of `Markdown`Guillaume Gomez-46/+49
2024-12-05Always display first line of impl blocks even when collapsedGuillaume Gomez-21/+74
2024-12-02Remove static HashSet for default IDs listGuillaume Gomez-60/+56
2024-12-01Store default ID map in a staticGuillaume Gomez-3/+7
2024-12-01Split ID maps in two parts: the constant one and the updated oneGuillaume Gomez-61/+57
2024-12-01Stop cloning `Context` so muchGuillaume Gomez-49/+64
2024-11-28Fix new clippy lintsGuillaume Gomez-18/+15
2024-11-13Fix duplicated footnote IDsGuillaume Gomez-16/+30
2024-10-19rustdoc: Extract footnote logic into it's own module.Alona Enraght-Moony-79/+5
2024-10-16rustdoc: Rename "object safe" to "dyn compatible"León Orell Valerian Liehr-1/+1
2024-10-08Auto merge of #131368 - GuillaumeGomez:rustdoc-dead-code, r=notriddlebors-48/+1
[rustdoc] Remove intra-doc links dead code While working on https://github.com/rust-lang/rust/pull/130278, I wondered what `resolve_display_text` was doing. I removed it and ran all rustdoc tests, and nothing failed. Are some intra-doc links tests missing or is it really dead code? Couldn't figure it out. r? `@notriddle`
2024-10-07Remove dead codeGuillaume Gomez-48/+1
2024-10-06Handle `librustdoc` cases of `rustc::potential_query_instability` lintismailarilik-4/+4
2024-09-29Rename doctest attribute `standalone-crate` into `standalone_crate` for ↵Guillaume Gomez-3/+3
coherency
2024-09-28Improve mistyped docblock attribute warning messagesGuillaume Gomez-31/+27
2024-09-28Add warning if `standalone-crate` is mistypedGuillaume Gomez-0/+14
2024-09-28Improve code for codeblock invalid attributesGuillaume Gomez-25/+18
2024-09-28Rename `standalone` doctest attribute into `standalone-crate`Guillaume Gomez-4/+4
2024-09-22Reformat using the new identifier sorting from rustfmtMichael Goulet-2/+2
2024-09-22Strip last backline from non-rust code examplesGuillaume Gomez-1/+3
2024-09-05Rollup merge of #120736 - notriddle:notriddle/toc, r=t-rustdocMatthias Krüger-11/+62
rustdoc: add header map to the table of contents ## Summary Add header sections to the sidebar TOC. ### Preview ![image](https://github.com/user-attachments/assets/eae4df02-86aa-4df4-8c61-a95685cd8829) * http://notriddle.com/rustdoc-html-demo-9/toc/rust/std/index.html * http://notriddle.com/rustdoc-html-demo-9/toc/rust-derive-builder/derive_builder/index.html ## Motivation Some pages are very wordy, like these. | crate | word count | |--|--| | [std::option](https://doc.rust-lang.org/stable/std/option/index.html) | 2,138 | [derive_builder](https://docs.rs/derive_builder/0.13.0/derive_builder/index.html) | 2,403 | [tracing](https://docs.rs/tracing/0.1.40/tracing/index.html) | 3,912 | [regex](https://docs.rs/regex/1.10.3/regex/index.html) | 8,412 This kind of very long document is more navigable with a table of contents, like Wikipedia's or the one [GitHub recently added](https://github.blog/changelog/2021-04-13-table-of-contents-support-in-markdown-files/) for READMEs. In fact, the use case is so compelling, that it's been requested multiple times and implemented in an extension: * https://github.com/rust-lang/rust/issues/80858 * https://github.com/rust-lang/rust/issues/28056 * https://github.com/rust-lang/rust/issues/14475 * https://rust.extension.sh/#show-table-of-content (Some of these issues ask for more than this, so don’t close them.) It's also been implemented by hand in some crates, because the author really thought it was needed. Protip: for a more exhaustive list, run [`site:docs.rs table of contents`](https://duckduckgo.com/?t=ffab&q=site%3Adocs.rs+table+of+contents&ia=web), though some of them are false positives. * https://docs.rs/figment/0.10.14/figment/index.html#table-of-contents * https://docs.rs/csv/1.3.0/csv/tutorial/index.html#table-of-contents * https://docs.rs/axum/0.7.4/axum/response/index.html#table-of-contents * https://docs.rs/regex-automata/0.4.5/regex_automata/index.html#table-of-contents Unfortunately for these hand-built ToCs, because they're just part of the docs, there's no consistent way to turn them off if the reader doesn't want them. It's also more complicated to ensure they stay in sync with the docs they're supposed to describe, and they don't stay with you when you scroll like Wikipedia's [does now](https://uxdesign.cc/design-notes-on-the-2023-wikipedia-redesign-d6573b9af28d). ## Guide-level explanation When writing docs for a top-level item, the first and second level of headers will be shown in an outline in the sidebar. In this context, "top level" means "not associated". This means, if you're writing very long guides or explanations, and you want it to have a table of contents in the sidebar for its headings, the ideal place to attach it is usually the *module* or *crate*, because this page has fewer other things on it (and is the ideal place to describe "cross-cutting concerns" for its child items). If you're reading documentation, and want to get rid of the table of contents, open the ![image](https://github.com/rust-lang/rust/assets/1593513/2ad82466-5fe3-4684-b1c2-6be4c99a8666) Settings panel and checkmark "Hide table of contents." ## Reference-level explanation Top-level items have an outline generated. This works for potentially-malformed header trees by pairing a header with the nearest header with a higher level. For example: ```markdown ## A # B # C ## D ## E ``` A, B, and C are all siblings, and D and E are children of C. Rustdoc only presents two layers of tree, but it tracks up to the full depth of 6 while preparing it. That means that these two doc comment both generate the same outline: ```rust /// # First /// ## Second struct One; /// ## First /// ### Second struct Two; ``` ## Drawbacks The biggest drawback is adding more stuff to the sidebar. My crawl through docs.rs shows this to, surprisingly, be less of a problem than I thought. The manually-built tables of contents, and the pages with dozens of headers, usually seem to be modules or crates, not types (where extreme scrolling would become a problem, since they already have methods to deal with). The best example of a type with many headers is [vec::Vec](https://doc.rust-lang.org/1.75.0/std/vec/struct.Vec.html), which still only has five headers, not dozens like [axum::extract](https://docs.rs/axum/0.7.4/axum/extract/index.html). ## Rationale and alternatives ### Why in the existing sidebar? The method links and the top-doc header links have more in common with each other than either of them do with the "In [parent module]" links, and should go together. ### Why limited to two levels? The sidebar is pretty narrow, and I don't want too much space used by indentation. Making the sidebar wider, while it has some upsides, also takes up more space on middling-sized screens or tiled WMs. ### Why not line wrap? That behaves strangely when resizing. ## Prior art ### Doc generators that have TOC for headers https://hexdocs.pm/phoenix/Phoenix.Controller.html is very close, in the sense that it also has header sections directly alongside functions and types. Another example, referenced as part of the [early sidebar discussion](https://github.com/rust-lang/rust/issues/37856) that added methods, Ruby will show a table of contents in the sidebar (for example, on the [ARGF](https://docs.ruby-lang.org/en/master/ARGF.html) class). According to their changelog, [they added it in 2013](https://github.com/ruby/rdoc/blob/06137bde8ccc48cd502bc28178bcd8f2dfe37624/History.rdoc#400--2013-02-24-). Haskell seems to mix text and functions even more freely than Elixir. For example, this [Naming conventions](https://hackage.haskell.org/package/base-4.19.0.0/docs/Control-Monad.html#g:3) is plain text, and is immediately followed by functions. And the [Pandoc top level](https://hackage.haskell.org/package/pandoc-3.1.11.1/docs/Text-Pandoc.html) has items split up by function, rather than by kind. Their TOC matches exactly with the contents of the page. ### Doc generators that don't have header TOC, but still have headers Elm, interestingly enough, seems to have the same setup that Rust used to have: sibling navigation between modules, and no index within a single page. [They keep Haskell's habit of named sections with machine-generated type signatures](https://package.elm-lang.org/packages/elm/browser/latest/Browser-Dom), though. [PHP](https://www.php.net/manual/en/book.datetime.php), like elm, also has a right-hand sidebar with sibling navigation. However, PHP has a single page for a single method, unlike Rust's page for an entire "class." So even though these pages have headers, it's never more than ten at most. And when they have guides, those guides are also multi-page. ## Unresolved questions * Writing recommendations for anyone who wants to take advantage of this. * Right now, it does not line wrap. That might be a bad idea: a lot of these are getting truncated. * Split sidebars, which I [tried implementing](https://rust-lang.zulipchat.com/#narrow/stream/266220-t-rustdoc/topic/Table.20of.20contents), are not required. The TOC can be turned off, if it's really a problem. Implemented in https://github.com/rust-lang/rust/pull/120818, but needs more, separate, discussion. ## Future possibilities I would like to do a better job of distinguishing global navigation from local navigation. Rustdoc has a pretty reasonable information architecture, if only we did a better job of communicating it. This PR aims, mostly, to help doc authors help their users by writing docs that can be more effectively skimmed. But it doesn't do anything to make it easier to tell the TOC and the Module Nav apart.