Skip to content

Commit 35cf96a

Browse files
authored
Merge pull request #2550 from ehuss/fix-expected-source-path
Fix issue with None source_path
2 parents 132ca0d + 5777a0e commit 35cf96a

File tree

3 files changed

+33
-11
lines changed

3 files changed

+33
-11
lines changed

src/book/book.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,8 @@ pub struct Chapter {
173173
/// `index.md` via the [`Chapter::path`] field. The `source_path` field
174174
/// exists if you need access to the true file path.
175175
///
176-
/// This is `None` for a draft chapter.
176+
/// This is `None` for a draft chapter, or a synthetically generated
177+
/// chapter that has no file on disk.
177178
pub source_path: Option<PathBuf>,
178179
/// An ordered list of the names of each chapter above this one in the hierarchy.
179180
pub parent_names: Vec<String>,

src/renderer/html_handlebars/search.rs

+11-9
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,11 @@ pub fn create_files(search_config: &Search, destination: &Path, book: &Book) ->
4343
BookItem::Chapter(ch) if !ch.is_draft_chapter() => ch,
4444
_ => continue,
4545
};
46-
let chapter_settings =
47-
get_chapter_settings(&chapter_configs, chapter.source_path.as_ref().unwrap());
48-
if !chapter_settings.enable.unwrap_or(true) {
49-
continue;
46+
if let Some(path) = settings_path(chapter) {
47+
let chapter_settings = get_chapter_settings(&chapter_configs, path);
48+
if !chapter_settings.enable.unwrap_or(true) {
49+
continue;
50+
}
5051
}
5152
render_item(&mut index, search_config, &mut doc_urls, chapter)?;
5253
}
@@ -321,6 +322,10 @@ fn clean_html(html: &str) -> String {
321322
AMMONIA.clean(html).to_string()
322323
}
323324

325+
fn settings_path(ch: &Chapter) -> Option<&Path> {
326+
ch.source_path.as_deref().or_else(|| ch.path.as_deref())
327+
}
328+
324329
fn validate_chapter_config(
325330
chapter_configs: &[(PathBuf, SearchChapterSettings)],
326331
book: &Book,
@@ -329,13 +334,10 @@ fn validate_chapter_config(
329334
let found = book
330335
.iter()
331336
.filter_map(|item| match item {
332-
BookItem::Chapter(ch) if !ch.is_draft_chapter() => Some(ch),
337+
BookItem::Chapter(ch) if !ch.is_draft_chapter() => settings_path(ch),
333338
_ => None,
334339
})
335-
.any(|chapter| {
336-
let ch_path = chapter.source_path.as_ref().unwrap();
337-
ch_path.starts_with(path)
338-
});
340+
.any(|source_path| source_path.starts_with(path));
339341
if !found {
340342
bail!(
341343
"[output.html.search.chapter] key `{}` does not match any chapter paths",

tests/rendered_output.rs

+20-1
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ mod dummy_book;
33
use crate::dummy_book::{assert_contains_strings, assert_doesnt_contain_strings, DummyBook};
44

55
use anyhow::Context;
6+
use mdbook::book::Chapter;
67
use mdbook::config::Config;
78
use mdbook::errors::*;
89
use mdbook::utils::fs::write_file;
9-
use mdbook::MDBook;
10+
use mdbook::{BookItem, MDBook};
1011
use pretty_assertions::assert_eq;
1112
use select::document::Document;
1213
use select::predicate::{Attr, Class, Name, Predicate};
@@ -1031,3 +1032,21 @@ fn custom_header_attributes() {
10311032
];
10321033
assert_contains_strings(&contents, summary_strings);
10331034
}
1035+
1036+
#[test]
1037+
fn with_no_source_path() {
1038+
// Test for a regression where search would fail if source_path is None.
1039+
let temp = DummyBook::new().build().unwrap();
1040+
let mut md = MDBook::load(temp.path()).unwrap();
1041+
let chapter = Chapter {
1042+
name: "Sample chapter".to_string(),
1043+
content: "".to_string(),
1044+
number: None,
1045+
sub_items: Vec::new(),
1046+
path: Some(PathBuf::from("sample.html")),
1047+
source_path: None,
1048+
parent_names: Vec::new(),
1049+
};
1050+
md.book.sections.push(BookItem::Chapter(chapter));
1051+
md.build().unwrap();
1052+
}

0 commit comments

Comments
 (0)