diff --git a/crates/epub/src/lib.rs b/crates/epub/src/lib.rs index c151f530c..a5719a1c3 100644 --- a/crates/epub/src/lib.rs +++ b/crates/epub/src/lib.rs @@ -1065,7 +1065,8 @@ fn nav_list(entries: &[(String, String, u8)]) -> String { open.push(*level); } Some(_) => { - while open.len() > 1 && open.last().is_some_and(|t| t > level) { + // Close the levels whose parent is as deep as this entry or deeper. + while open.len() > 1 && open.len().checked_sub(2).and_then(|i| open.get(i)).is_some_and(|parent| *parent >= *level) { open.pop(); out.push_str(""); } @@ -1627,6 +1628,17 @@ mod tests { plain.add_text_frame(SpreadRef::Doc(0), Rect::new(36.0, 36.0, 300.0, 100.0), lid, "Opening\nrest", ParaFormat::default()).unwrap(); let nav = read(&plain, &EpubOptions::default(), "OEBPS/nav.xhtml"); assert!(nav.contains(&format!("