
Vela
Markdown
package com.composables.vela.components
import androidx.compose.foundation.background
import androidx.compose.foundation.horizontalScroll
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.widthIn
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.text.BasicText
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.text.AnnotatedString
import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.withStyle
import com.composables.vela.theme.LocalContentColor
import com.composables.vela.theme.Space
import com.composables.vela.theme.Theme
@Composable
fun Markdown(
text: String,
modifier: Modifier = Modifier,
) {
val blocks = remember(text) { parseBlocks(text) }
val codeStyle =
SpanStyle(
fontFamily = Theme.typography.code.fontFamily,
fontSize = Theme.typography.code.fontSize,
background = Theme.colors.surfaceVariant,
)
Column(modifier.fillMaxWidth(), verticalArrangement = Arrangement.spacedBy(Space * 3)) {
blocks.forEach { block ->
when (block) {
is Block.Heading ->
Text(inline(block.text, codeStyle).text, style = Theme.typography.title)
is Block.Paragraph -> InlineText(inline(block.text, codeStyle))
is Block.ListItems ->
Column(verticalArrangement = Arrangement.spacedBy(Space * 2)) {
block.items.forEachIndexed { index, item ->
Row {
Text(
if (block.ordered) "${index + 1}." else "•",
color = Theme.colors.onBackgroundMuted,
modifier = Modifier.widthIn(min = Space * 6),
)
InlineText(inline(item, codeStyle))
}
}
}
is Block.Code ->
Text(
block.code,
style = Theme.typography.code,
softWrap = false,
modifier =
Modifier.fillMaxWidth()
.clip(Theme.shapes.small)
.background(Theme.colors.surface)
.horizontalScroll(rememberScrollState())
.padding(Space * 4),
)
}
}
}
}
@Composable
private fun InlineText(text: AnnotatedString) {
BasicText(text, style = Theme.typography.body.copy(color = LocalContentColor.current))
}
private sealed interface Block {
data class Heading(val text: String) : Block
data class Paragraph(val text: String) : Block
data class ListItems(val ordered: Boolean, val items: List<String>) : Block
data class Code(val code: String) : Block
}
private val orderedItem = Regex("""^\d+\.\s+""")
private fun parseBlocks(text: String): List<Block> {
val blocks = mutableListOf<Block>()
val lines = text.lines()
var index = 0
while (index < lines.size) {
val line = lines[index]
when {
line.startsWith("```") -> {
val code = mutableListOf<String>()
index++
while (index < lines.size && !lines[index].startsWith("```")) code += lines[index++]
blocks += Block.Code(code.joinToString("\n"))
}
line.startsWith("#") -> blocks += Block.Heading(line.trimStart('#').trim())
line.startsWith("- ") || orderedItem.containsMatchIn(line) -> {
val ordered = !line.startsWith("- ")
val items = mutableListOf<String>()
while (
index < lines.size &&
(if (ordered) orderedItem.containsMatchIn(lines[index])
else lines[index].startsWith("- "))
) {
items +=
if (ordered) lines[index].replace(orderedItem, "")
else lines[index].removePrefix("- ")
index++
}
blocks += Block.ListItems(ordered, items)
continue
}
line.isNotBlank() -> {
val paragraph = mutableListOf(line)
while (
index + 1 < lines.size &&
lines[index + 1].isNotBlank() &&
!startsBlock(lines[index + 1])
) paragraph += lines[++index]
blocks += Block.Paragraph(paragraph.joinToString(" "))
}
}
index++
}
return blocks
}
private fun startsBlock(line: String) =
line.startsWith("```") ||
line.startsWith("#") ||
line.startsWith("- ") ||
orderedItem.containsMatchIn(line)
private fun inline(text: String, codeStyle: SpanStyle): AnnotatedString = buildAnnotatedString {
var index = 0
while (index < text.length) {
when {
text.startsWith("**", index) -> {
val end = text.indexOf("**", index + 2).takeIf { it >= 0 } ?: text.length
withStyle(SpanStyle(fontWeight = FontWeight.SemiBold)) {
append(text.substring(index + 2, end))
}
index = end + 2
}
text[index] == '`' -> {
val end = text.indexOf('`', index + 1).takeIf { it >= 0 } ?: text.length
withStyle(codeStyle) { append(" ${text.substring(index + 1, end)} ") }
index = end + 1
}
else -> append(text[index++])
}
}
}
package com.composables.vela.components
import androidx.compose.foundation.text.BasicText
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.isSpecified
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.style.TextOverflow
import com.composables.vela.theme.LocalContentColor
import com.composables.vela.theme.LocalTextStyle
@Composable
fun Text(
text: String,
modifier: Modifier = Modifier,
color: Color = Color.Unspecified,
style: TextStyle = LocalTextStyle.current,
overflow: TextOverflow = TextOverflow.Clip,
softWrap: Boolean = true,
maxLines: Int = Int.MAX_VALUE,
) {
val resolvedColor =
when {
color.isSpecified -> color
style.color.isSpecified -> style.color
else -> LocalContentColor.current
}
BasicText(
text = text,
modifier = modifier,
style = LocalTextStyle.current.merge(style).copy(color = resolvedColor),
overflow = overflow,
softWrap = softWrap,
maxLines = maxLines,
)
}
package com.composables.vela.theme
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.selection.LocalTextSelectionColors
import androidx.compose.foundation.text.selection.TextSelectionColors
import androidx.compose.runtime.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.compositionLocalOf
import androidx.compose.runtime.staticCompositionLocalOf
import androidx.compose.ui.graphics.*
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.*
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.*
import androidx.compose.ui.unit.*
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import com.composables.googlefonts.rememberGoogleFontFamily
@Immutable
data class ThemeColors(
val `background`: Color = Color(0xFF0E0F12),
val `onBackground`: Color = Color(0xFFECECEF),
val `surface`: Color = Color(0xFF18191E),
val `onSurface`: Color = Color(0xFFECECEF),
val `surfaceVariant`: Color = Color(0xFF24252C),
val `onSurfaceVariant`: Color = Color(0xFFECECEF),
val `onBackgroundMuted`: Color = Color(0xFF8D8F99),
val `primary`: Color = Color(0xFFD6F36B),
val `onPrimary`: Color = Color(0xFF0E0F12),
val `border`: Color = Color(0xFF2B2C34),
val `textSelection`: Color = Color(0x66D6F36B),
val `destructive`: Color = Color(0xFFFF7B72),
val `onDestructive`: Color = Color(0xFF0E0F12),
)
@Immutable
data class ThemeTypography(
val `display`: TextStyle =
TextStyle(
fontFamily = FontFamily.SansSerif,
fontSize = 30.sp,
lineHeight = 36.sp,
fontWeight = FontWeight(500),
),
val `title`: TextStyle =
TextStyle(
fontFamily = FontFamily.SansSerif,
fontSize = 18.sp,
lineHeight = 24.sp,
fontWeight = FontWeight(500),
),
val `body`: TextStyle =
TextStyle(
fontFamily = FontFamily.SansSerif,
fontSize = 16.sp,
lineHeight = 26.sp,
fontWeight = FontWeight(400),
),
val `label`: TextStyle =
TextStyle(
fontFamily = FontFamily.SansSerif,
fontSize = 14.sp,
lineHeight = 20.sp,
fontWeight = FontWeight(500),
),
val `caption`: TextStyle =
TextStyle(
fontFamily = FontFamily.SansSerif,
fontSize = 12.sp,
lineHeight = 16.sp,
fontWeight = FontWeight(400),
),
val `code`: TextStyle =
TextStyle(
fontFamily = FontFamily.SansSerif,
fontSize = 13.sp,
lineHeight = 20.sp,
fontWeight = FontWeight(400),
),
)
@Immutable
data class ThemeShapes(
val `small`: Shape = RoundedCornerShape(12.dp),
val `medium`: Shape = RoundedCornerShape(20.dp),
val `large`: Shape = RoundedCornerShape(28.dp),
val `fullyRounded`: Shape = RoundedCornerShape(50),
)
fun themeColors(dark: Boolean = false): ThemeColors = ThemeColors()
@Composable
fun resolveThemeTypography(typography: ThemeTypography = ThemeTypography()): ThemeTypography {
val font0 = rememberGoogleFontFamily("Space Grotesk", weights = intArrayOf(500))
val font1 = rememberGoogleFontFamily("Inter", weights = intArrayOf(400, 500))
val font2 = rememberGoogleFontFamily("JetBrains Mono", weights = intArrayOf(400))
return typography.copy(
`display` = typography.`display`.copy(fontFamily = font0),
`title` = typography.`title`.copy(fontFamily = font0),
`body` = typography.`body`.copy(fontFamily = font1),
`label` = typography.`label`.copy(fontFamily = font1),
`caption` = typography.`caption`.copy(fontFamily = font1),
`code` = typography.`code`.copy(fontFamily = font2),
)
}
private val LocalColors = staticCompositionLocalOf<ThemeColors> { error("Theme is missing") }
private val LocalTypography =
staticCompositionLocalOf<ThemeTypography> { error("Theme is missing") }
private val LocalSpace = staticCompositionLocalOf { 4.dp }
private val LocalShapes = staticCompositionLocalOf<ThemeShapes> { error("Theme is missing") }
val LocalContentColor = compositionLocalOf<Color> { error("Theme is missing") }
val LocalTextStyle = compositionLocalOf<TextStyle> { error("Theme is missing") }
object Theme {
val colors: ThemeColors
@Composable get() = LocalColors.current
val typography: ThemeTypography
@Composable get() = LocalTypography.current
val shapes: ThemeShapes
@Composable get() = LocalShapes.current
}
val Space: Dp
@Composable get() = LocalSpace.current
@Composable
fun VelaTheme(
colors: ThemeColors = themeColors(isSystemInDarkTheme()),
typography: ThemeTypography = resolveThemeTypography(),
space: Dp = 4.dp,
shapes: ThemeShapes = ThemeShapes(),
contentColor: Color = colors.onBackground,
textStyle: TextStyle = typography.body,
content: @Composable () -> Unit,
) {
CompositionLocalProvider(
LocalColors provides colors,
LocalTypography provides typography,
LocalSpace provides space,
LocalShapes provides shapes,
LocalContentColor provides contentColor,
LocalTextStyle provides textStyle,
LocalTextSelectionColors provides
TextSelectionColors(
handleColor = colors.primary,
backgroundColor = colors.textSelection,
),
content = content,
)
}
@Composable
fun ProvideContentColor(color: Color, content: @Composable () -> Unit) {
CompositionLocalProvider(LocalContentColor provides color, content = content)
}
@Composable
fun ProvideTextStyle(style: TextStyle, content: @Composable () -> Unit) {
CompositionLocalProvider(
LocalTextStyle provides LocalTextStyle.current.merge(style),
content = content,
)
}




























