Skip to content

Commit

Permalink
Merge pull request #335 from MohamedRejeb/1.x
Browse files Browse the repository at this point in the history
Fix $ sign disappears issue when trying to encode RichTextState from Markdown
  • Loading branch information
MohamedRejeb authored Aug 23, 2024
2 parents 14286f0 + dbac6e0 commit a507cec
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import org.intellij.markdown.ast.ASTNode
import org.intellij.markdown.ast.findChildOfType
import org.intellij.markdown.ast.getTextInNode
import org.intellij.markdown.flavours.gfm.GFMElementTypes
import org.intellij.markdown.flavours.gfm.GFMTokenTypes

internal object RichTextStateMarkdownParser : RichTextStateParser<String> {

Expand Down Expand Up @@ -128,6 +129,12 @@ internal object RichTextStateMarkdownParser : RichTextStateParser<String> {
currentRichParagraph.children.add(newRichSpan)
currentRichSpan = newRichSpan
}

if (
openedNodes.getOrNull(openedNodes.lastIndex - 1)?.type != GFMElementTypes.INLINE_MATH &&
node.type == GFMTokenTypes.DOLLAR
)
newRichSpan.text = "$".repeat(node.endOffset - node.startOffset)
}
},
onCloseNode = { node ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,45 @@ class RichTextStateMarkdownParserTest {
)
}

@Test
fun testEncodeMarkdownWithSingleDollar() {
val markdown = "Hello World $100!"
val expectedText = "Hello World $100!"
val state = RichTextStateMarkdownParser.encode(markdown)
val actualText = state.annotatedString.text

assertEquals(
expected = expectedText,
actual = actualText,
)
}

@Test
fun testEncodeMarkdownWithDoubleDollar() {
val markdown = "Hello World $$100!"
val expectedText = "Hello World $$100!"
val state = RichTextStateMarkdownParser.encode(markdown)
val actualText = state.annotatedString.text

assertEquals(
expected = expectedText,
actual = actualText,
)
}

@Test
fun testEncodeMarkdownWithInlineMath() {
val markdown = "Hello World \$100$!"
val expectedText = "Hello World 100!"
val state = RichTextStateMarkdownParser.encode(markdown)
val actualText = state.annotatedString.text

assertEquals(
expected = expectedText,
actual = actualText,
)
}

/**
* Decode tests
*/
Expand Down

0 comments on commit a507cec

Please sign in to comment.