Skip to main content

repoint_escaping_links

Function repoint_escaping_links 

Source
pub fn repoint_escaping_links(html: &str, depth: usize, base: &str) -> String
Expand description

Repoints the links that leave the book.

docs/*.md is read by three people (book.toml says so): somebody browsing the checkout, somebody reading the book, and cargo doc. The first and third want ../README.md, because in a checkout that file is right there. The second gets a 404: mdBook renders src and nothing above it, so ../README.html names a file the published artifact does not contain — twenty-one such links, from four pages.

Rather than making the pages worse for two readers to fix the third, the escaping links are repointed at the repository itself at publish time. base is a browsable tree URL (the workflow builds one from the commit it is publishing, so the link is pinned rather than tracking a branch), and it never appears in a tracked file — the same rule site-url follows.

depth is how many directories below the book root the page sits, so index.html is 0 and using/workflow.html is 1. A link escapes when it climbs further than that, and the first .. past the book root is the step from book/ to the checkout — which is why one is stripped before the rest becomes a repository path.

.html goes back to .md because mdBook rewrote it on the way in. Anything that was not a markdown link is left exactly as it was: a rewriter that guessed here would break the links that work.