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
|
|
|
|
|
|
|
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"));
|
|
|
|
}
|
|
|
|
}
|