From 19a27ff006b7a61186b5153513ca7c90a16cc3bc Mon Sep 17 00:00:00 2001 From: floatingghost Date: Mon, 1 Aug 2022 12:46:52 +0000 Subject: [PATCH] allow small/center tags in misskeymarkdown (#132) Reviewed-on: https://akkoma.dev/AkkomaGang/akkoma/pulls/132 --- lib/pleroma/formatter.ex | 7 ++++++- lib/pleroma/web/common_api/utils.ex | 12 ++++++++++-- priv/scrubbers/default.ex | 3 +++ .../article_note_page_validator_test.exs | 9 ++++++--- 4 files changed, 25 insertions(+), 6 deletions(-) diff --git a/lib/pleroma/formatter.ex b/lib/pleroma/formatter.ex index ad29d21d7..575bf9b2d 100644 --- a/lib/pleroma/formatter.ex +++ b/lib/pleroma/formatter.ex @@ -136,7 +136,12 @@ def html_escape(text, "text/html") do HTML.filter_tags(text) end - def html_escape(text, format) when format in ["text/plain", "text/x.misskeymarkdown"] do + def html_escape(text, "text/x.misskeymarkdown") do + text + |> HTML.filter_tags() + end + + def html_escape(text, "text/plain") do Regex.split(@link_regex, text, include_captures: true) |> Enum.map_every(2, fn chunk -> {:safe, part} = Phoenix.HTML.html_escape(chunk) diff --git a/lib/pleroma/web/common_api/utils.ex b/lib/pleroma/web/common_api/utils.ex index f5bc3acf5..826160a23 100644 --- a/lib/pleroma/web/common_api/utils.ex +++ b/lib/pleroma/web/common_api/utils.ex @@ -259,8 +259,7 @@ def format_input(text, format, options \\ []) @doc """ Formatting text to plain text, BBCode, HTML, or Markdown """ - def format_input(text, format, options) - when format in ["text/plain", "text/x.misskeymarkdown"] do + def format_input(text, "text/plain", options) do text |> Formatter.html_escape("text/plain") |> Formatter.linkify(options) @@ -284,6 +283,15 @@ def format_input(text, "text/html", options) do |> Formatter.linkify(options) end + def format_input(text, "text/x.misskeymarkdown", options) do + text + |> Formatter.linkify(options) + |> Formatter.html_escape("text/x.misskeymarkdown") + |> (fn {text, mentions, tags} -> + {String.replace(text, ~r/\r?\n/, "
"), mentions, tags} + end).() + end + def format_input(text, "text/markdown", options) do text |> Formatter.mentions_escape(options) diff --git a/priv/scrubbers/default.ex b/priv/scrubbers/default.ex index 4694a92a5..478cbde9b 100644 --- a/priv/scrubbers/default.ex +++ b/priv/scrubbers/default.ex @@ -97,5 +97,8 @@ defmodule Pleroma.HTML.Scrubber.Default do Meta.allow_tag_with_these_attributes(:font, ["face"]) end + Meta.allow_tag_with_these_attributes(:center, []) + Meta.allow_tag_with_these_attributes(:small, []) + Meta.strip_everything_not_covered() end diff --git a/test/pleroma/web/activity_pub/object_validators/article_note_page_validator_test.exs b/test/pleroma/web/activity_pub/object_validators/article_note_page_validator_test.exs index 8b3982916..09cd1a964 100644 --- a/test/pleroma/web/activity_pub/object_validators/article_note_page_validator_test.exs +++ b/test/pleroma/web/activity_pub/object_validators/article_note_page_validator_test.exs @@ -130,18 +130,21 @@ test "a misskey MFM status with a _misskey_content field should work and be link |> Jason.decode!() expected_content = - "@akkoma_user linkifylink #dancedance $[jelly mfm goes here]

## aaa" + "@akkoma_user linkifylink #dancedance $[jelly mfm goes here]

## aaa" + + changes = ArticleNotePageValidator.cast_and_validate(note) %{ valid?: true, changes: %{ - content: ^expected_content, source: %{ "content" => "@akkoma_user linkifylink #dancedance $[jelly mfm goes here] \n\n## aaa", "mediaType" => "text/x.misskeymarkdown" } } - } = ArticleNotePageValidator.cast_and_validate(note) + } = changes + + assert changes.changes[:content] == expected_content end end end