Skip to content

Commit

Permalink
fix rss again 💀
Browse files Browse the repository at this point in the history
  • Loading branch information
aslilac committed Aug 27, 2023
1 parent db66933 commit 0851649
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 13 deletions.
23 changes: 16 additions & 7 deletions src/blog_post.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,8 @@ impl Serialize for BlogPostMetadata {
where
S: Serializer,
{
let ser_date = |date: &Option<NaiveDate>, format: &str| {
date.map(|date| date.format(format).to_string())
};
let ser_date =
|date: &Option<NaiveDate>, format: &str| date.map(|date| date.format(format).to_string());

let mut state = ser.serialize_struct("BlogPostMetadata", 10)?;
state.serialize_field("title", &self.title)?;
Expand Down Expand Up @@ -95,10 +94,7 @@ where
.expect(&format!("missing blog post metadata in {}", path.display()));

BlogPost {
canonical_url: BLOG
.canonical_origin
.join(path.to_str().expect("path contains invalid characters"))
.expect("failed to create canonical_url"),
canonical_url: BlogPost::canonicalize_path(&path),
path,
metadata,
content: page.content,
Expand All @@ -125,3 +121,16 @@ impl AsHtml for BlogPost {
.expect("failed to render handlebars")
}
}

impl BlogPost {
fn canonicalize_path(path: &Path) -> Url {
BLOG
.canonical_origin
.join(path.to_str().expect("path contains invalid characters"))
.expect("failed to create canonical_url")
}

pub fn canonicalize(&mut self) {
self.canonical_url = Self::canonicalize_path(&self.path)
}
}
10 changes: 4 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ fn main() -> io::Result<()> {
.strip_prefix("/")
.expect("`from` for redirect should be an absolute url"),
);
fs::create_dir_all(output_path.parent().unwrap())
.expect("failed to create output directory");
fs::create_dir_all(output_path.parent().unwrap()).expect("failed to create output directory");
fs::write(output_path, redirect.as_html())?;
}

// Collect posts into a `PageCollection`
let mut posts = pages_from_directory("content/posts/").collect::<Vec<BlogPost>>();
posts.iter_mut().for_each(|post| {
post.path = post.path.strip_prefix("content/").unwrap().to_owned();
post.canonicalize();
});
if options.publish {
// Skip unpublished posts if we're building a version for publishing
Expand All @@ -61,8 +61,7 @@ fn main() -> io::Result<()> {
// Render posts
for post in posts.iter() {
let output_path = options.output.join(&post.path);
fs::create_dir_all(output_path.parent().unwrap())
.expect("failed to create output directory");
fs::create_dir_all(output_path.parent().unwrap()).expect("failed to create output directory");
fs::write(output_path, post.as_html())?;
}

Expand All @@ -78,8 +77,7 @@ fn main() -> io::Result<()> {
// Render talks
for talk in talks.iter() {
let output_path = options.output.join(&talk.path);
fs::create_dir_all(output_path.parent().unwrap())
.expect("failed to create output directory");
fs::create_dir_all(output_path.parent().unwrap()).expect("failed to create output directory");
fs::write(output_path, talk.as_html())?;
}

Expand Down

0 comments on commit 0851649

Please sign in to comment.