From 4b0d68c9a18f0f416c86016fe1f1c553ec1e5d2a Mon Sep 17 00:00:00 2001 From: Jakub Zerko Date: Tue, 21 May 2024 12:21:15 +0200 Subject: [PATCH 1/3] fix: support markdown link reference --- .../android/ui/markdown/MarkdownHelper.kt | 21 ++++++++++++++++++- .../wire/android/ui/markdown/MarkdownNode.kt | 2 ++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/app/src/main/kotlin/com/wire/android/ui/markdown/MarkdownHelper.kt b/app/src/main/kotlin/com/wire/android/ui/markdown/MarkdownHelper.kt index 895f44e367c..dae18231f25 100644 --- a/app/src/main/kotlin/com/wire/android/ui/markdown/MarkdownHelper.kt +++ b/app/src/main/kotlin/com/wire/android/ui/markdown/MarkdownHelper.kt @@ -19,6 +19,7 @@ package com.wire.android.ui.markdown +import com.wire.android.appLogger import kotlinx.collections.immutable.persistentListOf import kotlinx.collections.immutable.toPersistentList import org.commonmark.ext.gfm.strikethrough.Strikethrough @@ -40,6 +41,7 @@ import org.commonmark.node.HtmlInline import org.commonmark.node.Image import org.commonmark.node.IndentedCodeBlock import org.commonmark.node.Link +import org.commonmark.node.LinkReferenceDefinition import org.commonmark.node.ListItem import org.commonmark.node.Node import org.commonmark.node.OrderedList @@ -89,7 +91,20 @@ fun T.toContent(isParentDocument: Boolean = false): MarkdownNode { is ThematicBreak -> MarkdownNode.Block.ThematicBreak(convertChildren(), isParentDocument) is Strikethrough -> MarkdownNode.Inline.Strikethrough(convertChildren()) is HardLineBreak, is SoftLineBreak -> MarkdownNode.Inline.Break(convertChildren()) - else -> throw IllegalArgumentException("Unsupported node type: ${this.javaClass.simpleName}") + is LinkReferenceDefinition -> { + MarkdownNode.Block.Paragraph( + listOf(MarkdownNode.Inline.Text("[${label}]: ${this.destination} ${this.title}")), + isParentDocument + ) + } + + else -> { + appLogger.e( + "Unsupported markdown", + IllegalArgumentException("Unsupported node type: ${this.javaClass.simpleName}") + ) + MarkdownNode.Unsupported(isParentDocument = isParentDocument) + } } } @@ -167,6 +182,7 @@ fun MarkdownNode.filterNodesContainingQuery(query: String): MarkdownNode? { is MarkdownNode.Block.ThematicBreak -> null is MarkdownNode.Inline.Break -> this + is MarkdownNode.Unsupported -> null } } @@ -191,6 +207,7 @@ fun MarkdownNode.getFirstInlines(): MarkdownPreview? { is MarkdownNode.TableCell -> children.toPreview() is MarkdownNode.TableRow -> children.firstOrNull()?.children?.toPreview() + is MarkdownNode.Unsupported -> null } } @@ -307,6 +324,7 @@ private fun MarkdownNode.copy(children: List): MarkdownNode { // Custom nodes is MarkdownNode.TableRow -> this.copy(children = children.filterIsInstance()) is MarkdownNode.TableCell -> this.copy(children = children.filterIsInstance()) + is MarkdownNode.Unsupported -> this } } @@ -348,6 +366,7 @@ fun printMarkdownNodeTree(node: MarkdownNode?, indentLevel: Int = 0) { is MarkdownNode.Inline.Code -> "${indent}Code: '${node.literal}'" is MarkdownNode.Inline.Strikethrough -> "${indent}Strikethrough: [${node.children.size} children]" is MarkdownNode.Inline.Break -> "${indent}Break" + is MarkdownNode.Unsupported -> "${indent}Unsupported" } println(printLog) diff --git a/app/src/main/kotlin/com/wire/android/ui/markdown/MarkdownNode.kt b/app/src/main/kotlin/com/wire/android/ui/markdown/MarkdownNode.kt index 02453a8f0d0..d0d731214f1 100644 --- a/app/src/main/kotlin/com/wire/android/ui/markdown/MarkdownNode.kt +++ b/app/src/main/kotlin/com/wire/android/ui/markdown/MarkdownNode.kt @@ -143,6 +143,8 @@ sealed class MarkdownNode { override val children: List = listOf(), override val isParentDocument: Boolean = false ) : MarkdownNode() + + data class Unsupported(override val children: List = listOf(), override val isParentDocument: Boolean) : MarkdownNode() } data class MarkdownPreview(val children: PersistentList) From 271242afeee07bfcacaa710914e526b7d78225ad Mon Sep 17 00:00:00 2001 From: Jakub Zerko Date: Tue, 21 May 2024 12:31:38 +0200 Subject: [PATCH 2/3] detekt fix --- .../com/wire/android/ui/markdown/MarkdownHelper.kt | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/app/src/main/kotlin/com/wire/android/ui/markdown/MarkdownHelper.kt b/app/src/main/kotlin/com/wire/android/ui/markdown/MarkdownHelper.kt index dae18231f25..81e1ed8f29d 100644 --- a/app/src/main/kotlin/com/wire/android/ui/markdown/MarkdownHelper.kt +++ b/app/src/main/kotlin/com/wire/android/ui/markdown/MarkdownHelper.kt @@ -91,12 +91,10 @@ fun T.toContent(isParentDocument: Boolean = false): MarkdownNode { is ThematicBreak -> MarkdownNode.Block.ThematicBreak(convertChildren(), isParentDocument) is Strikethrough -> MarkdownNode.Inline.Strikethrough(convertChildren()) is HardLineBreak, is SoftLineBreak -> MarkdownNode.Inline.Break(convertChildren()) - is LinkReferenceDefinition -> { - MarkdownNode.Block.Paragraph( - listOf(MarkdownNode.Inline.Text("[${label}]: ${this.destination} ${this.title}")), - isParentDocument - ) - } + is LinkReferenceDefinition -> MarkdownNode.Block.Paragraph( + listOf(MarkdownNode.Inline.Text("[${label}]: ${this.destination} ${this.title}")), + isParentDocument + ) else -> { appLogger.e( From 8daf11c54c1908ebd796b30913af04c72294f9c3 Mon Sep 17 00:00:00 2001 From: Jakub Zerko Date: Tue, 21 May 2024 12:41:38 +0200 Subject: [PATCH 3/3] detekt fix --- .../main/kotlin/com/wire/android/ui/markdown/MarkdownHelper.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/kotlin/com/wire/android/ui/markdown/MarkdownHelper.kt b/app/src/main/kotlin/com/wire/android/ui/markdown/MarkdownHelper.kt index 81e1ed8f29d..f45a04418cd 100644 --- a/app/src/main/kotlin/com/wire/android/ui/markdown/MarkdownHelper.kt +++ b/app/src/main/kotlin/com/wire/android/ui/markdown/MarkdownHelper.kt @@ -92,7 +92,7 @@ fun T.toContent(isParentDocument: Boolean = false): MarkdownNode { is Strikethrough -> MarkdownNode.Inline.Strikethrough(convertChildren()) is HardLineBreak, is SoftLineBreak -> MarkdownNode.Inline.Break(convertChildren()) is LinkReferenceDefinition -> MarkdownNode.Block.Paragraph( - listOf(MarkdownNode.Inline.Text("[${label}]: ${this.destination} ${this.title}")), + listOf(MarkdownNode.Inline.Text("[$label]: $destination $title")), isParentDocument )