Skip to content

Commit c671629

Browse files
committed
refactor(android): unify duplicated AST segment splitting into shared utility
1 parent 01e36f5 commit c671629

3 files changed

Lines changed: 65 additions & 112 deletions

File tree

android/src/main/java/com/swmansion/enriched/markdown/EnrichedMarkdown.kt

Lines changed: 7 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import com.swmansion.enriched.markdown.spans.ImageSpan
1919
import com.swmansion.enriched.markdown.spoiler.SpoilerMode
2020
import com.swmansion.enriched.markdown.styles.StyleConfig
2121
import com.swmansion.enriched.markdown.utils.common.FeatureFlags
22+
import com.swmansion.enriched.markdown.utils.common.MarkdownSegment
23+
import com.swmansion.enriched.markdown.utils.common.splitASTIntoSegments
2224
import com.swmansion.enriched.markdown.utils.text.view.emitLinkLongPressEvent
2325
import com.swmansion.enriched.markdown.utils.text.view.emitLinkPressEvent
2426
import com.swmansion.enriched.markdown.views.BlockSegmentView
@@ -199,38 +201,11 @@ class EnrichedMarkdown
199201
}
200202

201203
val processedSegments =
202-
splitASTIntoSegments(ast).map { segmentNode ->
203-
when (segmentNode) {
204-
is MarkdownASTNode -> {
205-
when (segmentNode.type) {
206-
MarkdownASTNode.NodeType.Table -> {
207-
RenderSegment.Table(segmentNode)
208-
}
209-
210-
MarkdownASTNode.NodeType.LatexMathDisplay -> {
211-
val latex =
212-
if (segmentNode.children.isNotEmpty()) {
213-
segmentNode.children.first().content
214-
} else {
215-
segmentNode.content
216-
}
217-
RenderSegment.Math(latex)
218-
}
219-
220-
else -> {
221-
renderTextSegment(listOf(segmentNode), style)
222-
}
223-
}
224-
}
225-
226-
is List<*> -> {
227-
@Suppress("UNCHECKED_CAST")
228-
renderTextSegment(segmentNode as List<MarkdownASTNode>, style)
229-
}
230-
231-
else -> {
232-
throw IllegalArgumentException("Unknown segment type")
233-
}
204+
splitASTIntoSegments(ast).map { segment ->
205+
when (segment) {
206+
is MarkdownSegment.Text -> renderTextSegment(segment.nodes, style)
207+
is MarkdownSegment.Table -> RenderSegment.Table(segment.node)
208+
is MarkdownSegment.Math -> RenderSegment.Math(segment.latex)
234209
}
235210
}
236211

@@ -324,32 +299,6 @@ class EnrichedMarkdown
324299
}
325300
}
326301

