Skip to content

Commit 591dfb3

Browse files
committed
Merge branch 'sroebert-bugfix/unescape-leaf-node-text' into develop
2 parents 16b92fd + 797b74c commit 591dfb3

File tree

6 files changed

+30
-22
lines changed

6 files changed

+30
-22
lines changed

app/src/screenshotTest/kotlin/com.mikepenz.markdown.ui/SnapshotTests.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,11 @@ private val MARKDOWN_DEFAULT = """
6363
6464
###### This is an H6
6565
66-
This is a paragraph with some *italic* and **bold** text.
66+
This is a paragraph with some *italic* and **bold** text\.
6767
68-
This is a paragraph with some `inline code`.
68+
This is a paragraph with some `inline code`\.
6969
70-
This is a paragraph with a [link](https://www.jetbrains.com/).
70+
This is a paragraph with a [link](https://www.jetbrains.com/)\.
7171
7272
This is a code block:
7373
```kotlin

multiplatform-markdown-renderer/src/commonMain/kotlin/com/mikepenz/markdown/compose/components/MarkdownComponents.kt

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ import androidx.compose.ui.Modifier
99
import com.mikepenz.markdown.compose.LocalReferenceLinkHandler
1010
import com.mikepenz.markdown.compose.elements.*
1111
import com.mikepenz.markdown.model.MarkdownTypography
12+
import com.mikepenz.markdown.utils.getUnescapedTextInNode
1213
import org.intellij.markdown.IElementType
1314
import org.intellij.markdown.MarkdownElementTypes
1415
import org.intellij.markdown.MarkdownTokenTypes
1516
import org.intellij.markdown.ast.ASTNode
1617
import org.intellij.markdown.ast.findChildOfType
17-
import org.intellij.markdown.ast.getTextInNode
1818

1919
typealias MarkdownComponent = @Composable ColumnScope.(MarkdownComponentModel) -> Unit
2020

@@ -29,7 +29,7 @@ data class MarkdownComponentModel(
2929
val typography: MarkdownTypography,
3030
)
3131

32-
private fun MarkdownComponentModel.getTextInNode() = node.getTextInNode(content)
32+
private fun MarkdownComponentModel.getUnescapedTextInNode() = node.getUnescapedTextInNode(content)
3333

3434
fun markdownComponents(
3535
text: MarkdownComponent = CurrentComponentsBridge.text,
@@ -130,7 +130,7 @@ private class DefaultMarkdownComponents(
130130
*/
131131
object CurrentComponentsBridge {
132132
val text: MarkdownComponent = {
133-
MarkdownText(it.getTextInNode().toString())
133+
MarkdownText(it.getUnescapedTextInNode())
134134
}
135135
val eol: MarkdownComponent = { }
136136
val codeFence: MarkdownComponent = {
@@ -184,11 +184,10 @@ object CurrentComponentsBridge {
184184
}
185185
val linkDefinition: MarkdownComponent = {
186186
val linkLabel =
187-
it.node.findChildOfType(MarkdownElementTypes.LINK_LABEL)?.getTextInNode(it.content)
188-
?.toString()
187+
it.node.findChildOfType(MarkdownElementTypes.LINK_LABEL)?.getUnescapedTextInNode(it.content)
189188
if (linkLabel != null) {
190189
val destination = it.node.findChildOfType(MarkdownElementTypes.LINK_DESTINATION)
191-
?.getTextInNode(it.content)?.toString()
190+
?.getUnescapedTextInNode(it.content)
192191
LocalReferenceLinkHandler.current.store(linkLabel, destination)
193192
}
194193
}

multiplatform-markdown-renderer/src/commonMain/kotlin/com/mikepenz/markdown/compose/elements/MarkdownImage.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ import androidx.compose.foundation.Image
44
import androidx.compose.runtime.Composable
55
import com.mikepenz.markdown.compose.LocalImageTransformer
66
import com.mikepenz.markdown.utils.findChildOfTypeRecursive
7+
import com.mikepenz.markdown.utils.getUnescapedTextInNode
78
import org.intellij.markdown.MarkdownElementTypes
89
import org.intellij.markdown.ast.ASTNode
9-
import org.intellij.markdown.ast.getTextInNode
1010

1111
@Composable
1212
fun MarkdownImage(content: String, node: ASTNode) {
1313

14-
val link = node.findChildOfTypeRecursive(MarkdownElementTypes.LINK_DESTINATION)?.getTextInNode(content)?.toString() ?: return
14+
val link = node.findChildOfTypeRecursive(MarkdownElementTypes.LINK_DESTINATION)?.getUnescapedTextInNode(content) ?: return
1515

1616
LocalImageTransformer.current.transform(link)?.let { imageData ->
1717
Image(

multiplatform-markdown-renderer/src/commonMain/kotlin/com/mikepenz/markdown/compose/elements/MarkdownList.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ import androidx.compose.ui.Modifier
99
import androidx.compose.ui.text.TextStyle
1010
import com.mikepenz.markdown.compose.*
1111
import com.mikepenz.markdown.compose.elements.material.MarkdownBasicText
12+
import com.mikepenz.markdown.utils.getUnescapedTextInNode
1213
import org.intellij.markdown.MarkdownElementTypes
1314
import org.intellij.markdown.MarkdownElementTypes.ORDERED_LIST
1415
import org.intellij.markdown.MarkdownElementTypes.UNORDERED_LIST
1516
import org.intellij.markdown.MarkdownTokenTypes.Companion.LIST_BULLET
1617
import org.intellij.markdown.MarkdownTokenTypes.Companion.LIST_NUMBER
1718
import org.intellij.markdown.ast.ASTNode
1819
import org.intellij.markdown.ast.findChildOfType
19-
import org.intellij.markdown.ast.getTextInNode
2020

2121
@Composable
2222
fun MarkdownListItems(
@@ -65,7 +65,7 @@ fun MarkdownOrderedList(
6565
MarkdownBasicText(
6666
text = orderedListHandler.transform(
6767
LIST_NUMBER,
68-
child.findChildOfType(LIST_NUMBER)?.getTextInNode(content),
68+
child.findChildOfType(LIST_NUMBER)?.getUnescapedTextInNode(content),
6969
index
7070
),
7171
style = style,
@@ -98,7 +98,7 @@ fun MarkdownBulletList(
9898
MarkdownBasicText(
9999
bulletHandler.transform(
100100
LIST_BULLET,
101-
child.findChildOfType(LIST_BULLET)?.getTextInNode(content),
101+
child.findChildOfType(LIST_BULLET)?.getUnescapedTextInNode(content),
102102
index
103103
),
104104
style = style,

multiplatform-markdown-renderer/src/commonMain/kotlin/com/mikepenz/markdown/utils/AnnotatedStringKtx.kt

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,21 @@ import org.intellij.markdown.MarkdownElementTypes
1515
import org.intellij.markdown.MarkdownTokenTypes
1616
import org.intellij.markdown.ast.ASTNode
1717
import org.intellij.markdown.ast.findChildOfType
18-
import org.intellij.markdown.ast.getTextInNode
1918
import org.intellij.markdown.flavours.gfm.GFMElementTypes
2019
import org.intellij.markdown.flavours.gfm.GFMTokenTypes
2120

2221
@Composable
2322
internal fun AnnotatedString.Builder.appendMarkdownLink(content: String, node: ASTNode) {
2423
val linkText = node.findChildOfType(MarkdownElementTypes.LINK_TEXT)?.children?.innerList()
2524
if (linkText == null) {
26-
append(node.getTextInNode(content).toString())
25+
append(node.getUnescapedTextInNode(content))
2726
return
2827
}
2928
val destination = node.findChildOfType(MarkdownElementTypes.LINK_DESTINATION)
30-
?.getTextInNode(content)
29+
?.getUnescapedTextInNode(content)
3130
?.toString()
3231
val linkLabel = node.findChildOfType(MarkdownElementTypes.LINK_LABEL)
33-
?.getTextInNode(content)?.toString()
32+
?.getUnescapedTextInNode(content)
3433
val annotation = destination ?: linkLabel
3534
if (annotation != null) pushStringAnnotation(MARKDOWN_TAG_URL, annotation)
3635
val linkColor = LocalMarkdownColors.current.linkText
@@ -46,7 +45,7 @@ internal fun AnnotatedString.Builder.appendAutoLink(content: String, node: ASTNo
4645
val targetNode = node.children.firstOrNull {
4746
it.type.name == MarkdownElementTypes.AUTOLINK.name
4847
} ?: node
49-
val destination = targetNode.getTextInNode(content).toString()
48+
val destination = targetNode.getUnescapedTextInNode(content)
5049
pushStringAnnotation(MARKDOWN_TAG_URL, (destination))
5150
val linkColor = LocalMarkdownColors.current.linkText
5251
val linkTextStyle = LocalMarkdownTypography.current.link.copy(color = linkColor).toSpanStyle()
@@ -93,7 +92,7 @@ fun AnnotatedString.Builder.buildMarkdownAnnotatedString(content: String, childr
9392
MarkdownElementTypes.IMAGE -> child.findChildOfTypeRecursive(
9493
MarkdownElementTypes.LINK_DESTINATION
9594
)?.let {
96-
appendInlineContent(MARKDOWN_TAG_IMAGE_URL, it.getTextInNode(content).toString())
95+
appendInlineContent(MARKDOWN_TAG_IMAGE_URL, it.getUnescapedTextInNode(content))
9796
}
9897

9998
MarkdownElementTypes.EMPH -> {
@@ -134,9 +133,9 @@ fun AnnotatedString.Builder.buildMarkdownAnnotatedString(content: String, childr
134133
MarkdownElementTypes.FULL_REFERENCE_LINK -> appendMarkdownLink(content, child)
135134

136135
// Token Types
137-
MarkdownTokenTypes.TEXT -> append(child.getTextInNode(content).toString())
136+
MarkdownTokenTypes.TEXT -> append(child.getUnescapedTextInNode(content))
138137
GFMTokenTypes.GFM_AUTOLINK -> if (child.parent == MarkdownElementTypes.LINK_TEXT) {
139-
append(child.getTextInNode(content).toString())
138+
append(child.getUnescapedTextInNode(content))
140139
} else appendAutoLink(content, child)
141140

142141
MarkdownTokenTypes.SINGLE_QUOTE -> append('\'')

multiplatform-markdown-renderer/src/commonMain/kotlin/com/mikepenz/markdown/utils/Extensions.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package com.mikepenz.markdown.utils
22

33
import org.intellij.markdown.IElementType
44
import org.intellij.markdown.ast.ASTNode
5+
import org.intellij.markdown.ast.getTextInNode
6+
import org.intellij.markdown.html.entities.EntityConverter
57

68
/**
79
* Tag used to indicate an url for inline content. Required for click handling.
@@ -35,3 +37,11 @@ internal fun ASTNode.findChildOfTypeRecursive(type: IElementType): ASTNode? {
3537
* E.g. we don't want to render the brackets of a link
3638
*/
3739
internal fun List<ASTNode>.innerList(): List<ASTNode> = this.subList(1, this.size - 1)
40+
41+
internal fun ASTNode.getUnescapedTextInNode(allFileText: CharSequence): String {
42+
val escapedText = getTextInNode(allFileText).toString()
43+
return EntityConverter.replaceEntities(escapedText,
44+
processEntities = false,
45+
processEscapes = true
46+
)
47+
}

0 commit comments

Comments
 (0)