|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- mod file_info;
- mod page;
- mod section;
- mod ser;
-
- pub use self::file_info::FileInfo;
- pub use self::page::Page;
- pub use self::section::Section;
- pub use self::ser::{SerializingPage, SerializingSection};
-
- use rendering::Header;
-
- pub fn has_anchor(headings: &[Header], anchor: &str) -> bool {
- 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![
- Header {
- level: 1,
- id: "1".to_string(),
- permalink: String::new(),
- title: String::new(),
- children: vec![],
- },
- Header {
- level: 2,
- id: "1-1".to_string(),
- permalink: String::new(),
- title: String::new(),
- children: vec![],
- },
- Header {
- level: 3,
- id: "1-1-1".to_string(),
- permalink: String::new(),
- title: String::new(),
- children: vec![],
- },
- Header {
- 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() {
- let input = vec![Header {
- level: 1,
- id: "1".to_string(),
- permalink: String::new(),
- title: String::new(),
- children: vec![
- Header {
- level: 2,
- id: "1-1".to_string(),
- permalink: String::new(),
- title: String::new(),
- children: vec![],
- },
- Header {
- level: 3,
- id: "1-1-1".to_string(),
- permalink: String::new(),
- title: String::new(),
- children: vec![],
- },
- Header {
- level: 2,
- id: "1-2".to_string(),
- permalink: String::new(),
- title: String::new(),
- children: vec![],
- },
- ],
- }];
-
- assert!(has_anchor(&input, "1-2"));
- }
- }
|