File tree 4 files changed +19
-3
lines changed
4 files changed +19
-3
lines changed Original file line number Diff line number Diff line change @@ -8,6 +8,14 @@ defmodule Tableau.PageExtension.Pages.Page do
8
8
|> Map . put ( :body , body )
9
9
|> Map . put ( :file , filename )
10
10
|> 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 )
11
19
|> build_permalink ( page_config )
12
20
end
13
21
Original file line number Diff line number Diff line change @@ -9,7 +9,7 @@ defmodule Tableau.PostExtension do
9
9
Frontmatter is compiled with `yaml_elixir` and all keys are converted to atoms.
10
10
11
11
* `: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.
13
13
* `:permalink` - The permalink of the post. `:title` will be replaced with the posts title and non alphanumeric characters removed. Optional.
14
14
* `:date` - A string representation of an Elixir `NaiveDateTime`, often presented as a `sigil_N`. This will be converted to your configured timezone.
15
15
* `:layout` - A string representation of a Tableau layout module.
Original file line number Diff line number Diff line change @@ -10,6 +10,14 @@ defmodule Tableau.PostExtension.Posts.Post do
10
10
|> Map . put ( :body , body )
11
11
|> Map . put ( :file , filename )
12
12
|> 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 )
13
21
|> Map . put (
14
22
:date ,
15
23
DateTime . from_naive! (
Original file line number Diff line number Diff line change @@ -43,11 +43,11 @@ defmodule Tableau.MixProject do
43
43
{ :web_dev_utils , "~> 0.1" } ,
44
44
{ :websock_adapter , "~> 0.5" } ,
45
45
{ :yaml_elixir , "~> 2.9" } ,
46
+ { :floki , "~> 0.34" } ,
46
47
47
48
# dev
48
49
{ :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 }
51
51
]
52
52
end
53
53
You can’t perform that action at this time.
0 commit comments