Skip to content

Commit

Permalink
- add support for nested MarkdownBlockQuote's
Browse files Browse the repository at this point in the history
  • Loading branch information
mikepenz committed Jan 13, 2024
1 parent ede190b commit 091204b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
package com.mikepenz.markdown.compose.elements

import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.*
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.drawBehind
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.unit.LayoutDirection
import androidx.compose.ui.unit.dp
import com.mikepenz.markdown.compose.LocalMarkdownColors
import com.mikepenz.markdown.compose.LocalMarkdownDimens
import com.mikepenz.markdown.compose.LocalMarkdownPadding
import com.mikepenz.markdown.compose.LocalMarkdownTypography
import com.mikepenz.markdown.compose.elements.material.Text
import org.intellij.markdown.MarkdownElementTypes
import org.intellij.markdown.ast.ASTNode
import org.intellij.markdown.ast.getTextInNode
import org.intellij.markdown.ast.findChildOfType

@Composable
fun MarkdownBlockQuote(
Expand All @@ -26,28 +25,32 @@ fun MarkdownBlockQuote(
val blockQuoteColor = LocalMarkdownColors.current.text
val blockQuoteThickness = LocalMarkdownDimens.current.blockQuoteThickness
val blockQuote = LocalMarkdownPadding.current.blockQuote
val blockQuoteText = LocalMarkdownPadding.current.blockQuoteText
val blockQuoteBar = LocalMarkdownPadding.current.blockQuoteBar
Box(

Column(
modifier = Modifier
.drawBehind {
drawLine(
color = blockQuoteColor,
strokeWidth = blockQuoteThickness.value,
start = Offset(blockQuoteBar.calculateLeftPadding(LayoutDirection.Ltr).value, 0f),
end = Offset(blockQuoteBar.calculateRightPadding(LayoutDirection.Ltr).value, size.height)
strokeWidth = blockQuoteThickness.toPx(),
start = Offset(blockQuoteBar.calculateStartPadding(LayoutDirection.Ltr).toPx(), blockQuoteBar.calculateTopPadding().toPx()),
end = Offset(blockQuoteBar.calculateStartPadding(LayoutDirection.Ltr).toPx(), size.height - blockQuoteBar.calculateBottomPadding().toPx())
)
}
.padding(blockQuote)
) {
val text = buildAnnotatedString {
pushStyle(style.toSpanStyle())
append(node.getTextInNode(content).toString())
pop()
val quote = node.findChildOfType(MarkdownElementTypes.PARAGRAPH)
if (quote != null) {
MarkdownParagraph(content, quote, style = style, modifier = Modifier.padding(blockQuoteText))
}

val nestedQuote = node.findChildOfType(MarkdownElementTypes.BLOCK_QUOTE)
if (nestedQuote != null) {
if (quote != null) {
Spacer(Modifier.height(8.dp))
}
MarkdownBlockQuote(content, nestedQuote, style)
}
Text(
text,
style,
color = LocalMarkdownColors.current.text,
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ interface MarkdownPadding {
val indentList: Dp
val codeBlock: PaddingValues
val blockQuote: PaddingValues
val blockQuoteText: PaddingValues
val blockQuoteBar: PaddingValues.Absolute
}

Expand All @@ -24,6 +25,7 @@ private class DefaultMarkdownPadding(
override val indentList: Dp,
override val codeBlock: PaddingValues,
override val blockQuote: PaddingValues,
override val blockQuoteText: PaddingValues,
override val blockQuoteBar: PaddingValues.Absolute,
) : MarkdownPadding

Expand All @@ -34,14 +36,16 @@ fun markdownPadding(
listItemBottom: Dp = 4.dp,
indentList: Dp = 8.dp,
codeBlock: PaddingValues = PaddingValues(8.dp),
blockQuote: PaddingValues = PaddingValues(start = 16.dp, top = 16.dp, bottom = 16.dp),
blockQuoteBar: PaddingValues.Absolute = PaddingValues.Absolute(left = 4.dp, right = 4.dp),
blockQuote: PaddingValues = PaddingValues(horizontal = 16.dp, vertical = 0.dp),
blockQuoteText: PaddingValues = PaddingValues(vertical = 4.dp),
blockQuoteBar: PaddingValues.Absolute = PaddingValues.Absolute(left = 4.dp, top = 2.dp, right = 4.dp, bottom = 2.dp),
): MarkdownPadding = DefaultMarkdownPadding(
block = block,
list = list,
listItemBottom = listItemBottom,
indentList = indentList,
codeBlock = codeBlock,
blockQuote = blockQuote,
blockQuoteText = blockQuoteText,
blockQuoteBar = blockQuoteBar,
)

0 comments on commit 091204b

Please sign in to comment.