Skip to content

feat(posts): allow multiple source directories #130

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 19 commits into from
Apr 10, 2025
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions lib/tableau/extensions/post_extension.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ defmodule Tableau.PostExtension do
title: "The elixir-tools Update Vol. 3"
permalink: "/news/:title"
date: "~N[2023-09-19 01:00:00]"
draft: false
layout: "ElixirTools.PostLayout"
converter: "MyConverter"
```
Expand All @@ -40,6 +41,7 @@ defmodule Tableau.PostExtension do
- `:future` - boolean - Show posts that have dates later than the current timestamp, or time at which the site is generated.
- `: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_.
- `:layout` - string - Elixir module providing page layout for posts. Default is nil
- `:draft` - boolean - Only show this post in dev.

### Example

Expand Down Expand Up @@ -90,6 +92,8 @@ defmodule Tableau.PostExtension do
map(%{
optional(:enabled) => bool(),
optional(:dir, "_posts") => str(),
optional(:drafts, false) => bool(),
optional(:drafts_dir, "_drafts") => str(),
optional(:future, false) => bool(),
optional(:permalink) => str(),
optional(:layout) => oneof([atom(), str()])
Expand Down Expand Up @@ -129,6 +133,19 @@ defmodule Tableau.PostExtension do
Enum.reject(posts, fn {post, _} -> DateTime.after?(post.date, DateTime.utc_now()) end)
end
end)
|> then(fn posts ->
if config.drafts do
posts
else
Enum.reject(posts, fn {post, _} ->
unless config.drafts do
String.starts_with?(post.file, config.drafts_dir) || Map.get(post, :draft, false)
else
Map.get(post, :draft, false)
end
end)
end
end)

graph =
Tableau.Graph.insert(
Expand Down
51 changes: 49 additions & 2 deletions test/tableau/extensions/post_extension_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ defmodule Tableau.PostExtensionTest do
@moduletag :tmp_dir

describe "config" do
test "provides defaults for dir and future fields" do
assert {:ok, %{dir: "_posts", future: false}} = PostExtension.config(%{})
test "provides defaults for dir, drafts, and future fields" do
assert {:ok, %{dir: "_posts", drafts: false, drafts_dir: "_drafts", future: false}} = PostExtension.config(%{})
end
end

Expand Down Expand Up @@ -163,6 +163,53 @@ defmodule Tableau.PostExtensionTest do
assert Blog.PostLayout in vertices
end

test "drafts: true will not render a post", %{tmp_dir: dir, token: token} do
File.write!(Path.join(dir, "my-draft-post.md"), """
---
layout: Blog.PostLayout
title: My Draft Post
date: 2020-01-01
categories: post
permalink: /post/2020/01/01/my-draft-post/
draft: true
---

Do androids dream of electric sheep?
""")

assert {:ok, config} = PostExtension.config(%{dir: dir, enabled: true})

token = put_in(token.extensions.posts.config, config)

assert {:ok, token} = PostExtension.run(token)

assert [] == token.posts
end

test "files in config.drafts_dir will not render a post", %{tmp_dir: dir, token: token} do
File.write(Path.join("_drafts", "my-post-in-drafts-folder.md"), """
---
layout: Blog.PostLayout
title: My Draft Post in Drafts Folder
date: 2020-01-01
categories: post
permalink: /post/2020/01/02/my-post-in-drafts-folder/
---

Do androids dream of electric sheep?
""")

assert {:ok, config} = PostExtension.config(%{dir: dir, enabled: true})

token = put_in(token.extensions.posts.config, config)

assert {:ok, token} = PostExtension.run(token)

assert %{
posts: []
} = token
end

test "configured permalink works when you dont specify one", %{tmp_dir: dir, token: token} do
File.write(Path.join(dir, "my-future-post.md"), """
---
Expand Down