Merge branch 'fix/linkify' into 'develop'

Fix links with HTML elements and/or parentheses.

See merge request pleroma/pleroma!38
This commit is contained in:
lambda 2017-12-08 12:34:03 +00:00
commit 35cfbf43a0
3 changed files with 22 additions and 3 deletions

View File

@ -1,7 +1,7 @@
defmodule Pleroma.Formatter do defmodule Pleroma.Formatter do
alias Pleroma.User alias Pleroma.User
@link_regex ~r/https?:\/\/[\w\.\/?=\-#%&@~]+[\w\/]/u @link_regex ~r/https?:\/\/[\w\.\/?=\-#%&@~\(\)]+[\w\/]/u
def linkify(text) do def linkify(text) do
Regex.replace(@link_regex, text, "<a href='\\0'>\\0</a>") Regex.replace(@link_regex, text, "<a href='\\0'>\\0</a>")
end end
@ -24,6 +24,15 @@ def parse_mentions(text) do
|> Enum.filter(fn ({_match, user}) -> user end) |> Enum.filter(fn ({_match, user}) -> user end)
end end
def html_escape(text) do
Regex.split(@link_regex, text, include_captures: true)
|> Enum.map_every(2, fn chunk ->
{:safe, part} = Phoenix.HTML.html_escape(chunk)
part
end)
|> Enum.join("")
end
@finmoji [ @finmoji [
"a_trusted_friend", "a_trusted_friend",
"alandislands", "alandislands",

View File

@ -62,8 +62,8 @@ def add_attachments(text, attachments) do
end end
def format_input(text, mentions, _tags) do def format_input(text, mentions, _tags) do
Phoenix.HTML.html_escape(text) text
|> elem(1) |> Formatter.html_escape
|> Formatter.linkify |> Formatter.linkify
|> String.replace("\n", "<br>") |> String.replace("\n", "<br>")
|> add_user_links(mentions) |> add_user_links(mentions)

View File

@ -25,6 +25,16 @@ test "turning urls into links" do
expected = "<a href='http://www.cs.vu.nl/~ast/intel/'>http://www.cs.vu.nl/~ast/intel/</a>" expected = "<a href='http://www.cs.vu.nl/~ast/intel/'>http://www.cs.vu.nl/~ast/intel/</a>"
assert Formatter.linkify(text) == expected assert Formatter.linkify(text) == expected
text = "https://forum.zdoom.org/viewtopic.php?f=44&t=57087"
expected = "<a href='https://forum.zdoom.org/viewtopic.php?f=44&t=57087'>https://forum.zdoom.org/viewtopic.php?f=44&t=57087</a>"
assert Formatter.linkify(text) == expected
text = "https://en.wikipedia.org/wiki/Sophia_(Gnosticism)#Mythos_of_the_soul"
expected = "<a href='https://en.wikipedia.org/wiki/Sophia_(Gnosticism)#Mythos_of_the_soul'>https://en.wikipedia.org/wiki/Sophia_(Gnosticism)#Mythos_of_the_soul</a>"
assert Formatter.linkify(text) == expected
end end
end end