@@ -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 ( ) {
@@ -104,20 +108,15 @@ pub fn normalize(markdown: &str, slug: &str, inside_rust: bool) -> eyre::Result<
104
108
slug = slug. split_once( '@' ) . map( |( s, _) | s) . unwrap_or( slug) ,
105
109
) ;
106
110
}
107
- front_matter. aliases = vec ! [ format!( "{}.html" , front_matter. path) ] ;
108
111
109
112
if front_matter. extra . team . is_some ( ) ^ front_matter. extra . team_url . is_some ( ) {
110
113
bail ! ( "extra.team and extra.team_url must always come in a pair" ) ;
111
114
}
112
115
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)
121
120
}
122
121
123
122
#[ cfg( test) ]
@@ -146,11 +145,21 @@ mod tests {
146
145
. contains ( "content/inside-rust/" ) ;
147
146
148
147
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| {
150
150
panic ! ( "failed to normalize {:?}: {err}" , post. file_name( ) . unwrap( ) ) ;
151
151
} ) ;
152
152
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
+ ) ;
154
163
if env:: var ( "FIX_FRONT_MATTER" ) . is_ok ( ) {
155
164
fs:: write ( post, normalized) . unwrap ( ) ;
156
165
continue ;
0 commit comments