···11+# mdbookkit — _toolkit for 📖 [mdBook]!_
22+33+[`mdbookkit`] provides quality-of-life plugins that you can use in your mdBook project.
44+55+Right now, there are two mdBook [preprocessors], both for generating **correct and
66+versioned** external links from **easy-to-write** markup.
77+88+- [**`mdbook-rustdoc-link`**](https://tonywu6.github.io/mdbookkit/rustdoc-link)
99+1010+ _rustdoc_-style linking for Rust APIs: write types and function names, get links to
1111+ docs.rs
1212+1313+- [**`mdbook-link-forever`**](https://tonywu6.github.io/mdbookkit/link-forever)
1414+1515+ _Permalinks_ for your source tree: write relative paths, get links to GitHub.
1616+1717+> [!TIP]
1818+>
1919+> Preprocessors are standalone programs that mdBook invokes to transform your Markdown
2020+> sources before rendering them.
2121+2222+## Installation
2323+2424+If you are interested in any of these plugins, visit their respective pages for usage
2525+instructions, linked above.
2626+2727+If you want to install all of them:
2828+2929+```bash
3030+cargo install mdbookkit --all-features
3131+```
3232+3333+Precompiled binaries are also available from [GitHub releases][gh-releases].
3434+3535+## License
3636+3737+This project is released under the [Apache 2.0 License](/LICENSE-APACHE.md) and the
3838+[MIT License](/LICENSE-MIT.md).
3939+4040+<!-- prettier-ignore-start -->
4141+4242+[mdBook]: https://rust-lang.github.io/mdBook/
4343+[`mdbookkit`]: https://crates.io/crates/mdbookkit
4444+[preprocessors]: https://rust-lang.github.io/mdBook/format/configuration/preprocessors.html
4545+[gh-releases]: https://github.com/tonywu6/mdbookkit/releases
4646+4747+<!-- prettier-ignore-end -->
···11---
22source: crates/mdbookkit/tests/rustdoc_link.rs
33+assertion_line: 31
34expression: output
45---
56# mdbook-rustdoc-link
···44454546For **writing documentation** —
46474747-- To know more about how the preprocessor is resolving items into links, see
4848+- To know more about how the preprocessor is resolving items into links, including
4949+ [feature-gated items](rustdoc-link/name-resolution.md#feature-gated-items), see
4850 [Name resolution](rustdoc-link/name-resolution.md).
4951- To know how to link to other types of items like
5052 [functions, macros](rustdoc-link/supported-syntax.md#functions-and-macros), and
···24242525> [!TIP]
2626>
2727-> Linking by path is cool! Not only is it well-supported by GitHub, but editors like VS
2828-> Code also provide smart features like [path completions][path-completions] and [link
2929-> validation][link-validation].
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].
30303131URL fragments are preserved:
3232···5656> are supported, thanks to the [`gix_url`] crate.
57575858If you use Git but not GitHub, you can configure a custom URL pattern using the
5959-[`url-pattern`](configuration.md#url-pattern) option. For example:
5959+[`repo-url-template`](configuration.md#repo-url-template) option. For example:
60606161```toml
6262[preprocessor.link-forever]
6363-url-pattern = "https://gitlab.haskell.org/ghc/ghc/-/tree/{ref}/{path}"
6363+repo-url-template = "https://gitlab.haskell.org/ghc/ghc/-/tree/{ref}/{path}"
6464```
65656666## Link validation
···86868787<!-- prettier-ignore-start -->
88888989-[path-completions]: https://code.visualstudio.com/docs/languages/markdown#_path-completions
8989+[vscode-path-completions]: https://code.visualstudio.com/docs/languages/markdown#_path-completions
9090+[github-relative-links]: https://docs.github.com/en/get-started/writing-on-github/getting-started-with-writing-and-formatting-on-github/basic-writing-and-formatting-syntax#relative-links
9091[link-validation]: https://code.visualstudio.com/docs/languages/markdown#_link-validation
9192[mdbook-src-build]: https://rust-lang.github.io/mdBook/guide/creating.html#source-files
9293[`output.html.git-repository-url`]: https://rust-lang.github.io/mdBook/format/configuration/renderers.html#html-renderer-options
+13
docs/src/link-forever/known-issues.md
···11+# Known issues
22+33+## Working with `{{#include}}`
44+55+Linking by relative paths may not make sense when the links are in files that are
66+embedded using mdBook's `{{#include}}` directive.
77+88+See [Working with `{{#include}}`](working-with-include.md) for some possible
99+workarounds.
1010+1111+## Links in HTML
1212+1313+Links in HTML (`href` and `src`) are currently neither transformed nor checked.
···11+# Working with `{{#include}}`
22+33+mdBook provides an [`{{#include}}` directive][mdbook-include] that you can embed files
44+in book pages. If the embedded content also contains path-based links, then some extra
55+care may be needed:
66+77+- 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.
1010+1111+- 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.
1414+1515+This page describes some workarounds for linking in included files.
1616+1717+- [Using absolute paths](#using-absolute-paths)
1818+- [Using URLs to link to book pages](#using-urls-to-link-to-book-pages)
1919+2020+## Using absolute paths
2121+2222+To 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.
2828+2929+> ```md
3030+> Front page of this book is actually from
3131+> [the crate README](/crates/mdbookkit/README.md).
3232+> ```
3333+>
3434+> Front page of this book is actually from
3535+> [the crate README](/crates/mdbookkit/README.md).
3636+3737+## Using URLs to link to book pages
3838+3939+You may be in a situation where you have to use full URLs to link to your book rather
4040+than relying on paths.
4141+4242+> An example (that this project encountered) is including files that are also intended
4343+> for displaying on [crates.io][cargo-readme].
4444+>
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.
4848+4949+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+5252+In your `book.toml`, in the `[preprocessor.link-forever]` table, specify the URL prefix
5353+at which you will deploy your book:
5454+5555+```toml
5656+[preprocessor.link-forever]
5757+book-url = "https://example.org/"
5858+```
5959+6060+Then, in Markdown, you may use full URLs, for example:
6161+6262+> ```md
6363+> For a list of the crate's features, see [Feature flags](https://example.org/features).
6464+> ```
6565+>
6666+> For a list of the crate's features, see [Feature flags](https://example.org/features).
6767+6868+Specifying `book-url` enables the preprocessor to check URLs to your book against local
6969+paths. If a URL does not have a corresponding `.md` file under your book's `src/`
7070+directory, the preprocessor will warn you:
7171+7272+<figure>
7373+7474+
7575+7676+</figure>
7777+7878+<!-- prettier-ignore-start -->
7979+8080+[mdbook-include]: https://rust-lang.github.io/mdBook/format/mdbook.html#including-files
8181+[vscode-path-completions]: https://code.visualstudio.com/docs/languages/markdown#_path-completions
8282+[github-relative-links]: https://docs.github.com/en/get-started/writing-on-github/getting-started-with-writing-and-formatting-on-github/basic-writing-and-formatting-syntax#relative-links
8383+[cargo-readme]: https://doc.rust-lang.org/cargo/reference/manifest.html#the-readme-field
8484+8585+<!-- prettier-ignore-end -->
+2-1
docs/src/rustdoc-link.md
···40404141For **writing documentation** —
42424343-- To know more about how the preprocessor is resolving items into links, see
4343+- To know more about how the preprocessor is resolving items into links, including
4444+ [feature-gated items](rustdoc-link/name-resolution.md#feature-gated-items), see
4445 [Name resolution](rustdoc-link/name-resolution.md).
4546- To know how to link to other types of items like
4647 [functions, macros](rustdoc-link/supported-syntax.md#functions-and-macros), and
+5
docs/src/rustdoc-link/caching.md
···50505151## How it works
52525353+> [!NOTE]
5454+>
5555+> The following are implementation details. See
5656+> [rustdoc_link/cache.rs](/crates/mdbookkit/src/bin/rustdoc_link/cache.rs).
5757+5358The effectiveness of this mechanism is based on the following assumptions:
54595560- Most of the changes made during authoring don't actually involve item links.
+3
docs/src/rustdoc-link/getting-started.md
···1919 cargo install mdbookkit --features rustdoc-link
2020 ```
21212222+ Or you can grab precompiled binaries from [GitHub releases][gh-releases].
2323+2224## Configure
23252426Configure your `book.toml` to use it as a [preprocessor]:
···8890[ra-install]: https://rust-analyzer.github.io/book/rust_analyzer_binary.html
8991[open-docs]: https://rust-analyzer.github.io/book/features.html#open-docs
9092[ra-extension]: https://marketplace.visualstudio.com/items?itemName=rust-lang.rust-analyzer
9393+[gh-releases]: https://github.com/tonywu6/mdbookkit/releases
91949295<!-- prettier-ignore-end -->
+2-1
docs/src/rustdoc-link/name-resolution.md
···126126127127> [!NOTE]
128128>
129129-> The following are implementation details.
129129+> The following are implementation details. See
130130+> [rustdoc_link/mod.rs](/crates/mdbookkit/src/bin/rustdoc_link/mod.rs).
130131131132`mdbook-rustdoc-link` parses your book and collects every link that looks like a Rust
132133item. Then it synthesizes a Rust function that spells out all the items, which looks
···11+//! Postprocess mdBook HTML output to add OpenGraph metadata, for social images, etc.
22+//!
33+//! mdBook doesn't support frontmatters yet, so this cannot be a preprocessor.
44+15use std::{collections::HashMap, path::PathBuf};
2637use anyhow::{Context, Result};
···87918892 Settings {
8993 element_content_handlers: vec![
9090- text!("main > h1", |text| {
9494+ text!("main > h1:first-of-type", |text| {
9195 title.push_str(text.as_str());
9296 Ok(())
9397 }),
+6
utils/rust-analyzer/src/main.rs
···11+//! Download a copy of rust-analyzer to /.bin to use in testing.
22+//!
33+//! Version can be controlled with the `RA_VERSION` environment variable.
44+15use std::{
26 fs, io,
37 os::unix::fs::PermissionsExt,
···156160 }
157161}
158162163163+/// Preprocessor to replace `<ra-version>(version)</ra-version>` with the currently
164164+/// used RA version. Used in docs.
159165#[cfg(feature = "ra-version")]
160166mod ra_version {
161167 use std::io::{Read, Write};
+2
utils/testing/src/lib.rs
···11+//! Test helpers.
22+13use std::path::Path;
2435use anyhow::Result;