Skip to content

Commit

Permalink
Merge pull request #105 from mikepenz/develop
Browse files Browse the repository at this point in the history
dev -> main
  • Loading branch information
mikepenz authored Jan 12, 2024
2 parents b2afeab + f242d01 commit 431eea7
Show file tree
Hide file tree
Showing 18 changed files with 138 additions and 87 deletions.
44 changes: 43 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,48 @@ CompositionLocalProvider(LocalOrderedListHandler provides { "A.) " }) {
}
```

### Custom Components

Since v0.9.0 it is possible to provide custom components, instead of the default ones.
This can be done by providing the components `MarkdownComponents` to the `Markdown` composable.

Use the `markdownComponents()` to keep defaults for non overwritten components.

The `MarkdownComponent` will expose access to the `content: String`, `node: ASTNode`, `typography: MarkdownTypography`, offering full flexibility.

```kotlin
// Simple adjusted paragraph with different Modifier.
val customParagraphComponent: MarkdownComponent = {
MarkdownParagraph(it.content, it.node, Modifier.align(Alignment.End))
}

// Full custom paragraph example
val customParagraphComponent: MarkdownComponent = {
// build a styled paragraph. (util function provided by the library)
val styledText = buildAnnotatedString {
pushStyle(LocalMarkdownTypography.current.paragraph.toSpanStyle())
buildMarkdownAnnotatedString(content, it.node)
pop()
}

// define the `Text` composable
Text(
styledText,
modifier = Modifier.align(Alignment.End),
textAlign = TextAlign.End
)
}

// Define the `Markdown` composeable and pass in the custom paragraph component
Markdown(
content,
components = markdownComponents(
paragraph = customParagraphComponent
)
)

