FrontMatter (front_matter v1.0.1) View Source

Parse a file or string containing front matter and a document body. Front matter is a block of yaml wrapped between two lines containing ---.

In this example, the front matter contains title: Hello and tags: x, y, z, and the body is Hello, world:

---
title: Hello
tags: x, y, z
---
Hello, world

After parsing the document, front matter is returned as a map, and the body as a string.

FrontMatter.parse_file "example.md"
{:ok, %{"title" => "Hello", "tags" => ["x", "y", "z"]}, "Hello, world"}

Link to this section Summary

Functions

Parse a string and return it's front matter and body. Returns {:ok, matter, body} on success (matter is a map), or {:error, error} on error.

Parse a string and return it's front matter and body. Returns {matter, body} on success (matter is a map), throws on error.

Read a file, parse it's contents, and return it's front matter and body. Returns {:ok, matter, body} on success (matter is a map), or {:error, error} on error.

Read a file, parse it's contents, and return it's front matter and body. Returns {matter, body} on success (matter is a map), throws on error.

Link to this section Functions

Parse a string and return it's front matter and body. Returns {:ok, matter, body} on success (matter is a map), or {:error, error} on error.

iex> FrontMatter.parse "---\ntitle: Hello\n---\nHello, world"
{:ok, %{"title" => "Hello"}, "Hello, world"}
iex> FrontMatter.parse "---\ntitle: Hello\n--\nHello, world"
{:error, :invalid_front_matter}

Parse a string and return it's front matter and body. Returns {matter, body} on success (matter is a map), throws on error.

iex> FrontMatter.parse! "---\ntitle: Hello\n---\nHello, world"
{%{"title" => "Hello"}, "Hello, world"}
iex> try do
...>   FrontMatter.parse! "---\ntitle: Hello\n--\nHello, world"
...> rescue
...>   e in FrontMatter.Error -> e.message
...> end
"Error parsing yaml front matter"

Read a file, parse it's contents, and return it's front matter and body. Returns {:ok, matter, body} on success (matter is a map), or {:error, error} on error.

iex> FrontMatter.parse_file "test/fixtures/dumb.md"
{:ok, %{"title" => "Hello", "tags" => ["x", "y", "z"]}, "Hello, world\n"}
iex> FrontMatter.parse_file "test/fixtures/idontexist.md"
{:error, :enoent}

Read a file, parse it's contents, and return it's front matter and body. Returns {matter, body} on success (matter is a map), throws on error.

iex> FrontMatter.parse_file! "test/fixtures/dumb.md"
{%{"title" => "Hello", "tags" => ["x", "y", "z"]}, "Hello, world\n"}
iex> try do
...>   FrontMatter.parse_file! "test/fixtures/idontexist.md"
...> rescue
...>   e in FrontMatter.Error -> e.message
...> end
"File not found"
iex> try do
...>   FrontMatter.parse_file! "test/fixtures/invalid.md"
...> rescue
...>   e in FrontMatter.Error -> e.message
...> end
"Error parsing yaml front matter"