Skip to content

Improve error message for bad front matter #1520

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

Merged
merged 1 commit into from
Mar 11, 2025
Merged
Show file tree
Hide file tree
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
10 changes: 6 additions & 4 deletions front_matter/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use eyre::bail;
use eyre::{ContextCompat, bail};
use serde::{Deserialize, Serialize};
use toml::value::Date;

Expand All @@ -21,12 +21,12 @@ pub struct FrontMatter {
/// the tuple.
pub fn parse(markdown: &str) -> eyre::Result<(FrontMatter, &str)> {
if !markdown.starts_with("+++\n") {
bail!("markdown file must start with the line `+++`");
bail!("missing start of TOML front matter (+++)");
}
let (front_matter, content) = markdown
.trim_start_matches("+++\n")
.split_once("\n+++\n")
.expect("couldn't find the end of the front matter: `+++`");
.context("missing end of TOML front matter (+++)")?;

Ok((toml::from_str(front_matter)?, content))
}
Expand Down Expand Up @@ -63,7 +63,9 @@ mod tests {

for post in posts {
let content = fs::read_to_string(&post).unwrap();
let normalized = normalize(&content).unwrap();
let normalized = normalize(&content).unwrap_or_else(|err| {
panic!("failed to normalize {:?}: {err}", post.file_name().unwrap());
});

if content != normalized {
if env::var("FIX_FRONT_MATTER").is_ok() {
Expand Down
4 changes: 3 additions & 1 deletion src/posts.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::blogs::Manifest;
use eyre::Context;
use front_matter::FrontMatter;
use regex::Regex;
use serde::Serialize;
Expand Down Expand Up @@ -56,7 +57,8 @@ impl Post {
..
},
contents,
) = front_matter::parse(&contents)?;
) = front_matter::parse(&contents)
.with_context(|| format!("failed to parse {filename}"))?;

let options = comrak::Options {
render: comrak::RenderOptions::builder().unsafe_(true).build(),
Expand Down