From c916ea6d702310d7b94ae2da3461b2b1c078ba18 Mon Sep 17 00:00:00 2001 From: Sky Date: Wed, 22 Nov 2023 14:37:32 -0500 Subject: [PATCH] better empty fm check --- src/md.rs | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/md.rs b/src/md.rs index cb89d0e..9cdf183 100644 --- a/src/md.rs +++ b/src/md.rs @@ -101,13 +101,17 @@ pub fn take_front_matter(content: &str) -> Result<(FrontMatter, &str)> { } pub fn prepend_front_matter(fm: &FrontMatter, content: &str) -> String { - let fm_yaml = serde_yaml::to_string(fm).expect("ser error????"); + let Ok(serde_yaml::Value::Mapping(map)) = serde_yaml::to_value(fm) else { + panic!("ser error/fm not a map???"); + }; - // FIXME use a better check - if fm_yaml.trim() == "{}" { - // dont put an empty fm block + if map.is_empty() { + // skip empty fm blocks content.to_string() } else { - format!("---\n{fm_yaml}---\n{content}") + format!( + "---\n{}---\n{content}", + serde_yaml::to_string(&map).unwrap() + ) } }