Skip to content

Commit

Permalink
feat: allow multiple date formats in frontmatter published date
Browse files Browse the repository at this point in the history
  • Loading branch information
tglman committed May 13, 2023
1 parent 6c5ff33 commit c46581a
Showing 1 changed file with 32 additions and 1 deletion.
33 changes: 32 additions & 1 deletion crates/config/src/frontmatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ pub struct Frontmatter {
pub tags: Option<Vec<liquid_core::model::KString>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub excerpt_separator: Option<liquid_core::model::KString>,
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(
skip_serializing_if = "Option::is_none",
deserialize_with = "deserialize_date_time"
)]
pub published_date: Option<DateTime>,
#[serde(skip_serializing_if = "Option::is_none")]
pub format: Option<SourceFormat>,
Expand All @@ -46,6 +49,34 @@ pub struct Frontmatter {
pub collection: Option<liquid_core::model::KString>,
}

fn deserialize_date_time<'de, D>(deserializer: D) -> Result<Option<DateTime>, D::Error>
where
D: serde::Deserializer<'de>,
{
let s: std::borrow::Cow<'_, str> = serde::Deserialize::deserialize(deserializer)?;
if s == "now" || s == "today" {
// The date time parsing support now and today not needed in this context
Err(serde::de::Error::custom(format!(
"value '{}' not a valid date time format",
s
)))
} else {
let parsed = DateTime::from_str(&s);
if parsed.is_some() {
Ok(parsed)
} else {
if !s.trim().is_empty() {
Err(serde::de::Error::custom(format!(
"value '{}' not a valid date time format",
s
)))
} else {
Ok(None)
}
}
}
}

impl Frontmatter {
pub fn empty() -> Self {
Self::default()
Expand Down

0 comments on commit c46581a

Please sign in to comment.