diff --git a/src/Simplex/Chat/Markdown.hs b/src/Simplex/Chat/Markdown.hs index cd2e337aff..c877a22c2b 100644 --- a/src/Simplex/Chat/Markdown.hs +++ b/src/Simplex/Chat/Markdown.hs @@ -211,7 +211,7 @@ markdownP = mconcat <$> A.many' fragmentP Just c -> case c of ' ' -> unmarked <$> A.takeWhile (== ' ') '+' -> phoneP <|> wordP - '*' -> formattedP '*' Bold + '*' -> boldP <|> formattedP '*' Bold '_' -> formattedP '_' Italic '~' -> formattedP '~' StrikeThrough '`' -> formattedP '`' Snippet @@ -233,6 +233,12 @@ markdownP = mconcat <$> A.many' fragmentP | T.null s || T.head s == ' ' || T.last s == ' ' = unmarked $ c `T.cons` s `T.snoc` c | otherwise = markdown f s + boldP :: Parser Markdown + boldP = do + s <- A.string "**" *> A.takeTill (== '*') <* A.string "**" + if T.null s || T.head s == ' ' || T.last s == ' ' + then fail "not bold" + else pure $ markdown Bold s secretP :: Parser Markdown secretP = secret <$?> ((,,) <$> A.takeWhile (== '#') <*> A.takeTill (== '#') <*> A.takeWhile1 (== '#')) secret :: (Text, Text, Text) -> Either String Markdown diff --git a/tests/MarkdownTests.hs b/tests/MarkdownTests.hs index e315b59f5e..1f9936044c 100644 --- a/tests/MarkdownTests.hs +++ b/tests/MarkdownTests.hs @@ -74,6 +74,19 @@ textFormat = describe "text format (bold)" do <==> "this is " <> bold "bold" <> " " "this is *bold* " <==> "this is " <> bold "bold" <> " " + it "correct markdown with double asterisk" do + "this is **bold formatted** text" + ==> "this is " <> bold "bold formatted" <> " text" + "**bold formatted** text" + ==> bold "bold formatted" <> " text" + "this is **bold**" + ==> "this is " <> bold "bold" + " **bold** text" + ==> " " <> bold "bold" <> " text" + "this is **bold** " + ==> "this is " <> bold "bold" <> " " + "this is **bold** " + ==> "this is " <> bold "bold" <> " " it "ignored as markdown" do "this is * unformatted * text" <==> "this is * unformatted * text" @@ -81,8 +94,16 @@ textFormat = describe "text format (bold)" do <==> "this is *unformatted * text" "this is * unformatted* text" <==> "this is * unformatted* text" - "this is **unformatted** text" - <==> "this is **unformatted** text" + "this is ** unformatted ** text" + <==> "this is ** unformatted ** text" + "this is **unformatted ** text" + <==> "this is **unformatted ** text" + "this is ** unformatted** text" + <==> "this is ** unformatted** text" + "this is **unformatted text" + <==> "this is **unformatted text" + "this is **unformatted* text" + <==> "this is **unformatted* text" "this is*unformatted* text" <==> "this is*unformatted* text" "this is *unformatted text"