Skip to content

Commit

Permalink
fix: markdown support link reference [WPB-9220] 🍒 (#3020)
Browse files Browse the repository at this point in the history
Co-authored-by: Jakub Żerko <[email protected]>
Co-authored-by: Yamil Medina <[email protected]>
  • Loading branch information
3 people authored May 22, 2024
1 parent b22b097 commit d26b742
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -89,7 +91,18 @@ fun <T : Node> T.toContent(isParentDocument: Boolean = false): MarkdownNode {
is ThematicBreak -> MarkdownNode.Block.ThematicBreak(convertChildren<MarkdownNode.Inline>(), isParentDocument)
is Strikethrough -> MarkdownNode.Inline.Strikethrough(convertChildren<MarkdownNode.Inline>())
is HardLineBreak, is SoftLineBreak -> MarkdownNode.Inline.Break(convertChildren<MarkdownNode.Inline>())
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)
}
}
}

Expand Down Expand Up @@ -167,6 +180,7 @@ fun MarkdownNode.filterNodesContainingQuery(query: String): MarkdownNode? {

is MarkdownNode.Block.ThematicBreak -> null
is MarkdownNode.Inline.Break -> this
is MarkdownNode.Unsupported -> null
}
}

Expand All @@ -191,6 +205,7 @@ fun MarkdownNode.getFirstInlines(): MarkdownPreview? {

is MarkdownNode.TableCell -> children.toPreview()
is MarkdownNode.TableRow -> children.firstOrNull()?.children?.toPreview()
is MarkdownNode.Unsupported -> null
}
}

Expand Down Expand Up @@ -307,6 +322,7 @@ private fun MarkdownNode.copy(children: List<MarkdownNode>): MarkdownNode {
// Custom nodes
is MarkdownNode.TableRow -> this.copy(children = children.filterIsInstance<MarkdownNode.TableCell>())
is MarkdownNode.TableCell -> this.copy(children = children.filterIsInstance<MarkdownNode.Inline>())
is MarkdownNode.Unsupported -> this
}
}

Expand Down Expand Up @@ -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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ sealed class MarkdownNode {
override val children: List<Inline> = listOf(),
override val isParentDocument: Boolean = false
) : MarkdownNode()

data class Unsupported(override val children: List<MarkdownNode> = listOf(), override val isParentDocument: Boolean) : MarkdownNode()
}

data class MarkdownPreview(val children: PersistentList<MarkdownNode.Inline>)

0 comments on commit d26b742

Please sign in to comment.