From 2bd880be88839746040cd69e60380ab42a005ec4 Mon Sep 17 00:00:00 2001 From: William Pitcock Date: Fri, 26 Apr 2019 09:22:26 +0000 Subject: [PATCH 1/3] mix: add bbcode dependency --- mix.exs | 1 + mix.lock | 1 + 2 files changed, 2 insertions(+) diff --git a/mix.exs b/mix.exs index 15e182239..efaa06a1c 100644 --- a/mix.exs +++ b/mix.exs @@ -84,6 +84,7 @@ defp deps do {:ex_aws, "~> 2.0"}, {:ex_aws_s3, "~> 2.0"}, {:earmark, "~> 1.3"}, + {:bbcode, "~> 0.1"}, {:ex_machina, "~> 2.3", only: :test}, {:credo, "~> 0.9.3", only: [:dev, :test]}, {:mock, "~> 0.3.1", only: :test}, diff --git a/mix.lock b/mix.lock index d494cc82d..979d599b4 100644 --- a/mix.lock +++ b/mix.lock @@ -2,6 +2,7 @@ "accept": {:hex, :accept, "0.3.5", "b33b127abca7cc948bbe6caa4c263369abf1347cfa9d8e699c6d214660f10cd1", [:rebar3], [], "hexpm"}, "auto_linker": {:git, "https://git.pleroma.social/pleroma/auto_linker.git", "90613b4bae875a3610c275b7056b61ffdd53210d", [ref: "90613b4bae875a3610c275b7056b61ffdd53210d"]}, "base64url": {:hex, :base64url, "0.0.1", "36a90125f5948e3afd7be97662a1504b934dd5dac78451ca6e9abf85a10286be", [:rebar], [], "hexpm"}, + "bbcode": {:hex, :bbcode, "0.1.0", "400e618b640b635261611d7fb7f79d104917fc5b084aae371ab6b08477cb035b", [:mix], [{:nimble_parsec, "~> 0.5", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm"}, "bunt": {:hex, :bunt, "0.2.0", "951c6e801e8b1d2cbe58ebbd3e616a869061ddadcc4863d0a2182541acae9a38", [:mix], [], "hexpm"}, "cachex": {:hex, :cachex, "3.0.2", "1351caa4e26e29f7d7ec1d29b53d6013f0447630bbf382b4fb5d5bad0209f203", [:mix], [{:eternal, "~> 1.2", [hex: :eternal, repo: "hexpm", optional: false]}, {:unsafe, "~> 1.0", [hex: :unsafe, repo: "hexpm", optional: false]}], "hexpm"}, "calendar": {:hex, :calendar, "0.17.4", "22c5e8d98a4db9494396e5727108dffb820ee0d18fed4b0aa8ab76e4f5bc32f1", [:mix], [{:tzdata, "~> 0.5.8 or ~> 0.1.201603", [hex: :tzdata, repo: "hexpm", optional: false]}], "hexpm"}, From 501af917b5a9611a4b1fabb4944b3af96b676568 Mon Sep 17 00:00:00 2001 From: William Pitcock Date: Fri, 26 Apr 2019 10:17:57 +0000 Subject: [PATCH 2/3] add support for bbcode --- config/config.exs | 3 ++- lib/pleroma/web/common_api/utils.ex | 12 ++++++++++++ test/web/common_api/common_api_utils_test.exs | 16 ++++++++++++++++ 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/config/config.exs b/config/config.exs index a1cca06f8..1a9738cff 100644 --- a/config/config.exs +++ b/config/config.exs @@ -221,7 +221,8 @@ allowed_post_formats: [ "text/plain", "text/html", - "text/markdown" + "text/markdown", + "text/bbcode" ], mrf_transparency: true, autofollowed_nicknames: [], diff --git a/lib/pleroma/web/common_api/utils.ex b/lib/pleroma/web/common_api/utils.ex index 887f878c4..1dfe50b40 100644 --- a/lib/pleroma/web/common_api/utils.ex +++ b/lib/pleroma/web/common_api/utils.ex @@ -182,6 +182,18 @@ def format_input(text, "text/plain", options) do end).() end + @doc """ + Formatting text as BBCode. + """ + def format_input(text, "text/bbcode", options) do + text + |> String.replace(~r/\r/, "") + |> Formatter.html_escape("text/plain") + |> BBCode.to_html() + |> (fn {:ok, html} -> html end).() + |> Formatter.linkify(options) + end + @doc """ Formatting text to html. """ diff --git a/test/web/common_api/common_api_utils_test.exs b/test/web/common_api/common_api_utils_test.exs index 837a66063..df9955d5d 100644 --- a/test/web/common_api/common_api_utils_test.exs +++ b/test/web/common_api/common_api_utils_test.exs @@ -119,6 +119,22 @@ test "works for bare text/markdown" do assert output == expected end + test "works for bare text/bbcode" do + text = "[b]hello world[/b]" + expected = "hello world" + + {output, [], []} = Utils.format_input(text, "text/bbcode") + + assert output == expected + + text = "[b]hello world![/b]\n\nsecond paragraph!" + expected = "hello world!

second paragraph!" + + {output, [], []} = Utils.format_input(text, "text/bbcode") + + assert output == expected + end + test "works for text/markdown with mentions" do {:ok, user} = UserBuilder.insert(%{nickname: "user__test", ap_id: "http://foo.com/user__test"}) From ef52aa0e6647d00f5ff984d655ef4b27ab3e6b1a Mon Sep 17 00:00:00 2001 From: William Pitcock Date: Fri, 26 Apr 2019 22:38:56 +0000 Subject: [PATCH 3/3] tests: fix up for changed bbcode library output and verify html is properly escaped --- test/web/common_api/common_api_utils_test.exs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/test/web/common_api/common_api_utils_test.exs b/test/web/common_api/common_api_utils_test.exs index df9955d5d..ab4c62b35 100644 --- a/test/web/common_api/common_api_utils_test.exs +++ b/test/web/common_api/common_api_utils_test.exs @@ -128,7 +128,16 @@ test "works for bare text/bbcode" do assert output == expected text = "[b]hello world![/b]\n\nsecond paragraph!" - expected = "hello world!

second paragraph!" + expected = "hello world!
\n
\nsecond paragraph!" + + {output, [], []} = Utils.format_input(text, "text/bbcode") + + assert output == expected + + text = "[b]hello world![/b]\n\nsecond paragraph!" + + expected = + "hello world!
\n
\n<strong>second paragraph!</strong>" {output, [], []} = Utils.format_input(text, "text/bbcode")