From bf91e5659ffd03b15110b6f1094b30aed940e45b Mon Sep 17 00:00:00 2001 From: eal Date: Thu, 7 Dec 2017 21:34:25 +0200 Subject: [PATCH 1/2] Fix HTML escape breaking some links. --- lib/pleroma/formatter.ex | 9 +++++++++ lib/pleroma/web/common_api/utils.ex | 4 ++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/lib/pleroma/formatter.ex b/lib/pleroma/formatter.ex index fbcbca979..275c60f32 100644 --- a/lib/pleroma/formatter.ex +++ b/lib/pleroma/formatter.ex @@ -24,6 +24,15 @@ defmodule Pleroma.Formatter do |> Enum.filter(fn ({_match, user}) -> user 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 [ "a_trusted_friend", "alandislands", diff --git a/lib/pleroma/web/common_api/utils.ex b/lib/pleroma/web/common_api/utils.ex index 1a23b1ad2..f46db4cf0 100644 --- a/lib/pleroma/web/common_api/utils.ex +++ b/lib/pleroma/web/common_api/utils.ex @@ -58,8 +58,8 @@ defmodule Pleroma.Web.CommonAPI.Utils do end def format_input(text, mentions, _tags) do - Phoenix.HTML.html_escape(text) - |> elem(1) + text + |> Formatter.html_escape |> Formatter.linkify |> String.replace("\n", "
") |> add_user_links(mentions) From a3e68f02330a3c38f1d3f568ad507e5111b82600 Mon Sep 17 00:00:00 2001 From: eal Date: Thu, 7 Dec 2017 21:38:31 +0200 Subject: [PATCH 2/2] Allow parentheses in links. --- lib/pleroma/formatter.ex | 2 +- test/formatter_test.exs | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/pleroma/formatter.ex b/lib/pleroma/formatter.ex index 275c60f32..c98db2d94 100644 --- a/lib/pleroma/formatter.ex +++ b/lib/pleroma/formatter.ex @@ -1,7 +1,7 @@ defmodule Pleroma.Formatter do alias Pleroma.User - @link_regex ~r/https?:\/\/[\w\.\/?=\-#%&@~]+[\w\/]/u + @link_regex ~r/https?:\/\/[\w\.\/?=\-#%&@~\(\)]+[\w\/]/u def linkify(text) do Regex.replace(@link_regex, text, "\\0") end diff --git a/test/formatter_test.exs b/test/formatter_test.exs index f91973881..cb7695e8e 100644 --- a/test/formatter_test.exs +++ b/test/formatter_test.exs @@ -25,6 +25,16 @@ defmodule Pleroma.FormatterTest do expected = "http://www.cs.vu.nl/~ast/intel/" assert Formatter.linkify(text) == expected + + text = "https://forum.zdoom.org/viewtopic.php?f=44&t=57087" + expected = "https://forum.zdoom.org/viewtopic.php?f=44&t=57087" + + assert Formatter.linkify(text) == expected + + text = "https://en.wikipedia.org/wiki/Sophia_(Gnosticism)#Mythos_of_the_soul" + expected = "https://en.wikipedia.org/wiki/Sophia_(Gnosticism)#Mythos_of_the_soul" + + assert Formatter.linkify(text) == expected end end