summary refs log tree commit diff
path: root/src/librustdoc/html/markdown.rs
AgeCommit message (Collapse)AuthorLines
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.
2024-08-30Remove `#[macro_use] extern crate tracing` from rustdoc.Nicholas Nethercote-0/+1
2024-08-20rustdoc: consistentify `#TOC` and `#ModNav` to lowercaseMichael Howell-2/+3
2024-08-20rustdoc: show code spans as `<code>` in TOCMichael Howell-2/+27
2024-08-20Add Top TOC support to rustdocMichael Howell-11/+36
This commit adds the headers for the top level documentation to rustdoc's existing table of contents, along with associated items. It only show two levels of headers. Going further would require the sidebar to be wider, and that seems unnecessary (the crates that have manually-built TOCs usually don't need deeply nested headers).
2024-08-20Remove unneeded conversion to `DefId` for `ExtraInfo`Guillaume Gomez-24/+20
2024-08-13Unify naming of `DocTest`Guillaume Gomez-2/+2
2024-08-13Improve code readabilityGuillaume Gomez-1/+1
2024-08-13Rename `DocTest` into `DocTestBuilder`Guillaume Gomez-1/+1
2024-08-13Correctly handle doctests with invalid ASTGuillaume Gomez-1/+1
2024-08-13Add new doc codeblock `standalone` attributeGuillaume Gomez-0/+6
2024-08-13Split standalone and mergeable doctestsGuillaume Gomez-2/+2
2024-08-13Split doctests between standalone and mergeable onesGuillaume Gomez-2/+2
2024-08-13Add `DocTest` typeGuillaume Gomez-2/+3
2024-08-12Rollup merge of #128394 - GuillaumeGomez:run-button, r=t-rustdocGuillaume Gomez-1/+2
Unify run button display with "copy code" button and with mdbook buttons Follow-up of https://github.com/rust-lang/rust/pull/128339. It looks like this (coherency++, yeay!): ![Screenshot from 2024-07-30 15-16-31](https://github.com/user-attachments/assets/5e262e5b-f338-4085-94ca-e223033a43db) Can be tested [here](https://rustdoc.crud.net/imperio/run-button/foo/struct.Bar.html). r? `@notriddle`
2024-08-08rustdoc: do not run doctests with invalid langstringsMichael Howell-4/+16
2024-08-05Unify run button display with "copy code" button and with mdbook buttonsGuillaume Gomez-1/+2
2024-07-29Reformat `use` declarations.Nicholas Nethercote-14/+13
The previous commit updated `rustfmt.toml` appropriately. This commit is the outcome of running `x fmt --all` with the new formatting options.
2024-06-30rustdoc: update to pulldown-cmark 0.11Michael Howell-57/+54
2024-06-07Move some arguments to fields and reorganize fieldsNoah Lev-2/+10
I moved some local arguments and options to either the local options struct or, if it made sense, the global options struct.
2024-06-07rustdoc: Remove `DoctestVisitor::get_line`Noah Lev-1/+23
This was used to get the line number of the first line from the current docstring, which was then used together with an offset within the docstring. It's simpler to just pass the offset to the visitor and have it do the math because it's clearer and this calculation only needs to be done in one place (the Rust doctest visitor).
2024-06-07rustdoc: Rename `Tester` to `DoctestVisitor`Noah Lev-4/+4
The new name more accurately captures what it is.
2024-06-01Auto merge of #124577 - ↵bors-107/+18
GuillaumeGomez:stabilize-custom_code_classes_in_docs, r=rustdoc Stabilize `custom_code_classes_in_docs` feature Fixes #79483. This feature has been around for quite some time now, I think it's fine to stabilize it now. ## Summary ## What is the feature about? In short, this PR changes two things, both related to codeblocks in doc comments in Rust documentation: * Allow to disable generation of `language-*` CSS classes with the `custom` attribute. * Add your own CSS classes to a code block so that you can use other tools to highlight them. #### The `custom` attribute Let's start with the new `custom` attribute: it will disable the generation of the `language-*` CSS class on the generated HTML code block. For example: ```rust /// ```custom,c /// int main(void) { /// return 0; /// } /// ``` ``` The generated HTML code block will not have `class="language-c"` because the `custom` attribute has been set. The `custom` attribute becomes especially useful with the other thing added by this feature: adding your own CSS classes. #### Adding your own CSS classes The second part of this feature is to allow users to add CSS classes themselves so that they can then add a JS library which will do it (like `highlight.js` or `prism.js`), allowing to support highlighting for other languages than Rust without increasing burden on rustdoc. To disable the automatic `language-*` CSS class generation, you need to use the `custom` attribute as well. This allow users to write the following: ```rust /// Some code block with `{class=language-c}` as the language string. /// /// ```custom,{class=language-c} /// int main(void) { /// return 0; /// } /// ``` fn main() {} ``` This will notably produce the following HTML: ```html <pre class="language-c"> int main(void) { return 0; }</pre> ``` Instead of: ```html <pre class="rust rust-example-rendered"> <span class="ident">int</span> <span class="ident">main</span>(<span class="ident">void</span>) { <span class="kw">return</span> <span class="number">0</span>; } </pre> ``` To be noted, we could have written `{.language-c}` to achieve the same result. `.` and `class=` have the same effect. One last syntax point: content between parens (`(like this)`) is now considered as comment and is not taken into account at all. In addition to this, I added an `unknown` field into `LangString` (the parsed code block "attribute") because of cases like this: ```rust /// ```custom,class:language-c /// main; /// ``` pub fn foo() {} ``` Without this `unknown` field, it would generate in the DOM: `<pre class="language-class:language-c language-c">`, which is quite bad. So instead, it now stores all unknown tags into the `unknown` field and use the first one as "language". So in this case, since there is no unknown tag, it'll simply generate `<pre class="language-c">`. I added tests to cover this. EDIT(camelid): This description is out-of-date. Using `custom,class:language-c` will generate the output `<pre class="language-class:language-c">` as would be expected; it treats `class:language-c` as just the name of a language (similar to the langstring `c` or `js` or what have you) since it does not use the designed class syntax. Finally, I added a parser for the codeblock attributes to make it much easier to maintain. It'll be pretty easy to extend. As to why this syntax for adding attributes was picked: it's [Pandoc's syntax](https://pandoc.org/MANUAL.html#extension-fenced_code_attributes). Even if it seems clunkier in some cases, it's extensible, and most third-party Markdown renderers are smart enough to ignore Pandoc's brace-delimited attributes (from [this comment](https://github.com/rust-lang/rust/pull/110800#issuecomment-1522044456)). r? `@notriddle`
2024-05-23Remove `LintDiagnostic::msg`León Orell Valerian Liehr-4/+7
* instead simply set the primary message inside the lint decorator functions * it used to be this way before [#]101986 which introduced `msg` to prevent good path delayed bugs (which no longer exist) from firing under certain circumstances when lints were suppressed / silenced * this is no longer necessary for various reasons I presume * it shaves off complexity and makes further changes easier to implement
2024-05-01Stabilize `custom_code_classes_in_docs` featureGuillaume Gomez-107/+18