Skip to content

Commit c128593

Browse files
authored
fix: allow atom layout module names (#108)
String values for layout module names are required to be supported in the front matter, but it feels odd to require that they be strings in config. This enables the specification of layout modules as atoms and fixes a bug in PageExtension which required a layout value in the front matter at all times.
1 parent b8ddba3 commit c128593

File tree

4 files changed

+61
-5
lines changed

4 files changed

+61
-5
lines changed

lib/tableau/extensions/page_extension.ex

+2-2
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ defmodule Tableau.PageExtension do
7373
optional(:enabled, true) => bool(),
7474
optional(:dir, "_pages") => str(),
7575
optional(:permalink) => str(),
76-
optional(:layout) => str()
76+
optional(:layout) => oneof([str(), atom()])
7777
}),
7878
input
7979
)
@@ -113,7 +113,7 @@ defmodule Tableau.PageExtension do
113113
|> Map.put(:__tableau_page_extension__, true)
114114
|> Map.put(:body, body)
115115
|> Map.put(:file, filename)
116-
|> Map.put(:layout, Module.concat([front_matter.layout || pages_config.layout]))
116+
|> Map.put(:layout, Module.concat([front_matter[:layout] || pages_config.layout]))
117117
|> Common.build_permalink(pages_config)
118118
end
119119
end

lib/tableau/extensions/post_extension.ex

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ defmodule Tableau.PostExtension do
8181
optional(:dir, "_posts") => str(),
8282
optional(:future, false) => bool(),
8383
optional(:permalink) => str(),
84-
optional(:layout) => str()
84+
optional(:layout) => oneof([atom(), str()])
8585
}),
8686
config
8787
)

test/tableau/extensions/page_extension_test.exs

+29-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ defmodule Tableau.PageExtensionTest do
1313

1414
describe "run" do
1515
setup %{tmp_dir: dir} do
16-
assert {:ok, config} = PageExtension.config(%{dir: dir, enabled: true})
16+
assert {:ok, config} = PageExtension.config(%{dir: dir, enabled: true, layout: Blog.DefaultPageLayout})
1717

1818
token = %{
1919
site: %{config: %{converters: [md: Tableau.MDExConverter]}},
@@ -200,5 +200,33 @@ defmodule Tableau.PageExtensionTest do
200200
]
201201
} = token
202202
end
203+
204+
test "inherits layout from page extension config", %{tmp_dir: dir, token: token} do
205+
File.write(Path.join(dir, "a-page.md"), """
206+
---
207+
title: missing layout key
208+
type: articles
209+
permalink: /:type/:title
210+
---
211+
212+
A great page
213+
""")
214+
215+
assert {:ok, token} = PageExtension.run(token)
216+
217+
assert %{
218+
pages: [
219+
%{
220+
__tableau_page_extension__: true,
221+
body: "\nA great page\n",
222+
file: ^dir <> "/a-page.md",
223+
layout: Blog.DefaultPageLayout,
224+
permalink: "/articles/missing-layout-key",
225+
title: "missing layout key",
226+
type: "articles"
227+
}
228+
]
229+
} = token
230+
end
203231
end
204232
end

test/tableau/extensions/post_extension_test.exs

+29-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ defmodule Tableau.PostExtensionTest do
1313

1414
describe "run" do
1515
setup %{tmp_dir: dir} do
16-
assert {:ok, config} = PostExtension.config(%{dir: dir, enabled: true})
16+
assert {:ok, config} = PostExtension.config(%{dir: dir, enabled: true, layout: Blog.DefaultPostLayout})
1717

1818
token = %{
1919
site: %{config: %{converters: [md: Tableau.MDExConverter]}},
@@ -233,6 +233,34 @@ defmodule Tableau.PostExtensionTest do
233233
} = token
234234
end
235235

236+
test "inherits layout from post extension config", %{tmp_dir: dir, token: token} do
237+
File.write(Path.join(dir, "a-post.md"), """
238+
---
239+
title: Missing layout key
240+
date: 2018-02-28
241+
permalink: /:title
242+
---
243+
244+
A great post
245+
""")
246+
247+
assert {:ok, token} = PostExtension.run(token)
248+
249+
assert %{
250+
posts: [
251+
%{
252+
__tableau_post_extension__: true,
253+
body: "\nA great post\n",
254+
date: ~U[2018-02-28 00:00:00Z],
255+
file: ^dir <> "/a-post.md",
256+
layout: Blog.DefaultPostLayout,
257+
permalink: "/missing-layout-key",
258+
title: "Missing layout key"
259+
}
260+
]
261+
} = token
262+
end
263+
236264
test "handles frontmatter data in the permalink", %{tmp_dir: dir, token: token} do
237265
File.write(Path.join(dir, "a-post.md"), """
238266
---

0 commit comments

Comments
 (0)