Skip to content

Commit 9268884

Browse files
authored
Merge pull request #1221 from manuel-woelker/fb-539-not-found-page
Generate 404.html page (#539)
2 parents 4f435c6 + d7df832 commit 9268884

File tree

8 files changed

+137
-4
lines changed

8 files changed

+137
-4
lines changed

book-example/book.toml

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ edition = "2018"
99

1010
[output.html]
1111
mathjax-support = true
12+
site-url = "/mdBook/"
1213

1314
[output.html.playground]
1415
editable = true

book-example/src/404.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Document not found (404)
2+
3+
This URL is invalid, sorry. Try the search instead!

book-example/src/format/config.md

+8
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,12 @@ The following configuration options are available:
204204
`/appendices/bibliography.html`). The value can be any valid URI the
205205
browser should navigate to (e.g. `https://rust-lang.org/`,
206206
`/overview.html`, or `../bibliography.html`).
207+
- **input-404:** The name of the markdown file used for misssing files.
208+
The corresponding output file will be the same, with the extension replaced with `html`.
209+
Defaults to `404.md`.
210+
- **site-url:** The url where the book will be hosted. This is required to ensure
211+
navigation links and script/css imports in the 404 file work correctly, even when accessing
212+
urls in subdirectories. Defaults to `/`.
207213

208214
Available configuration options for the `[output.html.fold]` table:
209215

@@ -266,6 +272,8 @@ additional-js = ["custom.js"]
266272
no-section-label = false
267273
git-repository-url = "https://github.com/rust-lang/mdBook"
268274
git-repository-icon = "fa-github"
275+
site-url = "/example-book/"
276+
input-404 = "not-found.md"
269277

270278
[output.html.fold]
271279
enable = false

src/cmd/serve.rs

+22-4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use futures_util::sink::SinkExt;
66
use futures_util::StreamExt;
77
use mdbook::errors::*;
88
use mdbook::utils;
9+
use mdbook::utils::fs::get_404_output_file;
910
use mdbook::MDBook;
1011
use std::net::{SocketAddr, ToSocketAddrs};
1112
use std::path::PathBuf;
@@ -68,6 +69,8 @@ pub fn execute(args: &ArgMatches) -> Result<()> {
6869
if let Some(dest_dir) = args.value_of("dest-dir") {
6970
book.config.build.build_dir = dest_dir.into();
7071
}
72+
// Override site-url for local serving of the 404 file
73+
book.config.set("output.html.site-url", "/")?;
7174

7275
book.build()?;
7376

@@ -76,13 +79,20 @@ pub fn execute(args: &ArgMatches) -> Result<()> {
7679
.next()
7780
.ok_or_else(|| anyhow::anyhow!("no address found for {}", address))?;
7881
let build_dir = book.build_dir_for("html");
82+
let input_404 = book
83+
.config
84+
.get("output.html.input-404")
85+
.map(toml::Value::as_str)
86+
.and_then(std::convert::identity) // flatten
87+
.map(ToString::to_string);
88+
let file_404 = get_404_output_file(&input_404);
7989

8090
// A channel used to broadcast to any websockets to reload when a file changes.
8191
let (tx, _rx) = tokio::sync::broadcast::channel::<Message>(100);
8292

8393
let reload_tx = tx.clone();
8494
let thread_handle = std::thread::spawn(move || {
85-
serve(build_dir, sockaddr, reload_tx);
95+
serve(build_dir, sockaddr, reload_tx, &file_404);
8696
});
8797

8898
let serving_url = format!("http://{}", address);
@@ -120,7 +130,12 @@ pub fn execute(args: &ArgMatches) -> Result<()> {
120130
}
121131

122132
#[tokio::main]
123-
async fn serve(build_dir: PathBuf, address: SocketAddr, reload_tx: broadcast::Sender<Message>) {
133+
async fn serve(
134+
build_dir: PathBuf,
135+
address: SocketAddr,
136+
reload_tx: broadcast::Sender<Message>,
137+
file_404: &str,
138+
) {
124139
// A warp Filter which captures `reload_tx` and provides an `rx` copy to
125140
// receive reload messages.
126141
let sender = warp::any().map(move || reload_tx.subscribe());
@@ -142,7 +157,10 @@ async fn serve(build_dir: PathBuf, address: SocketAddr, reload_tx: broadcast::Se
142157
})
143158
});
144159
// A warp Filter that serves from the filesystem.
145-
let book_route = warp::fs::dir(build_dir);
146-
let routes = livereload.or(book_route);
160+
let book_route = warp::fs::dir(build_dir.clone());
161+
// The fallback route for 404 errors
162+
let fallback_route = warp::fs::file(build_dir.join(file_404))
163+
.map(|reply| warp::reply::with_status(reply, warp::http::StatusCode::NOT_FOUND));
164+
let routes = livereload.or(book_route).or(fallback_route);
147165
warp::serve(routes).run(address).await;
148166
}

