Skip to content

Commit

Permalink
Merge pull request #217 from aegis123/feature/custom-link-spanstyle
Browse files Browse the repository at this point in the history
Make links in text stylable via the MarkdownTypography class
  • Loading branch information
mikepenz authored Oct 10, 2024
2 parents 5ae7314 + a763a48 commit 16b92fd
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.font.FontStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextDecoration
import com.mikepenz.markdown.model.DefaultMarkdownTypography
import com.mikepenz.markdown.model.MarkdownTypography

Expand All @@ -23,9 +25,13 @@ fun markdownTypography(
paragraph: TextStyle = MaterialTheme.typography.body1,
ordered: TextStyle = MaterialTheme.typography.body1,
bullet: TextStyle = MaterialTheme.typography.body1,
list: TextStyle = MaterialTheme.typography.body1
list: TextStyle = MaterialTheme.typography.body1,
link: TextStyle = MaterialTheme.typography.body1.copy(
fontWeight = FontWeight.Bold,
textDecoration = TextDecoration.Underline
),
): MarkdownTypography = DefaultMarkdownTypography(
h1 = h1, h2 = h2, h3 = h3, h4 = h4, h5 = h5, h6 = h6,
text = text, quote = quote, code = code, paragraph = paragraph,
ordered = ordered, bullet = bullet, list = list
ordered = ordered, bullet = bullet, list = list, link = link,
)
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.font.FontStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextDecoration
import com.mikepenz.markdown.model.DefaultMarkdownTypography
import com.mikepenz.markdown.model.MarkdownTypography

Expand All @@ -23,9 +25,13 @@ fun markdownTypography(
paragraph: TextStyle = MaterialTheme.typography.bodyLarge,
ordered: TextStyle = MaterialTheme.typography.bodyLarge,
bullet: TextStyle = MaterialTheme.typography.bodyLarge,
list: TextStyle = MaterialTheme.typography.bodyLarge
list: TextStyle = MaterialTheme.typography.bodyLarge,
link: TextStyle = MaterialTheme.typography.bodyLarge.copy(
fontWeight = FontWeight.Bold,
textDecoration = TextDecoration.Underline
),
): MarkdownTypography = DefaultMarkdownTypography(
h1 = h1, h2 = h2, h3 = h3, h4 = h4, h5 = h5, h6 = h6,
text = text, quote = quote, code = code, paragraph = paragraph,
ordered = ordered, bullet = bullet, list = list
ordered = ordered, bullet = bullet, list = list, link = link,
)
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ interface MarkdownTypography {
val ordered: TextStyle
val bullet: TextStyle
val list: TextStyle
val link: TextStyle
}

@Immutable
Expand All @@ -33,5 +34,6 @@ class DefaultMarkdownTypography(
override val paragraph: TextStyle,
override val ordered: TextStyle,
override val bullet: TextStyle,
override val list: TextStyle
override val list: TextStyle,
override val link: TextStyle,
) : MarkdownTypography
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextDecoration
import com.mikepenz.markdown.compose.LocalMarkdownAnnotator
import com.mikepenz.markdown.compose.LocalMarkdownColors
import com.mikepenz.markdown.compose.LocalMarkdownTypography
import org.intellij.markdown.MarkdownElementTypes
import org.intellij.markdown.MarkdownTokenTypes
import org.intellij.markdown.ast.ASTNode
Expand All @@ -32,13 +33,9 @@ internal fun AnnotatedString.Builder.appendMarkdownLink(content: String, node: A
?.getTextInNode(content)?.toString()
val annotation = destination ?: linkLabel
if (annotation != null) pushStringAnnotation(MARKDOWN_TAG_URL, annotation)
pushStyle(
SpanStyle(
color = LocalMarkdownColors.current.linkText,
textDecoration = TextDecoration.Underline,
fontWeight = FontWeight.Bold
)
)
val linkColor = LocalMarkdownColors.current.linkText
val linkTextStyle = LocalMarkdownTypography.current.link.copy(color = linkColor).toSpanStyle()
pushStyle(linkTextStyle)
buildMarkdownAnnotatedString(content, linkText)
pop()
if (annotation != null) pop()
Expand All @@ -51,13 +48,9 @@ internal fun AnnotatedString.Builder.appendAutoLink(content: String, node: ASTNo
} ?: node
val destination = targetNode.getTextInNode(content).toString()
pushStringAnnotation(MARKDOWN_TAG_URL, (destination))
pushStyle(
SpanStyle(
color = LocalMarkdownColors.current.linkText,
textDecoration = TextDecoration.Underline,
fontWeight = FontWeight.Bold
)
)
val linkColor = LocalMarkdownColors.current.linkText
val linkTextStyle = LocalMarkdownTypography.current.link.copy(color = linkColor).toSpanStyle()
pushStyle(linkTextStyle)
append(destination)
pop()
}
Expand Down

0 comments on commit 16b92fd

Please sign in to comment.