Skip to content

Commit

Permalink
Merge pull request #110 from mikepenz/develop
Browse files Browse the repository at this point in the history
dev -> main
  • Loading branch information
mikepenz authored Jan 13, 2024
2 parents 431eea7 + 744fe01 commit 349981c
Show file tree
Hide file tree
Showing 11 changed files with 148 additions and 30 deletions.
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Maven stuff
GROUP=com.mikepenz
VERSION_NAME=0.11.0
VERSION_CODE=1100
VERSION_NAME=0.12.0
VERSION_CODE=1200

POM_URL=https://github.com/mikepenz/multiplatform-markdown-renderer

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ val LocalMarkdownPadding = staticCompositionLocalOf<MarkdownPadding> {
error("No local Padding")
}

/**
* Local [MarkdownDimens] provider
*/
val LocalMarkdownDimens = compositionLocalOf<MarkdownDimens> {
error("No local MarkdownDimens")
}


/**
* Local [ImageTransformer] provider
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,17 @@ fun Markdown(
content: String,
colors: MarkdownColors = markdownColor(),
typography: MarkdownTypography = markdownTypography(),
padding: MarkdownPadding = markdownPadding(),
modifier: Modifier = Modifier.fillMaxSize(),
padding: MarkdownPadding = markdownPadding(),
dimens: MarkdownDimens = markdownDimens(),
flavour: MarkdownFlavourDescriptor = GFMFlavourDescriptor(),
imageTransformer: ImageTransformer = ImageTransformerImpl(),
components: MarkdownComponents = markdownComponents(),
) {
CompositionLocalProvider(
LocalReferenceLinkHandler provides ReferenceLinkHandlerImpl(),
LocalMarkdownPadding provides padding,
LocalMarkdownDimens provides dimens,
LocalMarkdownColors provides colors,
LocalMarkdownTypography provides typography,
LocalImageTransformer provides imageTransformer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package com.mikepenz.markdown.compose.components
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.ColumnScope
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.material.Divider
import androidx.compose.runtime.Composable
import androidx.compose.runtime.Stable
import androidx.compose.ui.Modifier
Expand Down Expand Up @@ -195,7 +194,7 @@ object CurrentComponentsBridge {
}
}
val horizontalRule: MarkdownComponent = {
Divider(Modifier.fillMaxWidth())
MarkdownDivider(Modifier.fillMaxWidth())
}
val custom: CustomMarkdownComponent? = null
}
Original file line number Diff line number Diff line change
@@ -1,42 +1,56 @@
package com.mikepenz.markdown.compose.elements

import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.padding
import androidx.compose.material.Text
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 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(
content: String,
node: ASTNode,
style: TextStyle = LocalMarkdownTypography.current.quote
) {
Box(
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

Column(
modifier = Modifier
.drawBehind {
drawLine(
color = style.color,
strokeWidth = 2f,
start = Offset(12.dp.value, 0f),
end = Offset(12.dp.value, size.height)
color = blockQuoteColor,
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(start = 16.dp, top = 16.dp, bottom = 16.dp)
.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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.text.TextStyle
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 org.intellij.markdown.ast.ASTNode

Expand All @@ -21,15 +23,17 @@ private fun MarkdownCode(
style: TextStyle = LocalMarkdownTypography.current.code
) {
val backgroundCodeColor = LocalMarkdownColors.current.codeBackground
val codeBackgroundCornerSize = LocalMarkdownDimens.current.codeBackgroundCornerSize
val codeBlockPadding = LocalMarkdownPadding.current.codeBlock
Surface(
color = backgroundCodeColor,
shape = RoundedCornerShape(8.dp),
modifier = Modifier.fillMaxWidth().padding(top = 8.dp, bottom = 8.dp)
shape = RoundedCornerShape(codeBackgroundCornerSize),
modifier = Modifier.fillMaxWidth().padding(vertical = 8.dp)
) {
Text(
code,
color = LocalMarkdownColors.current.codeText,
modifier = Modifier.horizontalScroll(rememberScrollState()).padding(8.dp),
modifier = Modifier.horizontalScroll(rememberScrollState()).padding(codeBlockPadding),
style = style
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.mikepenz.markdown.compose.elements

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import com.mikepenz.markdown.compose.LocalMarkdownColors
import com.mikepenz.markdown.compose.LocalMarkdownDimens

@Composable
fun MarkdownDivider(
modifier: Modifier = Modifier,
color: Color = LocalMarkdownColors.current.codeBackground,
thickness: Dp = LocalMarkdownDimens.current.dividerThickness,
) {
val targetThickness = if (thickness == Dp.Hairline) {
(1f / LocalDensity.current.density).dp
} else {
thickness
}
Box(
modifier
.fillMaxWidth()
.height(targetThickness)
.background(color = color)
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.unit.dp
import com.mikepenz.markdown.compose.*
import com.mikepenz.markdown.utils.buildMarkdownAnnotatedString
import com.mikepenz.markdown.utils.filterNonListTypes
Expand Down Expand Up @@ -67,6 +66,7 @@ fun MarkdownOrderedList(
level: Int = 0
) {
val orderedListHandler = LocalOrderedListHandler.current
val listItemBottom = LocalMarkdownPadding.current.listItemBottom
MarkdownListItems(content, node, style, level) { index, child ->
Row(Modifier.fillMaxWidth()) {
Text(
Expand All @@ -79,7 +79,7 @@ fun MarkdownOrderedList(
buildMarkdownAnnotatedString(content, child.children.filterNonListTypes())
pop()
}
MarkdownText(text, Modifier.padding(bottom = 4.dp), style = style)
MarkdownText(text, Modifier.padding(bottom = listItemBottom), style = style)
}
}
}
Expand All @@ -92,6 +92,7 @@ fun MarkdownBulletList(
level: Int = 0
) {
val bulletHandler = LocalBulletListHandler.current
val listItemBottom = LocalMarkdownPadding.current.listItemBottom
MarkdownListItems(content, node, style, level) { index, child ->
Row(Modifier.fillMaxWidth()) {
Text(
Expand All @@ -104,7 +105,7 @@ fun MarkdownBulletList(
buildMarkdownAnnotatedString(content, child.children.filterNonListTypes())
pop()
}
MarkdownText(text, Modifier.padding(bottom = 4.dp), style = style)
MarkdownText(text, Modifier.padding(bottom = listItemBottom), style = style)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ interface MarkdownColors {

/** Represents the color used for the inline background of code. */
val inlineCodeBackground: Color

