Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include chapters with no headers in the search index #1637

Merged
merged 1 commit into from
Sep 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/renderer/html_handlebars/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,12 @@ fn render_item(
}
}

if !heading.is_empty() {
if !body.is_empty() || !heading.is_empty() {
if heading.is_empty() {
if let Some(chapter) = breadcrumbs.first() {
heading = chapter.clone();
}
}
// Make sure the last section is added to the index
add_doc(
index,
Expand Down
1 change: 1 addition & 0 deletions tests/dummy_book/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
- [Recursive](first/recursive.md)
- [Markdown](first/markdown.md)
- [Unicode](first/unicode.md)
- [No Headers](first/no-headers.md)
- [Second Chapter](second.md)
- [Nested Chapter](second/nested.md)

Expand Down
3 changes: 3 additions & 0 deletions tests/dummy_book/src/first/no-headers.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Capybara capybara capybara.

Capybara capybara capybara.
12 changes: 11 additions & 1 deletion tests/rendered_output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const TOC_SECOND_LEVEL: &[&str] = &[
"1.3. Recursive",
"1.4. Markdown",
"1.5. Unicode",
"1.6. No Headers",
"2.1. Nested Chapter",
];

Expand Down Expand Up @@ -631,6 +632,7 @@ mod search {
let introduction = get_doc_ref("intro.html#introduction");
let some_section = get_doc_ref("first/index.html#some-section");
let summary = get_doc_ref("first/includes.html#summary");
let no_headers = get_doc_ref("first/no-headers.html");
let conclusion = get_doc_ref("conclusion.html#conclusion");

let bodyidx = &index["index"]["index"]["body"]["root"];
Expand All @@ -644,13 +646,21 @@ mod search {
assert_eq!(docs[&some_section]["body"], "");
assert_eq!(
docs[&summary]["body"],
"Dummy Book Introduction First Chapter Nested Chapter Includes Recursive Markdown Unicode Second Chapter Nested Chapter Conclusion"
"Dummy Book Introduction First Chapter Nested Chapter Includes Recursive Markdown Unicode No Headers Second Chapter Nested Chapter Conclusion"
);
assert_eq!(
docs[&summary]["breadcrumbs"],
"First Chapter » Includes » Summary"
);
assert_eq!(docs[&conclusion]["body"], "I put <HTML> in here!");
assert_eq!(
docs[&no_headers]["breadcrumbs"],
"First Chapter » No Headers"
);
assert_eq!(
docs[&no_headers]["body"],
"Capybara capybara capybara. Capybara capybara capybara."
);
}

// Setting this to `true` may cause issues with `cargo watch`,
Expand Down
Loading