@@ -3,7 +3,7 @@ use serde::{Deserialize, Serialize};
33use toml:: value:: Date ;
44
55/// The front matter of a markdown blog post.
6- #[ derive( Debug , PartialEq , Serialize , Deserialize ) ]
6+ #[ derive( Debug , Clone , PartialEq , Serialize , Deserialize ) ]
77pub struct FrontMatter {
88 /// Deprecated. The plan was probably to have more specialized templates
99 /// at some point. That didn't materialize, all posts are rendered with the
@@ -42,7 +42,7 @@ pub struct FrontMatter {
4242 pub extra : Extra ,
4343}
4444
45- #[ derive( Debug , Default , PartialEq , Serialize , Deserialize ) ]
45+ #[ derive( Debug , Clone , Default , PartialEq , Serialize , Deserialize ) ]
4646pub struct Extra {
4747 pub team : Option < String > ,
4848 pub team_url : Option < String > ,
@@ -73,8 +73,12 @@ pub fn parse(markdown: &str) -> eyre::Result<(FrontMatter, &str)> {
7373}
7474
7575/// Normalizes the front matter of a markdown file.
76- pub fn normalize ( markdown : & str , slug : & str , inside_rust : bool ) -> eyre:: Result < String > {
77- let ( mut front_matter, content) = parse ( markdown) ?;
76+ pub fn normalize (
77+ front_matter : & FrontMatter ,
78+ slug : & str ,
79+ inside_rust : bool ,
80+ ) -> eyre:: Result < FrontMatter > {
81+ let mut front_matter = front_matter. clone ( ) ;
7882
7983 // migrate "author" to "authors" key
8084 if let Some ( author) = front_matter. author . take ( ) {
@@ -110,14 +114,10 @@ pub fn normalize(markdown: &str, slug: &str, inside_rust: bool) -> eyre::Result<
110114 bail ! ( "extra.team and extra.team_url must always come in a pair" ) ;
111115 }
112116
113- Ok ( format ! (
114- "\
115- +++
116- {}\
117- +++
118- {content}" ,
119- toml:: to_string_pretty( & front_matter) ?
120- ) )
117+ let serialized = toml:: to_string_pretty ( & front_matter) ?;
118+ let deserialized = toml:: from_str ( & serialized) ?;
119+
120+ Ok ( deserialized)
121121}
122122
123123#[ cfg( test) ]
@@ -146,11 +146,21 @@ mod tests {
146146 . contains ( "content/inside-rust/" ) ;
147147
148148 let content = fs:: read_to_string ( & post) . unwrap ( ) ;
149- let normalized = normalize ( & content, slug, inside_rust) . unwrap_or_else ( |err| {
149+ let ( front_matter, rest) = parse ( & content) . unwrap ( ) ;
150+ let normalized = normalize ( & front_matter, slug, inside_rust) . unwrap_or_else ( |err| {
150151 panic ! ( "failed to normalize {:?}: {err}" , post. file_name( ) . unwrap( ) ) ;
151152 } ) ;
152153
153- if content != normalized {
154+ if front_matter != normalized {
155+ let normalized = format ! (
156+ "\
157+ +++\n \
158+ {}\
159+ +++\n \
160+ {rest}\
161+ ",
162+ toml:: to_string_pretty( & normalized) . unwrap( ) ,
163+ ) ;
154164 if env:: var ( "FIX_FRONT_MATTER" ) . is_ok ( ) {
155165 fs:: write ( post, normalized) . unwrap ( ) ;
156166 continue ;
0 commit comments