aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorhref <href+git-pleroma@random.sh>2018-12-14 20:30:35 +0000
committerhref <href+git-pleroma@random.sh>2018-12-14 20:30:35 +0000
commite74f384b685edff5e4fac9da788a7516dd83fe94 (patch)
tree85c4462b333b2d8d58291fc81b6e8db002cddb8c /lib
parentbfe27c1b557b19c171c8168c5f1244987246c47f (diff)
parentbc6262d2503fa5a9656898fa2dcd91b2111cf2b5 (diff)
downloadpleroma-e74f384b685edff5e4fac9da788a7516dd83fe94.tar.gz
Merge branch 'fix/issue_433' into 'develop'
[#433] fix markdown formatting See merge request pleroma/pleroma!545
Diffstat (limited to 'lib')
-rw-r--r--lib/pleroma/formatter.ex14
-rw-r--r--lib/pleroma/html.ex10
-rw-r--r--lib/pleroma/web/common_api/utils.ex10
-rw-r--r--lib/pleroma/web/twitter_api/views/activity_view.ex3
4 files changed, 28 insertions, 9 deletions
diff --git a/lib/pleroma/formatter.ex b/lib/pleroma/formatter.ex
index 133683794..46d0d926a 100644
--- a/lib/pleroma/formatter.ex
+++ b/lib/pleroma/formatter.ex
@@ -5,6 +5,8 @@ defmodule Pleroma.Formatter do
alias Pleroma.Emoji
@tag_regex ~r/\#\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)
@@ -76,6 +78,18 @@ defmodule Pleroma.Formatter do
|> Enum.join("")
end
+ @doc """
+ Escapes a special characters in mention names.
+ """
+ @spec mentions_escape(String.t(), list({String.t(), any()})) :: String.t()
+ def mentions_escape(text, mentions) do
+ mentions
+ |> Enum.reduce(text, fn {name, _}, acc ->
+ escape_name = String.replace(name, @markdown_characters_regex, "\\\\\\1")
+ String.replace(acc, name, escape_name)
+ end)
+ end
+
@doc "changes scheme:... urls to html links"
def add_links({subs, text}) do
links =
diff --git a/lib/pleroma/html.ex b/lib/pleroma/html.ex
index 8a0333461..583f05aeb 100644
--- a/lib/pleroma/html.ex
+++ b/lib/pleroma/html.ex
@@ -17,15 +17,9 @@ defmodule Pleroma.HTML do
end)
end
- def filter_tags(html, scrubber) do
- html |> Scrubber.scrub(scrubber)
- end
-
+ def filter_tags(html, scrubber), do: Scrubber.scrub(html, scrubber)
def filter_tags(html), do: filter_tags(html, nil)
-
- def strip_tags(html) do
- html |> Scrubber.scrub(Scrubber.StripTags)
- end
+ def strip_tags(html), do: Scrubber.scrub(html, Scrubber.StripTags)
end
defmodule Pleroma.HTML.Scrubber.TwitterText do
diff --git a/lib/pleroma/web/common_api/utils.ex b/lib/pleroma/web/common_api/utils.ex
index ce0926b99..142283684 100644
--- a/lib/pleroma/web/common_api/utils.ex
+++ b/lib/pleroma/web/common_api/utils.ex
@@ -112,6 +112,9 @@ defmodule Pleroma.Web.CommonAPI.Utils do
Enum.join([text | attachment_text], "<br>")
end
+ @doc """
+ Formatting text to plain text.
+ """
def format_input(text, mentions, tags, "text/plain") do
text
|> Formatter.html_escape("text/plain")
@@ -123,6 +126,9 @@ defmodule Pleroma.Web.CommonAPI.Utils do
|> Formatter.finalize()
end
+ @doc """
+ Formatting text to html.
+ """
def format_input(text, mentions, _tags, "text/html") do
text
|> Formatter.html_escape("text/html")
@@ -132,8 +138,12 @@ defmodule Pleroma.Web.CommonAPI.Utils do
|> Formatter.finalize()
end
+ @doc """
+ Formatting text to markdown.
+ """
def format_input(text, mentions, tags, "text/markdown") do
text
+ |> Formatter.mentions_escape(mentions)
|> Earmark.as_html!()
|> Formatter.html_escape("text/html")
|> String.replace(~r/\r?\n/, "")
diff --git a/lib/pleroma/web/twitter_api/views/activity_view.ex b/lib/pleroma/web/twitter_api/views/activity_view.ex
index e5caed28f..433c3b141 100644
--- a/lib/pleroma/web/twitter_api/views/activity_view.ex
+++ b/lib/pleroma/web/twitter_api/views/activity_view.ex
@@ -239,7 +239,8 @@ defmodule Pleroma.Web.TwitterAPI.ActivityView do
{summary, content} = render_content(object)
html =
- HTML.filter_tags(content, User.html_filter_policy(opts[:for]))
+ content
+ |> HTML.filter_tags(User.html_filter_policy(opts[:for]))
|> Formatter.emojify(object["emoji"])
reply_parent = Activity.get_in_reply_to_activity(activity)