Skip to content

Commit 16b92fd

Browse files
authored
Merge pull request #217 from aegis123/feature/custom-link-spanstyle
Make links in text stylable via the MarkdownTypography class
2 parents 5ae7314 + a763a48 commit 16b92fd

File tree

4 files changed

+26
-19
lines changed

4 files changed

+26
-19
lines changed

multiplatform-markdown-renderer-m2/src/commonMain/kotlin/com/mikepenz/markdown/m2/MarkdownTypography.kt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import androidx.compose.ui.text.SpanStyle
66
import androidx.compose.ui.text.TextStyle
77
import androidx.compose.ui.text.font.FontFamily
88
import androidx.compose.ui.text.font.FontStyle
9+
import androidx.compose.ui.text.font.FontWeight
10+
import androidx.compose.ui.text.style.TextDecoration
911
import com.mikepenz.markdown.model.DefaultMarkdownTypography
1012
import com.mikepenz.markdown.model.MarkdownTypography
1113

@@ -23,9 +25,13 @@ fun markdownTypography(
2325
paragraph: TextStyle = MaterialTheme.typography.body1,
2426
ordered: TextStyle = MaterialTheme.typography.body1,
2527
bullet: TextStyle = MaterialTheme.typography.body1,
26-
list: TextStyle = MaterialTheme.typography.body1
28+
list: TextStyle = MaterialTheme.typography.body1,
29+
link: TextStyle = MaterialTheme.typography.body1.copy(
30+
fontWeight = FontWeight.Bold,
31+
textDecoration = TextDecoration.Underline
32+
),
2733
): MarkdownTypography = DefaultMarkdownTypography(
2834
h1 = h1, h2 = h2, h3 = h3, h4 = h4, h5 = h5, h6 = h6,
2935
text = text, quote = quote, code = code, paragraph = paragraph,
30-
ordered = ordered, bullet = bullet, list = list
36+
ordered = ordered, bullet = bullet, list = list, link = link,
3137
)

multiplatform-markdown-renderer-m3/src/commonMain/kotlin/com/mikepenz/markdown/m3/MarkdownTypography.kt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import androidx.compose.ui.text.SpanStyle
66
import androidx.compose.ui.text.TextStyle
77
import androidx.compose.ui.text.font.FontFamily
88
import androidx.compose.ui.text.font.FontStyle
9+
import androidx.compose.ui.text.font.FontWeight
10+
import androidx.compose.ui.text.style.TextDecoration
911
import com.mikepenz.markdown.model.DefaultMarkdownTypography
1012
import com.mikepenz.markdown.model.MarkdownTypography
1113

@@ -23,9 +25,13 @@ fun markdownTypography(
2325
paragraph: TextStyle = MaterialTheme.typography.bodyLarge,
2426
ordered: TextStyle = MaterialTheme.typography.bodyLarge,
2527
bullet: TextStyle = MaterialTheme.typography.bodyLarge,
26-
list: TextStyle = MaterialTheme.typography.bodyLarge
28+
list: TextStyle = MaterialTheme.typography.bodyLarge,
29+
link: TextStyle = MaterialTheme.typography.bodyLarge.copy(
30+
fontWeight = FontWeight.Bold,
31+
textDecoration = TextDecoration.Underline
32+
),
2733
): MarkdownTypography = DefaultMarkdownTypography(
2834
h1 = h1, h2 = h2, h3 = h3, h4 = h4, h5 = h5, h6 = h6,
2935
text = text, quote = quote, code = code, paragraph = paragraph,
30-
ordered = ordered, bullet = bullet, list = list
36+
ordered = ordered, bullet = bullet, list = list, link = link,
3137
)

multiplatform-markdown-renderer/src/commonMain/kotlin/com/mikepenz/markdown/model/MarkdownTypography.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ interface MarkdownTypography {
1717
val ordered: TextStyle
1818
val bullet: TextStyle
1919
val list: TextStyle
20+
val link: TextStyle
2021
}
2122

2223
@Immutable
@@ -33,5 +34,6 @@ class DefaultMarkdownTypography(
3334
override val paragraph: TextStyle,
3435
override val ordered: TextStyle,
3536
override val bullet: TextStyle,
36-
override val list: TextStyle
37+
override val list: TextStyle,
38+
override val link: TextStyle,
3739
) : MarkdownTypography

multiplatform-markdown-renderer/src/commonMain/kotlin/com/mikepenz/markdown/utils/AnnotatedStringKtx.kt

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import androidx.compose.ui.text.font.FontWeight
1010
import androidx.compose.ui.text.style.TextDecoration
1111
import com.mikepenz.markdown.compose.LocalMarkdownAnnotator
1212
import com.mikepenz.markdown.compose.LocalMarkdownColors
13+
import com.mikepenz.markdown.compose.LocalMarkdownTypography
1314
import org.intellij.markdown.MarkdownElementTypes
1415
import org.intellij.markdown.MarkdownTokenTypes
1516
import org.intellij.markdown.ast.ASTNode
@@ -32,13 +33,9 @@ internal fun AnnotatedString.Builder.appendMarkdownLink(content: String, node: A
3233
?.getTextInNode(content)?.toString()
3334
val annotation = destination ?: linkLabel
3435
if (annotation != null) pushStringAnnotation(MARKDOWN_TAG_URL, annotation)
35-
pushStyle(
36-
SpanStyle(
37-
color = LocalMarkdownColors.current.linkText,
38-
textDecoration = TextDecoration.Underline,
39-
fontWeight = FontWeight.Bold
40-
)
41-
)
36+
val linkColor = LocalMarkdownColors.current.linkText
37+
val linkTextStyle = LocalMarkdownTypography.current.link.copy(color = linkColor).toSpanStyle()
38+
pushStyle(linkTextStyle)
4239
buildMarkdownAnnotatedString(content, linkText)
4340
pop()
4441
if (annotation != null) pop()
@@ -51,13 +48,9 @@ internal fun AnnotatedString.Builder.appendAutoLink(content: String, node: ASTNo
5148
} ?: node
5249
val destination = targetNode.getTextInNode(content).toString()
5350
pushStringAnnotation(MARKDOWN_TAG_URL, (destination))
54-
pushStyle(
55-
SpanStyle(
56-
color = LocalMarkdownColors.current.linkText,
57-
textDecoration = TextDecoration.Underline,
58-
fontWeight = FontWeight.Bold
59-
)
60-
)
51+
val linkColor = LocalMarkdownColors.current.linkText
52+
val linkTextStyle = LocalMarkdownTypography.current.link.copy(color = linkColor).toSpanStyle()
53+
pushStyle(linkTextStyle)
6154
append(destination)
6255
pop()
6356
}

0 commit comments

Comments
 (0)