@@ -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 ( ) {
@@ -104,20 +108,15 @@ pub fn normalize(markdown: &str, slug: &str, inside_rust: bool) -> eyre::Result<
104108 slug = slug. split_once( '@' ) . map( |( s, _) | s) . unwrap_or( slug) ,
105109 ) ;
106110 }
107- front_matter. aliases = vec ! [ format!( "{}.html" , front_matter. path) ] ;
108111
109112 if front_matter. extra . team . is_some ( ) ^ front_matter. extra . team_url . is_some ( ) {
110113 bail ! ( "extra.team and extra.team_url must always come in a pair" ) ;
111114 }
112115
113- Ok ( format ! (
114- "\
115- +++
116- {}\
117- +++
118- {content}" ,
119- toml:: to_string_pretty( & front_matter) ?
120- ) )
116+ let serialized = toml:: to_string_pretty ( & front_matter) ?;
117+ let deserialized = toml:: from_str ( & serialized) ?;
118+
119+ Ok ( deserialized)
121120}
122121
123122#[ cfg( test) ]
@@ -146,11 +145,21 @@ mod tests {
146145 . contains ( "content/inside-rust/" ) ;
147146
148147 let content = fs:: read_to_string ( & post) . unwrap ( ) ;
149- let normalized = normalize ( & content, slug, inside_rust) . unwrap_or_else ( |err| {
148+ let ( front_matter, rest) = parse ( & content) . unwrap ( ) ;
149+ let normalized = normalize ( & front_matter, slug, inside_rust) . unwrap_or_else ( |err| {
150150 panic ! ( "failed to normalize {:?}: {err}" , post. file_name( ) . unwrap( ) ) ;
151151 } ) ;
152152
153- if content != normalized {
153+ if front_matter != normalized {
154+ let normalized = format ! (
155+ "\
156+ +++\n \
157+ {}\
158+ +++\n \
159+ {rest}\
160+ ",
161+ toml:: to_string_pretty( & normalized) . unwrap( ) ,
162+ ) ;
154163 if env:: var ( "FIX_FRONT_MATTER" ) . is_ok ( ) {
155164 fs:: write ( post, normalized) . unwrap ( ) ;
156165 continue ;
0 commit comments