```

</p>
</details>

Expand Down Expand Up @@ -148,7 +190,7 @@ Copyright for portions of the code are held by [Erik Hellman, 2020] as part of p

## License

Copyright 2023 Mike Penz
Copyright 2024 Mike Penz

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
4 changes: 2 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ buildscript {
}

dependencies {
classpath("com.android.tools.build:gradle:8.1.2")
classpath("com.android.tools.build:gradle:8.2.1")
classpath("io.gitlab.arturbosch.detekt:detekt-gradle-plugin:1.21.0")
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.9.20")
classpath("org.jetbrains.dokka:dokka-gradle-plugin:1.9.10")
classpath("com.mikepenz.aboutlibraries.plugin:aboutlibraries-plugin:10.9.2")
classpath("com.mikepenz.aboutlibraries.plugin:aboutlibraries-plugin:10.10.0")
classpath("org.jetbrains.compose:compose-gradle-plugin:1.5.11")
}
}
Expand Down
7 changes: 3 additions & 4 deletions buildSrc/src/main/java/Dependencies.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@ object Versions {

const val kotlin = "1.9.21"

const val markdown = "0.6.0"
const val markdown = "0.6.1"

const val coil = "2.5.0"
const val compose = "1.5.4"
const val composeCompiler = "1.5.5"

const val material = "1.10.0"
const val activityCompose = "1.8.1"
const val lifecycleKtx = "2.3.1"
const val material = "1.11.0"
const val activityCompose = "1.8.2"
}

object Deps {
Expand Down
2 changes: 1 addition & 1 deletion compose-desktop/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
kotlin("jvm")
id("org.jetbrains.compose")
application
id("org.jetbrains.compose")
}

group = "com.mikepenz"
Expand Down
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.10.0
VERSION_CODE=1000
VERSION_NAME=0.11.0
VERSION_CODE=1100

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

Expand Down
Binary file modified gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.2.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.5-all.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
Expand Down
17 changes: 9 additions & 8 deletions gradlew
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ done
# This is normally unused
# shellcheck disable=SC2034
APP_BASE_NAME=${0##*/}
APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit
# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036)
APP_HOME=$( cd "${APP_HOME:-./}" > /dev/null && pwd -P ) || exit

# Use the maximum available, or set MAX_FD != -1 to use that value.
MAX_FD=maximum
Expand Down Expand Up @@ -144,15 +145,15 @@ if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then
case $MAX_FD in #(
max*)
# In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked.
# shellcheck disable=SC3045
# shellcheck disable=SC2039,SC3045
MAX_FD=$( ulimit -H -n ) ||
warn "Could not query maximum file descriptor limit"
esac
case $MAX_FD in #(
'' | soft) :;; #(
*)
# In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked.
# shellcheck disable=SC3045
# shellcheck disable=SC2039,SC3045
ulimit -n "$MAX_FD" ||
warn "Could not set maximum file descriptor limit to $MAX_FD"
esac
Expand Down Expand Up @@ -201,11 +202,11 @@ fi
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'

# Collect all arguments for the java command;
# * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of
# shell script including quotes and variable substitutions, so put them in
# double quotes to make sure that they get re-expanded; and
# * put everything else in single quotes, so that it's not re-expanded.
# Collect all arguments for the java command:
# * DEFAULT_JVM_OPTS, JAVA_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments,
# and any embedded shellness will be escaped.
# * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be
# treated as '${Hostname}' itself on the command line.

set -- \
"-Dorg.gradle.appname=$APP_BASE_NAME" \
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,13 @@
package com.mikepenz.markdown.compose

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.ui.Modifier
import com.mikepenz.markdown.compose.components.MarkdownComponentModel
import com.mikepenz.markdown.compose.components.MarkdownComponents
import com.mikepenz.markdown.compose.components.markdownComponents
import com.mikepenz.markdown.model.ImageTransformer
import com.mikepenz.markdown.model.ImageTransformerImpl
import com.mikepenz.markdown.model.MarkdownColors
import com.mikepenz.markdown.model.MarkdownPadding
import com.mikepenz.markdown.model.MarkdownTypography
import com.mikepenz.markdown.model.ReferenceLinkHandlerImpl
import com.mikepenz.markdown.model.markdownColor
import com.mikepenz.markdown.model.markdownPadding
import com.mikepenz.markdown.model.markdownTypography
import com.mikepenz.markdown.model.*
import org.intellij.markdown.MarkdownElementTypes.ATX_1
import org.intellij.markdown.MarkdownElementTypes.ATX_2
import org.intellij.markdown.MarkdownElementTypes.ATX_3
Expand All @@ -36,6 +25,7 @@ import org.intellij.markdown.MarkdownElementTypes.SETEXT_1
import org.intellij.markdown.MarkdownElementTypes.SETEXT_2
import org.intellij.markdown.MarkdownElementTypes.UNORDERED_LIST
import org.intellij.markdown.MarkdownTokenTypes.Companion.EOL
import org.intellij.markdown.MarkdownTokenTypes.Companion.HORIZONTAL_RULE
import org.intellij.markdown.MarkdownTokenTypes.Companion.TEXT
import org.intellij.markdown.ast.ASTNode
import org.intellij.markdown.flavours.MarkdownFlavourDescriptor
Expand Down Expand Up @@ -63,9 +53,9 @@ fun Markdown(
Column(modifier) {
val parsedTree = MarkdownParser(flavour).buildMarkdownTreeFromString(content)
parsedTree.children.forEach { node ->
if (!node.handleElement(components, content)) {
if (!handleElement(node, components, content)) {
node.children.forEach { child ->
child.handleElement(components, content)
handleElement(child, components, content)
}
}
}
Expand All @@ -74,35 +64,36 @@ fun Markdown(
}

@Composable
private fun ASTNode.handleElement(components: MarkdownComponents, content: String): Boolean {
private fun ColumnScope.handleElement(node: ASTNode, components: MarkdownComponents, content: String): Boolean {
val model = MarkdownComponentModel(
content = content,
node = this,
node = node,
typography = LocalMarkdownTypography.current
)
var handled = true
Spacer(Modifier.height(LocalMarkdownPadding.current.block))
when (type) {
TEXT -> components.text(model)
EOL -> components.eol(model)
CODE_FENCE -> components.codeFence(model)
CODE_BLOCK -> components.codeBlock(model)
ATX_1 -> components.heading1(model)
ATX_2 -> components.heading2(model)
ATX_3 -> components.heading3(model)
ATX_4 -> components.heading4(model)
ATX_5 -> components.heading5(model)
ATX_6 -> components.heading6(model)
SETEXT_1 -> components.setextHeading1(model)
SETEXT_2 -> components.setextHeading2(model)
BLOCK_QUOTE -> components.blockQuote(model)
PARAGRAPH -> components.paragraph(model)
ORDERED_LIST -> components.orderedList(model)
UNORDERED_LIST -> components.unorderedList(model)
IMAGE -> components.image(model)
LINK_DEFINITION -> components.linkDefinition(model)
when (node.type) {
TEXT -> components.text(this@handleElement, model)
EOL -> components.eol(this@handleElement, model)
CODE_FENCE -> components.codeFence(this@handleElement, model)
CODE_BLOCK -> components.codeBlock(this@handleElement, model)
ATX_1 -> components.heading1(this@handleElement, model)
ATX_2 -> components.heading2(this@handleElement, model)
ATX_3 -> components.heading3(this@handleElement, model)
ATX_4 -> components.heading4(this@handleElement, model)
ATX_5 -> components.heading5(this@handleElement, model)
ATX_6 -> components.heading6(this@handleElement, model)
SETEXT_1 -> components.setextHeading1(this@handleElement, model)
SETEXT_2 -> components.setextHeading2(this@handleElement, model)
BLOCK_QUOTE -> components.blockQuote(this@handleElement, model)
PARAGRAPH -> components.paragraph(this@handleElement, model)
ORDERED_LIST -> components.orderedList(this@handleElement, model)
UNORDERED_LIST -> components.unorderedList(this@handleElement, model)
IMAGE -> components.image(this@handleElement, model)
LINK_DEFINITION -> components.linkDefinition(this@handleElement, model)
HORIZONTAL_RULE -> components.horizontalRule(this@handleElement, model)
else -> {
handled = components.custom?.invoke(type, model) != null
handled = components.custom?.invoke(this@handleElement, node.type, model) != null
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
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
import com.mikepenz.markdown.compose.LocalReferenceLinkHandler
import com.mikepenz.markdown.compose.elements.MarkdownBlockQuote
import com.mikepenz.markdown.compose.elements.MarkdownBulletList
import com.mikepenz.markdown.compose.elements.MarkdownCodeBlock
import com.mikepenz.markdown.compose.elements.MarkdownCodeFence
import com.mikepenz.markdown.compose.elements.MarkdownHeader
import com.mikepenz.markdown.compose.elements.MarkdownImage
import com.mikepenz.markdown.compose.elements.MarkdownOrderedList
import com.mikepenz.markdown.compose.elements.MarkdownParagraph
import com.mikepenz.markdown.compose.elements.MarkdownText
import com.mikepenz.markdown.compose.elements.*
import com.mikepenz.markdown.model.MarkdownTypography
import org.intellij.markdown.IElementType
import org.intellij.markdown.MarkdownElementTypes
Expand All @@ -22,9 +17,9 @@ import org.intellij.markdown.ast.ASTNode
import org.intellij.markdown.ast.findChildOfType
import org.intellij.markdown.ast.getTextInNode

typealias MarkdownComponent = @Composable (MarkdownComponentModel) -> Unit
typealias MarkdownComponent = @Composable ColumnScope.(MarkdownComponentModel) -> Unit

typealias CustomMarkdownComponent = @Composable (IElementType, MarkdownComponentModel) -> Unit
typealias CustomMarkdownComponent = @Composable ColumnScope.(IElementType, MarkdownComponentModel) -> Unit

/**
* Model holding data relevant for a component
Expand Down Expand Up @@ -57,6 +52,7 @@ fun markdownComponents(
unorderedList: MarkdownComponent = CurrentComponentsBridge.unorderedList,
image: MarkdownComponent = CurrentComponentsBridge.image,
linkDefinition: MarkdownComponent = CurrentComponentsBridge.linkDefinition,
horizontalRule: MarkdownComponent = CurrentComponentsBridge.horizontalRule,
custom: CustomMarkdownComponent? = CurrentComponentsBridge.custom,
): MarkdownComponents = DefaultMarkdownComponents(
text = text,
Expand All @@ -77,6 +73,7 @@ fun markdownComponents(
unorderedList = unorderedList,
image = image,
linkDefinition = linkDefinition,
horizontalRule = horizontalRule,
custom = custom,
)

Expand All @@ -103,6 +100,7 @@ interface MarkdownComponents {
val unorderedList: MarkdownComponent
val image: MarkdownComponent
val linkDefinition: MarkdownComponent
val horizontalRule: MarkdownComponent
val custom: CustomMarkdownComponent?
}

Expand All @@ -125,6 +123,7 @@ private class DefaultMarkdownComponents(
override val unorderedList: MarkdownComponent,
override val image: MarkdownComponent,
override val linkDefinition: MarkdownComponent,
override val horizontalRule: MarkdownComponent,
override val custom: CustomMarkdownComponent?,
) : MarkdownComponents

Expand Down Expand Up @@ -170,7 +169,7 @@ object CurrentComponentsBridge {
MarkdownBlockQuote(it.content, it.node)
}
val paragraph: MarkdownComponent = {
MarkdownParagraph(it.content, it.node, it.typography.paragraph)
MarkdownParagraph(it.content, it.node, style = it.typography.paragraph)
}
val orderedList: MarkdownComponent = {
Column(modifier = Modifier) {
Expand All @@ -195,5 +194,8 @@ object CurrentComponentsBridge {
LocalReferenceLinkHandler.current.store(linkLabel, destination)
}
}
val horizontalRule: MarkdownComponent = {
Divider(Modifier.fillMaxWidth())
}
val custom: CustomMarkdownComponent? = null
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import org.intellij.markdown.ast.ASTNode
import org.intellij.markdown.ast.getTextInNode

@Composable
internal fun MarkdownBlockQuote(
fun MarkdownBlockQuote(
content: String,
node: ASTNode,
style: TextStyle = LocalMarkdownTypography.current.quote
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ private fun MarkdownCode(
}

@Composable
internal fun MarkdownCodeFence(
fun MarkdownCodeFence(
content: String,
node: ASTNode
) {
Expand All @@ -51,7 +51,7 @@ internal fun MarkdownCodeFence(
}

@Composable
internal fun MarkdownCodeBlock(
fun MarkdownCodeBlock(
content: String,
node: ASTNode
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import org.intellij.markdown.ast.ASTNode
import org.intellij.markdown.ast.findChildOfType

@Composable
internal fun MarkdownHeader(
fun MarkdownHeader(
content: String,
node: ASTNode,
style: TextStyle,
Expand Down
Loading

0 comments on commit 431eea7

Please sign in to comment.