Skip to content

Commit 224e50f

Browse files
committed
s/path/permalink, add docs
1 parent 0e4a56d commit 224e50f

File tree

1 file changed

+33
-12
lines changed

1 file changed

+33
-12
lines changed

lib/tableau/extensions/post_extension.ex

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,18 @@
11
defmodule Tableau.PostExtension.Config do
2+
@moduledoc """
3+
Configuration for PostExtension.
4+
5+
## Options
6+
- `:enabled` - boolean - Extension is active or not.
7+
- `:dir` - string - Directory to scan for markdown files. Defaults to `_posts`
8+
- `:future` - boolean - Show posts that have dates later than the current timestamp, or time at which the site is generated.
9+
- `:permalink` - string - Default output path for posts. Accepts `:slug` as a replacement keyword, replaced with the post's provided slug.
10+
- `layout` - string - Elixir module providing page layout for posts. Default is nil
11+
"""
12+
213
import Schematic
314

4-
defstruct enabled: true, dir: "_posts", future: false, path: "/posts/:slug", layout: nil
15+
defstruct enabled: true, dir: "_posts", future: false, permalink: "/posts/:slug", layout: nil
516

617
def new(input), do: unify(schematic(), input)
718

@@ -11,8 +22,8 @@ defmodule Tableau.PostExtension.Config do
1122
%{
1223
optional(:enabled) => bool(),
1324
optional(:dir) => str(),
14-
optional(:path) => str(),
1525
optional(:future) => bool(),
26+
optional(:permalink) => str(),
1627
optional(:layout) => str()
1728
},
1829
convert: false
@@ -49,24 +60,24 @@ defmodule Tableau.PostExtension.Posts.Post do
4960

5061
defp build_permalink(%{permalink: permalink} = attrs, _config) do
5162
permalink
52-
|> transform_path(attrs)
63+
|> transform_permalink(attrs)
5364
|> then(&Map.put(attrs, :permalink, &1))
5465
end
5566

56-
defp build_permalink(%{slug: slug} = attrs, %{path: path}) do
67+
defp build_permalink(%{slug: slug} = attrs, %{permalink: permalink}) do
5768
slug
58-
|> transform_path(attrs)
59-
|> then(&Map.put(attrs, :permalink, String.replace(path, :slug, &1)))
69+
|> transform_permalink(attrs)
70+
|> then(&Map.put(attrs, :permalink, String.replace(permalink, :slug, &1)))
6071
end
6172

62-
defp build_permalink(%{file: filename} = attrs, %{path: path}) do
73+
defp build_permalink(%{file: filename} = attrs, %{permalink: permalink}) do
6374
filename
6475
|> Path.basename(".md")
65-
|> transform_path(attrs)
66-
|> then(&Map.put(attrs, :permalink, String.replace(path, ":slug", &1)))
76+
|> transform_permalink(attrs)
77+
|> then(&Map.put(attrs, :permalink, String.replace(permalink, ":slug", &1)))
6778
end
6879

69-
defp transform_path(path, attrs) do
80+
defp transform_permalink(path, attrs) do
7081
path
7182
|> String.replace(":title", attrs.title)
7283
|> String.replace(" ", "-")
@@ -87,8 +98,8 @@ defmodule Tableau.PostExtension do
8798
8899
* `:id` - An Elixir module to be used when compiling the backing `Tableau.Page`
89100
* `:title` - The title of the post
90-
* `:permalink` - The permalink of the post. `:title` will be replaced with the posts title and non alphanumeric characters removed
91-
* `:slug` - A URL slug of the post. Only used if `:permalink` is unset
101+
* `:permalink` - The permalink of the post. `:title` will be replaced with the posts title and non alphanumeric characters removed. Optional.
102+
* `:slug` - A URL slug of the post. Only used if `:permalink` is unset. `:title` will be replaced with the posts title. Optional
92103
* `:date` - An Elixir `NaiveDateTime`, often presented as a `sigil_N`
93104
* `:layout` - A Tableau layout module.
94105
@@ -103,6 +114,16 @@ defmodule Tableau.PostExtension do
103114
layout: "ElixirTools.PostLayout"
104115
---
105116
```
117+
118+
## URL generation
119+
120+
If a `:permalink` is specified in the front matter, whatever is there _will_ be the post's permalink, regardless of presence of `:slug`
121+
122+
If `:slug` is specified, the globally configured permalink will be used, with the slug provided as a replacement string. See `Tableau.PostExtension.Config` for details.
123+
124+
If neither `:permalink` nor `:slug` is specified, the files name will be used as the slug.
125+
126+
In all cases, permalinks are stripped of non-alphanumeric characters.
106127
"""
107128

108129
{:ok, config} =

0 commit comments

Comments
 (0)