Made links in descriptions clickable

This commit is contained in:
Kieran Eglin 2024-10-24 12:15:30 -07:00
parent 197aee473a
commit b1366bfc90
No known key found for this signature in database
GPG key ID: 193984967FCF432D
2 changed files with 15 additions and 8 deletions

View file

@ -62,17 +62,24 @@ defmodule PinchflatWeb.CustomComponents.TextComponents do
end end
@doc """ @doc """
Renders a block of text with each line broken into a separate span. Renders a block of text with each line broken into a separate span and links highlighted.
""" """
attr :text, :string, required: true attr :text, :string, required: true
def break_on_newline(assigns) do def render_description(assigns) do
broken_text = formatted_text =
assigns.text Regex.split(~r{https?://\S+}, assigns.text, include_captures: true)
|> String.split("\n", trim: false) |> Enum.map(fn
|> Enum.intersperse(Phoenix.HTML.Tag.tag(:span, class: "inline-block mt-2")) "http" <> _ = url ->
Phoenix.HTML.Tag.content_tag(:a, url, class: "text-blue-500 hover:text-blue-300", href: url, target: "_blank")
assigns = Map.put(assigns, :text, broken_text) text ->
text
|> String.split("\n", trim: false)
|> Enum.intersperse(Phoenix.HTML.Tag.tag(:span, class: "inline-block mt-2"))
end)
assigns = Map.put(assigns, :text, formatted_text)
~H""" ~H"""
<span><%= @text %></span> <span><%= @text %></span>

View file

@ -45,7 +45,7 @@
</span> </span>
</div> </div>
<div :if={@media_item.description} class="mt-4 text-bodydark"> <div :if={@media_item.description} class="mt-4 text-bodydark">
<.break_on_newline text={@media_item.description} /> <.render_description text={@media_item.description} />
</div> </div>
</aside> </aside>
</section> </section>