···11# mdbookkit
2233-
33+
4455-[`mdbookkit`] provides quality-of-life plugins that you can use in your [mdBook]
66-project.
55+Quality-of-life plugins for your [mdBook] project.
7687Right now, there are two mdBook [preprocessors], both for generating **correct and
98versioned** external links from **easy-to-write** markup.
···1110- [**`mdbook-rustdoc-link`**](https://tonywu6.github.io/mdbookkit/rustdoc-link)
12111312 _rustdoc_-style linking for Rust APIs: write types and function names, get links to
1414- docs.rs
1313+ _docs.rs_
15141615- [**`mdbook-link-forever`**](https://tonywu6.github.io/mdbookkit/link-forever)
1716
+1-1
crates/mdbookkit/src/bin/rustdoc_link/env.rs
···8888 ///
8989 /// This is only meaningful if your links happen to have visible text that has
9090 /// specific punctuation. The processor otherwise passes through the rest of your
9191- /// Markdown source.
9191+ /// Markdown source untouched.
9292 ///
9393 /// **In `book.toml`** — this option is not needed because
9494 /// `output.html.smart-punctuation` is honored.
···11---
22source: crates/mdbookkit/tests/rustdoc_link.rs
33-assertion_line: 31
43expression: output
54---
65# Getting started
···98109## Install
11101212-You will need:
1111+You will need to:
13121414-1. [rust-analyzer]:
1313+1. Have [rust-analyzer]:
15141616- - If you already use the [VS Code extension][ra-extension], this project will
1717- automatically use the server binary that comes with it, no extra setup is needed!
1515+ - If you already use the [VS Code extension][ra-extension]: this crate automatically
1616+ uses the server binary that comes with it, no extra setup is needed!
1817 - Otherwise, [install][ra-install] rust-analyzer (e.g. via `rustup`) and make sure
1918 it's on your `PATH`.
20192121-2. This crate:
2020+2. Install this crate:
22212322 ```
2423 cargo install mdbookkit --features rustdoc-link
···11---
22source: crates/mdbookkit/tests/rustdoc_link.rs
33-assertion_line: 31
43expression: output
54---
65# mdbook-rustdoc-link
66+77+<div class="hidden">
88+99+**For best results, view this page at
1010+<https://tonywu6.github.io/mdbookkit/rustdoc-link>.**
1111+1212+</div>
713814**[_rustdoc_-style linking][rustdoc] for [mdBook]** (with the help of [rust-analyzer]).
915···45514652For **writing documentation** —
47534848-- To know more about how the preprocessor is resolving items into links, including
5454+- To learn more about how it is resolving items into links, including
4955 [feature-gated items](rustdoc-link/name-resolution.md#feature-gated-items), see
5056 [Name resolution](rustdoc-link/name-resolution.md).
5157- To know how to link to other types of items like
···11---
22-source: crates/rustdoc-link/tests/lib.rs
22+source: crates/mdbookkit/tests/rustdoc_link.rs
33expression: output
44---
55# Supported syntax
6677+<div class="hidden">
88+99+**For best results, view this page at
1010+<https://tonywu6.github.io/mdbookkit/rustdoc-link/supported-syntax>.**
1111+1212+</div>
1313+714This page showcases all the syntax supported by `mdbook-rustdoc-link`.
815916Most of the formats [supported by rustdoc][rustdoc-linking] are supported. Unsupported
1017syntax and differences in behavior are emphasized below.
11181212-[rustdoc-linking]:
1313- https://doc.rust-lang.org/rustdoc/write-documentation/linking-to-items-by-name.html
1414-1519In general, specifying items as you would when writing Rust code should "just work".
16201721<details class="toc" open>
···2832- [Linking to page sections](#linking-to-page-sections)
29333034</details>
3535+3636+> [!TIP]
3737+>
3838+> This page is also used for snapshot testing! To see how all the links would look like
3939+> in Markdown after they have been processed, see
4040+> [supported-syntax.snap](/crates/mdbookkit/tests/snaps/rustdoc_link/supported-syntax.snap)
4141+> and
4242+> [supported-syntax.stderr.snap](/crates/mdbookkit/tests/snaps/rustdoc_link/supported-syntax.stderr.snap).
31433244## Types, modules, and associated items
3345···262274[gh-issues]: https://github.com/tonywu6/mdbookkit/issues
263275[namespaces]: https://doc.rust-lang.org/reference/names/namespaces.html
264276[rust-types]: https://doc.rust-lang.org/reference/types.html#r-type.kinds
277277+[rustdoc-linking]: https://doc.rust-lang.org/rustdoc/write-documentation/linking-to-items-by-name.html
265278266279<!-- prettier-ignore-end -->
···11---
22-source: crates/rustdoc-link/tests/lib.rs
22+source: crates/mdbookkit/tests/rustdoc_link.rs
33expression: report
44---
55 info: successfully resolved all links
66- ╭─[supported-syntax:34:10]
77- 33 │ >
88- 34 │ > Module [`alloc`][std::alloc] — Memory allocation APIs.
66+ ╭─[supported-syntax:46:10]
77+ 45 │ >
88+ 46 │ > Module [`alloc`][std::alloc] — Memory allocation APIs.
99 · ──────────┬──────────
1010 · ╰── https://doc.rust-lang.org/stable/std/alloc/index.html
1111- 35 │ >
1212- 36 │ > ```md
1313- 37 │ > Every [`Option`] is either [`Some`][Option::Some][^1] and contains a value, or
1414- 38 │ > [`None`][Option::None][^1], and does not.
1515- 39 │ > ```
1616- 40 │ >
1717- 41 │ > Every [`Option`] is either [`Some`][Option::Some][^1] and contains a value, or
1111+ 47 │ >
1212+ 48 │ > ```md
1313+ 49 │ > Every [`Option`] is either [`Some`][Option::Some][^1] and contains a value, or
1414+ 50 │ > [`None`][Option::None][^1], and does not.
1515+ 51 │ > ```
1616+ 52 │ >
1717+ 53 │ > Every [`Option`] is either [`Some`][Option::Some][^1] and contains a value, or
1818 · ─────┬──── ───────────┬──────────
1919 · │ ╰── https://doc.rust-lang.org/stable/core/option/enum.Option.html#variant.Some
2020 · ╰── https://doc.rust-lang.org/stable/core/option/enum.Option.html
2121- 42 │ > [`None`][Option::None][^1], and does not.
2121+ 54 │ > [`None`][Option::None][^1], and does not.
2222 · ───────────┬──────────
2323 · ╰── https://doc.rust-lang.org/stable/core/option/enum.Option.html#variant.None
2424- 43 │ >
2525- 44 │ > ```md
2626- 45 │ > [`Ipv4Addr::LOCALHOST`][core::net::Ipv4Addr::LOCALHOST] — An IPv4 address with the
2727- 46 │ > address pointing to localhost: `127.0.0.1`.
2828- 47 │ > ```
2929- 48 │ >
3030- 49 │ > [`Ipv4Addr::LOCALHOST`][core::net::Ipv4Addr::LOCALHOST] — An IPv4 address with the
2424+ 55 │ >
2525+ 56 │ > ```md
2626+ 57 │ > [`Ipv4Addr::LOCALHOST`][core::net::Ipv4Addr::LOCALHOST] — An IPv4 address with the
2727+ 58 │ > address pointing to localhost: `127.0.0.1`.
2828+ 59 │ > ```
2929+ 60 │ >
3030+ 61 │ > [`Ipv4Addr::LOCALHOST`][core::net::Ipv4Addr::LOCALHOST] — An IPv4 address with the
3131 · ───────────────────────────┬───────────────────────────
3232 · ╰── https://doc.rust-lang.org/stable/core/net/ip_addr/struct.Ipv4Addr.html#associatedconstant.LOCALHOST
3333- 50 │ > address pointing to localhost: `127.0.0.1`.
3434- 51 │
3535- 52 │ ## Generic parameters
3636- 53 │
3737- 54 │ Types can contain generic parameters. This is _compatible_ with rustdoc.
3838- 55 │
3939- 56 │ > ```md
4040- 57 │ > [`Vec<T>`] — A heap-allocated _vector_ that is resizable at runtime.
4141- 58 │ > ```
4242- 59 │ >
4343- 60 │ > [`Vec<T>`] — A heap-allocated _vector_ that is resizable at runtime.
3333+ 62 │ > address pointing to localhost: `127.0.0.1`.
3434+ 63 │
3535+ 64 │ ## Generic parameters
3636+ 65 │
3737+ 66 │ Types can contain generic parameters. This is _compatible_ with rustdoc.
3838+ 67 │
3939+ 68 │ > ```md
4040+ 69 │ > [`Vec<T>`] — A heap-allocated _vector_ that is resizable at runtime.
4141+ 70 │ > ```
4242+ 71 │ >
4343+ 72 │ > [`Vec<T>`] — A heap-allocated _vector_ that is resizable at runtime.
4444 · ─────┬────
4545 · ╰── https://doc.rust-lang.org/stable/alloc/vec/struct.Vec.html
4646- 61 │ >
4747- 62 │ > ```md
4848- 63 │ > | Phantom type | variance of `T` |
4949- 64 │ > | :------------------------------------------------- | :---------------- |
5050- 65 │ > | [`&'a mut T`][std::marker::PhantomData<&'a mut T>] | **inv**ariant |
5151- 66 │ > | [`fn(T)`][std::marker::PhantomData<fn(T)>] | **contra**variant |
5252- 67 │ > ```
5353- 68 │ >
5454- 69 │ > | Phantom type | variance of `T` |
5555- 70 │ > | :------------------------------------------------- | :---------------- |
5656- 71 │ > | [`&'a mut T`][std::marker::PhantomData<&'a mut T>] | **inv**ariant |
4646+ 73 │ >
4747+ 74 │ > ```md
4848+ 75 │ > | Phantom type | variance of `T` |
4949+ 76 │ > | :------------------------------------------------- | :---------------- |
5050+ 77 │ > | [`&'a mut T`][std::marker::PhantomData<&'a mut T>] | **inv**ariant |
5151+ 78 │ > | [`fn(T)`][std::marker::PhantomData<fn(T)>] | **contra**variant |
5252+ 79 │ > ```
5353+ 80 │ >
5454+ 81 │ > | Phantom type | variance of `T` |
5555+ 82 │ > | :------------------------------------------------- | :---------------- |
5656+ 83 │ > | [`&'a mut T`][std::marker::PhantomData<&'a mut T>] | **inv**ariant |
5757 · ─────────────────────────┬────────────────────────
5858 · ╰── https://doc.rust-lang.org/stable/core/marker/struct.PhantomData.html
5959- 72 │ > | [`fn(T)`][std::marker::PhantomData<fn(T)>] | **contra**variant |
5959+ 84 │ > | [`fn(T)`][std::marker::PhantomData<fn(T)>] | **contra**variant |
6060 · ─────────────────────┬────────────────────
6161 · ╰── https://doc.rust-lang.org/stable/core/marker/struct.PhantomData.html
6262- 73 │
6363- 74 │ This includes if you use turbofish:
6464- 75 │
6565- 76 │ > ```md
6666- 77 │ > `collect()` is one of the few times you’ll see the syntax affectionately known as the
6767- 78 │ > "turbofish", for example: [`Iterator::collect::<Vec<i32>>()`].
6868- 79 │ > ```
6969- 80 │ >
7070- 81 │ > `collect()` is one of the few times you’ll see the syntax affectionately known as the
7171- 82 │ > "turbofish", for example: [`Iterator::collect::<Vec<i32>>()`].
6262+ 85 │
6363+ 86 │ This includes if you use turbofish:
6464+ 87 │
6565+ 88 │ > ```md
6666+ 89 │ > `collect()` is one of the few times you’ll see the syntax affectionately known as the
6767+ 90 │ > "turbofish", for example: [`Iterator::collect::<Vec<i32>>()`].
6868+ 91 │ > ```
6969+ 92 │ >
7070+ 93 │ > `collect()` is one of the few times you’ll see the syntax affectionately known as the
7171+ 94 │ > "turbofish", for example: [`Iterator::collect::<Vec<i32>>()`].
7272 · ─────────────────┬─────────────────
7373 · ╰── https://doc.rust-lang.org/stable/core/iter/traits/iterator/trait.Iterator.html#method.collect
7474- 83 │
7575- 84 │ ## Functions and macros
7676- 85 │
7777- 86 │ To indicate that an item is a function, add `()` after the function name. To indicate
7878- 87 │ that an item is a macro, add `!` after the macro name, optionally followed by `()`,
7979- 88 │ `[]`, or `{}`. This is _compatible_ with rustdoc.
8080- 89 │
8181- 90 │ Note that there cannot be arguments within `()`, `[]`, or `{}`.
8282- 91 │
8383- 92 │ > ```md
8484- 93 │ > [`vec!`][std::vec!][^2] is different from [`vec`][std::vec], and don't accidentally
8585- 94 │ > use [`format()`][std::fmt::format()] in place of [`format!()`][std::format!()][^2]!
8686- 95 │ > ```
8787- 96 │ >
8888- 97 │ > [`vec!`][std::vec!][^2] is different from [`vec`][std::vec], and don't accidentally
7474+ 95 │
7575+ 96 │ ## Functions and macros
7676+ 97 │
7777+ 98 │ To indicate that an item is a function, add `()` after the function name. To indicate
7878+ 99 │ that an item is a macro, add `!` after the macro name, optionally followed by `()`,
7979+ 100 │ `[]`, or `{}`. This is _compatible_ with rustdoc.
8080+ 101 │
8181+ 102 │ Note that there cannot be arguments within `()`, `[]`, or `{}`.
8282+ 103 │
8383+ 104 │ > ```md
8484+ 105 │ > [`vec!`][std::vec!][^2] is different from [`vec`][std::vec], and don't accidentally
8585+ 106 │ > use [`format()`][std::fmt::format()] in place of [`format!()`][std::format!()][^2]!
8686+ 107 │ > ```
8787+ 108 │ >
8888+ 109 │ > [`vec!`][std::vec!][^2] is different from [`vec`][std::vec], and don't accidentally
8989 · ─────────┬───────── ────────┬────────
9090 · │ ╰── https://doc.rust-lang.org/stable/alloc/vec/index.html
9191 · ╰── https://doc.rust-lang.org/stable/alloc/macros/macro.vec.html
9292- 98 │ > use [`format()`][std::fmt::format()] in place of [`format!()`][std::format!()][^2]!
9292+ 110 │ > use [`format()`][std::fmt::format()] in place of [`format!()`][std::format!()][^2]!
9393 · ────────────────┬─────────────── ──────────────┬──────────────
9494 · │ ╰── https://doc.rust-lang.org/stable/alloc/macros/macro.format.html
9595 · ╰── https://doc.rust-lang.org/stable/alloc/fmt/fn.format.html
9696- 99 │
9797- 100 │ The macro syntax works for attribute and derive macros as well (even though this is not
9898- 101 │ how they are invoked).
9999- 102 │
100100- 103 │ > ```md
101101- 104 │ > There is a [derive macro][serde::Serialize!] to generate implementations of the
102102- 105 │ > [`Serialize`][serde::Serialize] trait.
103103- 106 │ > ```
104104- 107 │ >
105105- 108 │ > There is a [derive macro][serde::Serialize!] to generate implementations of the
9696+ 111 │
9797+ 112 │ The macro syntax works for attribute and derive macros as well (even though this is not
9898+ 113 │ how they are invoked).
9999+ 114 │
100100+ 115 │ > ```md
101101+ 116 │ > There is a [derive macro][serde::Serialize!] to generate implementations of the
102102+ 117 │ > [`Serialize`][serde::Serialize] trait.
103103+ 118 │ > ```
104104+ 119 │ >
105105+ 120 │ > There is a [derive macro][serde::Serialize!] to generate implementations of the
106106 · ────────────────┬────────────────
107107 · ╰── https://docs.rs/serde_derive/1.0.219/serde_derive/derive.Serialize.html
108108- 109 │ > [`Serialize`][serde::Serialize] trait.
108108+ 121 │ > [`Serialize`][serde::Serialize] trait.
109109 · ───────────────┬───────────────
110110 · ╰── https://docs.rs/serde/1.0.219/serde/ser/trait.Serialize.html
111111- 110 │
112112- 111 │ ## Implementors and fully qualified syntax
113113- 112 │
114114- 113 │ Trait implementors may supply additional documentation about their implementations. To
115115- 114 │ link to implemented items instead of the traits themselves, use fully qualified paths,
116116- 115 │ including `<... as Trait>` if necessary. This is a _new feature_ that rustdoc does not
117117- 116 │ currently support.
118118- 117 │
119119- 118 │ > ```md
120120- 119 │ > [`Result<T, E>`] implements [`IntoIterator`]; its
121121- 120 │ > [**`into_iter()`**][Result::<(), ()>::into_iter] returns an iterator that yields one
122122- 121 │ > value if the result is [`Result::Ok`], otherwise none.
123123- 122 │ >
124124- 123 │ > [`Vec<T>`] also implements [`IntoIterator`]; a vector cannot be used after you call
125125- 124 │ > [**`into_iter()`**][<Vec<()> as IntoIterator>::into_iter].
126126- 125 │ > ```
127127- 126 │ >
128128- 127 │ > [`Result<T, E>`] implements [`IntoIterator`]; its
111111+ 122 │
112112+ 123 │ ## Implementors and fully qualified syntax
113113+ 124 │
114114+ 125 │ Trait implementors may supply additional documentation about their implementations. To
115115+ 126 │ link to implemented items instead of the traits themselves, use fully qualified paths,
116116+ 127 │ including `<... as Trait>` if necessary. This is a _new feature_ that rustdoc does not
117117+ 128 │ currently support.
118118+ 129 │
119119+ 130 │ > ```md
120120+ 131 │ > [`Result<T, E>`] implements [`IntoIterator`]; its
121121+ 132 │ > [**`into_iter()`**][Result::<(), ()>::into_iter] returns an iterator that yields one
122122+ 133 │ > value if the result is [`Result::Ok`], otherwise none.
123123+ 134 │ >
124124+ 135 │ > [`Vec<T>`] also implements [`IntoIterator`]; a vector cannot be used after you call
125125+ 136 │ > [**`into_iter()`**][<Vec<()> as IntoIterator>::into_iter].
126126+ 137 │ > ```
127127+ 138 │ >
128128+ 139 │ > [`Result<T, E>`] implements [`IntoIterator`]; its
129129 · ────────┬─────── ────────┬───────
130130 · │ ╰── https://doc.rust-lang.org/stable/core/iter/traits/collect/trait.IntoIterator.html
131131 · ╰── https://doc.rust-lang.org/stable/core/result/enum.Result.html
132132- 128 │ > [**`into_iter()`**][Result::<(), ()>::into_iter] returns an iterator that yields one
132132+ 140 │ > [**`into_iter()`**][Result::<(), ()>::into_iter] returns an iterator that yields one
133133 · ────────────────────────┬───────────────────────
134134 · ╰── https://doc.rust-lang.org/stable/core/result/enum.Result.html#method.into_iter
135135- 129 │ > value if the result is [`Result::Ok`], otherwise none.
135135+ 141 │ > value if the result is [`Result::Ok`], otherwise none.
136136 · ───────┬──────
137137 · ╰── https://doc.rust-lang.org/stable/core/result/enum.Result.html#variant.Ok
138138- 130 │ >
139139- 131 │ > [`Vec<T>`] also implements [`IntoIterator`]; a vector cannot be used after you call
138138+ 142 │ >
139139+ 143 │ > [`Vec<T>`] also implements [`IntoIterator`]; a vector cannot be used after you call
140140 · ─────┬──── ────────┬───────
141141 · │ ╰── https://doc.rust-lang.org/stable/core/iter/traits/collect/trait.IntoIterator.html
142142 · ╰── https://doc.rust-lang.org/stable/alloc/vec/struct.Vec.html
143143- 132 │ > [**`into_iter()`**][<Vec<()> as IntoIterator>::into_iter].
143143+ 144 │ > [**`into_iter()`**][<Vec<()> as IntoIterator>::into_iter].
144144 · ────────────────────────────┬────────────────────────────
145145 · ╰── https://doc.rust-lang.org/stable/alloc/vec/struct.Vec.html#method.into_iter
146146- 133 │
147147- 134 │ > [!NOTE]
148148- 135 │ >
149149- 136 │ > If your type has generic parameters, you must supply concrete types for them for
150150- 137 │ > rust-analyzer to be able to locate an implementation. That is, `Result<T, E>` won't
151151- 138 │ > work, but `Result<(), ()>` will (unless there happen to be types `T` and `E` literally
152152- 139 │ > in scope).
153153- 140 │
154154- 141 │ ## Disambiguators
155155- 142 │
156156- 143 │ rustdoc's [disambiguator syntax][disambiguator] `prefix@name` is **accepted but
157157- 144 │ ignored**:
158146 145 │
159159- 146 │ > ```md
160160- 147 │ > [`std::vec`], [`mod@std::vec`], and [`macro@std::vec`] all link to the `vec` _module_.
161161- 148 │ > ```
162162- 149 │ >
163163- 150 │ > [`std::vec`], [`mod@std::vec`], and [`macro@std::vec`] all link to the `vec` >
147147+ 146 │ > [!NOTE]
148148+ 147 │ >
149149+ 148 │ > If your type has generic parameters, you must supply concrete types for them for
150150+ 149 │ > rust-analyzer to be able to locate an implementation. That is, `Result<T, E>` won't
151151+ 150 │ > work, but `Result<(), ()>` will (unless there happen to be types `T` and `E` literally
152152+ 151 │ > in scope).
153153+ 152 │
154154+ 153 │ ## Disambiguators
155155+ 154 │
156156+ 155 │ rustdoc's [disambiguator syntax][disambiguator] `prefix@name` is **accepted but
157157+ 156 │ ignored**:
158158+ 157 │
159159+ 158 │ > ```md
160160+ 159 │ > [`std::vec`], [`mod@std::vec`], and [`macro@std::vec`] all link to the `vec` _module_.
161161+ 160 │ > ```
162162+ 161 │ >
163163+ 162 │ > [`std::vec`], [`mod@std::vec`], and [`macro@std::vec`] all link to the `vec` >
164164 · ──────┬───── ────────┬─────── ─────────┬────────
165165 · │ │ ╰── https://doc.rust-lang.org/stable/alloc/vec/index.html
166166 · │ ╰── https://doc.rust-lang.org/stable/alloc/vec/index.html
167167 · ╰── https://doc.rust-lang.org/stable/alloc/vec/index.html
168168- 151 │ > _module_.
169169- ╰────
170170- ╭─[supported-syntax:191:3]
171171- 190 │ >
172172- 191 │ > [The Option type](std::option::Option)
173173- · ───────────────────┬──────────────────
174174- · ╰── https://doc.rust-lang.org/stable/core/option/enum.Option.html
168168+ 163 │ > _module_.
169169+ 164 │
170170+ 165 │ Currently, duplicate names in Rust are allowed only if they correspond to items in
171171+ 166 │ different [namespaces], for example, between macros and modules, and between struct
172172+ 167 │ fields and methods — this is mostly covered by the function and macro syntax, described
173173+ 168 │ [above](#functions-and-macros).
174174+ 169 │
175175+ 170 │ If you encounter items that must be disambiguated using rustdoc's disambiguator syntax,
176176+ 171 │ other than [the "special types" listed below](#special-types), please [file an
177177+ 172 │ issue][gh-issues]!
178178+ 173 │
179179+ 174 │ ## Special types
180180+ 175 │
181181+ 176 │ > [!WARNING]
182182+ 177 │
183183+ 178 │ There is **no support** on types whose syntax is not a path; they are currently not
184184+ 179 │ parsed at all:
185185+ 180 │
186186+ 181 │ > references `&T`, slices `[T]`, arrays `[T; N]`, tuples `(T1, T2)`, pointers like
187187+ 182 │ > `*const T`, trait objects like `dyn Any`, and the never type `!`
188188+ 183 │
189189+ 184 │ Note that such types can still be used as generic params, just not as standalone types.
190190+ 185 │
191191+ 186 │ ## Struct fields
192192+ 187 │
193193+ 188 │ > [!WARNING]
194194+ 189 │
195195+ 190 │ Linking to struct fields is **not supported** yet. This is **incompatible** with
196196+ 191 │ rustdoc.
175197 192 │
176176- 193 │ Linking with reusable references:
198198+ 193 │ ## Markdown link syntax
177199 194 │
178178- 195 │ > ```md
179179- 196 │ > [The Option type][option-type]
180180- 197 │ >
181181- 198 │ > [option-type]: std::option::Option
182182- 199 │ > ```
183183- 200 │ >
184184- 201 │ > [The Option type][option-type]
185185- · ───────────────┬──────────────
186186- · ╰── https://doc.rust-lang.org/stable/core/option/enum.Option.html
200200+ 195 │ All Markdown link formats supported by rustdoc are supported:
201201+ 196 │
202202+ 197 │ Linking with URL inlined:
203203+ 198 │
204204+ 199 │ > ```md
205205+ 200 │ > [The Option type](std::option::Option)
206206+ 201 │ > ```
187207 202 │ >
188188- 203 │ > [option-type]: std::option::Option
208208+ 203 │ > [The Option type](std::option::Option)
209209+ · ───────────────────┬──────────────────
210210+ · ╰── https://doc.rust-lang.org/stable/core/option/enum.Option.html
189211 204 │
190190- 205 │ Reference-style links `[text][id]` without a corresponding `[id]: name` part will be
191191- 206 │ treated the same as inline-style links `[text](id)`:
192192- 207 │
193193- 208 │ > ```md
194194- 209 │ > [The Option type][std::option::Option]
195195- 210 │ > ```
196196- 211 │ >
197197- 212 │ > [The Option type][std::option::Option]
212212+ 205 │ Linking with reusable references:
213213+ 206 │
214214+ 207 │ > ```md
215215+ 208 │ > [The Option type][option-type]
216216+ 209 │ >
217217+ 210 │ > [option-type]: std::option::Option
218218+ 211 │ > ```
219219+ 212 │ >
220220+ 213 │ > [The Option type][option-type]
221221+ · ───────────────┬──────────────
222222+ · ╰── https://doc.rust-lang.org/stable/core/option/enum.Option.html
223223+ 214 │ >
224224+ 215 │ > [option-type]: std::option::Option
225225+ 216 │
226226+ 217 │ Reference-style links `[text][id]` without a corresponding `[id]: name` part will be
227227+ 218 │ treated the same as inline-style links `[text](id)`:
228228+ 219 │
229229+ 220 │ > ```md
230230+ 221 │ > [The Option type][std::option::Option]
231231+ 222 │ > ```
232232+ 223 │ >
233233+ 224 │ > [The Option type][std::option::Option]
198234 · ───────────────────┬──────────────────
199235 · ╰── https://doc.rust-lang.org/stable/core/option/enum.Option.html
200200- 213 │
201201- 214 │ Shortcuts are supported, and can contain inline markups:
202202- 215 │
203203- 216 │ > ```md
204204- 217 │ > You can create a [`Vec`] with [**`Vec::new`**], or by using the [_`vec!`_][^2] macro.
205205- 218 │ > ```
206206- 219 │ >
207207- 220 │ > You can create a [`Vec`] with [**`Vec::new`**], or by using the [_`vec!`_][^2] macro.
236236+ 225 │
237237+ 226 │ Shortcuts are supported, and can contain inline markups:
238238+ 227 │
239239+ 228 │ > ```md
240240+ 229 │ > You can create a [`Vec`] with [**`Vec::new`**], or by using the [_`vec!`_][^2] macro.
241241+ 230 │ > ```
242242+ 231 │ >
243243+ 232 │ > You can create a [`Vec`] with [**`Vec::new`**], or by using the [_`vec!`_][^2] macro.
208244 · ───┬─── ────────┬─────── ─────┬────
209245 · │ │ ╰── https://doc.rust-lang.org/stable/alloc/macros/macro.vec.html
210246 · │ ╰── https://doc.rust-lang.org/stable/alloc/vec/struct.Vec.html#method.new
211247 · ╰── https://doc.rust-lang.org/stable/alloc/vec/struct.Vec.html
212212- 221 │
213213- 222 │ (The items must still be resolvable; in this case `Vec` and `vec!` come from the
214214- 223 │ prelude.)
215215- 224 │
216216- 225 │ ## Linking to page sections
217217- 226 │
218218- 227 │ To link to a known section on a page, use a URL fragment, just like a normal link. This
219219- 228 │ is _compatible_ with rustdoc.
220220- 229 │
221221- 230 │ <!-- prettier-ignore-start -->
222222- 231 │
223223- 232 │ > ```md
224224- 233 │ > [When Should You Use Which Collection?][std::collections#when-should-you-use-which-collection]
225225- 234 │ > ```
226226- 235 │ >
227227- 236 │ > [When Should You Use Which Collection?][std::collections#when-should-you-use-which-collection]
248248+ 233 │
249249+ 234 │ (The items must still be resolvable; in this case `Vec` and `vec!` come from the
250250+ 235 │ prelude.)
251251+ 236 │
252252+ 237 │ ## Linking to page sections
253253+ 238 │
254254+ 239 │ To link to a known section on a page, use a URL fragment, just like a normal link. This
255255+ 240 │ is _compatible_ with rustdoc.
256256+ 241 │
257257+ 242 │ <!-- prettier-ignore-start -->
258258+ 243 │
259259+ 244 │ > ```md
260260+ 245 │ > [When Should You Use Which Collection?][std::collections#when-should-you-use-which-collection]
261261+ 246 │ > ```
262262+ 247 │ >
263263+ 248 │ > [When Should You Use Which Collection?][std::collections#when-should-you-use-which-collection]
228264 · ───────────────────────────────────────────────┬──────────────────────────────────────────────
229265 · ╰── https://doc.rust-lang.org/stable/std/collections/index.html
230230- 237 │
231231- 238 │ <!-- prettier-ignore-end -->
232232- 239 │
233233- 240 │ ---
234234- 241 │
235235- 242 │ [^1]:
236236- 243 │ rust-analyzer's ability to generate links for enum variants like `Option::Some` was
237237- 244 │ improved only somewhat recently: before
238238- 245 │ [#19246](https://github.com/rust-lang/rust-analyzer/pull/19246), links for variants
239239- 246 │ and associated items may only point to the types themselves. If linking to such
240240- 247 │ items doesn't seem to work for you, be sure to upgrade to a newer rust-analyzer
241241- 248 │ first!
242266 249 │
243243- 250 │ [^2]:
244244- 251 │ As of rust-analyzer <ra-version>(version)</ra-version>, links generated for macros
245245- 252 │ don't always work. Examples include [`std::format!`] (seen above) and
267267+ 250 │ <!-- prettier-ignore-end -->
268268+ 251 │
269269+ 252 │ ---
270270+ 253 │
271271+ 254 │ [^1]:
272272+ 255 │ rust-analyzer's ability to generate links for enum variants like `Option::Some` was
273273+ 256 │ improved only somewhat recently: before
274274+ 257 │ [#19246](https://github.com/rust-lang/rust-analyzer/pull/19246), links for variants
275275+ 258 │ and associated items may only point to the types themselves. If linking to such
276276+ 259 │ items doesn't seem to work for you, be sure to upgrade to a newer rust-analyzer
277277+ 260 │ first!
278278+ 261 │
279279+ 262 │ [^2]:
280280+ 263 │ As of rust-analyzer <ra-version>(version)</ra-version>, links generated for macros
281281+ 264 │ don't always work. Examples include [`std::format!`] (seen above) and
246282 · ────────┬───────
247283 · ╰── https://doc.rust-lang.org/stable/alloc/macros/macro.format.html
248248- 253 │ [`tokio::main!`]. For more info, see [Known issues](known-issues.md#macros).
284284+ 265 │ [`tokio::main!`]. For more info, see [Known issues](known-issues.md#macros).
249285 · ────────┬───────
250286 · ╰── https://docs.rs/tokio-macros/2.5.0/tokio_macros/macro.main.html
251251- 254 │
287287+ 266 │
252288 ╰────
···44443. Link to files using paths, like this:
45454646 ```md
4747- See [`book.toml`](../book.toml#L48-L51) for an example config.
4747+ See [`book.toml`](../book.toml#L44-L48) for an example config.
4848 ```
49495050 <figure class="fig-text">
51515252- See [`book.toml`](../book.toml#L48-L51) for an example config.
5252+ See [`book.toml`](../book.toml#L44-L48) for an example config.
53535454 </figure>
5555
+2-1
docs/src/link-forever/features.md
···2626>
2727> Linking by path is cool! Not only is it [well-supported by
2828> GitHub][github-relative-links], but editors like VS Code also provide smart features
2929-> like [path completions][path-completions] and [link validation][link-validation].
2929+> like [path completions][vscode-path-completions] and [link
3030+> validation][link-validation].
30313132URL fragments are preserved:
3233
+17-16
docs/src/link-forever/working-with-include.md
···44in book pages. If the embedded content also contains path-based links, then some extra
55care may be needed:
6677-- The preprocessor does not resolve links relative to the file being included because it
88- does not have enough information. In this case, relative paths could be valid for the
99- source file (and therefore valid for e.g. GitHub) but invalid for the book.
77+- The preprocessor does not resolve links relative to the file being included (because
88+ it doesn't have enough information to do so). In this case, relative paths could be
99+ valid for the source file (and therefore valid for e.g. GitHub) but invalid for the
1010+ book.
10111111-- In some cases, you cannot use path-based links and you have to use full URLs. This
1212- could be because the included file is also intended for platforms that don't support
1313- paths as links.
1212+- In some situations, you cannot use path-based links and you have to use full URLs.
1313+ This could be because the included file is also intended for platforms that don't
1414+ support paths as links.
14151516This page describes some workarounds for linking in included files.
1617···2021## Using absolute paths
21222223To use paths as links in included content, you can use absolute paths that start with a
2323-`/`.
2424-2525-Paths that start with a `/` are resolved relative to the root of your repository. This
2626-is supported both in [VS Code][vscode-path-completions] and on
2727-[GitHub][github-relative-links], for example.
2424+`/`. Paths that start with a `/` are resolved relative to the root of your repository:
28252926> ```md
3027> Front page of this book is actually from
···3431> Front page of this book is actually from
3532> [the crate README](/crates/mdbookkit/README.md).
36333434+> [!TIP]
3535+>
3636+> This is also the behavior both in [VS Code][vscode-path-completions] and on
3737+> [GitHub][github-relative-links].
3838+3739## Using URLs to link to book pages
38403941You may be in a situation where you have to use full URLs to link to your book rather
···4244> An example (that this project encountered) is including files that are also intended
4345> for displaying on [crates.io][cargo-readme].
4446>
4545-> In this case, linking to book pages would require writing down full URLs to the
4646-> deployed book, since other platforms like crates.io will not convert path-based `.md`
4747-> links to URLs.
4747+> In this case, since other platforms like crates.io will not convert path-based `.md`
4848+> links to URLs, linking to book pages would require writing down full URLs to the
4949+> deployed book.
48504949-To mitigate this, you can use the [`book-url`](configuration.md#book-url) option, and
5050-then use full URLs in the included content.
5151+To mitigate this, you can use the [`book-url`](configuration.md#book-url) option.
51525253In your `book.toml`, in the `[preprocessor.link-forever]` table, specify the URL prefix
5354at which you will deploy your book:
docs/src/media/banner.webp
This is a binary file and will not be displayed.
+8-1
docs/src/rustdoc-link.md
···11# mdbook-rustdoc-link
2233+<div class="hidden">
44+55+**For best results, view this page at
66+<https://tonywu6.github.io/mdbookkit/rustdoc-link>.**
77+88+</div>
99+310**[_rustdoc_-style linking][rustdoc] for [mdBook]** (with the help of [rust-analyzer]).
411512You write:
···40474148For **writing documentation** —
42494343-- To know more about how the preprocessor is resolving items into links, including
5050+- To learn more about how it is resolving items into links, including
4451 [feature-gated items](rustdoc-link/name-resolution.md#feature-gated-items), see
4552 [Name resolution](rustdoc-link/name-resolution.md).
4653- To know how to link to other types of items like
+1-1
docs/src/rustdoc-link/caching.md
···3030```toml
3131[preprocessor.rustdoc-link]
3232cache-dir = "cache"
3333-# You can also point to an arbitrary directory in target/
3333+# You could also point to an arbitrary directory in target/
3434```
35353636Now, when `mdbook` rebuilds your book during `build` or `serve`, the preprocessor reuses
+5-5
docs/src/rustdoc-link/getting-started.md
···4455## Install
6677-You will need:
77+You will need to:
8899-1. [rust-analyzer]:
99+1. Have [rust-analyzer]:
10101111- - If you already use the [VS Code extension][ra-extension], this project will
1212- automatically use the server binary that comes with it, no extra setup is needed!
1111+ - If you already use the [VS Code extension][ra-extension]: this crate automatically
1212+ uses the server binary that comes with it, no extra setup is needed!
1313 - Otherwise, [install][ra-install] rust-analyzer (e.g. via `rustup`) and make sure
1414 it's on your `PATH`.
15151616-2. This crate:
1616+2. Install this crate:
17171818 ```
1919 cargo install mdbookkit --features rustdoc-link
+2-3
docs/src/rustdoc-link/motivation.md
···11111212- APIs are mentioned without linking to reference docs.
13131414- This is probably fine for tutorials and examples, but it does mean a reduced
1515- connection between guide-level text and usage details. Readers won't be able to
1616- navigate from one to the other as easily.
1414+ This is probably fine for tutorials and examples, but it does mean readers of your
1515+ docs won't be able to move from guides to references as easily.
17161817- You do want at least some cross-references, but it is cumbersome to find and copy the
1918 correct links, and even more so to maintain them.
+3-4
docs/src/rustdoc-link/name-resolution.md
···6677> [!TIP]
88>
99-> If you use Cargo workspaces, or if your source tree has special layouts, see
99+> If you use Cargo workspaces, or if your source tree has special layout, see
1010> [Workspace layout](workspace-layout.md) for more information.
11111212An item must be **in scope in the entrypoint** for the proprocessor to generate a link
···9393## Feature-gated items
94949595To link to items that are gated behind features, use the
9696-[`cargo-features`](configuration.md#cargo-features) option.
9696+[`cargo-features`](configuration.md#cargo-features) option in `book.toml`.
97979898For example, [clap] is known for providing guide-level documentation through docs.rs.
9999The tutorial for its Derive API is gated behind the `unstable-doc` feature. To link to
···114114115115## Which entrypoint
116116117117-The "entrypoint" is usually `src/lib.rs` or `src/main.rs`.
117117+For this preprocessor, the "entrypoint" is usually `src/lib.rs` or `src/main.rs`.
118118119119- If your crate has multiple `bin` targets, it will use the first one listed in your
120120 `Cargo.toml`.
···174174<figure id="media-open-docs">
175175 <img src="media/open-docs.png" alt="the Open Docs option in VS Code">
176176</figure>
177177-178177<style>
179178 @media screen and (min-width: 768px) {
180179 #media-open-docs {
+1-2
docs/src/rustdoc-link/standalone-usage.md
···1414`book.toml`, such as [`--cache-dir`](configuration.md#cache-dir). Run
1515`mdbook-rustdoc-link markdown --help` to see them.
16161717-Use it in any text processing pipeline!
1818-1917<figure id="media-open-docs">
2018 <img src="media/standalone-usage.png" alt="example using mdbook-rustdoc-link as a command line tool">
1919+ <figcaption>Use it in any text processing pipeline!</figcaption>
2120</figure>
22212322<style>
+16-3
docs/src/rustdoc-link/supported-syntax.md
···11# Supported syntax
2233+<div class="hidden">
44+55+**For best results, view this page at
66+<https://tonywu6.github.io/mdbookkit/rustdoc-link/supported-syntax>.**
77+88+</div>
99+310This page showcases all the syntax supported by `mdbook-rustdoc-link`.
411512Most of the formats [supported by rustdoc][rustdoc-linking] are supported. Unsupported
613syntax and differences in behavior are emphasized below.
71488-[rustdoc-linking]:
99- https://doc.rust-lang.org/rustdoc/write-documentation/linking-to-items-by-name.html
1010-1115In general, specifying items as you would when writing Rust code should "just work".
12161317<details class="toc" open>
···2428- [Linking to page sections](#linking-to-page-sections)
25292630</details>
3131+3232+> [!TIP]
3333+>
3434+> This page is also used for snapshot testing! To see how all the links would look like
3535+> in Markdown after they have been processed, see
3636+> [supported-syntax.snap](/crates/mdbookkit/tests/snaps/rustdoc_link/supported-syntax.snap)
3737+> and
3838+> [supported-syntax.stderr.snap](/crates/mdbookkit/tests/snaps/rustdoc_link/supported-syntax.stderr.snap).
27392840## Types, modules, and associated items
2941···258270[gh-issues]: https://github.com/tonywu6/mdbookkit/issues
259271[namespaces]: https://doc.rust-lang.org/reference/names/namespaces.html
260272[rust-types]: https://doc.rust-lang.org/reference/types.html#r-type.kinds
273273+[rustdoc-linking]: https://doc.rust-lang.org/rustdoc/write-documentation/linking-to-items-by-name.html
261274262275<!-- prettier-ignore-end -->