Add absolute links via output.html.site-url#3139
Open
JesusPerez wants to merge 1 commit into
Open
Conversation
Port of the 0.4.x site-url absolute-links patch to the 0.5 crates/ layout, toward upstreaming as PR rust-lang#1802. When output.html.site-url is set, internal links and assets are emitted as absolute URLs anchored at site-url, so the book works under a sub-path (e.g. /cdcidao/) regardless of page depth. - html/tree.rs: fix_link/fix_html_link rewrite ./ content, image and raw-HTML links to {site_url}...; schemes and fragments untouched - html_handlebars/hbs_renderer.rs: path_to_root = site_url for normal and index pages; base_url = site_url only for the toc.html iframe (removed before the per-chapter clone so it cannot leak) - html_handlebars/helpers/resources.rs: {{resource}} honors an explicit path_to_root from data (absolute assets) with stock fallback - html/print.rs: print page honors site-url; internal cross-refs still fold to #anchors, non-chapter links keep absolute form - cmd/serve.rs: --preserve-site-url flag; serve still forces site-url to / for local preview but logs the override - tests/testsuite/rendering*: site_url fixture + tests (content, assets, print, no <base> leak, no-regression without site-url) - guide: document the serve flag and the renderer behavior
This was referenced Jun 19, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #1764.
Supersedes #1802 (a reimplementation of @joaofreires' approach on the current
crates/workspace layout, with the review feedback addressed).Problem
When a book is hosted in a subdirectory (or any page is reached at a deeply nested path), mdBook's depth-relative links can resolve incorrectly.
output.html.site-urlalready exists but only feeds the<base href>of the 404 page — ordinary navigation, sidebar, asset and in-content links are not affected. See #1764.What this does
When
output.html.site-urlis set (e.g.https://example.com/docs/), every generated link is rooted at that URL so the book resolves correctly regardless of the page's own depth:./pathlinks in chapter bodies (and images / raw HTML) are anchored tosite-url. Scheme links (https:,mailto:) and fragments are left untouched.path_to_rootis set tosite-url, so prev/next, the print link, search and thetoc.js-injected sidebar all become absolute.toc.html(the iframe fallback) gets a<base href>ofsite-url; this is scoped to that render so it never leaks onto regular pages (which would break their page-relative content links).{{resource}}helper honors the page'spath_to_root, so hashed CSS/JS are emitted absolutely too.print.htmlchrome/assets are rooted atsite-url, and cross-chapter references between chapters present on the page are still folded into intra-page#idanchors, keeping the consolidated page self-contained.During
mdbook servethe book is hosted at the local server root, sosite-urlis overridden to/(host-agnostic, resolves locally). This was previously silent; it now logs once, and a new--preserve-site-urlflag keeps the configured value to preview the production absolute links locally.Addressing the #1802 review
print.rs::rewrite_linksis nowsite-url-aware; cross-refs fold to anchors, externals stay absolute.master.HtmlConfigis public API → a new field is a semver break; use a wrapper."crates/layoutsite_urlis already a field onHtmlConfig. No public API fields were added; threading is via internalOption<&str>parameters.cargo semver-checkspasses.if letovermatch,Option<&str>overOption<&String>."Option<&str>(as_deref()) and let-chains throughout.Implementation
All link handling lives in
mdbook-html; no public API was added.crates/mdbook-html/src/html/tree.rs—fix_link/fix_html_linktakesite_url; root-relative./links are anchored. Threaded at the three call sites viaself.options.config.site_url.as_deref().crates/mdbook-html/src/html_handlebars/hbs_renderer.rs—path_to_root = site_urlper chapter (and theindex.htmlvariant);base_url = site_urlscoped to thetoc.htmlrender; print page rooted atsite_url.crates/mdbook-html/src/html_handlebars/helpers/resources.rs—{{resource}}honors@root/path_to_rootwith a fallback (byte-identical output whensite-urlis unset).crates/mdbook-html/src/html/print.rs—render_print_page/rewrite_linkstakesite_url; thesite_urlprefix is stripped and the remainder resolved as root-relative so in-print cross-refs still fold to#id.src/cmd/serve.rs—--preserve-site-urlflag; the/override is now logged and opt-out.Tests
tests/testsuite/rendering.rs—site_url_*(7): content link rewriting, external links untouched, absolutepath_to_root, absolute assets,<base href>only ontoc.html, no<base href>leak onto chapter pages, opt-in behavior when unset, and the print page (chrome absolute + cross-refs folded to anchors).crates/mdbook-html/src/html/tree.rs—fix_link_tests(4):.md→.html, schemes/fragments left alone,./→site-url, bare-relative links unchanged.tests/testsuite/rendering/site_url/.cargo test --workspace,cargo clippy --all-targetsandcargo fmt --checkall pass.Docs
guide/src/format/configuration/renderers.md— thesite-urlbullet now describes whole-site absolute links and the serve override.guide/src/cli/serve.md—--preserve-site-urlsection.Notes
./pathform, matching the original patch's semantics; bare relative links keep their page-relative meaning../link to the first chapter (which mdBook aliasesREADME→index) stays an absolute link to its standalone page in the print output rather than folding to an anchor. This is a pre-existingREADME/indexaliasing quirk, independent of this feature.canonical-site-url(Addcanonical-site-urlsetting #2706): the two settings are complementary and do not overlap.site-urlis the deployment location and, with this change, the root for every emitted link/asset so the book resolves under a sub-path.canonical-site-urlonly emits<link rel="canonical">for SEO and does not affect link resolution. They touch different code paths and can land independently.Supersedes #1802; builds on @joaofreires' original approach there.