Parse the TOML frontmatter from Zola posts.
This commit is contained in:
parent
8d16e13355
commit
0eadfcf6cc
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue