Business logic for the Micropub endpoint.
Handles the three directions the protocol needs to flow:
properties_from_params/1— parse an incoming create request into a normalized map of post properties.create/1— turn normalized properties into a markdown file on disk.update/3anddelete/1— mutate an existing post addressed by its URL.source/2— serialise a post back into MF2 JSON for editing clients.
Summary
Functions
Create a new post from a normalized properties/0 map.
Delete the post at the given canonical URL.
Normalize an incoming create request into a properties/0 map.
Serialize post attributes and body back to the %{...}---body file format.
Extract the blog post slug from a canonical post URL.
Convert a title into a URL-safe slug.
Return the MF2 JSON representation of a post for a Micropub source query.
Update an existing post, identified by its canonical URL.
Build the canonical URL for a post slug using the configured base URL.
Types
@type properties() :: %{ optional(:title) => String.t(), optional(:content) => String.t(), optional(:tags) => [String.t()], optional(:date) => Date.t(), optional(:slug) => String.t(), optional(:author) => String.t(), optional(:description) => String.t(), optional(:status) => StaticBlog.Post.status() }
Functions
@spec create(properties()) :: {:ok, String.t()} | {:error, atom()}
Create a new post from a normalized properties/0 map.
Arguments
propertiesis the normalized property map fromproperties_from_params/1.
Returns
{:ok, url}whereurlis the canonical URL on the configured site.{:error, reason}if required fields are missing.
@spec delete(String.t()) :: :ok | {:error, :not_found | :invalid_url}
Delete the post at the given canonical URL.
Arguments
urlis the canonical URL of the post to remove.
Returns
:okon success.{:error, :not_found}if no post exists at that URL.
@spec properties_from_params(map()) :: properties()
Normalize an incoming create request into a properties/0 map.
Handles both JSON-encoded Micropub payloads (where properties live under a
"properties" key and values are arrays) and form-encoded payloads (where
values are scalars or repeated keys).
Arguments
paramsis the decoded request body as a map.
Returns
- A normalized
properties/0map.
Serialize post attributes and body back to the %{...}---body file format.
Arguments
attrsis a map with:title,:author,:tags, and:description.bodyis the markdown body string.
Returns
- A binary ready to be written to disk.
Extract the blog post slug from a canonical post URL.
Arguments
urlis a URL of the form<base>/posts/<slug>/(trailing slash optional).
Returns
{:ok, slug}if the URL matches the expected shape.{:error, :invalid_url}otherwise.
Convert a title into a URL-safe slug.
Arguments
titleis the post title.
Returns
- A lowercase, dash-separated slug string.
Return the MF2 JSON representation of a post for a Micropub source query.
Arguments
urlis the canonical URL of the post to fetch.requested_propertiesis an optional list of property names to include.
Returns
{:ok, mf2}on success wheremf2is a map ready to be encoded as JSON.{:error, :not_found}if no post exists at that URL.
Update an existing post, identified by its canonical URL.
Arguments
urlis the canonical URL of the post to edit.replaceis a map of property -> replacement values.addis a map of property -> values to append to the existing list.delete_propertiesis either a list of property names to clear entirely, or a map of property -> values to remove from the existing list.
Returns
{:ok, url}on success.{:error, :not_found}if no post exists at that URL.
Build the canonical URL for a post slug using the configured base URL.
Arguments
slugis the post slug.
Returns
- The absolute URL string.