Tableau.Extension behaviour (tableau v0.26.0)

View Source

A tableau extension.

An extension can be used to generate other kinds of content.

Options

  • :key - The key in which the extensions configuration and data is loaded.
  • :priority - An integer used for ordering extensions of the same type.
  • :enabled - Whether or not to enable the extension. Defaults to true, and can be configured differently based on the extension.

Callbacks

There are currently the following extension callbacks:

  • :pre_build - executed before tableau builds your site and writes anything to disk.
  • :pre_write - executed after tableau builds your site but before it writes anything to disk.
  • :post_write - executed after tableau builds your site and writes everything to disk.

The Graph

Tableau pages and layouts form a DAG, a Directed Acyclic Graph, and this graph is used to build each page when writing to disk.

Your extension can create data to insert into the site token and can also inserted pages into the graph to be written to disk when the time comes.

Example

In this example, we create a simple post extension that reads markdown files from disk, inserts them into the graph, and inserts them into the token.

By inserting them into the token, you are able to access them inside your templates, for example, a posts listing page.

defmodule MySite.PostsExtension do
  use Tableau.Extension, key: :posts, priority: 300

  @impl Tableau.Extension
  def pre_build(token) do
    posts = 
      for post <- Path.wildcard("_posts/**/*.md") do
        %Tableau.Page{
          parent: MySite.RootLayout,
          permalink: Path.join("posts", Path.rootname(post)),
          template: Markdown.render(post),
          opts: %{}
        }
      end

    graph = Tableau.Graph.insert(token.graph, posts)

    {:ok,
     token
     |> Map.put(:posts, posts)
     |> Map.put(:graph, graph)}
  end
end

Summary

Callbacks

Optional callback to validate the config for an extension. Useful for providing more useful error messages for misconfigurations.

Called in the post_write phase.

Called in the pre_build phase.

Called in the pre_render phase.

Called in the pre_write phase.

Types

token()

@type token() :: map()

Callbacks

config(arg1)

(optional)
@callback config(Keyword.t() | map()) :: {:ok, map()} | {:error, any()}

Optional callback to validate the config for an extension. Useful for providing more useful error messages for misconfigurations.

post_write(token)

(optional)
@callback post_write(token()) :: {:ok, token()} | :error

Called in the post_write phase.

The function is passed a token and can return a new token with new data loaded into it.

pre_build(token)

(optional)
@callback pre_build(token()) :: {:ok, token()} | :error

Called in the pre_build phase.

The function is passed a token and can return a new token with new data loaded into it.

pre_render(token)

(optional)
@callback pre_render(token()) :: {:ok, token()} | :error

Called in the pre_render phase.

The function is passed a token and can return a new token with new data loaded into it.

pre_write(token)

(optional)
@callback pre_write(token()) :: {:ok, token()} | :error

Called in the pre_write phase.

The function is passed a token and can return a new token with new data loaded into it.