-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoc.ex
44 lines (38 loc) · 1.09 KB
/
toc.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
defmodule Pdx.Toc do
use Tableau.Extension, key: :toc, type: :pre_build, priority: 200
def run(%{posts: posts} = token) do
:global.trans(
{:toc_extension, make_ref()},
fn ->
tocs =
for %{body: body, file: file} = post <- posts, !post[:notoc], reduce: %{} do
acc ->
toc =
Floki.parse_fragment!(body)
|> Floki.find("h2, h3, h4")
|> Enum.map(fn {type, _, _} = node ->
depth =
case type do
"h2" -> 1
"h3" -> 2
"h4" -> 3
end
text = Floki.text(node)
[id | _] = Floki.attribute(node, "a", "id")
%{text: text, id: id, depth: depth}
end)
acc
|> Map.put(file, toc)
end
token
|> Map.put(:toc, tocs)
|> then(&{:ok, &1})
end,
[Node.self()],
:infinity
)
end
end
defmodule Pdx.Toc.Config do
def new(input), do: {:ok, input}
end