From d26b742dff65f8cb531582559db3a67854a00e25 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Wed, 22 May 2024 15:17:16 +0000 Subject: [PATCH] =?UTF-8?q?fix:=20markdown=20support=20link=20reference=20?= =?UTF-8?q?[WPB-9220]=20=F0=9F=8D=92=20(#3020)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Jakub Żerko Co-authored-by: Yamil Medina --- .../android/ui/markdown/MarkdownHelper.kt | 19 ++++++++++++++++++- .../wire/android/ui/markdown/MarkdownNode.kt | 2 ++ 2 files changed, 20 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..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 @@ -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,18 @@ 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]: $destination $title")), + isParentDocument + ) + + else -> { + appLogger.e( + "Unsupported markdown", + IllegalArgumentException("Unsupported node type: ${this.javaClass.simpleName}") + ) + MarkdownNode.Unsupported(isParentDocument = isParentDocument) + } } } @@ -167,6 +180,7 @@ fun MarkdownNode.filterNodesContainingQuery(query: String): MarkdownNode? { is MarkdownNode.Block.ThematicBreak -> null is MarkdownNode.Inline.Break -> this + is MarkdownNode.Unsupported -> null } } @@ -191,6 +205,7 @@ fun MarkdownNode.getFirstInlines(): MarkdownPreview? { is MarkdownNode.TableCell -> children.toPreview() is MarkdownNode.TableRow -> children.firstOrNull()?.children?.toPreview() + is MarkdownNode.Unsupported -> null } } @@ -307,6 +322,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 +364,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)