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.

139 lines
4.4KB

  1. use std::borrow::Cow;
  2. use std::collections::HashSet;
  3. use std::hash::{Hash, Hasher};
  4. use config::Config;
  5. use library::{Library, Taxonomy};
  6. use std::cmp::Ordering;
  7. use tera::{Map, Value};
  8. /// The sitemap only needs links, potentially date and extra for pages in case of updates
  9. /// for examples so we trim down all entries to only that
  10. #[derive(Debug, Serialize)]
  11. pub struct SitemapEntry<'a> {
  12. permalink: Cow<'a, str>,
  13. date: Option<String>,
  14. extra: Option<&'a Map<String, Value>>,
  15. }
  16. // Hash/Eq is not implemented for tera::Map but in our case we only care about the permalink
  17. // when comparing/hashing so we implement it manually
  18. impl<'a> Hash for SitemapEntry<'a> {
  19. fn hash<H: Hasher>(&self, state: &mut H) {
  20. self.permalink.hash(state);
  21. }
  22. }
  23. impl<'a> PartialEq for SitemapEntry<'a> {
  24. fn eq(&self, other: &SitemapEntry) -> bool {
  25. self.permalink == other.permalink
  26. }
  27. }
  28. impl<'a> Eq for SitemapEntry<'a> {}
  29. impl<'a> SitemapEntry<'a> {
  30. pub fn new(permalink: Cow<'a, str>, date: Option<String>) -> Self {
  31. SitemapEntry { permalink, date, extra: None }
  32. }
  33. pub fn add_extra(&mut self, extra: &'a Map<String, Value>) {
  34. self.extra = Some(extra);
  35. }
  36. }
  37. impl<'a> PartialOrd for SitemapEntry<'a> {
  38. fn partial_cmp(&self, other: &SitemapEntry) -> Option<Ordering> {
  39. Some(self.permalink.as_ref().cmp(other.permalink.as_ref()))
  40. }
  41. }
  42. impl<'a> Ord for SitemapEntry<'a> {
  43. fn cmp(&self, other: &SitemapEntry) -> Ordering {
  44. self.permalink.as_ref().cmp(other.permalink.as_ref())
  45. }
  46. }
  47. /// Finds out all the links to put in a sitemap from the pages/sections/taxonomies
  48. /// There are no duplicate permalinks in the output vec
  49. pub fn find_entries<'a>(
  50. library: &'a Library,
  51. taxonomies: &'a [Taxonomy],
  52. config: &'a Config,
  53. ) -> Vec<SitemapEntry<'a>> {
  54. let pages = library
  55. .pages_values()
  56. .iter()
  57. .filter(|p| !p.is_draft())
  58. .map(|p| {
  59. let date = match p.meta.date {
  60. Some(ref d) => Some(d.to_string()),
  61. None => None,
  62. };
  63. let mut entry = SitemapEntry::new(Cow::Borrowed(&p.permalink), date);
  64. entry.add_extra(&p.meta.extra);
  65. entry
  66. })
  67. .collect::<Vec<_>>();
  68. let mut sections = library
  69. .sections_values()
  70. .iter()
  71. .filter(|s| s.meta.render)
  72. .map(|s| SitemapEntry::new(Cow::Borrowed(&s.permalink), None))
  73. .collect::<Vec<_>>();
  74. for section in library.sections_values().iter().filter(|s| s.meta.paginate_by.is_some()) {
  75. let number_pagers =
  76. (section.pages.len() as f64 / section.meta.paginate_by.unwrap() as f64).ceil() as isize;
  77. for i in 1..=number_pagers {
  78. let permalink = format!("{}{}/{}/", section.permalink, section.meta.paginate_path, i);
  79. sections.push(SitemapEntry::new(Cow::Owned(permalink), None))
  80. }
  81. }
  82. let mut taxonomies_entries = vec![];
  83. for taxonomy in taxonomies {
  84. let name = &taxonomy.kind.name;
  85. let mut terms = vec![];
  86. terms.push(SitemapEntry::new(Cow::Owned(config.make_permalink(name)), None));
  87. for item in &taxonomy.items {
  88. terms.push(SitemapEntry::new(
  89. Cow::Owned(config.make_permalink(&format!("{}/{}", name, item.slug))),
  90. None,
  91. ));
  92. if taxonomy.kind.is_paginated() {
  93. let number_pagers = (item.pages.len() as f64
  94. / taxonomy.kind.paginate_by.unwrap() as f64)
  95. .ceil() as isize;
  96. for i in 1..=number_pagers {
  97. let permalink = config.make_permalink(&format!(
  98. "{}/{}/{}/{}",
  99. name,
  100. item.slug,
  101. taxonomy.kind.paginate_path(),
  102. i
  103. ));
  104. terms.push(SitemapEntry::new(Cow::Owned(permalink), None))
  105. }
  106. }
  107. }
  108. taxonomies_entries.push(terms);
  109. }
  110. let mut all_sitemap_entries = HashSet::new();
  111. for p in pages {
  112. all_sitemap_entries.insert(p);
  113. }
  114. for s in sections {
  115. all_sitemap_entries.insert(s);
  116. }
  117. for terms in taxonomies_entries {
  118. for term in terms {
  119. all_sitemap_entries.insert(term);
  120. }
  121. }
  122. all_sitemap_entries.into_iter().collect::<Vec<_>>()
  123. }