From ad0d130fc84b6ed2c11e3a9ddd3108d257b3624e Mon Sep 17 00:00:00 2001 From: Inhji Date: Sat, 9 Sep 2023 23:46:37 +0200 Subject: [PATCH 1/2] improve outline rendering --- lib/chiya_web/controllers/page_html.ex | 24 ++-------------- lib/chiya_web/outline_renderer.ex | 38 ++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 22 deletions(-) create mode 100644 lib/chiya_web/outline_renderer.ex diff --git a/lib/chiya_web/controllers/page_html.ex b/lib/chiya_web/controllers/page_html.ex index 69fceae..d95c03b 100644 --- a/lib/chiya_web/controllers/page_html.ex +++ b/lib/chiya_web/controllers/page_html.ex @@ -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 diff --git a/lib/chiya_web/outline_renderer.ex b/lib/chiya_web/outline_renderer.ex new file mode 100644 index 0000000..46fd183 --- /dev/null +++ b/lib/chiya_web/outline_renderer.ex @@ -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 -- 2.39.5 From 106f86521cabd6901994155550efc964d7a20a9b Mon Sep 17 00:00:00 2001 From: Inhji Date: Sat, 9 Sep 2023 23:46:59 +0200 Subject: [PATCH 2/2] generate excerpt from placeholder --- assets/css/app.css | 4 +- lib/chiya/notes/note.ex | 10 +++++ .../components/layouts/root_public.html.heex | 40 ++++++++----------- .../page_html/note_list_default.html.heex | 32 ++++++++------- 4 files changed, 46 insertions(+), 40 deletions(-) diff --git a/assets/css/app.css b/assets/css/app.css index b07c5c9..2ba3d84 100644 --- a/assets/css/app.css +++ b/assets/css/app.css @@ -47,8 +47,8 @@ @apply flex md:flex-row lg:flex-col mb-6 lg:mb-0; } - & .menu { - @apply flex-1; + & ul.menu { + @apply m-0; } } diff --git a/lib/chiya/notes/note.ex b/lib/chiya/notes/note.ex index 9db2e01..04d18d4 100644 --- a/lib/chiya/notes/note.ex +++ b/lib/chiya/notes/note.ex @@ -102,6 +102,16 @@ defmodule Chiya.Notes.Note do end end + def note_excerpt(note_content) do + if String.contains?(note_content, "") do + note_content + |> String.split("") + |> List.first() + else + String.slice(note_content, 0..150) <> ".." + end + end + @doc false def changeset(note, attrs) do # if you need to have a preloaded note here, diff --git a/lib/chiya_web/components/layouts/root_public.html.heex b/lib/chiya_web/components/layouts/root_public.html.heex index 976968b..6344eeb 100644 --- a/lib/chiya_web/components/layouts/root_public.html.heex +++ b/lib/chiya_web/components/layouts/root_public.html.heex @@ -72,30 +72,24 @@
diff --git a/lib/chiya_web/controllers/page_html/note_list_default.html.heex b/lib/chiya_web/controllers/page_html/note_list_default.html.heex index 9885f4e..ee90a3d 100644 --- a/lib/chiya_web/controllers/page_html/note_list_default.html.heex +++ b/lib/chiya_web/controllers/page_html/note_list_default.html.heex @@ -1,28 +1,30 @@
<%= for note <- assigns.notes do %> -
- -
- +
- <%= if assigns.show_content do %> -

- <%= String.slice(note.content, 0..150) %> -

- <% end %> + <%= if assigns.show_content do %> +

+ <%= Chiya.Notes.Note.note_excerpt(note.content) %> +

+ <% end %> +
<% end %>
-- 2.39.5