-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Markdown styling to theme (#367)
Add Markdown styling to theme, add simple components We were lacking a good, easy way to render Markdown, requiring a lot of ceremony. Now it's much easier, you just need to provide the styling, renderer, and processor as composition locals (with the defaults coming from ProvideMarkdownStyling). Then use either Markdown(), or LazyMarkdown(), and your content is displayed. Better! I have updated the samples to show how it works.
- Loading branch information
Showing
16 changed files
with
512 additions
and
208 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
151 changes: 151 additions & 0 deletions
151
markdown/core/src/main/kotlin/org/jetbrains/jewel/markdown/extensions/Markdown.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
package org.jetbrains.jewel.markdown.extensions | ||
|
||
import androidx.compose.foundation.layout.Arrangement | ||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.PaddingValues | ||
import androidx.compose.foundation.lazy.LazyColumn | ||
import androidx.compose.foundation.lazy.LazyListState | ||
import androidx.compose.foundation.lazy.items | ||
import androidx.compose.foundation.lazy.rememberLazyListState | ||
import androidx.compose.foundation.text.selection.SelectionContainer | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.LaunchedEffect | ||
import androidx.compose.runtime.ProvidableCompositionLocal | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.runtime.setValue | ||
import androidx.compose.runtime.staticCompositionLocalOf | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.unit.dp | ||
import kotlinx.coroutines.CoroutineDispatcher | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.withContext | ||
import org.intellij.lang.annotations.Language | ||
import org.jetbrains.jewel.foundation.ExperimentalJewelApi | ||
import org.jetbrains.jewel.foundation.theme.JewelTheme | ||
import org.jetbrains.jewel.markdown.MarkdownBlock | ||
import org.jetbrains.jewel.markdown.processing.MarkdownProcessor | ||
import org.jetbrains.jewel.markdown.rendering.MarkdownBlockRenderer | ||
import org.jetbrains.jewel.markdown.rendering.MarkdownStyling | ||
|
||
public val LocalMarkdownStyling: ProvidableCompositionLocal<MarkdownStyling> = staticCompositionLocalOf { | ||
error("No MarkdownStyling defined, have you forgotten to provide it?") | ||
} | ||
|
||
public val JewelTheme.Companion.markdownStyling: MarkdownStyling | ||
@Composable get() = LocalMarkdownStyling.current | ||
|
||
public val LocalMarkdownProcessor: ProvidableCompositionLocal<MarkdownProcessor> = staticCompositionLocalOf { | ||
error("No MarkdownProcessor defined, have you forgotten to provide it?") | ||
} | ||
|
||
public val JewelTheme.Companion.markdownProcessor: MarkdownProcessor | ||
@Composable get() = LocalMarkdownProcessor.current | ||
|
||
public val LocalMarkdownBlockRenderer: ProvidableCompositionLocal<MarkdownBlockRenderer> = staticCompositionLocalOf { | ||
error("No MarkdownBlockRenderer defined, have you forgotten to provide it?") | ||
} | ||
|
||
public val JewelTheme.Companion.markdownBlockRenderer: MarkdownBlockRenderer | ||
@Composable get() = LocalMarkdownBlockRenderer.current | ||
|
||
@ExperimentalJewelApi | ||
@Composable | ||
public fun Markdown( | ||
@Language("Markdown") markdown: String, | ||
modifier: Modifier = Modifier, | ||
isSelectable: Boolean = false, | ||
renderingDispatcher: CoroutineDispatcher = Dispatchers.Default, | ||
onUrlClick: (String) -> Unit = {}, | ||
onTextClick: () -> Unit = {}, | ||
markdownStyling: MarkdownStyling = JewelTheme.markdownStyling, | ||
processor: MarkdownProcessor = JewelTheme.markdownProcessor, | ||
blockRenderer: MarkdownBlockRenderer = JewelTheme.markdownBlockRenderer, | ||
) { | ||
var markdownBlocks by remember { mutableStateOf(emptyList<MarkdownBlock>()) } | ||
LaunchedEffect(markdown) { | ||
markdownBlocks = | ||
withContext(renderingDispatcher) { processor.processMarkdownDocument(markdown) } | ||
} | ||
|
||
Markdown( | ||
markdownBlocks = markdownBlocks, | ||
modifier = modifier, | ||
isSelectable = isSelectable, | ||
onUrlClick = onUrlClick, | ||
onTextClick = onTextClick, | ||
markdownStyling = markdownStyling, | ||
blockRenderer = blockRenderer, | ||
) | ||
} | ||
|
||
@ExperimentalJewelApi | ||
@Composable | ||
public fun Markdown( | ||
markdownBlocks: List<MarkdownBlock>, | ||
modifier: Modifier = Modifier, | ||
isSelectable: Boolean = false, | ||
onUrlClick: (String) -> Unit = {}, | ||
onTextClick: () -> Unit = {}, | ||
markdownStyling: MarkdownStyling = JewelTheme.markdownStyling, | ||
blockRenderer: MarkdownBlockRenderer = JewelTheme.markdownBlockRenderer, | ||
) { | ||
if (isSelectable) { | ||
SelectionContainer(modifier) { | ||
Column(verticalArrangement = Arrangement.spacedBy(markdownStyling.blockVerticalSpacing)) { | ||
for (block in markdownBlocks) { | ||
blockRenderer.render(block, onUrlClick, onTextClick) | ||
} | ||
} | ||
} | ||
} else { | ||
Column( | ||
modifier, | ||
verticalArrangement = Arrangement.spacedBy(markdownStyling.blockVerticalSpacing), | ||
) { | ||
for (block in markdownBlocks) { | ||
blockRenderer.render(block, onUrlClick, onTextClick) | ||
} | ||
} | ||
} | ||
} | ||
|
||
@ExperimentalJewelApi | ||
@Composable | ||
public fun LazyMarkdown( | ||
markdownBlocks: List<MarkdownBlock>, | ||
modifier: Modifier = Modifier, | ||
contentPadding: PaddingValues = PaddingValues(0.dp), | ||
state: LazyListState = rememberLazyListState(), | ||
isSelectable: Boolean = false, | ||
onUrlClick: (String) -> Unit = {}, | ||
onTextClick: () -> Unit = {}, | ||
markdownStyling: MarkdownStyling = JewelTheme.markdownStyling, | ||
blockRenderer: MarkdownBlockRenderer = JewelTheme.markdownBlockRenderer, | ||
) { | ||
if (isSelectable) { | ||
SelectionContainer(modifier) { | ||
LazyColumn( | ||
state = state, | ||
contentPadding = contentPadding, | ||
verticalArrangement = Arrangement.spacedBy(markdownStyling.blockVerticalSpacing), | ||
) { | ||
items(markdownBlocks) { block -> | ||
blockRenderer.render(block, onUrlClick, onTextClick) | ||
} | ||
} | ||
} | ||
} else { | ||
LazyColumn( | ||
modifier = modifier, | ||
state = state, | ||
contentPadding = contentPadding, | ||
verticalArrangement = Arrangement.spacedBy(markdownStyling.blockVerticalSpacing), | ||
) { | ||
items(markdownBlocks) { block -> | ||
blockRenderer.render(block, onUrlClick, onTextClick) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.