Skip to content

Commit 8c5a614

Browse files
authored
feat: write pages with .html permalink to corresponding file(#115)
1 parent f8fb9ac commit 8c5a614

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

lib/mix/tasks/tableau.build.ex

+12-2
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,12 @@ defmodule Mix.Tasks.Tableau.Build do
5555
token = mods |> extensions_for(:pre_write) |> run_extensions(token)
5656

5757
for %{body: body, permalink: permalink} <- pages do
58-
dir = Path.join(out, permalink)
58+
file_path = build_file_path(out, permalink)
59+
dir = Path.dirname(file_path)
5960

6061
File.mkdir_p!(dir)
6162

62-
File.write!(Path.join(dir, "index.html"), body)
63+
File.write!(file_path, body)
6364
end
6465

6566
if File.exists?(config.include_dir) do
@@ -71,6 +72,15 @@ defmodule Mix.Tasks.Tableau.Build do
7172
token
7273
end
7374

75+
@file_extensions [".html"]
76+
defp build_file_path(out, permalink) do
77+
if Path.extname(permalink) in @file_extensions do
78+
Path.join(out, permalink)
79+
else
80+
Path.join([out, permalink, "index.html"])
81+
end
82+
end
83+
7484
defp validate_config(module, raw_config) do
7585
if function_exported?(module, :config, 1) do
7686
module.config(raw_config)

test/mix/tasks/tableau.build_test.exs

+26
Original file line numberDiff line numberDiff line change
@@ -194,4 +194,30 @@ defmodule Mix.Tasks.Tableau.BuildTest do
194194
assert File.exists?(Path.join(out, "/a-bing-bong-blog-post/index.html"))
195195
assert File.exists?(Path.join(out, "/some/deeply/nested/page/my-page/index.html"))
196196
end
197+
198+
@tag :tmp_dir
199+
test "writes page with permalink with .html file extension to corresponding file", %{tmp_dir: out} do
200+
pages = out |> Path.join("_pages") |> tap(&File.mkdir_p!/1)
201+
Application.put_env(:tableau, Tableau.PageExtension, enabled: true, dir: pages)
202+
203+
File.write(Path.join(pages, "404.md"), """
204+
---
205+
layout: Mix.Tasks.Tableau.BuildTest.RootLayout
206+
title: A 404 error page
207+
permalink: /404.html
208+
---
209+
210+
## Ooops
211+
212+
Nothing here.
213+
""")
214+
215+
with_io(:stderr, fn ->
216+
with_log(fn ->
217+
_documents = Build.run(["--out", out])
218+
end)
219+
end)
220+
221+
assert File.exists?(Path.join(out, "/404.html"))
222+
end
197223
end

0 commit comments

Comments
 (0)