Skip to content

Commit c72ce18

Browse files
committed
Rewrite links in Markdown to point to fallback if missing in translation
It will follow relative links to other pages and embedded images.
1 parent ee740ac commit c72ce18

File tree

14 files changed

+296
-102
lines changed

14 files changed

+296
-102
lines changed

src/cmd/serve.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,11 @@ pub fn execute(args: &ArgMatches) -> Result<()> {
129129
info!("Building book...");
130130

131131
// FIXME: This area is really ugly because we need to re-set livereload :(
132-
let result = MDBook::load(&book_dir).and_then(|mut b| {
133-
update_config(&mut b);
134-
b.build()
135-
});
132+
let result =
133+
MDBook::load_with_build_opts(&book_dir, build_opts.clone()).and_then(|mut b| {
134+
update_config(&mut b);
135+
b.build()
136+
});
136137

137138
if let Err(e) = result {
138139
error!("Unable to load the book");

src/cmd/watch.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ pub fn make_subcommand<'a, 'b>() -> App<'a, 'b> {
3434
pub fn execute(args: &ArgMatches) -> Result<()> {
3535
let book_dir = get_book_dir(args);
3636
let build_opts = get_build_opts(args);
37-
let mut book = MDBook::load_with_build_opts(&book_dir, build_opts)?;
37+
let mut book = MDBook::load_with_build_opts(&book_dir, build_opts.clone())?;
3838

3939
let update_config = |book: &mut MDBook| {
4040
if let Some(dest_dir) = args.value_of("dest-dir") {
@@ -50,7 +50,7 @@ pub fn execute(args: &ArgMatches) -> Result<()> {
5050

5151
trigger_on_change(&book, |paths, book_dir| {
5252
info!("Files changed: {:?}\nBuilding book...\n", paths);
53-
let result = MDBook::load(&book_dir).and_then(|mut b| {
53+
let result = MDBook::load_with_build_opts(&book_dir, build_opts.clone()).and_then(|mut b| {
5454
update_config(&mut b);
5555
b.build()
5656
});

src/renderer/html_handlebars/hbs_renderer.rs

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -119,15 +119,15 @@ impl HtmlHandlebars {
119119

120120
let mut is_index = true;
121121
for item in book.iter() {
122-
let ctx = RenderItemContext {
122+
let item_ctx = RenderItemContext {
123123
handlebars: &handlebars,
124124
destination: destination.to_path_buf(),
125125
data: data.clone(),
126126
is_index,
127127
html_config: html_config.clone(),
128128
edition: ctx.config.rust.edition,
129129
};
130-
self.render_item(item, ctx, &mut print_content)?;
130+
self.render_item(item, item_ctx, src_dir, &ctx.config, &mut print_content)?;
131131
is_index = false;
132132
}
133133

@@ -138,6 +138,7 @@ impl HtmlHandlebars {
138138
&html_config,
139139
src_dir,
140140
destination,
141+
language_ident,
141142
handlebars,
142143
&mut data,
143144
)?;
@@ -193,6 +194,8 @@ impl HtmlHandlebars {
193194
&self,
194195
item: &BookItem,
195196
mut ctx: RenderItemContext<'_>,
197+
src_dir: &PathBuf,
198+
cfg: &Config,
196199
print_content: &mut String,
197200
) -> Result<()> {
198201
// FIXME: This should be made DRY-er and rely less on mutable state
@@ -216,11 +219,29 @@ impl HtmlHandlebars {
216219
.insert("git_repository_edit_url".to_owned(), json!(edit_url));
217220
}
218221

222+
let fallback_path = cfg.default_language().map(|lang_ident| {
223+
let mut fallback = PathBuf::from(utils::fs::path_to_root(&path));
224+
fallback.push("../");
225+
fallback.push(lang_ident.clone());
226+
fallback
227+
});
228+
219229
let content = ch.content.clone();
220-
let content = utils::render_markdown(&content, ctx.html_config.curly_quotes);
230+
let content = utils::render_markdown_with_path(
231+
&content,
232+
ctx.html_config.curly_quotes,
233+
Some(&path),
234+
Some(&src_dir),
235+
&fallback_path,
236+
);
221237

222-
let fixed_content =
223-
utils::render_markdown_with_path(&ch.content, ctx.html_config.curly_quotes, Some(path));
238+
let fixed_content = utils::render_markdown_with_path(
239+
&ch.content,
240+
ctx.html_config.curly_quotes,
241+
Some(&path),
242+
Some(&src_dir),
243+
&fallback_path,
244+
);
224245
if !ctx.is_index {
225246
// Add page break between chapters
226247
// See https://developer.mozilla.org/en-US/docs/Web/CSS/break-before and https://developer.mozilla.org/en-US/docs/Web/CSS/page-break-before
@@ -298,6 +319,7 @@ impl HtmlHandlebars {
298319
html_config: &HtmlConfig,
299320
src_dir: &PathBuf,
300321
destination: &PathBuf,
322+
language_ident: &Option<String>,
301323
handlebars: &mut Handlebars<'_>,
302324
data: &mut serde_json::Map<String, serde_json::Value>,
303325
) -> Result<()> {
@@ -321,16 +343,26 @@ impl HtmlHandlebars {
321343
let html_content_404 = utils::render_markdown(&content_404, html_config.curly_quotes);
322344

323345
let mut data_404 = data.clone();
324-
let base_url = if let Some(site_url) = &html_config.site_url {
325-
site_url
346+
let mut base_url = if let Some(site_url) = &html_config.site_url {
347+
site_url.clone()
326348
} else {
327349
debug!(
328350
"HTML 'site-url' parameter not set, defaulting to '/'. Please configure \
329351
this to ensure the 404 page work correctly, especially if your site is hosted in a \
330352
subdirectory on the HTTP server."
331353
);
332-
"/"
354+
String::from("/")
333355
};
356+
357+
// Set the subdirectory to the currently localized version if using a
358+
// multilingual output format.
359+
if let LoadedBook::Localized(_) = ctx.book {
360+
if let Some(lang_ident) = language_ident {
361+
base_url.push_str(lang_ident);
362+
base_url.push_str("/");
363+
}
364+
}
365+
334366
data_404.insert("base_url".to_owned(), json!(base_url));
335367
// Set a dummy path to ensure other paths (e.g. in the TOC) are generated correctly
336368
data_404.insert("path".to_owned(), json!("404.md"));

0 commit comments

Comments
 (0)