src/config.rs

+34
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,10 @@ pub struct HtmlConfig {
504504
/// FontAwesome icon class to use for the Git repository link.
505505
/// Defaults to `fa-github` if `None`.
506506
pub git_repository_icon: Option<String>,
507+
/// Input path for the 404 file, defaults to 404.md, set to "" to disable 404 file output
508+
pub input_404: Option<String>,
509+
/// Absolute url to site, used to emit correct paths for the 404 page, which might be accessed in a deeply nested directory
510+
pub site_url: Option<String>,
507511
/// This is used as a bit of a workaround for the `mdbook serve` command.
508512
/// Basically, because you set the websocket port from the command line, the
509513
/// `mdbook serve` command needs a way to let the HTML renderer know where
@@ -535,6 +539,8 @@ impl Default for HtmlConfig {
535539
search: None,
536540
git_repository_url: None,
537541
git_repository_icon: None,
542+
input_404: None,
543+
site_url: None,
538544
livereload_url: None,
539545
redirect: HashMap::new(),
540546
}
@@ -667,6 +673,7 @@ impl<'de, T> Updateable<'de> for T where T: Serialize + Deserialize<'de> {}
667673
#[cfg(test)]
668674
mod tests {
669675
use super::*;
676+
use crate::utils::fs::get_404_output_file;
670677

671678
const COMPLEX_CONFIG: &str = r#"
672679
[book]
@@ -1001,4 +1008,31 @@ mod tests {
10011008

10021009
assert_eq!(cfg.book.title, Some(should_be));
10031010
}
1011+
1012+
#[test]
1013+
fn file_404_default() {
1014+
let src = r#"
1015+
[output.html]
1016+
destination = "my-book"
1017+
"#;
1018+
1019+
let got = Config::from_str(src).unwrap();
1020+
let html_config = got.html_config().unwrap();
1021+
assert_eq!(html_config.input_404, None);
1022+
assert_eq!(&get_404_output_file(&html_config.input_404), "404.html");
1023+
}
1024+
1025+
#[test]
1026+
fn file_404_custom() {
1027+
let src = r#"
1028+
[output.html]
1029+
input-404= "missing.md"
1030+
output-404= "missing.html"
1031+
"#;
1032+
1033+
let got = Config::from_str(src).unwrap();
1034+
let html_config = got.html_config().unwrap();
1035+
assert_eq!(html_config.input_404, Some("missing.md".to_string()));
1036+
assert_eq!(&get_404_output_file(&html_config.input_404), "missing.html");
1037+
}
10041038
}

src/renderer/html_handlebars/hbs_renderer.rs

+58
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use std::collections::HashMap;
1212
use std::fs::{self, File};
1313
use std::path::{Path, PathBuf};
1414

15+
use crate::utils::fs::get_404_output_file;
1516
use handlebars::Handlebars;
1617
use regex::{Captures, Regex};
1718

@@ -105,6 +106,58 @@ impl HtmlHandlebars {
105106
Ok(())
106107
}
107108

109+
fn render_404(
110+
&self,
111+
ctx: &RenderContext,
112+
html_config: &HtmlConfig,
113+
src_dir: &PathBuf,
114+
handlebars: &mut Handlebars<'_>,
115+
data: &mut serde_json::Map<String, serde_json::Value>,
116+
) -> Result<()> {
117+
let destination = &ctx.destination;
118+
let content_404 = if let Some(ref filename) = html_config.input_404 {
119+
let path = src_dir.join(filename);
120+
std::fs::read_to_string(&path)
121+
.with_context(|| format!("unable to open 404 input file {:?}", path))?
122+
} else {
123+
// 404 input not explicitly configured try the default file 404.md
124+
let default_404_location = src_dir.join("404.md");
125+
if default_404_location.exists() {
126+
std::fs::read_to_string(&default_404_location).with_context(|| {
127+
format!("unable to open 404 input file {:?}", default_404_location)
128+
})?
129+
} else {
130+
"# Document not found (404)\n\nThis URL is invalid, sorry. Please use the \
131+
navigation bar or search to continue."
132+
.to_string()
133+
}
134+
};
135+
let html_content_404 = utils::render_markdown(&content_404, html_config.curly_quotes);
136+
137+
let mut data_404 = data.clone();
138+
let base_url = if let Some(site_url) = &html_config.site_url {
139+
site_url
140+
} else {
141+
debug!(
142+
"HTML 'site-url' parameter not set, defaulting to '/'. Please configure \
143+
this to ensure the 404 page work correctly, especially if your site is hosted in a \
144+
subdirectory on the HTTP server."
145+
);
146+
"/"
147+
};
148+
data_404.insert("base_url".to_owned(), json!(base_url));
149+
// Set a dummy path to ensure other paths (e.g. in the TOC) are generated correctly
150+
data_404.insert("path".to_owned(), json!("404.md"));
151+
data_404.insert("content".to_owned(), json!(html_content_404));
152+
let rendered = handlebars.render("index", &data_404)?;
153+
154+
let rendered = self.post_process(rendered, &html_config.playpen, ctx.config.rust.edition);
155+
let output_file = get_404_output_file(&html_config.input_404);
156+
utils::fs::write_file(&destination, output_file, rendered.as_bytes())?;
157+
debug!("Creating 404.html ✓");
158+
Ok(())
159+
}
160+
108161
#[cfg_attr(feature = "cargo-clippy", allow(clippy::let_and_return))]
109162
fn post_process(
110163
&self,
@@ -441,6 +494,11 @@ impl Renderer for HtmlHandlebars {
441494
is_index = false;
442495
}
443496

497+
// Render 404 page
498+
if html_config.input_404 != Some("".to_string()) {
499+
self.render_404(ctx, &html_config, &src_dir, &mut handlebars, &mut data)?;
500+
}
501+
444502
// Print version
445503
self.configure_print_version(&mut data, &print_content);
446504
if let Some(ref title) = ctx.config.book.title {

src/theme/index.hbs

+4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
{{#if is_print }}
88
<meta name="robots" content="noindex" />
99
{{/if}}
10+
{{#if base_url}}
11+
<base href="{{ base_url }}">
12+
{{/if}}
13+
1014

1115
<!-- Custom HTML head -->
1216
{{> head}}

src/utils/fs.rs

+7
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,13 @@ pub fn copy_files_except_ext(
177177
Ok(())
178178
}
179179

180+
pub fn get_404_output_file(input_404: &Option<String>) -> String {
181+
input_404
182+
.as_ref()
183+
.unwrap_or(&"404.md".to_string())
184+
.replace(".md", ".html")
185+
}
186+
180187
#[cfg(test)]
181188
mod tests {
182189
use super::copy_files_except_ext;

0 commit comments

Comments
 (0)