Skip to content

Commit 3324c4f

Browse files
committed
Parse date from front matter instead of file name
1 parent 7ba45d6 commit 3324c4f

File tree

1 file changed

+9
-16
lines changed

1 file changed

+9
-16
lines changed

src/posts.rs

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,18 @@ use std::{
66
path::{Path, PathBuf},
77
sync::LazyLock,
88
};
9+
use toml::value::Date;
910

1011
#[derive(Debug, Clone, Serialize)]
1112
pub struct Post {
1213
pub(crate) filename: String,
1314
pub(crate) layout: String,
1415
pub(crate) title: String,
1516
pub(crate) author: String,
16-
pub(crate) year: i32,
17+
pub(crate) year: u16,
1718
pub(crate) show_year: bool,
18-
pub(crate) month: u32,
19-
pub(crate) day: u32,
19+
pub(crate) month: u8,
20+
pub(crate) day: u8,
2021
pub(crate) contents: String,
2122
pub(crate) url: String,
2223
pub(crate) published: String,
@@ -30,16 +31,7 @@ pub struct Post {
3031
impl Post {
3132
pub(crate) fn open(path: &Path, manifest: &Manifest) -> eyre::Result<Self> {
3233
// yeah this might blow up, but it won't
33-
let filename = path.file_name().unwrap().to_str().unwrap();
34-
35-
// we need to get the metadata out of the url
36-
let mut split = filename.splitn(4, '-');
37-
38-
// we do some unwraps because these need to be valid
39-
let year = split.next().unwrap().parse::<i32>().unwrap();
40-
let month = split.next().unwrap().parse::<u32>().unwrap();
41-
let day = split.next().unwrap().parse::<u32>().unwrap();
42-
let filename = split.next().unwrap().to_string();
34+
let filename = path.file_name().unwrap().to_str().unwrap().into();
4335

4436
let contents = std::fs::read_to_string(path)?;
4537

@@ -50,6 +42,7 @@ impl Post {
5042
release,
5143
team: team_string,
5244
layout,
45+
date: Date { year, month, day },
5346
..
5447
},
5548
contents,
@@ -69,7 +62,7 @@ impl Post {
6962
let contents = comrak::markdown_to_html(contents, &options);
7063

7164
// finally, the url.
72-
let mut url = PathBuf::from(&*filename);
65+
let mut url = PathBuf::from(&filename);
7366
url.set_extension("html");
7467

7568
// this is fine
@@ -139,8 +132,8 @@ impl Post {
139132
}
140133
}
141134

142-
fn build_post_time(year: i32, month: u32, day: u32, seconds: u32) -> String {
143-
let date = chrono::NaiveDate::from_ymd_opt(year, month, day).unwrap();
135+
fn build_post_time(year: u16, month: u8, day: u8, seconds: u32) -> String {
136+
let date = chrono::NaiveDate::from_ymd_opt(year.into(), month.into(), day.into()).unwrap();
144137
let date_time = date.and_hms_opt(0, 0, seconds).unwrap();
145138
chrono::DateTime::<chrono::Utc>::from_naive_utc_and_offset(date_time, chrono::Utc).to_rfc3339()
146139
}

0 commit comments

Comments
 (0)