@@ -3,7 +3,7 @@ use serde::{Deserialize, Serialize};
3
3
use toml:: value:: Date ;
4
4
5
5
/// The front matter of a markdown blog post.
6
- #[ derive( Debug , PartialEq , Serialize , Deserialize ) ]
6
+ #[ derive( Debug , Clone , PartialEq , Serialize , Deserialize ) ]
7
7
pub struct FrontMatter {
8
8
/// Deprecated. The plan was probably to have more specialized templates
9
9
/// at some point. That didn't materialize, all posts are rendered with the
@@ -42,7 +42,7 @@ pub struct FrontMatter {
42
42
pub extra : Extra ,
43
43
}
44
44
45
- #[ derive( Debug , Default , PartialEq , Serialize , Deserialize ) ]
45
+ #[ derive( Debug , Clone , Default , PartialEq , Serialize , Deserialize ) ]
46
46
pub struct Extra {
47
47
pub team : Option < String > ,
48
48
pub team_url : Option < String > ,
@@ -73,8 +73,12 @@ pub fn parse(markdown: &str) -> eyre::Result<(FrontMatter, &str)> {
73
73
}
74
74
75
75
/// 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 ( ) ;
78
82
79
83
// migrate "author" to "authors" key
80
84
if let Some ( author) = front_matter. author . take ( ) {
@@ -110,14 +114,10 @@ pub fn normalize(markdown: &str, slug: &str, inside_rust: bool) -> eyre::Result<
110
114
bail ! ( "extra.team and extra.team_url must always come in a pair" ) ;
111
115
}
112
116
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)
121
121
}
122
122
123
123
#[ cfg( test) ]
@@ -146,11 +146,21 @@ mod tests {
146
146
. contains ( "content/inside-rust/" ) ;
147
147
148
148
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| {
150
151
panic ! ( "failed to normalize {:?}: {err}" , post. file_name( ) . unwrap( ) ) ;
151
152
} ) ;
152
153
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
+ ) ;
154
164
if env:: var ( "FIX_FRONT_MATTER" ) . is_ok ( ) {
155
165
fs:: write ( post, normalized) . unwrap ( ) ;
156
166
continue ;
0 commit comments