Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: markdown support link reference [WPB-9220] #3018

Merged
merged 3 commits into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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}]: ${this.destination} ${this.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>)
Loading