Parse the TOML frontmatter from Zola posts.

This commit is contained in:
Jessica Canady 2024-05-23 16:01:11 -04:00
parent 8d16e13355
commit 0eadfcf6cc
Signed by: phoenix
SSH key fingerprint: SHA256:aaLOzOrLi+0n4eDZNQKH97PehwRt6KSE5fYJc+ZRKCQ
2 changed files with 16 additions and 1 deletions

View file

@ -4,6 +4,16 @@ defmodule JOL.Blog.Parser do
%{"attrs" => attrs, "body" => body} =
Regex.named_captures(~r/\+\+\+\n(?<attrs>.*)\n\+\+\+\n\n(?<body>.*)/s, content)
{attrs, String.trim(body)}
{:ok, toml_attrs} = Toml.decode(attrs)
parsed_attrs = %{
title: toml_attrs["title"],
draft: toml_attrs["draft"],
tags: toml_attrs["taxonomies"]["tags"]
}
parsed_body = String.trim(body)
{parsed_attrs, parsed_body}
end
end

View file

@ -23,4 +23,9 @@ defmodule JOL.Blog.ParserTest do
{_attrs, body} = Parser.parse("/fake/filename", post.content)
assert body == "Body!"
end
test "parses the attrs from zola-style posts", post do
{attrs, _body} = Parser.parse("filepath", post.content)
assert attrs == %{title: "test post", draft: false, tags: ["howto", "hardware"]}
end
end