Skip to content

Commit 9f6bcfe

Browse files
committed
Add 404 error page and update book configuration
1 parent 80f66e0 commit 9f6bcfe

2 files changed

Lines changed: 40 additions & 0 deletions

File tree

book.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ additional-css = [
7272
site-url = "/comprehensive-rust/"
7373
git-repository-url = "https://github.com/google/comprehensive-rust"
7474
edit-url-template = "https://github.com/google/comprehensive-rust/edit/main/{path}"
75+
input-404 = "404.md"
7576

7677
[output.html.fold]
7778
enable = true

src/404.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Document not found (404)
2+
3+
This URL is invalid, sorry. Please use the navigation bar or search to continue.
4+
5+
<script>
6+
(function () {
7+
const baseEl = document.querySelector("base");
8+
const baseHref = baseEl ? baseEl.getAttribute("href") || "/" : "/";
9+
const knownLangs = new Set(
10+
Array.from(document.querySelectorAll("#language-list a[id]"), (link) => link.id),
11+
);
12+
13+
const basePath = new URL(baseHref, window.location.origin).pathname;
14+
15+
const fullPath = window.location.pathname;
16+
17+
let relPath = fullPath.startsWith(basePath)
18+
? fullPath.slice(basePath.length)
19+
: fullPath.replace(/^\/+/, "");
20+
21+
if (!relPath) {
22+
return;
23+
}
24+
25+
const segments = relPath.split("/");
26+
const maybeLang = segments[0];
27+
28+
if (!knownLangs.has(maybeLang) || maybeLang === "en") {
29+
return;
30+
}
31+
32+
if (relPath === maybeLang + "/404.html" || relPath === "404.html") {
33+
return;
34+
}
35+
36+
const targetUrl = new URL(maybeLang + "/404.html", baseHref).toString();
37+
window.location.replace(targetUrl);
38+
})();
39+
</script>

0 commit comments

Comments
 (0)