Skip to content

Commit ba2165c

Browse files
committed
Add draft front_matter and _drafts folder options
Completes elixir-tools#129
1 parent 059cd9d commit ba2165c

File tree

2 files changed

+62
-2
lines changed

2 files changed

+62
-2
lines changed

Diff for: lib/tableau/extensions/post_extension.ex

+11
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ defmodule Tableau.PostExtension do
2121
title: "The elixir-tools Update Vol. 3"
2222
permalink: "/news/:title"
2323
date: "~N[2023-09-19 01:00:00]"
24+
draft: false
2425
layout: "ElixirTools.PostLayout"
2526
converter: "MyConverter"
2627
```
@@ -40,6 +41,7 @@ defmodule Tableau.PostExtension do
4041
- `:future` - boolean - Show posts that have dates later than the current timestamp, or time at which the site is generated.
4142
- `:permalink` - string - Default output path for posts. Accepts `:title` as a replacement keyword, replaced with the post's provided title. If a post has a `:permalink` provided, that will override this value _for that post_.
4243
- `:layout` - string - Elixir module providing page layout for posts. Default is nil
44+
- `:draft` - boolean - Only show this post in dev.
4345
4446
### Example
4547
@@ -90,6 +92,7 @@ defmodule Tableau.PostExtension do
9092
map(%{
9193
optional(:enabled) => bool(),
9294
optional(:dir, "_posts") => str(),
95+
optional(:drafts, "_drafts") => str(),
9396
optional(:future, false) => bool(),
9497
optional(:permalink) => str(),
9598
optional(:layout) => oneof([atom(), str()])
@@ -129,6 +132,14 @@ defmodule Tableau.PostExtension do
129132
Enum.reject(posts, fn {post, _} -> DateTime.after?(post.date, DateTime.utc_now()) end)
130133
end
131134
end)
135+
|> then(fn posts ->
136+
unless config.drafts do
137+
posts
138+
else
139+
Enum.reject(posts, fn {post, _} -> post.file =~ config.drafts end)
140+
end
141+
end)
142+
|> Enum.reject(fn {post, _} -> Map.get(post, :draft, false) == true end)
132143

133144
graph =
134145
Tableau.Graph.insert(

Diff for: test/tableau/extensions/post_extension_test.exs

+51-2
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ defmodule Tableau.PostExtensionTest do
2626
@moduletag :tmp_dir
2727

2828
describe "config" do
29-
test "provides defaults for dir and future fields" do
30-
assert {:ok, %{dir: "_posts", future: false}} = PostExtension.config(%{})
29+
test "provides defaults for dir, drafts, and future fields" do
30+
assert {:ok,%{dir: "_posts", drafts: "_drafts", future: false}} = PostExtension.config(%{})
3131
end
3232
end
3333

@@ -163,6 +163,55 @@ defmodule Tableau.PostExtensionTest do
163163
assert Blog.PostLayout in vertices
164164
end
165165

166+
test "drafts: true will not render a post", %{tmp_dir: dir, token: token} do
167+
File.write(Path.join(dir, "my-draft-post.md"), """
168+
---
169+
layout: Blog.PostLayout
170+
title: My Draft Post
171+
date: 2020-01-01
172+
categories: post
173+
permalink: /post/2020/01/01/my-draft-post/
174+
draft: true
175+
---
176+
177+
Do androids dream of electric sheep?
178+
""")
179+
180+
assert {:ok, config} = PostExtension.config(%{dir: dir, enabled: true})
181+
182+
token = put_in(token.extensions.posts.config, config)
183+
184+
assert {:ok, token} = PostExtension.run(token)
185+
186+
assert %{
187+
posts: [],
188+
} = token
189+
end
190+
191+
test "files in config.drafts will not render a post", %{tmp_dir: dir, token: token} do
192+
File.write(Path.join("_drafts", "my-post-in-drafts-folder.md"), """
193+
---
194+
layout: Blog.PostLayout
195+
title: My Draft Post in Drafts Folder
196+
date: 2020-01-01
197+
categories: post
198+
permalink: /post/2020/01/02/my-post-in-drafts-folder/
199+
---
200+
201+
Do androids dream of electric sheep?
202+
""")
203+
204+
assert {:ok, config} = PostExtension.config(%{dir: dir, enabled: true})
205+
206+
token = put_in(token.extensions.posts.config, config)
207+
208+
assert {:ok, token} = PostExtension.run(token)
209+
210+
assert %{
211+
posts: [],
212+
} = token
213+
end
214+
166215
test "configured permalink works when you dont specify one", %{tmp_dir: dir, token: token} do
167216
File.write(Path.join(dir, "my-future-post.md"), """
168217
---

0 commit comments

Comments
 (0)