Skip to content

Commit 1db52ff

Browse files
committed
Fix search for custom heading attributes
1 parent e3be293 commit 1db52ff

File tree

3 files changed

+11
-4
lines changed

3 files changed

+11
-4
lines changed

src/renderer/html_handlebars/search.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,11 @@ fn render_item(
138138

139139
in_heading = true;
140140
}
141-
Event::End(Tag::Heading(i, ..)) if i as u32 <= max_section_depth => {
141+
Event::End(Tag::Heading(i, id, _classes)) if i as u32 <= max_section_depth => {
142142
in_heading = false;
143-
section_id = Some(utils::unique_id_from_content(&heading, &mut id_counter));
143+
section_id = id
144+
.map(|id| id.to_string())
145+
.or_else(|| Some(utils::unique_id_from_content(&heading, &mut id_counter)));
144146
breadcrumbs.push(heading.clone());
145147
}
146148
Event::Start(Tag::FootnoteDefinition(name)) => {

tests/rendered_output.rs

+5
Original file line numberDiff line numberDiff line change
@@ -755,6 +755,7 @@ mod search {
755755
let no_headers = get_doc_ref("first/no-headers.html");
756756
let duplicate_headers_1 = get_doc_ref("first/duplicate-headers.html#header-text-1");
757757
let conclusion = get_doc_ref("conclusion.html#conclusion");
758+
let heading_attrs = get_doc_ref("first/heading-attributes.html#both");
758759

759760
let bodyidx = &index["index"]["index"]["body"]["root"];
760761
let textidx = &bodyidx["t"]["e"]["x"]["t"];
@@ -786,6 +787,10 @@ mod search {
786787
docs[&no_headers]["body"],
787788
"Capybara capybara capybara. Capybara capybara capybara. ThisLongWordIsIncludedSoWeCanCheckThatSufficientlyLongWordsAreOmittedFromTheSearchIndex."
788789
);
790+
assert_eq!(
791+
docs[&heading_attrs]["breadcrumbs"],
792+
"First Chapter » Heading Attributes » Heading with id and classes"
793+
);
789794
}
790795

791796
// Setting this to `true` may cause issues with `cargo watch`,

tests/searchindex_fixture.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323
"first/duplicate-headers.html#header-text",
2424
"first/duplicate-headers.html#header-text-1",
2525
"first/duplicate-headers.html#header-text-2",
26-
"first/heading-attributes.html#heading-attributes",
26+
"first/heading-attributes.html#attrs",
2727
"first/heading-attributes.html#heading-with-classes",
28-
"first/heading-attributes.html#heading-with-id-and-classes",
28+
"first/heading-attributes.html#both",
2929
"second.html#second-chapter",
3030
"second/nested.html#testing-relative-links-for-the-print-page",
3131
"second/nested.html#some-section",

0 commit comments

Comments
 (0)