|
| 1 | +defmodule Tableau.TagExtension do |
| 2 | + @moduledoc ~S''' |
| 3 | + Creates pages for tags found in posts created by the `Tableau.PostExtension`. |
| 4 | +
|
| 5 | + The `:tags` key provided on every page in the assigns is described by `t:tags/0`. |
| 6 | +
|
| 7 | + The `@page` assign passed to the `layout` provided in the configuration is described by `t:page/0`. |
| 8 | +
|
| 9 | + ## Configuration |
| 10 | +
|
| 11 | + - `:enabled` - boolean - Extension is active or not. |
| 12 | + * `:layout` - module - The `Tableau.Layout` implementation to use. |
| 13 | + * `:permalink` - string - The permalink prefix to use for the tag page, will be joined with the tag name. |
| 14 | +
|
| 15 | +
|
| 16 | + ## Layout and Page |
| 17 | +
|
| 18 | + To take advantage of tag extension, you'll need to define a layout that will render each "tag page" and a normal `Tableau.Page` that lists all tags on your site. |
| 19 | +
|
| 20 | + ### Layout to render a tag page |
| 21 | +
|
| 22 | + ```elixir |
| 23 | + defmodule MySite.TagLayout do |
| 24 | + use Tableau.Layout, layout: MySite.RootLayout |
| 25 | +
|
| 26 | + def template(assigns) do |
| 27 | + ~H""" |
| 28 | + <div> |
| 29 | + <h1>Tag: #{@page.tag}</h1> |
| 30 | +
|
| 31 | + <ul> |
| 32 | + <li :for={post <- @page.posts}> |
| 33 | + <a href={post.permalink}> {post.title}</a> |
| 34 | + </li> |
| 35 | + </ul> |
| 36 | + </div> |
| 37 | + """ |
| 38 | + end |
| 39 | + end |
| 40 | + ``` |
| 41 | +
|
| 42 | + ### Page to render all tags |
| 43 | +
|
| 44 | + This example page shows listing all takes, sorting them by the number of posts for each tag. |
| 45 | +
|
| 46 | + ```elixir |
| 47 | + defmodule MySite.TagPage do |
| 48 | + use Tableau.Page, |
| 49 | + layout: MySite.RootLayout, |
| 50 | + permalink: "/tags", |
| 51 | + title: "Tags" |
| 52 | +
|
| 53 | +
|
| 54 | + def template(assigns) do |
| 55 | + sorted_tags = Enum.sort_by(assigns.tags, fn {_, p} -> length(p) end, :desc) |
| 56 | + assigns = Map.put(assigns, :tags, sorted_tags) |
| 57 | +
|
| 58 | + ~H""" |
| 59 | + <div> |
| 60 | + <h1>Tags</h1> |
| 61 | +
|
| 62 | + <ul> |
| 63 | + <li :for={{tag, posts} <- @tags}> |
| 64 | + <a href={tag.permalink}>tag.tag</a> |
| 65 | +
|
| 66 | + <span>- {length(posts)}</span> |
| 67 | + </li> |
| 68 | + </ul> |
| 69 | + </div> |
| 70 | + """ |
| 71 | + end |
| 72 | + end |
| 73 | + ``` |
| 74 | + ''' |
| 75 | + use Tableau.Extension, key: :tag, priority: 200, type: :pre_build |
| 76 | + |
| 77 | + import Schematic |
| 78 | + |
| 79 | + @type page :: %{ |
| 80 | + title: String.t(), |
| 81 | + tag: String.t(), |
| 82 | + permalink: String.t(), |
| 83 | + posts: [Tableau.PostExtension.post()] |
| 84 | + } |
| 85 | + |
| 86 | + @type tag :: %{ |
| 87 | + title: String.t(), |
| 88 | + tag: String.t(), |
| 89 | + permalink: String.t() |
| 90 | + } |
| 91 | + |
| 92 | + @type tags :: %{ |
| 93 | + tag() => [Tableau.PostExtension.post()] |
| 94 | + } |
| 95 | + |
| 96 | + @impl Tableau.Extension |
| 97 | + def config(config) do |
| 98 | + unify( |
| 99 | + oneof([ |
| 100 | + map(%{enabled: false}), |
| 101 | + map(%{ |
| 102 | + enabled: true, |
| 103 | + layout: atom(), |
| 104 | + permalink: str() |
| 105 | + }) |
| 106 | + ]), |
| 107 | + config |
| 108 | + ) |
| 109 | + end |
| 110 | + |
| 111 | + @impl Tableau.Extension |
| 112 | + def run(token) do |
| 113 | + posts = token.posts |
| 114 | + layout = token.extensions.tag.config.layout |
| 115 | + permalink = token.extensions.tag.config.permalink |
| 116 | + |
| 117 | + tags = |
| 118 | + for post <- posts, tag <- post |> Map.get(:tags, []) |> Enum.uniq(), reduce: Map.new() do |
| 119 | + acc -> |
| 120 | + permalink = Path.join(permalink, tag) |
| 121 | + |
| 122 | + tag = %{title: tag, permalink: permalink, tag: tag} |
| 123 | + Map.update(acc, tag, [post], &[post | &1]) |
| 124 | + end |
| 125 | + |
| 126 | + graph = |
| 127 | + Tableau.Graph.insert( |
| 128 | + token.graph, |
| 129 | + for {tag, posts} <- tags do |
| 130 | + posts = Enum.sort_by(posts, & &1.date, {:desc, DateTime}) |
| 131 | + |
| 132 | + opts = Map.put(tag, :posts, posts) |
| 133 | + |
| 134 | + %Tableau.Page{ |
| 135 | + parent: layout, |
| 136 | + permalink: tag.permalink, |
| 137 | + template: fn _ -> "" end, |
| 138 | + opts: opts |
| 139 | + } |
| 140 | + end |
| 141 | + ) |
| 142 | + |
| 143 | + {:ok, token |> Map.put(:graph, graph) |> Map.put(:tags, tags)} |
| 144 | + end |
| 145 | +end |
0 commit comments