Skip to content

Commit c46581a

Browse files
committed
feat: allow multiple date formats in frontmatter published date
1 parent 6c5ff33 commit c46581a

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

crates/config/src/frontmatter.rs

Lines changed: 32 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,34 @@ 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 {
68+
if !s.trim().is_empty() {
69+
Err(serde::de::Error::custom(format!(
70+
"value '{}' not a valid date time format",
71+
s
72+
)))
73+
} else {
74+
Ok(None)
75+
}
76+
}
77+
}
78+
}
79+
4980
impl Frontmatter {
5081
pub fn empty() -> Self {
5182
Self::default()

0 commit comments

Comments
 (0)