fix(formatter): preserve remote MFM mentions
This commit is contained in:
parent
e6598ae5c7
commit
e63b7d6715
4 changed files with 62 additions and 1 deletions
|
|
@ -129,6 +129,7 @@ defmodule Pleroma.Formatter do
|
||||||
|
|
||||||
def markdown_to_html(text, opts) do
|
def markdown_to_html(text, opts) do
|
||||||
breaks = Map.get(opts, :breaks, false)
|
breaks = Map.get(opts, :breaks, false)
|
||||||
|
strip_email_autolinks = Map.get(opts, :strip_email_autolinks, false)
|
||||||
text = normalize_markdown_quirks(text)
|
text = normalize_markdown_quirks(text)
|
||||||
|
|
||||||
MDEx.new(
|
MDEx.new(
|
||||||
|
|
@ -138,6 +139,7 @@ defmodule Pleroma.Formatter do
|
||||||
render: [hardbreaks: breaks, unsafe: true]
|
render: [hardbreaks: breaks, unsafe: true]
|
||||||
)
|
)
|
||||||
|> MDEx.Document.run()
|
|> MDEx.Document.run()
|
||||||
|
|> maybe_strip_email_autolinks(strip_email_autolinks)
|
||||||
|> MDEx.Document.update_nodes(MDEx.HtmlBlock, &strip_dangerous_raw_html/1)
|
|> MDEx.Document.update_nodes(MDEx.HtmlBlock, &strip_dangerous_raw_html/1)
|
||||||
|> MDEx.Document.update_nodes(MDEx.HtmlInline, &strip_dangerous_raw_html/1)
|
|> MDEx.Document.update_nodes(MDEx.HtmlInline, &strip_dangerous_raw_html/1)
|
||||||
|> MDEx.to_html!()
|
|> MDEx.to_html!()
|
||||||
|
|
@ -176,6 +178,24 @@ defmodule Pleroma.Formatter do
|
||||||
String.ends_with?(markdown, ">")
|
String.ends_with?(markdown, ">")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defp maybe_strip_email_autolinks(document, true) do
|
||||||
|
MDEx.Document.update_nodes(document, MDEx.Link, &strip_email_autolink/1)
|
||||||
|
end
|
||||||
|
|
||||||
|
defp maybe_strip_email_autolinks(document, _), do: document
|
||||||
|
|
||||||
|
defp strip_email_autolink(
|
||||||
|
%MDEx.Link{
|
||||||
|
nodes: [%MDEx.Text{literal: literal, sourcepos: text_sourcepos} = text],
|
||||||
|
url: "mailto:" <> email,
|
||||||
|
sourcepos: sourcepos
|
||||||
|
} = link
|
||||||
|
) do
|
||||||
|
if email == literal and sourcepos == text_sourcepos, do: text, else: link
|
||||||
|
end
|
||||||
|
|
||||||
|
defp strip_email_autolink(link), do: link
|
||||||
|
|
||||||
defp strip_dangerous_raw_html(%{literal: literal} = node) do
|
defp strip_dangerous_raw_html(%{literal: literal} = node) do
|
||||||
literal =
|
literal =
|
||||||
literal
|
literal
|
||||||
|
|
|
||||||
|
|
@ -324,7 +324,7 @@ defmodule Pleroma.Web.CommonAPI.Utils do
|
||||||
|
|
||||||
def format_input(text, "text/x.misskeymarkdown", options) do
|
def format_input(text, "text/x.misskeymarkdown", options) do
|
||||||
text
|
text
|
||||||
|> Formatter.markdown_to_html(%{breaks: true})
|
|> Formatter.markdown_to_html(%{breaks: true, strip_email_autolinks: true})
|
||||||
|> safe_mfm_to_html()
|
|> safe_mfm_to_html()
|
||||||
|> Formatter.linkify(options)
|
|> Formatter.linkify(options)
|
||||||
|> Formatter.html_escape("text/x.misskeymarkdown")
|
|> Formatter.html_escape("text/x.misskeymarkdown")
|
||||||
|
|
|
||||||
|
|
@ -216,6 +216,39 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.ArticleNotePageValidatorTest
|
||||||
assert content =~ "@uncached"
|
assert content =~ "@uncached"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "a Misskey MFM remote mention is not rendered as an email link" do
|
||||||
|
remote_user = insert(:user, ap_id: "https://mk.absturztau.be/users/alice")
|
||||||
|
mentioned_user = insert(:user, ap_id: "https://movsw.0x0.st/users/bob")
|
||||||
|
|
||||||
|
note = %{
|
||||||
|
"id" => "https://mk.absturztau.be/notes/1",
|
||||||
|
"type" => "Note",
|
||||||
|
"actor" => remote_user.ap_id,
|
||||||
|
"attributedTo" => remote_user.ap_id,
|
||||||
|
"to" => ["https://www.w3.org/ns/activitystreams#Public"],
|
||||||
|
"cc" => [mentioned_user.ap_id],
|
||||||
|
"content" => ~s(<p>@<a href="mailto:bob@movsw.0x0.st">bob@movsw.0x0.st</a> hello</p>),
|
||||||
|
"context" => Utils.generate_context_id(),
|
||||||
|
"tag" => [
|
||||||
|
%{
|
||||||
|
"type" => "Mention",
|
||||||
|
"name" => "@bob@movsw.0x0.st",
|
||||||
|
"href" => mentioned_user.ap_id
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source" => %{
|
||||||
|
"content" => "@bob@movsw.0x0.st hello",
|
||||||
|
"mediaType" => "text/x.misskeymarkdown"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
%{valid?: true, changes: %{content: content}} =
|
||||||
|
ArticleNotePageValidator.cast_and_validate(note)
|
||||||
|
|
||||||
|
assert content =~ ~s(href="#{mentioned_user.ap_id}")
|
||||||
|
refute content =~ "mailto:"
|
||||||
|
end
|
||||||
|
|
||||||
test "a Misskey MFM note drops oversized source content instead of parsing it" do
|
test "a Misskey MFM note drops oversized source content instead of parsing it" do
|
||||||
user = insert(:user, ap_id: "https://misskey.example/users/oversized")
|
user = insert(:user, ap_id: "https://misskey.example/users/oversized")
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -125,6 +125,14 @@ defmodule Pleroma.Web.CommonAPI.UtilsTest do
|
||||||
assert output == expected
|
assert output == expected
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "MFM keeps bare URLs containing Markdown delimiters intact" do
|
||||||
|
url = "https://example.org/a*b*c"
|
||||||
|
|
||||||
|
{output, [], []} = Utils.format_input(url, "text/x.misskeymarkdown")
|
||||||
|
|
||||||
|
assert output == ~s(<p><a href="#{url}">#{url}</a></p>)
|
||||||
|
end
|
||||||
|
|
||||||
test "works for bare text/bbcode" do
|
test "works for bare text/bbcode" do
|
||||||
text = "[b]hello world[/b]"
|
text = "[b]hello world[/b]"
|
||||||
expected = "<strong>hello world</strong>"
|
expected = "<strong>hello world</strong>"
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue