sloane.sh/lib/sloane_sh/markdown.ex

44 lines
887 B
Elixir
Raw Normal View History

2024-02-16 22:28:13 -05:00
defmodule SloaneSH.Markdown do
@moduledoc """
Markdown parsing using `Earmark` and `Earmark.Parser`
"""
require Logger
use TypedStruct
2024-02-16 22:28:13 -05:00
alias SloaneSH.Context
2024-02-24 08:43:08 -05:00
alias SloaneSH.FrontMatter
alias __MODULE__
2024-02-16 22:28:13 -05:00
typedstruct do
field :attrs, map(), default: %{}
field :html, String.t(), default: ""
end
def transform(%Context{} = ctx, data) when is_binary(data) do
data
2024-02-24 08:43:08 -05:00
|> FrontMatter.parse(ctx)
|> parse_markdown(ctx)
end
2024-02-16 22:28:13 -05:00
defp parse_markdown({:ok, attrs, body}, _ctx) do
with {:ok, html, msgs} <- Earmark.as_html(body) do
for msg <- msgs, do: Logger.warning(msg)
{:ok,
%Markdown{
attrs: attrs,
html: html
}}
else
{:error, _, msgs} ->
for msg <- msgs, do: Logger.error(msg)
:error
_ ->
:error
end
end
defp parse_markdown(other, _ctx), do: other
2024-02-16 22:28:13 -05:00
end