327-
private fun splitASTIntoSegments(root: MarkdownASTNode): List<Any> {
328-
val segments = mutableListOf<Any>()
329-
val currentTextBuffer = mutableListOf<MarkdownASTNode>()
330-
331-
fun flushTextBuffer() {
332-
if (currentTextBuffer.isNotEmpty()) {
333-
segments.add(currentTextBuffer.toList())
334-
currentTextBuffer.clear()
335-
}
336-
}
337-
338-
root.children.forEach { child ->
339-
if (child.type == MarkdownASTNode.NodeType.Table) {
340-
flushTextBuffer()
341-
segments.add(child)
342-
} else if (child.type == MarkdownASTNode.NodeType.LatexMathDisplay) {
343-
flushTextBuffer()
344-
segments.add(child)
345-
} else {
346-
currentTextBuffer.add(child)
347-
}
348-
}
349-
flushTextBuffer()
350-
return segments
351-
}
352-
353302
private fun postToMain(
354303
renderId: Long,
355304
action: () -> Unit,

android/src/main/java/com/swmansion/enriched/markdown/MeasurementStore.kt

Lines changed: 2 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,11 @@ import com.swmansion.enriched.markdown.spans.MathMetrics
2121
import com.swmansion.enriched.markdown.spans.MathRenderMode
2222
import com.swmansion.enriched.markdown.styles.StyleConfig
2323
import com.swmansion.enriched.markdown.utils.common.FeatureFlags
24+
import com.swmansion.enriched.markdown.utils.common.MarkdownSegment
2425
import com.swmansion.enriched.markdown.utils.common.getBooleanOrDefault
2526
import com.swmansion.enriched.markdown.utils.common.getMapOrNull
2627
import com.swmansion.enriched.markdown.utils.common.getStringOrDefault
28+
import com.swmansion.enriched.markdown.utils.common.splitASTIntoSegments
2729
import com.swmansion.enriched.markdown.utils.text.extensions.replaceMathSpansWithPlaceholders
2830
import com.swmansion.enriched.markdown.views.TableContainerView
2931
import java.util.concurrent.ConcurrentHashMap
@@ -275,22 +277,6 @@ object MeasurementStore {
275277
return adjustedSize
276278
}
277279

278-
/** Sealed interface for type-safe segment handling */
279-
private sealed interface MarkdownSegment {
280-
data class Text(
281-
val nodes: List<MarkdownASTNode>,
282-
) : MarkdownSegment
283-
284-
data class Table(
285-
val node: MarkdownASTNode,
286-
) : MarkdownSegment
287-
288-
data class Math(
289-
val latex: String,
290-
val node: MarkdownASTNode,
291-
) : MarkdownSegment
292-
}
293-
294280
private fun measureAndCacheSplit(
295281
context: Context,
296282
id: Int?,
@@ -427,44 +413,6 @@ object MeasurementStore {
427413
}.build()
428414
}
429415

430-
private fun splitASTIntoSegments(root: MarkdownASTNode): List<MarkdownSegment> {
431-
val segments = mutableListOf<MarkdownSegment>()
432-
val currentTextNodes = mutableListOf<MarkdownASTNode>()
433-
434-
fun flushTextNodes() {
435-
if (currentTextNodes.isNotEmpty()) {
436-
segments.add(MarkdownSegment.Text(currentTextNodes.toList()))
437-
currentTextNodes.clear()
438-
}
439-
}
440-
441-
for (child in root.children) {
442-
when (child.type) {
443-
MarkdownASTNode.NodeType.Table -> {
444-
flushTextNodes()
445-
segments.add(MarkdownSegment.Table(child))
446-
}
447-
448-
MarkdownASTNode.NodeType.LatexMathDisplay -> {
449-
flushTextNodes()
450-
val latex =
451-
if (child.children.isNotEmpty()) {
452-
child.children.first().content
453-
} else {
454-
child.content
455-
}
456-
segments.add(MarkdownSegment.Math(latex, child))
457-
}
458-
459-
else -> {
460-
currentTextNodes.add(child)
461-
}
462-
}
463-
}
464-
flushTextNodes()
465-
return segments
466-
}
467-
468416
private fun tryRenderMarkdown(
469417
markdown: String,
470418
styleMap: ReadableMap?,
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.swmansion.enriched.markdown.utils.common
2+
3+
import com.swmansion.enriched.markdown.parser.MarkdownASTNode
4+
5+
sealed interface MarkdownSegment {
6+
data class Text(
7+
val nodes: List<MarkdownASTNode>,
8+
) : MarkdownSegment
9+
10+
data class Table(
11+
val node: MarkdownASTNode,
12+
) : MarkdownSegment
13+
14+
data class Math(
15+
val latex: String,
16+
val node: MarkdownASTNode,
17+
) : MarkdownSegment
18+
}
19+
20+
fun splitASTIntoSegments(root: MarkdownASTNode): List<MarkdownSegment> {
21+
val segments = mutableListOf<MarkdownSegment>()
22+
val currentTextNodes = mutableListOf<MarkdownASTNode>()
23+
24+
fun flushTextNodes() {
25+
if (currentTextNodes.isNotEmpty()) {
26+
segments.add(MarkdownSegment.Text(currentTextNodes.toList()))
27+
currentTextNodes.clear()
28+
}
29+
}
30+
31+
for (child in root.children) {
32+
when (child.type) {
33+
MarkdownASTNode.NodeType.Table -> {
34+
flushTextNodes()
35+
segments.add(MarkdownSegment.Table(child))
36+
}
37+
38+
MarkdownASTNode.NodeType.LatexMathDisplay -> {
39+
flushTextNodes()
40+
val latex =
41+
if (child.children.isNotEmpty()) {
42+
child.children.first().content
43+
} else {
44+
child.content
45+
}
46+
segments.add(MarkdownSegment.Math(latex, child))
47+
}
48+
49+
else -> {
50+
currentTextNodes.add(child)
51+
}
52+
}
53+
}
54+
flushTextNodes()
55+
return segments
56+
}

0 commit comments

Comments
 (0)