This repository has been archived by the owner on May 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
pardall_markdown_helpers.ex
236 lines (195 loc) · 7.22 KB
/
pardall_markdown_helpers.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
defmodule PardallWeb.PardallMarkdownHelpers do
alias PardallMarkdown.Content.{Link, AnchorLink}
use Phoenix.HTML
import Phoenix.LiveView.Helpers
def collapsible_taxonomy_tree_list(nil), do: nil
def collapsible_taxonomy_tree_list(taxonomies), do: collapsible_taxonomy_tree(taxonomies)
defp collapsible_taxonomy_tree(
taxonomies,
all \\ "<ul>",
previous_level \\ -1,
parent_level \\ 0
)
defp collapsible_taxonomy_tree([%Link{level: level} = taxonomy | tail], all, -1, parent_level) do
collapsible_taxonomy_tree(
tail,
all <> "<li>" <> collapsible_taxonomy_link(taxonomy, tail, parent_level),
level,
parent_level
)
end
defp collapsible_taxonomy_tree(
[%Link{level: level} = taxonomy | tail],
all,
previous_level,
parent_level
)
when level > previous_level do
parent_level = parent_level + 1
new_level = ~s"""
<div class="collapse show" id="collapse-content-#{parent_level}">
<ul class="ls-inner">
<li>
"""
# nest new level
collapsible_taxonomy_tree(
tail,
all <> new_level <> collapsible_taxonomy_link(taxonomy, tail, parent_level),
level,
parent_level
)
end
defp collapsible_taxonomy_tree(
[%Link{level: level} = taxonomy | tail],
all,
previous_level,
parent_level
)
when level < previous_level do
# go up (previous_level - level) levels, closing nest(s)
diff = previous_level - level
close = String.duplicate("</ul></div></li>", diff)
collapsible_taxonomy_tree(
tail,
all <> close <> "<li>" <> collapsible_taxonomy_link(taxonomy, tail, parent_level),
level,
parent_level
)
end
defp collapsible_taxonomy_tree(
[%Link{level: level} = taxonomy | tail],
all,
previous_level,
parent_level
)
when level == previous_level do
# same level
collapsible_taxonomy_tree(
tail,
all <> "</li><li>" <> collapsible_taxonomy_link(taxonomy, tail, parent_level),
level,
parent_level
)
end
# Empty initial list provided
defp collapsible_taxonomy_tree([], "<ul>", _, _), do: ""
# No more taxonomies to traverse, finish and return the list
defp collapsible_taxonomy_tree([], all, previous_level, _parent_level),
do: all <> String.duplicate("</li></ul>", previous_level) <> "</li></ul>"
defp collapsible_taxonomy_link(
%Link{title: title, slug: slug, level: level},
[%Link{level: next_level} | _],
parent_level
)
when next_level > level do
live_link = live_redirect(title, to: slug) |> safe_to_string()
controls_level = parent_level + 1
~s"""
<div>
<a class="collapsed btn-toggle" role="button" data-bs-toggle="collapse"
data-bs-target="#collapse-content-#{controls_level}" aria-expanded="true" aria-controls="collapse-content-#{controls_level}"/>
#{live_link}
</div>
"""
end
defp collapsible_taxonomy_link(%Link{title: title, slug: slug}, _, _) do
live_redirect(title, to: slug)
|> safe_to_string()
end
@doc """
Generates a HTML string of nested `<ul/>` lists of Taxonomies names,
with LiveView links to the taxonomy slug.
## Example
Input list of taxonomies:
```elixir
[
%PardallMarkdown.Link{level: 0, name: "Home", parents: ["/"], slug: "/"},
%PardallMarkdown.Link{level: 0, name: "Blog", parents: ["/"], slug: "/blog"},
%PardallMarkdown.Link{level: 1, name: "Art", parents: ["/", "/blog"], slug: "/blog/art"},
%PardallMarkdown.Link{level: 2, name: "3D", parents: ["/", "/blog", "/blog/art"], slug: "/blog/art/3d"}
]
```
The resulting HTML string:
```html
<ul>
<li><a data-phx-link="redirect" data-phx-link-state="push" href="/">Home</a></li>
<li>
<a data-phx-link="redirect" data-phx-link-state="push" href="/blog">Blog</a>
<ul>
<li>
<a data-phx-link="redirect" data-phx-link-state="push" href="/blog/art">Art</a>
<ul>
<li><a data-phx-link="redirect" data-phx-link-state="push" href="/blog/art/3d">3D</a></li>
</ul>
</li>
</ul>
</li>
</ul>
```
"""
def taxonomy_tree_list(nil), do: nil
def taxonomy_tree_list(taxonomies), do: taxonomy_tree(taxonomies)
defp taxonomy_tree(taxonomies, all \\ "<ul>", previous_level \\ -1)
defp taxonomy_tree([%Link{level: level} = taxonomy | tail], all, -1) do
taxonomy_tree(tail, all <> "<li>" <> taxonomy_link(taxonomy), level)
end
defp taxonomy_tree([%Link{level: level} = taxonomy | tail], all, previous_level)
when level > previous_level do
# nest new level
taxonomy_tree(tail, all <> "<ul><li>" <> taxonomy_link(taxonomy), level)
end
defp taxonomy_tree([%Link{level: level} = taxonomy | tail], all, previous_level)
when level < previous_level do
# go up (previous_level - level) levels, closing nest(s)
diff = previous_level - level
close = String.duplicate("</ul></li>", diff)
taxonomy_tree(tail, all <> close <> "<li>" <> taxonomy_link(taxonomy), level)
end
defp taxonomy_tree([%Link{level: level} = taxonomy | tail], all, previous_level)
when level == previous_level do
# same level
taxonomy_tree(tail, all <> "</li><li>" <> taxonomy_link(taxonomy), level)
end
# Empty initial list provided
defp taxonomy_tree([], "<ul>", _), do: ""
# No more taxonomies to traverse, finish and return the list
defp taxonomy_tree([], all, previous_level),
do: all <> String.duplicate("</li></ul>", previous_level) <> "</li></ul>"
defp taxonomy_link(%Link{title: title, slug: slug}) do
live_redirect(title, to: slug)
|> safe_to_string()
end
def post_toc_list(nil), do: nil
def post_toc_list(links), do: post_toc(links)
defp post_toc(links, all \\ "<ul>", previous_level \\ -1)
defp post_toc([%AnchorLink{level: level} = link | tail], all, -1) do
post_toc(tail, all <> "<li>" <> toc_link(link), level)
end
defp post_toc([%AnchorLink{level: level} = link | tail], all, previous_level)
when level > previous_level do
# nest new level
post_toc(tail, all <> "<ul><li>" <> toc_link(link), level)
end
defp post_toc([%AnchorLink{level: level} = link | tail], all, previous_level)
when level < previous_level do
# go up (previous_level - level) levels, closing nest(s)
diff = previous_level - level
close = String.duplicate("</ul></li>", diff)
post_toc(tail, all <> close <> "<li>" <> toc_link(link), level)
end
defp post_toc([%AnchorLink{level: level} = link | tail], all, previous_level)
when level == previous_level do
# same level
post_toc(tail, all <> "</li><li>" <> toc_link(link), level)
end
# Empty initial list provided
defp post_toc([], "<ul>", _), do: ""
# No more taxonomies to traverse, finish and return the list
defp post_toc([], all, previous_level),
do: all <> String.duplicate("</li></ul>", previous_level) <> "</li></ul>"
defp toc_link(%AnchorLink{title: title, id: id}), do: "<a href=\"#{id}\">#{title}</a>"
def has_next_or_previous_posts?(%{link: %{previous: previous, next: next}})
when not is_nil(previous) or not is_nil(next),
do: true
def has_next_or_previous_posts?(_), do: false
end