From 4b57d5532a86c1f49edefd03a57025e2b26bdefb Mon Sep 17 00:00:00 2001 From: MohamedRejeb Date: Sun, 8 Dec 2024 20:41:11 +0100 Subject: [PATCH 1/2] Fix Markdown fails to parse lines with leading spaces --- .../markdown/RichTextStateMarkdownParser.kt | 5 ++++- .../RichTextStateMarkdownParserEncodeTest.kt | 20 +++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/richeditor-compose/src/commonMain/kotlin/com/mohamedrejeb/richeditor/parser/markdown/RichTextStateMarkdownParser.kt b/richeditor-compose/src/commonMain/kotlin/com/mohamedrejeb/richeditor/parser/markdown/RichTextStateMarkdownParser.kt index c4199ac4..d7ac28b3 100644 --- a/richeditor-compose/src/commonMain/kotlin/com/mohamedrejeb/richeditor/parser/markdown/RichTextStateMarkdownParser.kt +++ b/richeditor-compose/src/commonMain/kotlin/com/mohamedrejeb/richeditor/parser/markdown/RichTextStateMarkdownParser.kt @@ -183,7 +183,10 @@ internal object RichTextStateMarkdownParser : RichTextStateParser { newRichSpan.text = "$".repeat(node.endOffset - node.startOffset) } - if (node.type == GFMTokenTypes.GFM_AUTOLINK) { + if ( + node.type == GFMTokenTypes.GFM_AUTOLINK || + node.type == MarkdownTokenTypes.CODE_LINE + ) { onText(node.getTextInNode(input).toString()) } }, diff --git a/richeditor-compose/src/commonTest/kotlin/com.mohamedrejeb.richeditor/parser/markdown/RichTextStateMarkdownParserEncodeTest.kt b/richeditor-compose/src/commonTest/kotlin/com.mohamedrejeb.richeditor/parser/markdown/RichTextStateMarkdownParserEncodeTest.kt index e5d50242..cc6d7a47 100644 --- a/richeditor-compose/src/commonTest/kotlin/com.mohamedrejeb.richeditor/parser/markdown/RichTextStateMarkdownParserEncodeTest.kt +++ b/richeditor-compose/src/commonTest/kotlin/com.mohamedrejeb.richeditor/parser/markdown/RichTextStateMarkdownParserEncodeTest.kt @@ -427,4 +427,24 @@ class RichTextStateMarkdownParserEncodeTest { assertEquals("Prompt\nEmphasis", state.toText()) } + @Test + fun testEncodeWithLeadingSpaces() { + val markdown = """First line + + indented line""" + + val state = RichTextStateMarkdownParser.encode(markdown) + + val parsedString = state.toText() + + assertEquals( + expected = """ + First line + + indented line + """.trimIndent(), + actual = parsedString + ) + } + } \ No newline at end of file From fb15ff4dc010d1179c0d2176905ab8637fc8d2b4 Mon Sep 17 00:00:00 2001 From: MohamedRejeb Date: Sun, 8 Dec 2024 21:21:39 +0100 Subject: [PATCH 2/2] Fix Issue on parsing Markdown with code span split between two lines --- .../markdown/RichTextStateMarkdownParser.kt | 2 ++ .../RichTextStateMarkdownParserEncodeTest.kt | 26 +++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/richeditor-compose/src/commonMain/kotlin/com/mohamedrejeb/richeditor/parser/markdown/RichTextStateMarkdownParser.kt b/richeditor-compose/src/commonMain/kotlin/com/mohamedrejeb/richeditor/parser/markdown/RichTextStateMarkdownParser.kt index d7ac28b3..b0f71f34 100644 --- a/richeditor-compose/src/commonMain/kotlin/com/mohamedrejeb/richeditor/parser/markdown/RichTextStateMarkdownParser.kt +++ b/richeditor-compose/src/commonMain/kotlin/com/mohamedrejeb/richeditor/parser/markdown/RichTextStateMarkdownParser.kt @@ -60,6 +60,8 @@ internal object RichTextStateMarkdownParser : RichTextStateParser { } fun onText(text: String) { + val text = text.replace('\n', ' ') + if (text.isEmpty()) return if (richParagraphList.isEmpty()) diff --git a/richeditor-compose/src/commonTest/kotlin/com.mohamedrejeb.richeditor/parser/markdown/RichTextStateMarkdownParserEncodeTest.kt b/richeditor-compose/src/commonTest/kotlin/com.mohamedrejeb.richeditor/parser/markdown/RichTextStateMarkdownParserEncodeTest.kt index cc6d7a47..9a3e0ec2 100644 --- a/richeditor-compose/src/commonTest/kotlin/com.mohamedrejeb.richeditor/parser/markdown/RichTextStateMarkdownParserEncodeTest.kt +++ b/richeditor-compose/src/commonTest/kotlin/com.mohamedrejeb.richeditor/parser/markdown/RichTextStateMarkdownParserEncodeTest.kt @@ -447,4 +447,30 @@ class RichTextStateMarkdownParserEncodeTest { ) } + @Test + fun testEncodeInlineCodeInDifferentLines() { + val markdown = + """ + Hello `World! + Kotlin` MP + """.trimIndent() + + val state = RichTextStateMarkdownParser.encode(markdown) + + val parsedString = state.toText() + + assertEquals( + expected = + """ + Hello World! Kotlin MP + """.trimIndent(), + actual = parsedString, + ) + + assertEquals( + expected = 1, + actual = state.richParagraphList.size, + ) + } + } \ No newline at end of file