[parsing/mfm] Fix handling of newlines surrounding code blocks

This commit is contained in:
Laura Hausmann 2024-09-26 00:33:26 +02:00
parent 4c21cb5a1c
commit 0fc667527f
No known key found for this signature in database
GPG key ID: D044E84C5BE01605
3 changed files with 7 additions and 3 deletions

View file

@ -15,9 +15,9 @@ public static class MfmSerializer
{
case MfmCodeBlockNode mfmCodeBlockNode:
{
result.Append($"```{mfmCodeBlockNode.Language?.Value ?? ""}\n");
result.Append($"\n```{mfmCodeBlockNode.Language?.Value ?? ""}\n");
result.Append(mfmCodeBlockNode.Code);
result.Append("\n```");
result.Append("\n```\n");
break;
}
case MfmMathBlockNode mfmMathBlockNode:

View file

@ -202,11 +202,13 @@ module private MfmParser =
|>> fun v -> MfmInlineCodeNode(v) :> MfmNode
let codeBlockNode =
previousCharSatisfiesNot isNotWhitespace
opt (previousCharSatisfies isNewline >>. skipNewline)
>>. previousCharSatisfiesNot isNotNewline
>>. skipString "```"
>>. opt (many1CharsTill asciiLetter (lookAhead newline))
.>>. (skipNewline
>>. manyCharsTill anyChar (attempt (skipNewline >>. skipString "```")))
.>> (opt <| attempt (skipNewline >>. nextCharSatisfies isNewline))
|>> fun (lang: string option, code: string) -> MfmCodeBlockNode(code, lang) :> MfmNode
let mathNode =

View file

@ -142,9 +142,11 @@ public class MfmTests
{
const string input = """
test 123
```
this is a code block
```
test 123
""";
List<MfmNode> expected =