Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow multiple date formats in frontmatter published date #1134

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 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,32 @@ 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
Loading