@@ -15,8 +15,11 @@ import com.mikepenz.markdown.compose.LocalMarkdownPadding
1515import com.mikepenz.markdown.compose.LocalMarkdownTypography
1616import com.mikepenz.markdown.compose.LocalOrderedListHandler
1717import com.mikepenz.markdown.compose.components.MarkdownComponentModel
18+ import com.mikepenz.markdown.compose.components.MarkdownComponents
1819import com.mikepenz.markdown.compose.elements.material.MarkdownBasicText
1920import com.mikepenz.markdown.compose.handleElement
21+ import com.mikepenz.markdown.model.MarkdownPadding
22+ import com.mikepenz.markdown.model.MarkdownTypography
2023import com.mikepenz.markdown.utils.getUnescapedTextInNode
2124import kotlinx.collections.immutable.persistentMapOf
2225import 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
126180fun MarkdownOrderedList (
127181 content : String ,
0 commit comments