Merge branch 'fix/issue_433' into 'develop'

[#433] fix markdown formatting

See merge request pleroma/pleroma!545
This commit is contained in:
href 2018-12-14 20:30:35 +00:00
commit e74f384b68
7 changed files with 49 additions and 11 deletions

View file

@ -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 =

View file

@ -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

View file

@ -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/, "")

View file

@ -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)