Skip to content
This repository was archived by the owner on May 3, 2024. It is now read-only.

Commit b401ffa

Browse files
Header nesting working
1 parent 366f9e7 commit b401ffa

File tree

3 files changed

+23
-145
lines changed

3 files changed

+23
-145
lines changed

lib/pardall_markdown/content/html_utils.ex

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
defmodule PardallMarkdown.Content.HtmlUtils do
2-
alias PardallMarkdown.Content.{
3-
Utils,
4-
TOC
5-
}
2+
alias PardallMarkdown.Content.Utils
63

74
def generate_summary_from_html(html, expected_length \\ 157)
85
def generate_summary_from_html(html, _) when html == nil or html == "", do: nil
@@ -104,7 +101,7 @@ defmodule PardallMarkdown.Content.HtmlUtils do
104101
def generate_anchors_and_toc(html, %{slug: slug}) do
105102
{updated_tree, %{toc: toc}} =
106103
Floki.parse_fragment!(html)
107-
|> Floki.traverse_and_update(%{counters: %{}, toc_links: [], toc: [], toc_positions: %{}}, fn
104+
|> Floki.traverse_and_update(%{counters: %{}, toc: [], toc_positions: %{}}, fn
108105
{"h" <> header_level, attrs, children} = el, acc ->
109106
case find_node_text(children) do
110107
nil ->
@@ -138,15 +135,7 @@ defmodule PardallMarkdown.Content.HtmlUtils do
138135
title: title
139136
}
140137

141-
# toc_link = %TOC.Link{
142-
# id: link_id,
143-
# header: int_header_level,
144-
# parent_slug: slug,
145-
# title: title
146-
# }
147-
148138
acc = put_in(acc[:counters][id], increase_id_count(count))
149-
# acc = put_in(acc[:toc_links], acc.toc_links ++ [toc_link])
150139
acc = put_in(acc[:toc], acc.toc ++ [link])
151140
acc = put_in(acc[:toc_positions], positions)
152141

@@ -157,9 +146,6 @@ defmodule PardallMarkdown.Content.HtmlUtils do
157146
{el, acc}
158147
end)
159148

160-
# toc = TOC.generate(toc_links)
161-
# toc = toc |> PardallMarkdown.Utils.StructUtils.struct_to_map()
162-
163149
{:ok, updated_tree |> Floki.raw_html(), toc}
164150
end
165151

@@ -170,27 +156,28 @@ defmodule PardallMarkdown.Content.HtmlUtils do
170156
when header == curr_header, do:
171157
{:level, curr_level, :positions, %{level: curr_level, header: curr_header, index: idx + 1}}
172158

173-
# defp get_toc_level(_, header, %{level: curr_level, header: curr_header, index: idx})
174-
# when header > curr_header, do:
175-
# {:level, curr_level, :positions, %{level: curr_level, header: curr_header, index: idx + 1}}
176-
177159
defp get_toc_level(toc, header, %{level: curr_level, header: curr_header, index: idx})
178160
when header < curr_header do
179-
level =
180-
toc
181-
|> Enum.reverse()
182-
|> Enum.reduce(curr_level, fn
161+
[_|reversed_toc] = toc |> Enum.reverse()
162+
163+
{_, level} =
164+
reversed_toc
165+
|> Enum.reduce({curr_header, curr_level}, fn
166+
_, {acc_header, _} = acc
167+
when acc_header <= header ->
168+
acc
169+
170+
%{header: i_header, level: i_level}, _acc
171+
when i_header < header ->
172+
{i_header, i_level + 1}
173+
183174
%{header: i_header, level: i_level}, _acc
184175
when i_header == header ->
185-
i_level
176+
{i_header, i_level}
186177

187178
%{header: i_header}, acc
188179
when i_header > header ->
189180
acc
190-
191-
%{header: i_header, level: i_level}, _acc
192-
when i_header < header ->
193-
i_level + 1
194181
end)
195182

196183
{:level, level, :positions, %{level: level, header: header, index: idx + 1}}

lib/pardall_markdown/content/toc.ex

Lines changed: 0 additions & 110 deletions
This file was deleted.

test/pardall_markdown/html_test.exs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ defmodule PardallMarkdown.HtmlTest do
212212
%{
213213
header: 2,
214214
id: "#h2-level-2",
215-
level: 1,
215+
level: 2,
216216
parent_slug: "/headers",
217217
title: "H2 Level 2"
218218
},
@@ -372,14 +372,15 @@ defmodule PardallMarkdown.HtmlTest do
372372
assert toc == [
373373
%{
374374
id: "#this-item-should-be-level-1-in-the-toc",
375-
level: 3,
375+
level: 1,
376+
header: 3,
376377
parent_slug: "/foo",
377378
title: "This item should be level 1 in the TOC"
378379
},
379-
%{id: "#first-section", level: 1, parent_slug: "/foo", title: "First Section"},
380-
%{id: "#inner-title", level: 2, parent_slug: "/foo", title: "Inner- \"Title\""},
381-
%{id: "#second-section", level: 1, parent_slug: "/foo", title: "Second Section"},
382-
%{id: "#inner-title-1", level: 2, parent_slug: "/foo", title: "Inner- \"Title\""}
380+
%{id: "#first-section", level: 1, header: 1, parent_slug: "/foo", title: "First Section"},
381+
%{id: "#inner-title", level: 2, header: 2, parent_slug: "/foo", title: "Inner- \"Title\""},
382+
%{id: "#second-section", level: 1, header: 1, parent_slug: "/foo", title: "Second Section"},
383+
%{id: "#inner-title-1", level: 2, header: 2, parent_slug: "/foo", title: "Inner- \"Title\""}
383384
]
384385

385386
assert Enum.count(toc) == 5

0 commit comments

Comments
 (0)