From ea343b634da3fc598f2753e7e539e4091eda34e5 Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Tue, 29 Mar 2022 13:18:44 +0100 Subject: [PATCH] core: fix multiline mardown (#478) * core: fix multiline mardown * add test --- src/Simplex/Chat/Markdown.hs | 6 +++++- tests/MarkdownTests.hs | 11 +++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/Simplex/Chat/Markdown.hs b/src/Simplex/Chat/Markdown.hs index fe5d0e0d38..8242d46510 100644 --- a/src/Simplex/Chat/Markdown.hs +++ b/src/Simplex/Chat/Markdown.hs @@ -12,6 +12,7 @@ import qualified Data.Attoparsec.Text as A import Data.Char (isDigit) import Data.Either (fromRight) import Data.Functor (($>)) +import Data.List (intercalate) import Data.Maybe (fromMaybe, isNothing) import Data.String import Data.Text (Text) @@ -83,6 +84,9 @@ data FormattedText = FormattedText {format :: Maybe Format, text :: Text} instance ToJSON FormattedText where toEncoding = J.genericToEncoding J.defaultOptions {J.omitNothingFields = True} +instance IsString FormattedText where + fromString = FormattedText Nothing . T.pack + type MarkdownList = [FormattedText] unmarked :: Text -> Markdown @@ -90,7 +94,7 @@ unmarked = Markdown Nothing parseMaybeMarkdownList :: Text -> Maybe MarkdownList parseMaybeMarkdownList s = - let m = markdownToList $ parseMarkdown s + let m = intercalate ["\n"] . map (markdownToList . parseMarkdown) $ T.lines s in if all (isNothing . format) m then Nothing else Just m parseMarkdownList :: Text -> MarkdownList diff --git a/tests/MarkdownTests.hs b/tests/MarkdownTests.hs index 168c1aeb75..1782d74a3f 100644 --- a/tests/MarkdownTests.hs +++ b/tests/MarkdownTests.hs @@ -16,6 +16,7 @@ markdownTests = do textWithUri textWithEmail textWithPhone + multilineMarkdownList textFormat :: Spec textFormat = describe "text format (bold)" do @@ -180,3 +181,13 @@ textWithPhone = describe "text with Phone" do parseMarkdown "test 077777 test" `shouldBe` "test 077777 test" it "ignored as markdown (double spaces)" $ parseMarkdown "test 07777 777 777 test" `shouldBe` "test 07777 777 777 test" + +uri' :: Text -> FormattedText +uri' = FormattedText $ Just Uri + +multilineMarkdownList :: Spec +multilineMarkdownList = describe "multiline markdown" do + it "correct markdown" do + parseMaybeMarkdownList "http://simplex.chat\nhttp://app.simplex.chat" `shouldBe` Just [uri' "http://simplex.chat", "\n", uri' "http://app.simplex.chat"] + it "no markdown" do + parseMaybeMarkdownList "not a\nmarkdown" `shouldBe` Nothing