/** Represents the color used for the color of dividers. */
val dividerColor: Color
}

@Immutable
Expand All @@ -31,6 +34,7 @@ private class DefaultMarkdownColors(
override val linkText: Color,
override val codeBackground: Color,
override val inlineCodeBackground: Color,
override val dividerColor: Color,
) : MarkdownColors

@Composable
Expand All @@ -40,10 +44,12 @@ fun markdownColor(
linkText: Color = text,
codeBackground: Color = MaterialTheme.colors.onBackground.copy(alpha = 0.1f),
inlineCodeBackground: Color = codeBackground,
dividerColor: Color = MaterialTheme.colors.onSurface.copy(alpha = 0.12f),
): MarkdownColors = DefaultMarkdownColors(
text = text,
codeText = codeText,
linkText = linkText,
codeBackground = codeBackground,
inlineCodeBackground = inlineCodeBackground,
dividerColor = dividerColor,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.mikepenz.markdown.model

import androidx.compose.runtime.Composable
import androidx.compose.runtime.Immutable
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp

interface MarkdownDimens {
val dividerThickness: Dp
val codeBackgroundCornerSize: Dp
val blockQuoteThickness: Dp
}

@Immutable
private class DefaultMarkdownDimens(
override val dividerThickness: Dp,
override val codeBackgroundCornerSize: Dp,
override val blockQuoteThickness: Dp,
) : MarkdownDimens

@Composable
fun markdownDimens(
dividerThickness: Dp = 1.dp,
codeBackgroundCornerSize: Dp = 8.dp,
blockQuoteThickness: Dp = 2.dp,
): MarkdownDimens = DefaultMarkdownDimens(
dividerThickness = dividerThickness,
codeBackgroundCornerSize = codeBackgroundCornerSize,
blockQuoteThickness = blockQuoteThickness,
)
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.mikepenz.markdown.model

import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.runtime.Composable
import androidx.compose.runtime.Immutable
import androidx.compose.ui.unit.Dp
Expand All @@ -8,23 +9,43 @@ import androidx.compose.ui.unit.dp
interface MarkdownPadding {
val block: Dp
val list: Dp
val listItemBottom: Dp
val indentList: Dp
val codeBlock: PaddingValues
val blockQuote: PaddingValues
val blockQuoteText: PaddingValues
val blockQuoteBar: PaddingValues.Absolute
}

@Immutable
private class DefaultMarkdownPadding(
override val block: Dp,
override val list: Dp,
override val indentList: Dp
override val listItemBottom: Dp,
override val indentList: Dp,
override val codeBlock: PaddingValues,
override val blockQuote: PaddingValues,
override val blockQuoteText: PaddingValues,
override val blockQuoteBar: PaddingValues.Absolute,
) : MarkdownPadding

@Composable
fun markdownPadding(
block: Dp = 2.dp,
list: Dp = 8.dp,
indentList: Dp = 8.dp
listItemBottom: Dp = 4.dp,
indentList: Dp = 8.dp,
codeBlock: PaddingValues = PaddingValues(8.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,
indentList = indentList
listItemBottom = listItemBottom,
indentList = indentList,
codeBlock = codeBlock,
blockQuote = blockQuote,
blockQuoteText = blockQuoteText,
blockQuoteBar = blockQuoteBar,
)

0 comments on commit 349981c

Please sign in to comment.