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.

166 lines
4.8KB

  1. /// Populated while receiving events from the markdown parser
  2. #[derive(Debug, PartialEq, Clone, Serialize)]
  3. pub struct Header {
  4. #[serde(skip_serializing)]
  5. pub level: i32,
  6. pub id: String,
  7. pub permalink: String,
  8. pub title: String,
  9. pub children: Vec<Header>,
  10. }
  11. impl Header {
  12. pub fn new(level: i32) -> Header {
  13. Header {
  14. level,
  15. id: String::new(),
  16. permalink: String::new(),
  17. title: String::new(),
  18. children: Vec::new(),
  19. }
  20. }
  21. }
  22. impl Default for Header {
  23. fn default() -> Self {
  24. Header::new(0)
  25. }
  26. }
  27. // Takes a potential (mutable) parent and a header to try and insert into
  28. // Returns true when it performed the insertion, false otherwise
  29. fn insert_into_parent(potential_parent: Option<&mut Header>, header: &Header) -> bool {
  30. match potential_parent {
  31. None => {
  32. // No potential parent to insert into so it needs to be insert higher
  33. false
  34. }
  35. Some(parent) => {
  36. let diff = header.level - parent.level;
  37. if diff <= 0 {
  38. // Heading is same level or higher so we don't insert here
  39. return false;
  40. }
  41. if diff == 1 {
  42. // We have a direct child of the parent
  43. parent.children.push(header.clone());
  44. return true;
  45. }
  46. // We need to go deeper
  47. if !insert_into_parent(parent.children.iter_mut().last(), header) {
  48. // No, we need to insert it here
  49. parent.children.push(header.clone());
  50. }
  51. true
  52. }
  53. }
  54. }
  55. /// Converts the flat temp headers into a nested set of headers
  56. /// representing the hierarchy
  57. pub fn make_table_of_contents(headers: Vec<Header>) -> Vec<Header> {
  58. let mut toc = vec![];
  59. for header in headers {
  60. // First header or we try to insert the current header in a previous one
  61. if toc.is_empty() || !insert_into_parent(toc.iter_mut().last(), &header) {
  62. toc.push(header);
  63. }
  64. }
  65. toc
  66. }
  67. #[cfg(test)]
  68. mod tests {
  69. use super::*;
  70. #[test]
  71. fn can_make_basic_toc() {
  72. let input = vec![Header::new(1), Header::new(1), Header::new(1)];
  73. let toc = make_table_of_contents(input);
  74. assert_eq!(toc.len(), 3);
  75. }
  76. #[test]
  77. fn can_make_more_complex_toc() {
  78. let input = vec![
  79. Header::new(1),
  80. Header::new(2),
  81. Header::new(2),
  82. Header::new(3),
  83. Header::new(2),
  84. Header::new(1),
  85. Header::new(2),
  86. Header::new(3),
  87. Header::new(3),
  88. ];
  89. let toc = make_table_of_contents(input);
  90. assert_eq!(toc.len(), 2);
  91. assert_eq!(toc[0].children.len(), 3);
  92. assert_eq!(toc[1].children.len(), 1);
  93. assert_eq!(toc[0].children[1].children.len(), 1);
  94. assert_eq!(toc[1].children[0].children.len(), 2);
  95. }
  96. #[test]
  97. fn can_make_deep_toc() {
  98. let input = vec![
  99. Header::new(1),
  100. Header::new(2),
  101. Header::new(3),
  102. Header::new(4),
  103. Header::new(5),
  104. Header::new(4),
  105. ];
  106. let toc = make_table_of_contents(input);
  107. assert_eq!(toc.len(), 1);
  108. assert_eq!(toc[0].children.len(), 1);
  109. assert_eq!(toc[0].children[0].children.len(), 1);
  110. assert_eq!(toc[0].children[0].children[0].children.len(), 2);
  111. assert_eq!(toc[0].children[0].children[0].children[0].children.len(), 1);
  112. }
  113. #[test]
  114. fn can_make_deep_messy_toc() {
  115. let input = vec![
  116. Header::new(2), // toc[0]
  117. Header::new(3),
  118. Header::new(4),
  119. Header::new(5),
  120. Header::new(4),
  121. Header::new(2), // toc[1]
  122. Header::new(1), // toc[2]
  123. Header::new(2),
  124. Header::new(3),
  125. Header::new(4),
  126. ];
  127. let toc = make_table_of_contents(input);
  128. assert_eq!(toc.len(), 3);
  129. assert_eq!(toc[0].children.len(), 1);
  130. assert_eq!(toc[0].children[0].children.len(), 2);
  131. assert_eq!(toc[0].children[0].children[0].children.len(), 1);
  132. assert_eq!(toc[1].children.len(), 0);
  133. assert_eq!(toc[2].children.len(), 1);
  134. assert_eq!(toc[2].children[0].children.len(), 1);
  135. assert_eq!(toc[2].children[0].children[0].children.len(), 1);
  136. }
  137. #[test]
  138. fn can_make_messy_toc() {
  139. let input = vec![
  140. Header::new(3),
  141. Header::new(2),
  142. Header::new(2),
  143. Header::new(3),
  144. Header::new(2),
  145. Header::new(1),
  146. Header::new(4),
  147. ];
  148. let toc = make_table_of_contents(input);
  149. println!("{:#?}", toc);
  150. assert_eq!(toc.len(), 5);
  151. assert_eq!(toc[2].children.len(), 1);
  152. assert_eq!(toc[4].children.len(), 1);
  153. }
  154. }