Astral plugins extend static site discovery, rendering, and build lifecycle behavior.
Configure plugins
site do
plugins [
MySite.SEOPlugin,
{MySite.AnalyticsPlugin, id: "G-XXXX"}
]
endPlugins are modules implementing Astral.Plugin. Tuple options are passed to callbacks that define an extra argument.
Render hook example
defmodule MySite.AnalyticsPlugin do
@behaviour Astral.Plugin
@impl true
def name, do: "analytics"
@impl true
def render_page(html, _page, _site, opts) do
id = Keyword.fetch!(opts, :id)
{:ok, String.replace(html, "</body>", ~s(<script data-id="#{id}"></script></body>))}
end
endGenerated route example
defmodule MySite.SearchIndex do
@behaviour Astral.Plugin
@impl true
def name, do: "search-index"
@impl true
def routes(site) do
[Astral.Route.new("/search.json", site.config, content_type: "application/json")]
end
@impl true
def render_route(%Astral.Route{path: "/search.json"}, site) do
entries = Map.get(site.entries, :posts, [])
{:ok, JSON.encode!(Enum.map(entries, &%{title: &1.data.title, url: &1.route_path}))}
end
def render_route(_route, _site), do: nil
endHooks
Available hooks include:
config/1build_start/1site_discovered/1routes/1render_route/2render_page/3build_done/1
Use enforce/0 to return :pre or :post when ordering matters.