# mdbook-llms --- # Getting started Source: /mdbook-llms/getting-started.md mdbook-llms is an [mdBook](https://rust-lang.github.io/mdBook/) 0.5 renderer that follows the [llms.txt](https://llmstxt.org/) convention. Alongside your HTML site it writes: - [`llms.txt`](llms.txt): an index of every chapter, grouped by SUMMARY.md parts - [`llms-full.txt`](llms-full.txt): every chapter in reading order, in one file - `.md`: each chapter's Markdown at the path its HTML would use ## How it fits into mdBook ```mermaid flowchart LR A[SUMMARY.md + chapters] --> B["links preprocessor
expands includes"] B --> C["strip-frontmatter + mermaid
(HTML only)"] C --> D[html renderer] --> E[book/html/*.html] B --> F[mdbook-llms renderer] F --> G["llms.txt
llms-full.txt
chapter .md"] G -. written next to .-> E ``` The llms renderer reads the book before the HTML-only preprocessors run, so it sees chapter frontmatter and keeps Mermaid blocks as plain fenced code. ## Quick start Install the binaries: ```sh cargo install --locked mdbook-llms ``` Add the renderer and the frontmatter preprocessor to your `book.toml`: ```toml [preprocessor.strip-frontmatter] [output.html] [output.llms] ``` Run `mdbook build`. The llms files appear in `book/html/`, next to the site. This book is built the same way. See [Usage](/mdbook-llms/usage.md) for every option and [Contributing](/mdbook-llms/contributing.md) to work on the crate. --- # Usage Source: /mdbook-llms/usage.md ## Install ```sh cargo install --locked mdbook-llms ``` From a local checkout, use `cargo install --path .` instead. Prebuilt binaries for Linux, macOS and Windows are attached to each [GitHub Release](https://github.com/ttdonovan/mdbook-llms/releases) as `mdbook-llms--.tar.gz` (`.zip` on Windows), each with a `.sha256` file. The archive holds both binaries. This installs two binaries: the `mdbook-llms` renderer and the `mdbook-strip-frontmatter` preprocessor. Both must be on your `PATH` when you run `mdbook build`. ## Configure To generate only the llms files, list only this output. mdBook then skips HTML, CSS and JS completely, and the files land directly in `book/`: ```toml [output.llms] site-url = "https://example.com/my-book" ``` To publish alongside the site, keep both. The files go into `book/html/`, next to the HTML: ```toml [preprocessor.strip-frontmatter] [output.html] site-url = "/my-book/" [output.llms] ``` To skip HTML for one build without touching `book.toml`: ```sh MDBOOK_OUTPUT='{"llms":{}}' mdbook build ``` ## Options All go under `[output.llms]`. | Key | Default | Effect | |---|---|---| | `site-url` | `output.html.site-url` | Prefix for links. Without either, links are relative to the output root. | | `index` | `true` | Write `llms.txt`. | | `full` | `true` | Write `llms-full.txt`. | | `pages` | `true` | Write per-chapter `.md` files. When `false`, links point at `.html`. | | `merge-into-html` | `true` | When HTML also runs, write into its directory rather than `book/llms/`. | | `discovery-links` | `true` | When merged into the HTML output, add `` and `` to each HTML page, as [llms.txt v2](https://llmstxt.org/) recommends. | | `sizes` | `false` | End each `llms.txt` entry with its file's size, as `(about 1.4k tokens)`: a quarter of its bytes, rounded up to the hundred below a thousand and to a tenth of a thousand above. | | `preamble` | none | A file under `src/` whose text goes after the title and description of `llms.txt` and `llms-full.txt`. | | `optional-files` | none | Files that are not chapters, listed under `## Optional` at the end of `llms.txt` (see below). | ## Optional files The [llms.txt convention](https://llmstxt.org/) ends the index with an `## Optional` section for what a reader may skip. List each file under `[[output.llms.optional-files]]`: ```toml [[output.llms.optional-files]] path = "llms-full.txt" description = "Every chapter in one file" [[output.llms.optional-files]] path = "data/model.json" title = "model.json" description = "The whole model as JSON" ``` `path` is under `src/`, and the file is copied to the same path in the output; `llms-full.txt` is the one exception, since the renderer writes it. `title` defaults to the file name. With `sizes = true`, each entry shows its size too. A listed file that does not exist fails the build. ## Frontmatter A `description` key in a chapter's YAML frontmatter adds text to that chapter's `llms.txt` entry. Other keys are allowed and removed from all output: ```markdown --- description: How to install the CLI --- # Install ``` Add `[preprocessor.strip-frontmatter]` whenever HTML is enabled, or the HTML pages will show the block. --- # Contributing Source: /mdbook-llms/contributing.md ## Prerequisites - Rust (edition 2024) - [just](https://github.com/casey/just) - [mdBook](https://rust-lang.github.io/mdBook/) 0.5 - [mdbook-mermaid](https://github.com/badboy/mdbook-mermaid) 0.17 - [cargo-audit](https://github.com/rustsec/rustsec/tree/main/cargo-audit) `just setup` installs mdBook and mdbook-mermaid at the versions the docs are tested with. ## Layout - `src/lib.rs`: config, link rewriting, frontmatter parsing and output writing. - `src/main.rs`: the `mdbook-llms` renderer binary. - `src/bin/mdbook-strip-frontmatter.rs`: the frontmatter preprocessor binary. - `docs/`: this book, built with the renderer from the working tree. ## Workflow Run `just` to list recipes. Before opening a pull request, run: ```sh just fmt just check ``` `check` runs `fmt-check`, `clippy` (warnings are errors), `test` and `audit`. ## Releases Releases are made by [release-plz](https://release-plz.dev/) from [Conventional Commits](https://www.conventionalcommits.org/), so prefix commit messages with `feat:`, `fix:`, `docs:` and so on, and mark breaking changes with `!` (e.g. `feat!:`). On each push to `main`, release-plz opens or updates a release PR that bumps the version and updates `CHANGELOG.md`. Merging it publishes the new version to crates.io through [trusted publishing](https://crates.io/docs/trusted-publishing), tags `vX.Y.Z` and creates a GitHub Release. The `Release binaries` workflow then builds both binaries for five targets and attaches the archives and `.sha256` files to that release. To backfill an existing tag, run it by hand from the Actions tab with the tag. ## Building the docs ```sh just book ``` This builds the crate, puts `target/debug` first on `PATH` so mdBook uses the local binaries, and writes the site and llms files to `docs/book/`. Use `just serve` to preview the HTML. `docs/snapshots/` holds the expected llms output for this book: `llms.txt`, `llms-full.txt` and every per-chapter `.md` file. `just snapshot` builds into `target/book-snapshot/` and fails if any of them differ. It also fails if the build prints any `warning: mdbook-llms` broken-link warning. After an intended change to the renderer or the docs, run `just snapshot-update` and review the diff. --- # Example section Source: /mdbook-llms/example/index.md This section exists to show what mdbook-llms produces. Each chapter exercises one feature, and this page shows the resulting `llms.txt` for the whole book. ## What it shows - **Parts**: `# Guide` and `# Example` in `SUMMARY.md` become `##` headings in `llms.txt`. - **Section pages**: this file is `example/README.md`, so it is published as `example/index.md`. - **Descriptions**: each chapter's frontmatter `description` follows its link. - **Drafts**: `Draft chapter` has no file, so it is left out and [Included files](/mdbook-llms/example/included.md) moves up a level. ## Generated llms.txt This is a committed snapshot of `book/html/llms.txt`. `just snapshot` fails if it no longer matches the build. ```text # mdbook-llms ## Guide - [Getting started](/mdbook-llms/getting-started.md): What mdbook-llms does and how to try it in a minute - [Usage](/mdbook-llms/usage.md): Install, configure and the full list of `[output.llms]` options - [Contributing](/mdbook-llms/contributing.md): Prerequisites, project layout and the just-based dev workflow ## Example - [Example section](/mdbook-llms/example/index.md): A worked example showing each chapter's source next to the llms.txt it produces - [Frontmatter](/mdbook-llms/example/frontmatter.md): Frontmatter keys, and which ones reach the output - [Links](/mdbook-llms/example/links.md): How links between chapters are rewritten for each output - [Hidden lines](/mdbook-llms/example/hidden-lines.md): Code lines hidden in the HTML are kept, without their marker, in the Markdown output - [Included files](/mdbook-llms/example/included.md): An {{#include}}-built chapter, listed one level up because its parent is a draft ``` --- # Frontmatter Source: /mdbook-llms/example/frontmatter.md This chapter's source opens with: ```yaml --- description: Frontmatter keys, and which ones reach the output tags: - example - frontmatter --- ``` - `description` is added to this chapter's line in `llms.txt`. - `tags` is allowed and removed. It appears nowhere in the HTML or llms output. - The block itself is removed from the HTML page by `strip-frontmatter` and from the Markdown output by `mdbook-llms`. --- # Links Source: /mdbook-llms/example/links.md The source of this chapter links to other chapters by their `.md` paths: - [Usage](/mdbook-llms/usage.md) - [Usage options](/mdbook-llms/usage.md#options) - [Frontmatter](/mdbook-llms/example/frontmatter.md) In the output: - **HTML** links point at `.html` pages, as usual for mdBook. - **Per-chapter `.md` files** keep relative `.md` links. A link to a `README.md` chapter is changed to `index.md`, its published path. - **`llms-full.txt`** has every chapter in one file, so the links are rewritten to work from there. ## Broken links After the build, mdbook-llms checks the relative links in `llms.txt` and in every chapter's `.md` file. A missing target, such as a typo or a chapter left out of `SUMMARY.md`, prints a warning like this and the build carries on: ```text warning: mdbook-llms: broken link in example/links.md: nowhere.md ``` Anchors and external URLs are not checked. This repository's `just snapshot` treats these warnings as errors. ## Limitation: links to README.md mdBook publishes a `README.md` chapter as `index.html`, but its HTML renderer does not rewrite links to it. A link like `[Overview](README.md)` becomes `README.html`, which does not exist, so it is broken in the HTML site. mdbook-llms changes these links to `index.md` in its own output, but cannot fix the HTML. This book avoids linking to `README.md` chapters for that reason. --- # Hidden lines Source: /mdbook-llms/example/hidden-lines.md mdBook can hide boilerplate lines in code blocks. In the HTML they are folded away behind a toggle; in the llms output they are kept as ordinary code with the marker removed, so an agent gets complete, valid code. The rules match mdBook's: - Rust: a line starting with `# ` (or a lone `#`) is hidden, and `##` stands for a literal `#`. - Other languages: lines starting with the prefix set in `[output.html.code.hidelines]`, or with a fence attribute like `hidelines=~`. ## Source ````markdown ```rust # use std::collections::HashMap; # fn main() { let mut counts = HashMap::new(); counts.insert("pages", 7); let heading = r#" ## Pages "#; # } ``` ```python hidelines=~ ~import json print(json.dumps({"pages": 7})) ``` ```` ## Result The same blocks, rendered by this book. The HTML page hides the marked lines; this page's `.md` file and `llms-full.txt` show them in full. ```rust use std::collections::HashMap; fn main() { let mut counts = HashMap::new(); counts.insert("pages", 7); let heading = r#" # Pages "#; } ``` ```python hidelines=~ import json print(json.dumps({"pages": 7})) ``` --- # Included files Source: /mdbook-llms/example/included.md mdbook-llms reads the book after mdBook's links preprocessor has run, so `{{#include}}` is already expanded in every output. This is this book's own `book.toml`, included from outside `src/`: ```toml [book] title = "mdbook-llms" src = "src" [preprocessor.strip-frontmatter] [preprocessor.mermaid] command = "mdbook-mermaid" [output.html] site-url = "/mdbook-llms/" additional-js = ["mermaid.min.js", "mermaid-init.js"] [output.llms] ``` ## Including Rust source with anchors An include can pull a named region out of a source file. `src/lib.rs` marks the renderer's config with `// ANCHOR: config` and `// ANCHOR_END: config`, and this page includes it with: ```text {{#include ../../../src/lib.rs:config}} ``` mdBook drops the anchor lines, so the result is the real definition of every `[output.llms]` option and its default: ```rust #[derive(Debug, Deserialize)] #[serde(default, rename_all = "kebab-case")] pub struct Config { /// Write `llms.txt`. pub index: bool, /// Write `llms-full.txt`. pub full: bool, /// Write one `.md` file per chapter. When off, `llms.txt` links to `.html`. pub pages: bool, /// Prefix for links. Falls back to `output.html.site-url`. pub site_url: Option, /// When the HTML renderer also runs, publish into its directory. pub merge_into_html: bool, /// When merged, add llms.txt v2 `alternate` and `describedby` links to HTML pages. pub discovery_links: bool, /// End each `llms.txt` entry with its file's size in tokens. pub sizes: bool, /// A file under `src/` whose text follows the header of `llms.txt` and `llms-full.txt`. pub preamble: Option, /// Files listed under `## Optional` in `llms.txt`. Not `optional`, which /// mdBook reads under every output as a bool of its own. pub optional_files: Vec, } /// A file that is not a chapter, listed under `## Optional`: copied from /// `src/`, or `llms-full.txt`, which this renderer writes. #[derive(Debug, Deserialize)] #[serde(deny_unknown_fields)] pub struct Optional { /// Its path under `src/` and in the output. pub path: PathBuf, /// The entry's link text. Defaults to the file name. pub title: Option, /// Text after the link, as a chapter's `description` gives. pub description: Option, } impl Default for Config { fn default() -> Self { Self { index: true, full: true, pages: true, site_url: None, merge_into_html: true, discovery_links: true, sizes: false, preamble: None, optional_files: Vec::new(), } } } ``` Anchors move with the code, unlike line ranges such as `lib.rs:15:40`. Because the snapshot test covers this page, a change to `Config` also shows up as a docs change. ## Drafts In `SUMMARY.md` this chapter sits under `Draft chapter`, which has no file. Drafts get no page, so in `llms.txt` this chapter is listed one level up instead of disappearing.