You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

101 lines
2.5KB

  1. mod file_info;
  2. mod page;
  3. mod section;
  4. mod ser;
  5. pub use self::file_info::FileInfo;
  6. pub use self::page::Page;
  7. pub use self::section::Section;
  8. pub use self::ser::{SerializingPage, SerializingSection};
  9. use rendering::Heading;
  10. pub fn has_anchor(headings: &[Heading], anchor: &str) -> bool {
  11. for heading in headings {
  12. if heading.id == anchor {
  13. return true;
  14. }
  15. if has_anchor(&heading.children, anchor) {
  16. return true;
  17. }
  18. }
  19. false
  20. }
  21. #[cfg(test)]
  22. mod tests {
  23. use super::*;
  24. #[test]
  25. fn can_find_anchor_at_root() {
  26. let input = vec![
  27. Heading {
  28. level: 1,
  29. id: "1".to_string(),
  30. permalink: String::new(),
  31. title: String::new(),
  32. children: vec![],
  33. },
  34. Heading {
  35. level: 2,
  36. id: "1-1".to_string(),
  37. permalink: String::new(),
  38. title: String::new(),
  39. children: vec![],
  40. },
  41. Heading {
  42. level: 3,
  43. id: "1-1-1".to_string(),
  44. permalink: String::new(),
  45. title: String::new(),
  46. children: vec![],
  47. },
  48. Heading {
  49. level: 2,
  50. id: "1-2".to_string(),
  51. permalink: String::new(),
  52. title: String::new(),
  53. children: vec![],
  54. },
  55. ];
  56. assert!(has_anchor(&input, "1-2"));
  57. }
  58. #[test]
  59. fn can_find_anchor_in_children() {
  60. let input = vec![Heading {
  61. level: 1,
  62. id: "1".to_string(),
  63. permalink: String::new(),
  64. title: String::new(),
  65. children: vec![
  66. Heading {
  67. level: 2,
  68. id: "1-1".to_string(),
  69. permalink: String::new(),
  70. title: String::new(),
  71. children: vec![],
  72. },
  73. Heading {
  74. level: 3,
  75. id: "1-1-1".to_string(),
  76. permalink: String::new(),
  77. title: String::new(),
  78. children: vec![],
  79. },
  80. Heading {
  81. level: 2,
  82. id: "1-2".to_string(),
  83. permalink: String::new(),
  84. title: String::new(),
  85. children: vec![],
  86. },
  87. ],
  88. }];
  89. assert!(has_anchor(&input, "1-2"));
  90. }
  91. }