improve outline rendering

This commit is contained in:
Inhji 2023-09-09 23:46:37 +02:00
parent 11467a2d6f
commit ad0d130fc8
2 changed files with 40 additions and 22 deletions

View file

@ -1,6 +1,5 @@
defmodule ChiyaWeb.PageHTML do
use ChiyaWeb, :html_public
import Phoenix.HTML.Tag, only: [content_tag: 3, content_tag: 2]
import ChiyaWeb.Format, only: [pretty_datetime: 1, pretty_date: 1, datetime: 1]
embed_templates "page_html/*"
@ -27,30 +26,11 @@ defmodule ChiyaWeb.PageHTML do
def tag_list(assigns)
def render_outline(note) do
note.content
|> ChiyaWeb.Outline.get()
|> Enum.map(&do_render_outline/1)
|> Enum.map(&safe_to_string/1)
ChiyaWeb.OutlineRenderer.render_outline(note.content)
end
def has_outline?(note) do
outline_empty =
note.content
|> ChiyaWeb.Outline.get()
|> Enum.empty?()
!outline_empty
end
def do_render_outline(%{text: text, children: children, level: _level}) do
slug = Slugger.slugify_downcase(text)
content_tag(:ul, [class: "m-0"],
do: [
content_tag(:li, do: content_tag(:a, text, href: "##{slug}")),
Enum.map(children, &do_render_outline/1)
]
)
ChiyaWeb.OutlineRenderer.has_outline?(note.content)
end
def group_tags(notes) do

View file

@ -0,0 +1,38 @@
defmodule ChiyaWeb.OutlineRenderer do
import Phoenix.HTML, only: [safe_to_string: 1]
import Phoenix.HTML.Tag, only: [content_tag: 3, content_tag: 2]
def render_outline(content) do
children =
content
|> ChiyaWeb.Outline.get()
|> Enum.map(&do_render_outline/1)
root = content_tag(:ul, do: children)
safe_to_string(root)
end
def has_outline?(content) do
outline_empty =
content
|> ChiyaWeb.Outline.get()
|> Enum.empty?()
!outline_empty
end
def do_render_outline(%{text: text, children: children, level: _level}) do
slug = Slugger.slugify_downcase(text)
list_item = content_tag(:li, do: content_tag(:a, text, href: "##{slug}"))
if Enum.empty?(children) do
[list_item]
else
[
list_item,
content_tag(:ul, do: Enum.map(children, &do_render_outline/1))
]
end
end
end