Skip to content

Commit

Permalink
Ensure section numbers are correctly incremented after a horizontal s…
Browse files Browse the repository at this point in the history
…eparator (#790)

Fixes #779
  • Loading branch information
badboy authored and Michael-F-Bryan committed Sep 19, 2018
1 parent e4eddb3 commit 0c926b3
Showing 1 changed file with 40 additions and 1 deletion.
41 changes: 40 additions & 1 deletion src/book/summary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ impl<'a> SummaryParser<'a> {
/// already been consumed by a previous parser.
fn parse_numbered(&mut self) -> Result<Vec<SummaryItem>> {
let mut items = Vec::new();
let mut root_items = 0;
let root_number = SectionNumber::default();

// we need to do this funny loop-match-if-let dance because a rule will
Expand All @@ -308,9 +309,11 @@ impl<'a> SummaryParser<'a> {
// if we've resumed after something like a rule the root sections
// will be numbered from 1. We need to manually go back and update
// them
update_section_numbers(&mut bunch_of_items, 0, items.len() as u32);
update_section_numbers(&mut bunch_of_items, 0, root_items);
root_items += bunch_of_items.len() as u32;
items.extend(bunch_of_items);


match self.next_event() {
Some(Event::Start(Tag::Paragraph)) => {
// we're starting the suffix chapters
Expand Down Expand Up @@ -724,4 +727,40 @@ mod tests {
let got = parser.parse_numbered();
assert!(got.is_err());
}

/// Regression test for https://github.com/rust-lang-nursery/mdBook/issues/779
/// Ensure section numbers are correctly incremented after a horizontal separator.
#[test]
fn keep_numbering_after_separator() {
let src = "- [First](./first.md)\n---\n- [Second](./second.md)\n---\n- [Third](./third.md)\n";
let should_be = vec![
SummaryItem::Link(Link {
name: String::from("First"),
location: PathBuf::from("./first.md"),
number: Some(SectionNumber(vec![1])),
nested_items: Vec::new(),
}),
SummaryItem::Separator,
SummaryItem::Link(Link {
name: String::from("Second"),
location: PathBuf::from("./second.md"),
number: Some(SectionNumber(vec![2])),
nested_items: Vec::new(),
}),
SummaryItem::Separator,
SummaryItem::Link(Link {
name: String::from("Third"),
location: PathBuf::from("./third.md"),
number: Some(SectionNumber(vec![3])),
nested_items: Vec::new(),
}),
];

let mut parser = SummaryParser::new(src);
let _ = parser.stream.next();

let got = parser.parse_numbered().unwrap();

assert_eq!(got, should_be);
}
}

0 comments on commit 0c926b3

Please sign in to comment.