Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/school_house_web/templates/post/post.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
</div>

<div class="pt-6 text-center">
<h1 class="text-2xl leading-8 font-extrabold tracking-tight sm:text-2xl prose dark:prose-dark"><%= raw @post.title %></h1>
<h1 class="text-2xl leading-8 font-extrabold tracking-tight sm:text-2xl prose dark:prose-dark"><%= @post.title |> strip_p_tags() |> raw() %></h1>
</div>

<div class="py-2 text-center">
Expand Down
15 changes: 15 additions & 0 deletions lib/school_house_web/views/html_helpers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,19 @@ defmodule SchoolHouseWeb.HtmlHelpers do
def translation_status_css_class(%{status: :minor}), do: "bg-yellow-400"
def translation_status_css_class(%{status: :patch}), do: "bg-yellow-200"
def translation_status_css_class(_line), do: "bg-white"

@doc """
Removes all <p> and </p> tags from the given string, inserting newline if needed.

This is useful to avoid nesting <p> tags inside elements that don't allow them like headings or span,
for example in the blog posts titles.
"""
def strip_p_tags(str) when is_binary(str) do
str
|> String.replace("<p>", "")
|> String.replace("</p>", "\n")
|> String.trim()
end

def strip_p_tags(str), do: str
end
10 changes: 10 additions & 0 deletions test/school_house_web/views/html_helpers_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,14 @@ defmodule SchoolHouseWeb.HtmlHelpersTest do
assert 1 < length(locales)
end
end

describe "strip_p_tags" do
test "removes <p> and </p> tags from string" do
assert HtmlHelpers.strip_p_tags("<p>some content</p>") == "some content"
end

test "converts </p> tags to newline" do
assert HtmlHelpers.strip_p_tags("<p>one</p><p>two</p>") == "one\ntwo"
end
end
end
Loading