Skip to content

Commit dc5b9d6

Browse files
committed
feat: Allow multiple date formats in frontmatter
1 parent 6c5ff33 commit dc5b9d6

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

crates/config/src/frontmatter.rs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@ pub struct Frontmatter {
2424
pub tags: Option<Vec<liquid_core::model::KString>>,
2525
#[serde(skip_serializing_if = "Option::is_none")]
2626
pub excerpt_separator: Option<liquid_core::model::KString>,
27-
#[serde(skip_serializing_if = "Option::is_none")]
27+
#[serde(
28+
skip_serializing_if = "Option::is_none",
29+
deserialize_with = "deserialize_date_time"
30+
)]
2831
pub published_date: Option<DateTime>,
2932
#[serde(skip_serializing_if = "Option::is_none")]
3033
pub format: Option<SourceFormat>,
@@ -46,6 +49,32 @@ pub struct Frontmatter {
4649
pub collection: Option<liquid_core::model::KString>,
4750
}
4851

52+
fn deserialize_date_time<'de, D>(deserializer: D) -> Result<Option<DateTime>, D::Error>
53+
where
54+
D: serde::Deserializer<'de>,
55+
{
56+
let s: std::borrow::Cow<'_, str> = serde::Deserialize::deserialize(deserializer)?;
57+
if s == "now" || s == "today" {
58+
// The date time parsing support now and today not needed in this context
59+
Err(serde::de::Error::custom(format!(
60+
"value '{}' not a valid date time format",
61+
s
62+
)))
63+
} else {
64+
let parsed = DateTime::from_str(&s);
65+
if parsed.is_some() {
66+
Ok(parsed)
67+
} else if !s.trim().is_empty() {
68+
Err(serde::de::Error::custom(format!(
69+
"value '{}' not a valid date time format",
70+
s
71+
)))
72+
} else {
73+
Ok(None)
74+
}
75+
}
76+
}
77+
4978
impl Frontmatter {
5079
pub fn empty() -> Self {
5180
Self::default()

0 commit comments

Comments
 (0)