From 722cf7889c00beeaf0956698e888b15bd7058104 Mon Sep 17 00:00:00 2001 From: Remo Senekowitsch Date: Tue, 15 Apr 2025 22:02:24 +0200 Subject: [PATCH 1/3] Make front matter test more lenient During the migration to Zola, it was nice to have the enforcement that all front matter is formatted identically. But going into the future, the purpose of this test will be to help blog authors spot mistakes in their front matter. It would be annoying noise a fail because the keys are in a different order, whitespace, comments etc. --- front_matter/src/lib.rs | 38 ++++++++++++++++++++++++-------------- 1 file changed, 24 insertions(+), 14 deletions(-) diff --git a/front_matter/src/lib.rs b/front_matter/src/lib.rs index 3b4b8f666..21d6fcf90 100644 --- a/front_matter/src/lib.rs +++ b/front_matter/src/lib.rs @@ -3,7 +3,7 @@ use serde::{Deserialize, Serialize}; use toml::value::Date; /// The front matter of a markdown blog post. -#[derive(Debug, PartialEq, Serialize, Deserialize)] +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] pub struct FrontMatter { /// Deprecated. The plan was probably to have more specialized templates /// at some point. That didn't materialize, all posts are rendered with the @@ -42,7 +42,7 @@ pub struct FrontMatter { pub extra: Extra, } -#[derive(Debug, Default, PartialEq, Serialize, Deserialize)] +#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)] pub struct Extra { pub team: Option, pub team_url: Option, @@ -73,8 +73,12 @@ pub fn parse(markdown: &str) -> eyre::Result<(FrontMatter, &str)> { } /// Normalizes the front matter of a markdown file. -pub fn normalize(markdown: &str, slug: &str, inside_rust: bool) -> eyre::Result { - let (mut front_matter, content) = parse(markdown)?; +pub fn normalize( + front_matter: &FrontMatter, + slug: &str, + inside_rust: bool, +) -> eyre::Result { + let mut front_matter = front_matter.clone(); // migrate "author" to "authors" key if let Some(author) = front_matter.author.take() { @@ -110,14 +114,10 @@ pub fn normalize(markdown: &str, slug: &str, inside_rust: bool) -> eyre::Result< bail!("extra.team and extra.team_url must always come in a pair"); } - Ok(format!( - "\ -+++ -{}\ -+++ -{content}", - toml::to_string_pretty(&front_matter)? - )) + let serialized = toml::to_string_pretty(&front_matter)?; + let deserialized = toml::from_str(&serialized)?; + + Ok(deserialized) } #[cfg(test)] @@ -146,11 +146,21 @@ mod tests { .contains("content/inside-rust/"); let content = fs::read_to_string(&post).unwrap(); - let normalized = normalize(&content, slug, inside_rust).unwrap_or_else(|err| { + let (front_matter, rest) = parse(&content).unwrap(); + let normalized = normalize(&front_matter, slug, inside_rust).unwrap_or_else(|err| { panic!("failed to normalize {:?}: {err}", post.file_name().unwrap()); }); - if content != normalized { + if front_matter != normalized { + let normalized = format!( + "\ + +++\n\ + {}\ + +++\n\ + {rest}\ + ", + toml::to_string_pretty(&normalized).unwrap(), + ); if env::var("FIX_FRONT_MATTER").is_ok() { fs::write(post, normalized).unwrap(); continue; From e8d109aca5d6392a0ef3696ae659aa9b2da8e15f Mon Sep 17 00:00:00 2001 From: Remo Senekowitsch Date: Tue, 15 Apr 2025 22:31:40 +0200 Subject: [PATCH 2/3] Don't create aliases for new blog posts --- front_matter/src/lib.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/front_matter/src/lib.rs b/front_matter/src/lib.rs index 21d6fcf90..76949df3c 100644 --- a/front_matter/src/lib.rs +++ b/front_matter/src/lib.rs @@ -108,7 +108,6 @@ pub fn normalize( slug = slug.split_once('@').map(|(s, _)| s).unwrap_or(slug), ); } - front_matter.aliases = vec![format!("{}.html", front_matter.path)]; if front_matter.extra.team.is_some() ^ front_matter.extra.team_url.is_some() { bail!("extra.team and extra.team_url must always come in a pair"); From 6d240cda1eaf65299b2624a1f1f52a03af1494ef Mon Sep 17 00:00:00 2001 From: Remo Senekowitsch Date: Tue, 15 Apr 2025 22:13:21 +0200 Subject: [PATCH 3/3] Validate front matter in CI --- .github/workflows/main.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index b27c6c5da..cedca9cb7 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -22,6 +22,7 @@ jobs: - run: cargo clippy --workspace -- -D warnings - run: cargo fmt --check --all + - run: cargo test --package front_matter build: runs-on: ubuntu-latest