-
Notifications
You must be signed in to change notification settings - Fork 19
/
MarkdownComposer.kt
332 lines (304 loc) · 11.6 KB
/
MarkdownComposer.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/*
* Wire
* Copyright (C) 2024 Wire Swiss GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://www.gnu.org/licenses/.
*/
package com.wire.android.ui.markdown
import android.text.util.Linkify
import androidx.compose.runtime.Composable
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.AnnotatedString
import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.font.FontStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextDecoration
import com.wire.android.ui.common.LinkSpannableString
import com.wire.android.ui.markdown.MarkdownConstants.MENTION_MARK
import com.wire.android.ui.markdown.MarkdownConstants.TAG_URL
import com.wire.android.util.MatchQueryResult
import com.wire.android.util.QueryMatchExtractor
import com.wire.kalium.logic.data.message.mention.MessageMention
import org.commonmark.ext.gfm.strikethrough.Strikethrough
import org.commonmark.ext.gfm.tables.TableBlock
import org.commonmark.node.BlockQuote
import org.commonmark.node.BulletList
import org.commonmark.node.Code
import org.commonmark.node.Document
import org.commonmark.node.Emphasis
import org.commonmark.node.FencedCodeBlock
import org.commonmark.node.HardLineBreak
import org.commonmark.node.Heading
import org.commonmark.node.Image
import org.commonmark.node.IndentedCodeBlock
import org.commonmark.node.Link
import org.commonmark.node.Node
import org.commonmark.node.OrderedList
import org.commonmark.node.Paragraph
import org.commonmark.node.SoftLineBreak
import org.commonmark.node.StrongEmphasis
import org.commonmark.node.Text
import org.commonmark.node.ThematicBreak
import kotlin.math.max
import kotlin.math.min
import org.commonmark.node.Text as nodeText
@Composable
fun MarkdownDocument(
document: Document,
nodeData: NodeData,
clickable: Boolean
) {
MarkdownBlockChildren(
document,
nodeData,
clickable
)
}
@Composable
fun MarkdownBlockChildren(
parent: Node,
nodeData: NodeData,
clickable: Boolean = true
) {
var child = parent.firstChild
var updateMentions = nodeData.mentions
while (child != null) {
val updatedNodeData = nodeData.copy(mentions = updateMentions)
when (child) {
is Document -> MarkdownDocument(child, updatedNodeData, clickable)
is BlockQuote -> MarkdownBlockQuote(child, updatedNodeData)
is ThematicBreak -> MarkdownThematicBreak()
is Heading -> MarkdownHeading(child, updatedNodeData)
is Paragraph -> MarkdownParagraph(child, updatedNodeData, clickable) {
updateMentions = it
}
is FencedCodeBlock -> MarkdownFencedCodeBlock(child)
is IndentedCodeBlock -> MarkdownIndentedCodeBlock(child)
is BulletList -> MarkdownBulletList(child, updatedNodeData)
is OrderedList -> MarkdownOrderedList(child, updatedNodeData)
is TableBlock -> MarkdownTable(child, updatedNodeData) {
updateMentions = it
}
}
child = child.next
}
}
@Suppress("LongMethod")
fun inlineChildren(
parent: Node,
annotatedString: AnnotatedString.Builder,
nodeData: NodeData
): List<DisplayMention> {
var child = parent.firstChild
var updatedMentions = nodeData.mentions
while (child != null) {
when (child) {
is Paragraph ->
updatedMentions = inlineChildren(
child,
annotatedString,
nodeData.copy(mentions = updatedMentions),
)
is nodeText -> {
updatedMentions = appendLinksAndMentions(
annotatedString,
convertTypoGraphs(child),
nodeData.copy(mentions = updatedMentions)
)
}
is Image -> {
updatedMentions = appendLinksAndMentions(
annotatedString,
child.destination,
nodeData.copy(mentions = updatedMentions)
)
}
is Emphasis -> {
annotatedString.pushStyle(
SpanStyle(
fontFamily = nodeData.typography.body05.fontFamily,
fontStyle = FontStyle.Italic
)
)
updatedMentions = inlineChildren(
child,
annotatedString,
nodeData
)
annotatedString.pop()
}
is StrongEmphasis -> {
annotatedString.pushStyle(
SpanStyle(
fontFamily = nodeData.typography.body02.fontFamily,
fontWeight = FontWeight.Bold
)
)
updatedMentions = inlineChildren(
child,
annotatedString,
nodeData
)
annotatedString.pop()
}
is Code -> {
annotatedString.pushStyle(TextStyle(fontFamily = FontFamily.Monospace).toSpanStyle())
annotatedString.append(child.literal)
annotatedString.pop()
}
is HardLineBreak, is SoftLineBreak -> {
annotatedString.append("\n")
}
is Link -> {
annotatedString.pushStyle(
SpanStyle(
color = nodeData.colorScheme.primary,
textDecoration = TextDecoration.Underline
)
)
annotatedString.pushStringAnnotation(TAG_URL, child.destination)
updatedMentions = inlineChildren(
child,
annotatedString,
nodeData
)
annotatedString.pop()
annotatedString.pop()
}
is Strikethrough -> {
annotatedString.pushStyle(SpanStyle(textDecoration = TextDecoration.LineThrough))
updatedMentions = inlineChildren(child, annotatedString, nodeData)
annotatedString.pop()
}
}
child = child.next
}
return updatedMentions
}
@Suppress("LongMethod", "ComplexMethod")
fun appendLinksAndMentions(
annotatedString: AnnotatedString.Builder,
string: String,
nodeData: NodeData
): List<DisplayMention> {
val stringBuilder = StringBuilder(string)
val updatedMentions = nodeData.mentions.toMutableList()
var highlightIndexes = emptyList<MatchQueryResult>()
// get mentions from text, remove mention marks and update position of mentions
val mentionList: List<MessageMention> = if (stringBuilder.contains(MENTION_MARK) && updatedMentions.isNotEmpty()) {
nodeData.mentions.mapNotNull { displayMention ->
val markedMentionLength = (MENTION_MARK + displayMention.mentionUserName + MENTION_MARK).length
val startIndex = stringBuilder.indexOf(MENTION_MARK + displayMention.mentionUserName + MENTION_MARK)
val endIndex = startIndex + markedMentionLength
if (startIndex != -1) {
stringBuilder.replace(startIndex, endIndex, displayMention.mentionUserName)
// remove mention from list to not use the same mention twice
updatedMentions.removeAt(0)
MessageMention(
startIndex,
displayMention.length,
displayMention.userId,
displayMention.isSelfMention
)
} else {
null
}
}
} else {
listOf()
}
if (nodeData.searchQuery.isNotBlank()) {
highlightIndexes = QueryMatchExtractor.extractQueryMatchIndexes(
matchText = nodeData.searchQuery,
text = stringBuilder.toString()
)
}
val linkInfos = LinkSpannableString.getLinkInfos(stringBuilder.toString(), Linkify.WEB_URLS or Linkify.EMAIL_ADDRESSES)
val append = buildAnnotatedString {
append(stringBuilder)
with(nodeData.colorScheme) {
linkInfos.forEach {
val safeStart = max(it.start, 0)
val safeEnd = min(it.end, length)
if (safeStart > safeEnd) {
return@forEach
}
addStyle(
style = SpanStyle(
color = primary,
textDecoration = TextDecoration.Underline
),
start = safeStart,
end = safeEnd
)
addStringAnnotation(
tag = TAG_URL,
annotation = it.url,
start = safeStart,
end = safeEnd
)
}
if (mentionList.isNotEmpty()) {
mentionList.forEach { mention ->
if (mention.length <= 0 || mention.start >= length || mention.start + mention.length > length) {
return@forEach
}
addStyle(
style = SpanStyle(
fontWeight = nodeData.typography.body02.fontWeight,
color = onPrimaryVariant,
background = if (mention.isSelfMention) primaryVariant else Color.Unspecified
),
start = mention.start,
end = mention.start + mention.length
)
addStringAnnotation(
tag = MarkdownConstants.TAG_MENTION,
annotation = mention.userId.toString(),
start = mention.start,
end = mention.start + mention.length
)
}
}
highlightIndexes
.forEach { highLightIndex ->
if (highLightIndex.endIndex <= length) {
addStyle(
style = SpanStyle(
background = nodeData.colorScheme.highlight,
color = nodeData.colorScheme.onHighlight,
fontFamily = nodeData.typography.body02.fontFamily,
fontWeight = FontWeight.Bold
),
start = highLightIndex.startIndex,
end = highLightIndex.endIndex
)
}
}
}
}
annotatedString.append(append)
return updatedMentions
}
private fun convertTypoGraphs(child: Text) = child.literal
.replace("(c)", "©")
.replace("(C)", "©")
.replace("(r)", "®")
.replace("(R)", "®")
.replace("(tm)", "™")
.replace("(TM)", "™")
.replace("+-", "±")