Skip to content

Commit 0e1115b

Browse files
authored
feat(page): support sourcing from multiple directories (#134)
1 parent 4220c6e commit 0e1115b

File tree

2 files changed

+73
-4
lines changed

2 files changed

+73
-4
lines changed

lib/tableau/extensions/page_extension.ex

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ defmodule Tableau.PageExtension do
3232
## Configuration
3333
3434
- `:enabled` - boolean - Extension is active or not.
35-
- `:dir` - string - Directory to scan for markdown files. Defaults to `_pages`
35+
- `:dir` - string or list of strings - Directories to scan for markdown files. Defaults to `_pages`
3636
- `:permalink` - string - Default output path for pages. Accepts `:title` as a replacement keyword, replaced with the page's provided title. If a page has a `:permalink` provided, that will override this value _for that page_.
3737
- `:layout` - string - Elixir module providing page layout for pages. Default is nil.
3838
@@ -75,7 +75,7 @@ defmodule Tableau.PageExtension do
7575
unify(
7676
map(%{
7777
optional(:enabled, true) => bool(),
78-
optional(:dir, "_pages") => str(),
78+
optional(:dir, "_pages") => oneof([list(str()), str()]),
7979
optional(:permalink) => str(),
8080
optional(:layout) => oneof([str(), atom()])
8181
}),
@@ -90,8 +90,12 @@ defmodule Tableau.PageExtension do
9090

9191
pages =
9292
config.dir
93-
|> Path.join("**/*.{#{exts}}")
94-
|> Common.paths()
93+
|> List.wrap()
94+
|> Enum.flat_map(fn path ->
95+
path
96+
|> Path.join("**/*.{#{exts}}")
97+
|> Common.paths()
98+
end)
9599
|> Common.entries(fn %{path: path, front_matter: front_matter, pre_convert_body: body, ext: ext} ->
96100
{
97101
build(path, front_matter, body, config),

test/tableau/extensions/page_extension_test.exs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,71 @@ defmodule Tableau.PageExtensionTest do
102102
assert Enum.any?(vertices, fn v -> v == Blog.PageLayout end)
103103
end
104104

105+
test "can read pages from multiple directories", %{tmp_dir: dir, token: token} do
106+
dir_1 = Path.join(dir, "_pages")
107+
File.mkdir_p(dir_1)
108+
109+
File.write(Path.join(dir_1, "page-1.md"), """
110+
---
111+
layout: Blog.PageLayout
112+
title: Page One
113+
categories: page
114+
permalink: /page/gibberish/page-1/
115+
---
116+
117+
I am a real boy.
118+
""")
119+
120+
dir_2 = Path.join(dir, "_drafts")
121+
File.mkdir_p(dir_2)
122+
123+
File.write(Path.join(dir_2, "page-2.md"), """
124+
---
125+
layout: Blog.PageLayout
126+
title: Page Two
127+
categories: page
128+
permalink: /page/gibberish/page-2/
129+
---
130+
131+
The answer is not 42.
132+
""")
133+
134+
assert {:ok, config} = PageExtension.config(%{dir: [dir_1, dir_2], enabled: true})
135+
136+
token = put_in(token.extensions.pages.config, config)
137+
138+
assert {:ok, token} = PageExtension.run(token)
139+
140+
assert %{
141+
pages: [
142+
%{
143+
file: ^dir_1 <> "/page-1.md",
144+
title: "Page One",
145+
body: "\nI am a real boy.\n",
146+
layout: Blog.PageLayout,
147+
__tableau_page_extension__: true,
148+
permalink: "/page/gibberish/page-1/",
149+
categories: "page"
150+
} = page_1,
151+
%{
152+
file: ^dir_2 <> "/page-2.md",
153+
title: "Page Two",
154+
body: "\nThe answer is not 42.\n",
155+
layout: Blog.PageLayout,
156+
__tableau_page_extension__: true,
157+
permalink: "/page/gibberish/page-2/",
158+
categories: "page"
159+
} = page_2
160+
],
161+
graph: graph
162+
} = token
163+
164+
vertices = Graph.vertices(graph)
165+
166+
assert Enum.any?(vertices, fn v -> is_struct(v, Tableau.Page) and v.permalink == page_1.permalink end)
167+
assert Enum.any?(vertices, fn v -> is_struct(v, Tableau.Page) and v.permalink == page_2.permalink end)
168+
end
169+
105170
test "configured permalink works when you dont specify one", %{tmp_dir: dir, token: token} do
106171
File.write(Path.join(dir, "my-future-page.md"), """
107172
---

0 commit comments

Comments
 (0)