Skip to content

Commit effce24

Browse files
authored
Merge pull request #365 from keta1/feat/list
Optimize code structure and readability
2 parents 70003f2 + 1d596ba commit effce24

File tree

3 files changed

+141
-74
lines changed

3 files changed

+141
-74
lines changed

multiplatform-markdown-renderer/src/commonMain/kotlin/com/mikepenz/markdown/compose/elements/MarkdownCode.kt

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,18 @@ private fun MarkdownCode(
3939
MarkdownCodeBackground(
4040
color = backgroundCodeColor,
4141
shape = RoundedCornerShape(codeBackgroundCornerSize),
42-
modifier = Modifier.fillMaxWidth().padding(vertical = 8.dp)
42+
modifier = Modifier
43+
.fillMaxWidth()
44+
.padding(vertical = 8.dp)
4345
) {
4446
@Suppress("DEPRECATION")
4547
MarkdownBasicText(
4648
text = code,
4749
color = LocalMarkdownColors.current.codeText,
4850
style = style,
49-
modifier = Modifier.horizontalScroll(rememberScrollState()).padding(codeBlockPadding),
51+
modifier = Modifier
52+
.horizontalScroll(rememberScrollState())
53+
.padding(codeBlockPadding),
5054
)
5155
}
5256
}
@@ -97,10 +101,16 @@ fun MarkdownCodeBackground(
97101
content: @Composable () -> Unit,
98102
) {
99103
Box(
100-
modifier = modifier.shadow(elevation, shape, clip = false).then(if (border != null) Modifier.border(border, shape) else Modifier).background(color = color, shape = shape)
101-
.clip(shape).semantics(mergeDescendants = false) {
104+
modifier = modifier
105+
.shadow(elevation, shape, clip = false)
106+
.then(if (border != null) Modifier.border(border, shape) else Modifier)
107+
.background(color = color, shape = shape)
108+
.clip(shape)
109+
.semantics(mergeDescendants = false) {
102110
isTraversalGroup = true
103-
}.pointerInput(Unit) {}, propagateMinConstraints = true
111+
}
112+
.pointerInput(Unit) {},
113+
propagateMinConstraints = true
104114
) {
105115
content()
106116
}

multiplatform-markdown-renderer/src/commonMain/kotlin/com/mikepenz/markdown/compose/elements/MarkdownList.kt

Lines changed: 120 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,11 @@ import com.mikepenz.markdown.compose.LocalMarkdownPadding
1515
import com.mikepenz.markdown.compose.LocalMarkdownTypography
1616
import com.mikepenz.markdown.compose.LocalOrderedListHandler
1717
import com.mikepenz.markdown.compose.components.MarkdownComponentModel
18+
import com.mikepenz.markdown.compose.components.MarkdownComponents
1819
import com.mikepenz.markdown.compose.elements.material.MarkdownBasicText
1920
import com.mikepenz.markdown.compose.handleElement
21+
import com.mikepenz.markdown.model.MarkdownPadding
22+
import com.mikepenz.markdown.model.MarkdownTypography
2023
import com.mikepenz.markdown.utils.getUnescapedTextInNode
2124
import kotlinx.collections.immutable.persistentMapOf
2225
import org.intellij.markdown.MarkdownElementTypes
@@ -40,88 +43,139 @@ fun MarkdownListItems(
4043
listModifier: RowScope.() -> Modifier = { Modifier },
4144
bullet: @Composable (index: Int, child: ASTNode?) -> Unit,
4245
) {
43-
val listDp = LocalMarkdownPadding.current.list
44-
val indentListDp = LocalMarkdownPadding.current.listIndent
45-
val listItemPaddingDp = LocalMarkdownPadding.current.listItemTop
46-
val listItemBottom = LocalMarkdownPadding.current.listItemBottom
46+
val padding = LocalMarkdownPadding.current
4747
val markdownComponents = LocalMarkdownComponents.current
4848
val markdownTypography = LocalMarkdownTypography.current
49+
4950
Column(
5051
modifier = Modifier.padding(
51-
start = (indentListDp) * depth,
52-
top = listDp,
53-
bottom = listDp
52+
start = padding.listIndent * depth,
53+
top = padding.list,
54+
bottom = padding.list
5455
)
5556
) {
5657
var index = 0
5758
node.children.forEach { child ->
58-
when (child.type) {
59-
MarkdownElementTypes.LIST_ITEM -> {
60-
// LIST_NUMBER/LIST_BULLET, CHECK_BOX, PARAGRAPH
61-
val checkboxNode = child.children.getOrNull(1)?.takeIf { it.type == CHECK_BOX }
62-
val listIndicator = when (node.type) {
63-
ORDERED_LIST -> child.findChildOfType(LIST_NUMBER)
64-
UNORDERED_LIST -> child.findChildOfType(LIST_BULLET)
65-
else -> null
66-
}
67-
68-
Row(modifier = Modifier.fillMaxWidth().padding(top = listItemPaddingDp, bottom = listItemBottom)) {
69-
Box(modifier = markerModifier()) {
70-
if (checkboxNode != null) {
71-
val model = MarkdownComponentModel(
72-
content = content,
73-
node = checkboxNode,
74-
typography = markdownTypography,
75-
extra = persistentMapOf(MARKDOWN_LIST_DEPTH_KEY to depth + 1)
76-
)
77-
markdownComponents.checkbox.invoke(model)
78-
} else {
79-
bullet(index, listIndicator)
80-
}
81-
}
82-
Column(modifier = listModifier()) {
83-
child.children.onEach { nestedChild ->
84-
when (nestedChild.type) {
85-
ORDERED_LIST -> {
86-
val model = MarkdownComponentModel(
87-
content = content,
88-
node = nestedChild,
89-
typography = markdownTypography,
90-
extra = persistentMapOf(MARKDOWN_LIST_DEPTH_KEY to depth + 1)
91-
)
92-
markdownComponents.orderedList.invoke(model)
93-
}
59+
if (child.type == MarkdownElementTypes.LIST_ITEM) {
60+
MarkdownListItem(
61+
content = content,
62+
child = child,
63+
node = node,
64+
index = index,
65+
depth = depth,
66+
markdownComponents = markdownComponents,
67+
markdownTypography = markdownTypography,
68+
padding = padding,
69+
markerModifier = markerModifier,
70+
listModifier = listModifier,
71+
bullet = bullet
72+
)
73+
index++
74+
}
75+
}
76+
}
77+
}
9478

95-
UNORDERED_LIST -> {
96-
val model = MarkdownComponentModel(
97-
content = content,
98-
node = nestedChild,
99-
typography = markdownTypography,
100-
extra = persistentMapOf(MARKDOWN_LIST_DEPTH_KEY to depth + 1)
101-
)
102-
markdownComponents.unorderedList.invoke(model)
103-
}
79+
/**
80+
* Renders a single list item
81+
*/
82+
@Composable
83+
private fun MarkdownListItem(
84+
content: String,
85+
child: ASTNode,
86+
node: ASTNode,
87+
index: Int,
88+
depth: Int,
89+
markdownComponents: MarkdownComponents,
90+
markdownTypography: MarkdownTypography,
91+
padding: MarkdownPadding,
92+
markerModifier: RowScope.() -> Modifier,
93+
listModifier: RowScope.() -> Modifier,
94+
bullet: @Composable (index: Int, child: ASTNode?) -> Unit
95+
) {
96+
val checkboxNode = child.children.getOrNull(1)?.takeIf { it.type == CHECK_BOX }
97+
val listIndicator = when (node.type) {
98+
ORDERED_LIST -> child.findChildOfType(LIST_NUMBER)
99+
UNORDERED_LIST -> child.findChildOfType(LIST_BULLET)
100+
else -> null
101+
}
104102

105-
else -> {
106-
handleElement(
107-
node = nestedChild,
108-
components = markdownComponents,
109-
content = content,
110-
includeSpacer = false
111-
)
112-
}
113-
}
114-
}
115-
}
116-
}
103+
Row(
104+
modifier = Modifier
105+
.fillMaxWidth()
106+
.padding(top = padding.listItemTop, bottom = padding.listItemBottom)
107+
) {
108+
// Render marker symbol (checkbox or bullet)
109+
Box(modifier = markerModifier()) {
110+
if (checkboxNode != null) {
111+
val model = MarkdownComponentModel(
112+
content = content,
113+
node = checkboxNode,
114+
typography = markdownTypography,
115+
extra = persistentMapOf(MARKDOWN_LIST_DEPTH_KEY to depth + 1)
116+
)
117+
markdownComponents.checkbox.invoke(model)
118+
} else {
119+
bullet(index, listIndicator)
120+
}
121+
}
117122

118-
index++
119-
}
123+
// Render list item content
124+
Column(modifier = listModifier()) {
125+
child.children.forEach { nestedChild ->
126+
MarkdownNestedListItem(
127+
nestedChild = nestedChild,
128+
content = content,
129+
depth = depth,
130+
markdownComponents = markdownComponents,
131+
markdownTypography = markdownTypography
132+
)
120133
}
121134
}
122135
}
123136
}
124137

138+
/**
139+
* Renders nested list item content
140+
*/
141+
@Composable
142+
private fun MarkdownNestedListItem(
143+
nestedChild: ASTNode,
144+
content: String,
145+
depth: Int,
146+
markdownComponents: MarkdownComponents,
147+
markdownTypography: MarkdownTypography
148+
) {
149+
when (nestedChild.type) {
150+
ORDERED_LIST -> {
151+
val model = MarkdownComponentModel(
152+
content = content,
153+
node = nestedChild,
154+
typography = markdownTypography,
155+
extra = persistentMapOf(MARKDOWN_LIST_DEPTH_KEY to depth + 1)
156+
)
157+
markdownComponents.orderedList.invoke(model)
158+
}
159+
UNORDERED_LIST -> {
160+
val model = MarkdownComponentModel(
161+
content = content,
162+
node = nestedChild,
163+
typography = markdownTypography,
164+
extra = persistentMapOf(MARKDOWN_LIST_DEPTH_KEY to depth + 1)
165+
)
166+
markdownComponents.unorderedList.invoke(model)
167+
}
168+
else -> {
169+
handleElement(
170+
node = nestedChild,
171+
components = markdownComponents,
172+
content = content,
173+
includeSpacer = false
174+
)
175+
}
176+
}
177+
}
178+
125179
@Composable
126180
fun MarkdownOrderedList(
127181
content: String,

multiplatform-markdown-renderer/src/commonMain/kotlin/com/mikepenz/markdown/compose/elements/MarkdownTable.kt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,9 @@ fun MarkdownTable(
6464

6565
val backgroundCodeColor = LocalMarkdownColors.current.tableBackground
6666
BoxWithConstraints(
67-
modifier = Modifier.background(backgroundCodeColor, RoundedCornerShape(tableCornerSize)).widthIn(max = tableMaxWidth)
67+
modifier = Modifier
68+
.background(backgroundCodeColor, RoundedCornerShape(tableCornerSize))
69+
.widthIn(max = tableMaxWidth)
6870
) {
6971
val scrollable = maxWidth <= tableWidth
7072
Column(
@@ -160,7 +162,8 @@ fun MarkdownTableBasicText(
160162
overflow: TextOverflow = TextOverflow.Ellipsis,
161163
annotatorSettings: AnnotatorSettings = annotatorSettings(),
162164
) {
163-
@Suppress("DEPRECATION") MarkdownBasicText(
165+
@Suppress("DEPRECATION")
166+
MarkdownBasicText(
164167
text = content.buildMarkdownAnnotatedString(
165168
textNode = cell,
166169
style = style,
@@ -171,4 +174,4 @@ fun MarkdownTableBasicText(
171174
maxLines = maxLines,
172175
overflow = overflow,
173176
)
174-
}
177+
}

0 commit comments

Comments
 (0)