Do not rely on cached stripped content. We need the control to preserve the breaks.
This commit is contained in:
parent
baf7fd2142
commit
07064f73bc
@ -3,19 +3,17 @@
|
|||||||
# SPDX-License-Identifier: AGPL-3.0-only
|
# SPDX-License-Identifier: AGPL-3.0-only
|
||||||
|
|
||||||
defmodule Pleroma.Web.Metadata.Utils do
|
defmodule Pleroma.Web.Metadata.Utils do
|
||||||
alias Pleroma.Activity
|
|
||||||
alias Pleroma.Emoji
|
alias Pleroma.Emoji
|
||||||
alias Pleroma.Formatter
|
alias Pleroma.Formatter
|
||||||
alias Pleroma.HTML
|
alias Pleroma.HTML
|
||||||
|
|
||||||
def scrub_html_and_truncate(%{data: %{"content" => content}} = object) do
|
def scrub_html_and_truncate(%{data: %{"content" => content}} = _object) do
|
||||||
content
|
content
|
||||||
# html content comes from DB already encoded, decode first and scrub after
|
# html content comes from DB already encoded, decode first and scrub after
|
||||||
|> HtmlEntities.decode()
|
|
||||||
|> String.replace(~r/<br\s?\/?>/, " ")
|
|
||||||
|> Activity.HTML.get_cached_stripped_html_for_activity(object, "metadata")
|
|
||||||
|> Emoji.Formatter.demojify()
|
|> Emoji.Formatter.demojify()
|
||||||
|
|> HTML.filter_tags(Pleroma.HTML.Scrubber.BreaksOnly)
|
||||||
|> HtmlEntities.decode()
|
|> HtmlEntities.decode()
|
||||||
|
|> String.replace(~r/<br\s?\/?>/, " ")
|
||||||
|> Formatter.truncate()
|
|> Formatter.truncate()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -23,6 +23,22 @@ defmodule Pleroma.Web.Metadata.UtilsTest do
|
|||||||
assert Utils.scrub_html_and_truncate(note) == "Pleroma's really cool!"
|
assert Utils.scrub_html_and_truncate(note) == "Pleroma's really cool!"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "it replaces <br> with compatible HTML entity (objects)" do
|
||||||
|
user = insert(:user)
|
||||||
|
|
||||||
|
note =
|
||||||
|
insert(:note, %{
|
||||||
|
data: %{
|
||||||
|
"actor" => user.ap_id,
|
||||||
|
"id" => "https://pleroma.gov/objects/whatever",
|
||||||
|
"content" => "First line<br>Second line"
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
assert Utils.scrub_html_and_truncate(note) ==
|
||||||
|
"First line Second line"
|
||||||
|
end
|
||||||
|
|
||||||
test "it returns text without encode HTML (binaries)" do
|
test "it returns text without encode HTML (binaries)" do
|
||||||
assert Utils.scrub_html_and_truncate("Pleroma's really cool!") == "Pleroma's really cool!"
|
assert Utils.scrub_html_and_truncate("Pleroma's really cool!") == "Pleroma's really cool!"
|
||||||
end
|
end
|
||||||
@ -44,7 +60,8 @@ defmodule Pleroma.Web.Metadata.UtilsTest do
|
|||||||
end
|
end
|
||||||
|
|
||||||
test "it strips HTML tags and other entities (binaries)" do
|
test "it strips HTML tags and other entities (binaries)" do
|
||||||
assert Utils.scrub_html_and_truncate("<title>my title</title> <p>and a paragraph!</p>") == "my title and a paragraph!"
|
assert Utils.scrub_html_and_truncate("<title>my title</title> <p>and a paragraph!</p>") ==
|
||||||
|
"my title and a paragraph!"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user