2018-10-02 14:42:34 +00:00
|
|
|
mod file_info;
|
|
|
|
mod page;
|
|
|
|
mod section;
|
2018-10-24 09:40:57 +00:00
|
|
|
mod ser;
|
2018-10-02 14:42:34 +00:00
|
|
|
|
|
|
|
pub use self::file_info::FileInfo;
|
2018-10-24 09:40:57 +00:00
|
|
|
pub use self::page::Page;
|
|
|
|
pub use self::section::Section;
|
|
|
|
pub use self::ser::{SerializingPage, SerializingSection};
|
2019-06-06 17:49:31 +00:00
|
|
|
|
2019-09-06 21:36:30 +00:00
|
|
|
use rendering::Heading;
|
2019-06-06 17:49:31 +00:00
|
|
|
|
2019-09-06 21:36:30 +00:00
|
|
|
pub fn has_anchor(headings: &[Heading], anchor: &str) -> bool {
|
2019-06-06 17:49:31 +00:00
|
|
|
for heading in headings {
|
|
|
|
if heading.id == anchor {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if has_anchor(&heading.children, anchor) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn can_find_anchor_at_root() {
|
|
|
|
let input = vec![
|
2019-09-06 21:36:30 +00:00
|
|
|
Heading {
|
2019-06-06 17:49:31 +00:00
|
|
|
level: 1,
|
|
|
|
id: "1".to_string(),
|
|
|
|
permalink: String::new(),
|
|
|
|
title: String::new(),
|
|
|
|
children: vec![],
|
|
|
|
},
|
2019-09-06 21:36:30 +00:00
|
|
|
Heading {
|
2019-06-06 17:49:31 +00:00
|
|
|
level: 2,
|
|
|
|
id: "1-1".to_string(),
|
|
|
|
permalink: String::new(),
|
|
|
|
title: String::new(),
|
|
|
|
children: vec![],
|
|
|
|
},
|
2019-09-06 21:36:30 +00:00
|
|
|
Heading {
|
2019-06-06 17:49:31 +00:00
|
|
|
level: 3,
|
|
|
|
id: "1-1-1".to_string(),
|
|
|
|
permalink: String::new(),
|
|
|
|
title: String::new(),
|
|
|
|
children: vec![],
|
|
|
|
},
|
2019-09-06 21:36:30 +00:00
|
|
|
Heading {
|
2019-06-06 17:49:31 +00:00
|
|
|
level: 2,
|
|
|
|
id: "1-2".to_string(),
|
|
|
|
permalink: String::new(),
|
|
|
|
title: String::new(),
|
|
|
|
children: vec![],
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
assert!(has_anchor(&input, "1-2"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn can_find_anchor_in_children() {
|
2019-09-06 21:36:30 +00:00
|
|
|
let input = vec![Heading {
|
2019-06-06 17:49:31 +00:00
|
|
|
level: 1,
|
|
|
|
id: "1".to_string(),
|
|
|
|
permalink: String::new(),
|
|
|
|
title: String::new(),
|
|
|
|
children: vec![
|
2019-09-06 21:36:30 +00:00
|
|
|
Heading {
|
2019-06-06 17:49:31 +00:00
|
|
|
level: 2,
|
|
|
|
id: "1-1".to_string(),
|
|
|
|
permalink: String::new(),
|
|
|
|
title: String::new(),
|
|
|
|
children: vec![],
|
|
|
|
},
|
2019-09-06 21:36:30 +00:00
|
|
|
Heading {
|
2019-06-06 17:49:31 +00:00
|
|
|
level: 3,
|
|
|
|
id: "1-1-1".to_string(),
|
|
|
|
permalink: String::new(),
|
|
|
|
title: String::new(),
|
|
|
|
children: vec![],
|
|
|
|
},
|
2019-09-06 21:36:30 +00:00
|
|
|
Heading {
|
2019-06-06 17:49:31 +00:00
|
|
|
level: 2,
|
|
|
|
id: "1-2".to_string(),
|
|
|
|
permalink: String::new(),
|
|
|
|
title: String::new(),
|
|
|
|
children: vec![],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
}];
|
|
|
|
|
|
|
|
assert!(has_anchor(&input, "1-2"));
|
|
|
|
}
|
|
|
|
}
|