Skip to content

Commit

Permalink
Fix #67 - Add before/after markdown render functions
Browse files Browse the repository at this point in the history
  • Loading branch information
jeziellago committed Aug 29, 2024
1 parent fd23afc commit ef14f2d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package dev.jeziellago.compose.markdowntext

import android.content.Context
import android.text.Spanned
import android.widget.TextView
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.toArgb
import coil.ImageLoader
Expand All @@ -27,6 +29,8 @@ internal object MarkdownRender {
enableSoftBreakAddsNewLine: Boolean,
syntaxHighlightColor: Color,
headingBreakColor: Color,
beforeSetMarkdown: ((TextView, Spanned) -> Unit)? = null,
afterSetMarkdown: ((TextView) -> Unit)? = null,
onLinkClicked: ((String) -> Unit)? = null,
): Markwon {
val coilImageLoader = imageLoader ?: context.imageLoader
Expand All @@ -44,6 +48,15 @@ internal object MarkdownRender {
}
.usePlugin(SyntaxHighlightPlugin(syntaxHighlightColor.toArgb()))
.usePlugin(object : AbstractMarkwonPlugin() {

override fun beforeSetText(textView: TextView, markdown: Spanned) {
beforeSetMarkdown?.invoke(textView, markdown)
}

override fun afterSetText(textView: TextView) {
afterSetMarkdown?.invoke(textView)
}

override fun configureTheme(builder: MarkwonTheme.Builder) {
if (headingBreakColor == Color.Transparent) {
builder.headingBreakColor(1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package dev.jeziellago.compose.markdowntext

import android.content.Context
import android.os.Build
import android.text.Spanned
import android.text.method.LinkMovementMethod
import android.text.util.Linkify
import android.widget.TextView
import androidx.annotation.FontRes
import androidx.annotation.IdRes
import androidx.compose.foundation.clickable
Expand Down Expand Up @@ -48,6 +50,8 @@ fun MarkdownText(
enableSoftBreakAddsNewLine: Boolean = true,
syntaxHighlightColor: Color = Color.Red,
headingBreakColor: Color = Color.Transparent,
beforeSetMarkdown: ((TextView, Spanned) -> Unit)? = null,
afterSetMarkdown: ((TextView) -> Unit)? = null,
onLinkClicked: ((String) -> Unit)? = null,
onTextLayout: ((numLines: Int) -> Unit)? = null
) {
Expand Down Expand Up @@ -77,6 +81,8 @@ fun MarkdownText(
imageLoader = imageLoader,
linkifyMask = linkifyMask,
enableSoftBreakAddsNewLine = enableSoftBreakAddsNewLine,
beforeSetMarkdown = beforeSetMarkdown,
afterSetMarkdown = afterSetMarkdown,
onLinkClicked = onLinkClicked,
onTextLayout = onTextLayout,
syntaxHighlightColor = syntaxHighlightColor,
Expand Down Expand Up @@ -105,6 +111,8 @@ fun MarkdownText(
enableSoftBreakAddsNewLine: Boolean = true,
syntaxHighlightColor: Color = Color.LightGray,
headingBreakColor: Color = Color.Transparent,
beforeSetMarkdown: ((TextView, Spanned) -> Unit)? = null,
afterSetMarkdown: ((TextView) -> Unit)? = null,
onLinkClicked: ((String) -> Unit)? = null,
onTextLayout: ((numLines: Int) -> Unit)? = null
) {
Expand All @@ -119,6 +127,8 @@ fun MarkdownText(
enableSoftBreakAddsNewLine,
syntaxHighlightColor,
headingBreakColor,
beforeSetMarkdown,
afterSetMarkdown,
onLinkClicked,
)
}
Expand Down

0 comments on commit ef14f2d

Please sign in to comment.