Skip to content

Commit 707f4ef

Browse files
paradox460mhanberg
andauthoredNov 7, 2023
feat: fall back to first <h1> as title if no title in frontmatter (#36)
Co-authored-by: Mitchell Hanberg <[email protected]>
1 parent 75537eb commit 707f4ef

File tree

4 files changed

+19
-3
lines changed

4 files changed

+19
-3
lines changed
 

Diff for: ‎lib/tableau/extensions/page_extension/pages/page.ex

+8
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@ defmodule Tableau.PageExtension.Pages.Page do
88
|> Map.put(:body, body)
99
|> Map.put(:file, filename)
1010
|> Map.put(:layout, Module.concat([attrs.layout || page_config.layout]))
11+
|> Map.put_new_lazy(:title, fn ->
12+
with {:ok, document} <- Floki.parse_fragment(body),
13+
[hd | _] <- Floki.find(document, "h1") do
14+
Floki.text(hd)
15+
else
16+
_ -> nil
17+
end
18+
end)
1119
|> build_permalink(page_config)
1220
end
1321

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ defmodule Tableau.PostExtension do
99
Frontmatter is compiled with `yaml_elixir` and all keys are converted to atoms.
1010
1111
* `:id` - An Elixir module to be used when compiling the backing `Tableau.Page`. A leaking implementation detail that should be fixed eventually.
12-
* `:title` - The title of the post
12+
* `:title` - The title of the post. Falls back to the first `<h1>` tag if present in the body.
1313
* `:permalink` - The permalink of the post. `:title` will be replaced with the posts title and non alphanumeric characters removed. Optional.
1414
* `:date` - A string representation of an Elixir `NaiveDateTime`, often presented as a `sigil_N`. This will be converted to your configured timezone.
1515
* `:layout` - A string representation of a Tableau layout module.

Diff for: ‎lib/tableau/extensions/post_extension/posts/post.ex

+8
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@ defmodule Tableau.PostExtension.Posts.Post do
1010
|> Map.put(:body, body)
1111
|> Map.put(:file, filename)
1212
|> Map.put(:layout, Module.concat([attrs.layout || post_config.layout]))
13+
|> Map.put_new_lazy(:title, fn ->
14+
with {:ok, document} <- Floki.parse_fragment(body),
15+
[hd | _] <- Floki.find(document, "h1") do
16+
Floki.text(hd)
17+
else
18+
_ -> nil
19+
end
20+
end)
1321
|> Map.put(
1422
:date,
1523
DateTime.from_naive!(

Diff for: ‎mix.exs

+2-2
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@ defmodule Tableau.MixProject do
4343
{:web_dev_utils, "~> 0.1"},
4444
{:websock_adapter, "~> 0.5"},
4545
{:yaml_elixir, "~> 2.9"},
46+
{:floki, "~> 0.34"},
4647

4748
# dev
4849
{:ex_doc, ">= 0.0.0", only: :dev},
49-
{:styler, "~> 0.9", only: :dev},
50-
{:floki, "~> 0.34", only: :test}
50+
{:styler, "~> 0.9", only: :dev}
5151
]
5252
end
5353

0 commit comments

Comments
 (0)
Please sign in to comment.