diff --git a/app/src/debug/screenshotTest/reference/com/mikepenz/markdown/ui/SnapshotTests/ListCodeBlockTest_3c07bfff_0.png b/app/src/debug/screenshotTest/reference/com/mikepenz/markdown/ui/SnapshotTests/ListCodeBlockTest_3c07bfff_0.png index 8a224bf..1fa002e 100644 Binary files a/app/src/debug/screenshotTest/reference/com/mikepenz/markdown/ui/SnapshotTests/ListCodeBlockTest_3c07bfff_0.png and b/app/src/debug/screenshotTest/reference/com/mikepenz/markdown/ui/SnapshotTests/ListCodeBlockTest_3c07bfff_0.png differ diff --git a/app/src/debug/screenshotTest/reference/com/mikepenz/markdown/ui/SnapshotTests/ListCodeBlockTest_db9b1159_0.png b/app/src/debug/screenshotTest/reference/com/mikepenz/markdown/ui/SnapshotTests/ListCodeBlockTest_db9b1159_0.png index f19b69f..df2fd66 100644 Binary files a/app/src/debug/screenshotTest/reference/com/mikepenz/markdown/ui/SnapshotTests/ListCodeBlockTest_db9b1159_0.png and b/app/src/debug/screenshotTest/reference/com/mikepenz/markdown/ui/SnapshotTests/ListCodeBlockTest_db9b1159_0.png differ diff --git a/app/src/debug/screenshotTest/reference/com/mikepenz/markdown/ui/SnapshotTests/ListTest_9c1ad4f7_0.png b/app/src/debug/screenshotTest/reference/com/mikepenz/markdown/ui/SnapshotTests/ListTest_9c1ad4f7_0.png new file mode 100644 index 0000000..c51eab0 Binary files /dev/null and b/app/src/debug/screenshotTest/reference/com/mikepenz/markdown/ui/SnapshotTests/ListTest_9c1ad4f7_0.png differ diff --git a/app/src/debug/screenshotTest/reference/com/mikepenz/markdown/ui/SnapshotTests/ListTest_f33ffe9d_0.png b/app/src/debug/screenshotTest/reference/com/mikepenz/markdown/ui/SnapshotTests/ListTest_f33ffe9d_0.png new file mode 100644 index 0000000..a0d82fa Binary files /dev/null and b/app/src/debug/screenshotTest/reference/com/mikepenz/markdown/ui/SnapshotTests/ListTest_f33ffe9d_0.png differ diff --git a/app/src/screenshotTest/kotlin/com.mikepenz.markdown.ui/SnapshotTests.kt b/app/src/screenshotTest/kotlin/com.mikepenz.markdown.ui/SnapshotTests.kt index a763247..81d3236 100644 --- a/app/src/screenshotTest/kotlin/com.mikepenz.markdown.ui/SnapshotTests.kt +++ b/app/src/screenshotTest/kotlin/com.mikepenz.markdown.ui/SnapshotTests.kt @@ -29,6 +29,13 @@ class SnapshotTests { Markdown(MARKDOWN_LIST_CODE_BLOCK) } + @Preview(showBackground = true, backgroundColor = Color.WHITE.toLong(), heightDp = 1250) + @Preview(showBackground = true, backgroundColor = Color.BLACK.toLong(), heightDp = 1250, uiMode = Configuration.UI_MODE_NIGHT_YES) + @Composable + fun ListTest() = SampleTheme(isSystemInDarkTheme()) { + Markdown(MARKDOWN_LIST) + } + @Preview(showBackground = true, backgroundColor = Color.WHITE.toLong(), heightDp = 1250) @Preview(showBackground = true, backgroundColor = Color.BLACK.toLong(), heightDp = 1250, uiMode = Configuration.UI_MODE_NIGHT_YES) @Composable @@ -185,4 +192,42 @@ private val MARKDOWN_TABLE = """ | --- | --- | | bar | | bar | baz | boo | +""".trimIndent() + +private val MARKDOWN_LIST = """ +// unordered list + +- George Washington +- John Adams +- Thomas Jefferson + +// mixed unordered list (not properly supported by markdown lib) + +- George Washington +* John Adams ++ Thomas Jefferson + +// ordered list + +1. James Madison +2. James Monroe +3. John Quincy Adams + +// nested list + +1. First list item + - First nested list item + - Second nested list item + +// deep nested un-ordered list + +* 1 +* **Bold**: 2 + * 3 + * **Bold**: 4 + * 5 + * **Bold**: 6 + * 7 + * 8 +* **Bold**: 9 """.trimIndent() \ No newline at end of file diff --git a/multiplatform-markdown-renderer/src/commonMain/kotlin/com/mikepenz/markdown/compose/elements/MarkdownList.kt b/multiplatform-markdown-renderer/src/commonMain/kotlin/com/mikepenz/markdown/compose/elements/MarkdownList.kt index 08e7007..64551d6 100644 --- a/multiplatform-markdown-renderer/src/commonMain/kotlin/com/mikepenz/markdown/compose/elements/MarkdownList.kt +++ b/multiplatform-markdown-renderer/src/commonMain/kotlin/com/mikepenz/markdown/compose/elements/MarkdownList.kt @@ -7,6 +7,7 @@ import androidx.compose.foundation.layout.padding import androidx.compose.runtime.Composable import androidx.compose.ui.Modifier import androidx.compose.ui.text.TextStyle +import androidx.compose.ui.unit.dp import com.mikepenz.markdown.compose.* import com.mikepenz.markdown.compose.elements.material.MarkdownBasicText import com.mikepenz.markdown.utils.getUnescapedTextInNode @@ -24,26 +25,46 @@ fun MarkdownListItems( node: ASTNode, style: TextStyle = LocalMarkdownTypography.current.list, level: Int = 0, - item: @Composable (index: Int, child: ASTNode) -> Unit, + bullet: @Composable (index: Int, child: ASTNode?) -> Unit, ) { val listDp = LocalMarkdownPadding.current.list val indentListDp = LocalMarkdownPadding.current.indentList Column( modifier = Modifier.padding( start = (indentListDp) * level, - top = listDp, - bottom = listDp + top = if (level == 0) listDp else 0.dp, + bottom = if (level == 0) listDp else 0.dp ) ) { var index = 0 node.children.forEach { child -> when (child.type) { MarkdownElementTypes.LIST_ITEM -> { - item(index, child) - when (child.children.last().type) { - ORDERED_LIST -> MarkdownOrderedList(content, child, style, level + 1) - UNORDERED_LIST -> MarkdownBulletList(content, child, style, level + 1) + val listIndicator = when (child.type) { + ORDERED_LIST -> child.findChildOfType(LIST_NUMBER) + UNORDERED_LIST -> child.findChildOfType(LIST_BULLET) + else -> null } + + Row(Modifier.fillMaxWidth()) { + bullet(index, listIndicator) + + Column { + child.children.onEach { nestedChild -> + when (nestedChild.type) { + ORDERED_LIST -> MarkdownOrderedList(content, nestedChild, style, level + 1) + UNORDERED_LIST -> MarkdownBulletList(content, nestedChild, style, level + 1) + else -> handleElement( + node = nestedChild, + components = LocalMarkdownComponents.current, + content = content, + includeSpacer = false + ) + } + } + } + } + index++ } } @@ -61,26 +82,16 @@ fun MarkdownOrderedList( val orderedListHandler = LocalOrderedListHandler.current val listItemBottom = LocalMarkdownPadding.current.listItemBottom MarkdownListItems(content, node, style, level) { index, child -> - Row(Modifier.fillMaxWidth()) { - MarkdownBasicText( - text = orderedListHandler.transform( - LIST_NUMBER, - child.findChildOfType(LIST_NUMBER)?.getUnescapedTextInNode(content), - index - ), - style = style, - color = LocalMarkdownColors.current.text - ) - - Column(Modifier.padding(bottom = listItemBottom)) { - handleElement( - node = child, - components = LocalMarkdownComponents.current, - content = content, - includeSpacer = false - ) - } - } + MarkdownBasicText( + text = orderedListHandler.transform( + LIST_NUMBER, + child?.getUnescapedTextInNode(content), + index + ), + style = style, + color = LocalMarkdownColors.current.text, + modifier = Modifier.padding(bottom = listItemBottom) + ) } } @@ -94,25 +105,15 @@ fun MarkdownBulletList( val bulletHandler = LocalBulletListHandler.current val listItemBottom = LocalMarkdownPadding.current.listItemBottom MarkdownListItems(content, node, style, level) { index, child -> - Row(Modifier.fillMaxWidth()) { - MarkdownBasicText( - bulletHandler.transform( - LIST_BULLET, - child.findChildOfType(LIST_BULLET)?.getUnescapedTextInNode(content), - index - ), - style = style, - color = LocalMarkdownColors.current.text - ) - - Column(Modifier.padding(bottom = listItemBottom)) { - handleElement( - node = child, - components = LocalMarkdownComponents.current, - content = content, - includeSpacer = false - ) - } - } + MarkdownBasicText( + bulletHandler.transform( + LIST_BULLET, + child?.getUnescapedTextInNode(content), + index + ), + style = style, + color = LocalMarkdownColors.current.text, + modifier = Modifier.padding(bottom = listItemBottom) + ) } }