Skip to content

Commit

Permalink
Merge pull request #379 from MohamedRejeb/1.x
Browse files Browse the repository at this point in the history
Fix spaces around span style break formatting in resulting markdown
  • Loading branch information
MohamedRejeb authored Oct 1, 2024
2 parents 69fa04d + d60ba92 commit 66b9a45
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ internal object RichTextStateMarkdownParser : RichTextStateParser<String> {
}
}

return builder.toString()
return correctMarkdownText(builder.toString())
}

@OptIn(ExperimentalRichTextApi::class)
Expand All @@ -339,6 +339,9 @@ internal object RichTextStateMarkdownParser : RichTextStateParser<String> {
// Check if span is empty
if (richSpan.isEmpty()) return ""

// Check if span is blank
val isBlank = richSpan.isBlank()

// Convert span style to CSS string
val markdownOpen = mutableListOf<String>()
val markdownClose = mutableListOf<String>()
Expand All @@ -364,7 +367,8 @@ internal object RichTextStateMarkdownParser : RichTextStateParser<String> {
}

// Append markdown open
stringBuilder.append(markdownOpen.joinToString(separator = ""))
if (!isBlank)
stringBuilder.append(markdownOpen.joinToString(separator = ""))

// Apply rich span style to markdown
val spanMarkdown = decodeMarkdownElementFromRichSpan(richSpan.text, richSpan.richSpanStyle)
Expand All @@ -378,7 +382,8 @@ internal object RichTextStateMarkdownParser : RichTextStateParser<String> {
}

// Append markdown close
stringBuilder.append(markdownClose.reversed().joinToString(separator = ""))
if (!isBlank)
stringBuilder.append(markdownClose.reversed().joinToString(separator = ""))

return stringBuilder.toString()
}
Expand Down Expand Up @@ -468,7 +473,7 @@ internal object RichTextStateMarkdownParser : RichTextStateParser<String> {
}

/**
* Decodes HTML elements from [RichSpan].
* Decodes Markdown elements from [RichSpan].
*/
@OptIn(ExperimentalRichTextApi::class)
private fun decodeMarkdownElementFromRichSpan(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import com.mohamedrejeb.richeditor.paragraph.RichParagraph
import kotlin.test.Test
import kotlin.test.assertEquals

@ExperimentalRichTextApi
class RichTextStateMarkdownParserDecodeTest {

/**
Expand Down Expand Up @@ -174,4 +175,64 @@ class RichTextStateMarkdownParserDecodeTest {
)
}

@Test
fun testDecodeStyledTextWithSpacesInStyleEdges1() {
val state = RichTextState(
initialRichParagraphList = listOf(
RichParagraph().also {
it.children.add(
RichSpan(
text = " Hello ",
paragraph = it,
spanStyle = SpanStyle(fontWeight = FontWeight.Bold)
),
)

it.children.add(
RichSpan(
text = "World!",
paragraph = it,
),
)
},
)
)

assertEquals(
expected = " **Hello** World!",
actual = state.toMarkdown()
)
}

@Test
fun testDecodeStyledTextWithSpacesInStyleEdges2() {
val state = RichTextState(
initialRichParagraphList = listOf(
RichParagraph().also {
it.children.add(
RichSpan(
text = " Hello ",
paragraph = it,
spanStyle = SpanStyle(fontWeight = FontWeight.Bold)
).also {
it.children.add(
RichSpan(
text = " World! ",
paragraph = it.paragraph,
parent = it,
spanStyle = SpanStyle(fontStyle = FontStyle.Italic)
),
)
},
)
},
)
)

assertEquals(
expected = " **Hello *World!*** ",
actual = state.toMarkdown()
)
}

}

0 comments on commit 66b9a45

Please sign in to comment.