core: fix multiline mardown (#478)

* core: fix multiline mardown

* add test
This commit is contained in:
Evgeny Poberezkin
2022-03-29 13:18:44 +01:00
committed by GitHub
parent 41a2e0b1d5
commit ea343b634d
2 changed files with 16 additions and 1 deletions

View File

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

View File

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