Skip to content

Commit

Permalink
More efficient sitemap building
Browse files Browse the repository at this point in the history
  • Loading branch information
Keats committed May 2, 2022
1 parent a0630ef commit a297b11
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 51 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ also specify classes on headers now
- `skip_prefixes` is now checked before parsing external link URLs
- Add `render` attribute to taxonomies configuration in `config.toml`, for when you don't want to render
any pages related to that taxonomy
- Serialize `transparent` field from front-matter of sections

## 0.15.3 (2022-01-23)

Expand Down
79 changes: 28 additions & 51 deletions components/site/src/sitemap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use std::cmp::Ordering;
#[derive(Debug, Serialize)]
pub struct SitemapEntry<'a> {
pub permalink: Cow<'a, str>,
pub updated: Option<String>,
pub updated: &'a Option<String>,
pub extra: Option<&'a Map<String, Value>>,
}

Expand All @@ -33,7 +33,7 @@ impl<'a> PartialEq for SitemapEntry<'a> {
impl<'a> Eq for SitemapEntry<'a> {}

impl<'a> SitemapEntry<'a> {
pub fn new(permalink: Cow<'a, str>, updated: Option<String>) -> Self {
pub fn new(permalink: Cow<'a, str>, updated: &'a Option<String>) -> Self {
SitemapEntry { permalink, updated, extra: None }
}

Expand Down Expand Up @@ -61,52 +61,44 @@ pub fn find_entries<'a>(
taxonomies: &'a [Taxonomy],
config: &'a Config,
) -> Vec<SitemapEntry<'a>> {
let pages = library
.pages
.values()
.map(|p| {
let mut entry = SitemapEntry::new(
Cow::Borrowed(&p.permalink),
p.meta.updated.clone().or_else(|| p.meta.date.clone()),
);
entry.add_extra(&p.meta.extra);
entry
})
.collect::<Vec<_>>();

let mut sections = library
.sections
.values()
.filter(|s| s.meta.render)
.map(|s| {
let mut entry = SitemapEntry::new(Cow::Borrowed(&s.permalink), None);
let mut entries = HashSet::new();

for p in library.pages.values() {
let mut entry = SitemapEntry::new(
Cow::Borrowed(&p.permalink),
if p.meta.updated.is_some() { &p.meta.updated } else { &p.meta.date },
);
entry.add_extra(&p.meta.extra);
entries.insert(entry);
}

for s in library.sections.values() {
if s.meta.render {
let mut entry = SitemapEntry::new(Cow::Borrowed(&s.permalink), &None);
entry.add_extra(&s.meta.extra);
entry
})
.collect::<Vec<_>>();
entries.insert(entry);
}

for section in library.sections.values() {
if let Some(paginate_by) = section.paginate_by() {
let number_pagers = (section.pages.len() as f64 / paginate_by as f64).ceil() as isize;
if let Some(paginate_by) = s.paginate_by() {
let number_pagers = (s.pages.len() as f64 / paginate_by as f64).ceil() as isize;
for i in 1..=number_pagers {
let permalink =
format!("{}{}/{}/", section.permalink, section.meta.paginate_path, i);
sections.push(SitemapEntry::new(Cow::Owned(permalink), None))
let permalink = format!("{}{}/{}/", s.permalink, s.meta.paginate_path, i);
entries.insert(SitemapEntry::new(Cow::Owned(permalink), &None));
}
}
}

let mut taxonomies_entries = vec![];
for taxonomy in taxonomies {
if !taxonomy.kind.render {
continue;
}
let name = &taxonomy.kind.name;
let mut terms = vec![SitemapEntry::new(Cow::Owned(config.make_permalink(name)), None)];
entries.insert(SitemapEntry::new(Cow::Owned(config.make_permalink(name)), &None));

for item in &taxonomy.items {
terms.push(SitemapEntry::new(
entries.insert(SitemapEntry::new(
Cow::Owned(config.make_permalink(&format!("{}/{}", name, item.slug))),
None,
&None,
));

if taxonomy.kind.is_paginated() {
Expand All @@ -121,28 +113,13 @@ pub fn find_entries<'a>(
taxonomy.kind.paginate_path(),
i
));
terms.push(SitemapEntry::new(Cow::Owned(permalink), None))
entries.insert(SitemapEntry::new(Cow::Owned(permalink), &None));
}
}
}

taxonomies_entries.push(terms);
}

let mut all_sitemap_entries = HashSet::new();
for p in pages {
all_sitemap_entries.insert(p);
}
for s in sections {
all_sitemap_entries.insert(s);
}
for terms in taxonomies_entries {
for term in terms {
all_sitemap_entries.insert(term);
}
}

let mut entries = all_sitemap_entries.into_iter().collect::<Vec<_>>();
let mut entries = entries.into_iter().collect::<Vec<_>>();
entries.sort();
entries
}

0 comments on commit a297b11

Please sign in to comment.