From 059dd6f681caf14ee9d288ec14b82132be29ae2c Mon Sep 17 00:00:00 2001 From: rinpatch Date: Tue, 18 Dec 2018 21:38:15 +0300 Subject: [PATCH 1/3] Ignore HTML characters in formatter.ex --- lib/pleroma/formatter.ex | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/pleroma/formatter.ex b/lib/pleroma/formatter.ex index 46d0d926a..9401689cb 100644 --- a/lib/pleroma/formatter.ex +++ b/lib/pleroma/formatter.ex @@ -4,12 +4,12 @@ defmodule Pleroma.Formatter do alias Pleroma.HTML alias Pleroma.Emoji - @tag_regex ~r/\#\w+/u + @tag_regex ~r/((?<=[^&])|\A)(\#)(\w+)/u @markdown_characters_regex ~r/(`|\*|_|{|}|[|]|\(|\)|#|\+|-|\.|!)/ def parse_tags(text, data \\ %{}) do Regex.scan(@tag_regex, text) - |> Enum.map(fn ["#" <> tag = full_tag] -> {full_tag, String.downcase(tag)} end) + |> Enum.map(fn ["#" <> tag = full_tag | _] -> {full_tag, String.downcase(tag)} end) |> (fn map -> if data["sensitive"] in [true, "True", "true", "1"], do: [{"#nsfw", "nsfw"}] ++ map, @@ -155,7 +155,7 @@ defmodule Pleroma.Formatter do uuid_text = tags |> Enum.reduce(text, fn {match, _short, uuid}, text -> - String.replace(text, match, uuid) + String.replace(text, ~r/((?<=[^&])|(\A))#{match}/, uuid) end) subs = From 8a67677d7791287905898bc388fbd8d9c81ec2b1 Mon Sep 17 00:00:00 2001 From: rinpatch Date: Tue, 18 Dec 2018 22:10:56 +0300 Subject: [PATCH 2/3] Add test --- test/formatter_test.exs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/test/formatter_test.exs b/test/formatter_test.exs index bb318b7d5..cfa735795 100644 --- a/test/formatter_test.exs +++ b/test/formatter_test.exs @@ -22,6 +22,18 @@ defmodule Pleroma.FormatterTest do assert expected_text == Formatter.add_hashtag_links({[], text}, tags) |> Formatter.finalize() end + + test "does not turn html characters to tags" do + text = "Fact #3: pleroma does what mastodon't" + + expected_text = + "Fact #3: pleroma does what mastodon't" + + tags = Formatter.parse_tags(text) + + assert expected_text == + Formatter.add_hashtag_links({[], text}, tags) |> Formatter.finalize() + end end describe ".add_links" do From 196d9c0fd089942deff62aeeea60bef1972cbe38 Mon Sep 17 00:00:00 2001 From: Rin Toshaka Date: Tue, 18 Dec 2018 20:30:04 +0100 Subject: [PATCH 3/3] Fix tests --- test/formatter_test.exs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/formatter_test.exs b/test/formatter_test.exs index cfa735795..6cdfa4167 100644 --- a/test/formatter_test.exs +++ b/test/formatter_test.exs @@ -27,7 +27,7 @@ defmodule Pleroma.FormatterTest do text = "Fact #3: pleroma does what mastodon't" expected_text = - "Fact #3: pleroma does what mastodon't" + "Fact : pleroma does what mastodon't" tags = Formatter.parse_tags(text)