diff --git a/src/librustdoc/html/markdown.rs b/src/librustdoc/html/markdown.rs
index 20cf48915c3..7a922f570da 100644
--- a/src/librustdoc/html/markdown.rs
+++ b/src/librustdoc/html/markdown.rs
@@ -414,11 +414,13 @@ impl<'a, I: Iterator- >> Iterator for LinkReplacer<'a, I> {
}
}
+type SpannedEvent<'a> = (Event<'a>, Range);
+
/// Make headings links with anchor IDs and build up TOC.
struct HeadingLinks<'a, 'b, 'ids, I> {
inner: I,
toc: Option<&'b mut TocBuilder>,
- buf: VecDeque>,
+ buf: VecDeque>,
id_map: &'ids mut IdMap,
}
@@ -428,8 +430,10 @@ impl<'a, 'b, 'ids, I> HeadingLinks<'a, 'b, 'ids, I> {
}
}
-impl<'a, 'b, 'ids, I: Iterator
- >> Iterator for HeadingLinks<'a, 'b, 'ids, I> {
- type Item = Event<'a>;
+impl<'a, 'b, 'ids, I: Iterator
- >> Iterator
+ for HeadingLinks<'a, 'b, 'ids, I>
+{
+ type Item = SpannedEvent<'a>;
fn next(&mut self) -> Option {
if let Some(e) = self.buf.pop_front() {
@@ -437,31 +441,29 @@ impl<'a, 'b, 'ids, I: Iterator
- >> Iterator for HeadingLinks<'a,
}
let event = self.inner.next();
- if let Some(Event::Start(Tag::Heading(level))) = event {
+ if let Some((Event::Start(Tag::Heading(level)), _)) = event {
let mut id = String::new();
for event in &mut self.inner {
- match &event {
+ match &event.0 {
Event::End(Tag::Heading(..)) => break,
+ Event::Start(Tag::Link(_, _, _)) | Event::End(Tag::Link(..)) => {}
Event::Text(text) | Event::Code(text) => {
id.extend(text.chars().filter_map(slugify));
+ self.buf.push_back(event);
}
- _ => {}
- }
- match event {
- Event::Start(Tag::Link(_, _, _)) | Event::End(Tag::Link(..)) => {}
- event => self.buf.push_back(event),
+ _ => self.buf.push_back(event),
}
}
let id = self.id_map.derive(id);
if let Some(ref mut builder) = self.toc {
let mut html_header = String::new();
- html::push_html(&mut html_header, self.buf.iter().cloned());
+ html::push_html(&mut html_header, self.buf.iter().map(|(ev, _)| ev.clone()));
let sec = builder.push(level as u32, html_header, id.clone());
- self.buf.push_front(Event::Html(format!("{} ", sec).into()));
+ self.buf.push_front((Event::Html(format!("{} ", sec).into()), 0..0));
}
- self.buf.push_back(Event::Html(format!("", level).into()));
+ self.buf.push_back((Event::Html(format!("", level).into()), 0..0));
let start_tags = format!(
"