chiya/lib/chiya_web/controllers/page_html.ex

43 lines
1.1 KiB
Elixir
Raw Normal View History

2023-03-05 16:07:40 +01:00
defmodule ChiyaWeb.PageHTML do
use ChiyaWeb, :html_public
2023-07-11 07:23:07 +02:00
import Phoenix.HTML.Tag, only: [content_tag: 3, content_tag: 2]
2023-09-05 21:59:37 +02:00
import ChiyaWeb.Format, only: [pretty_datetime: 1, pretty_date: 1, datetime: 1]
2023-03-05 16:07:40 +01:00
embed_templates "page_html/*"
2023-04-10 19:18:27 +02:00
2023-09-05 21:59:37 +02:00
attr :notes, :list, required: true
attr :show_content, :boolean, default: true
2023-09-05 21:59:37 +02:00
def note_list_default(assigns)
2023-04-10 19:18:27 +02:00
def tag_list([]), do: "No Tags"
def tag_list(tags), do: Enum.map_join(tags, ", ", fn t -> t.name end)
2023-07-11 07:23:07 +02:00
def render_outline(note) do
2023-07-13 07:14:24 +02:00
note.content
|> ChiyaWeb.Outline.get()
|> Enum.map(&do_render_outline/1)
2023-07-13 09:52:12 +02:00
|> Enum.map(&safe_to_string/1)
2023-07-11 07:23:07 +02:00
end
2023-07-19 22:40:32 +02:00
def has_outline?(note) do
outline_empty =
note.content
|> ChiyaWeb.Outline.get()
|> Enum.empty?()
!outline_empty
end
2023-07-11 07:23:07 +02:00
def do_render_outline(%{text: text, children: children, level: _level}) do
2023-07-13 07:14:24 +02:00
slug = Slugger.slugify_downcase(text)
2023-07-16 17:16:44 +02:00
2023-07-11 07:23:07 +02:00
content_tag(:ul, [class: "m-0"],
do: [
2023-07-16 17:16:44 +02:00
content_tag(:li, do: content_tag(:a, text, href: "##{slug}")),
2023-07-11 07:23:07 +02:00
Enum.map(children, &do_render_outline/1)
]
2023-07-13 09:52:12 +02:00
)
2023-07-11 07:23:07 +02:00
end
2023-03-05 16:07:40 +01:00
end