Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix spacings for nested lists | Simplify logic #264

Merged
merged 1 commit into from
Jan 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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++
}
}
Expand All @@ -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)
)
}
}

Expand All @@ -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)
)
}
}