

























@@ -30,13 +30,16 @@ import androidx.compose.ui.Modifier
3030import androidx.compose.ui.graphics.Color
3131import androidx.compose.ui.layout.ContentScale
3232import androidx.compose.ui.text.AnnotatedString
33+import androidx.compose.ui.text.LinkAnnotation
3334import androidx.compose.ui.text.SpanStyle
35+import androidx.compose.ui.text.TextLinkStyles
3436import androidx.compose.ui.text.TextStyle
3537import androidx.compose.ui.text.buildAnnotatedString
3638import androidx.compose.ui.text.font.FontFamily
3739import androidx.compose.ui.text.font.FontStyle
3840import androidx.compose.ui.text.font.FontWeight
3941import androidx.compose.ui.text.style.TextDecoration
42+import androidx.compose.ui.text.withLink
4043import androidx.compose.ui.text.withStyle
4144import androidx.compose.ui.unit.dp
4245import androidx.compose.ui.unit.sp
@@ -499,19 +502,12 @@ private fun AnnotatedString.Builder.appendInlineNode(
499502 }
500503 }
501504is Link -> {
502- withStyle(
503-SpanStyle(
504- color = linkColor,
505- textDecoration = TextDecoration.Underline,
506- ),
507- ) {
508- appendInlineNode(
509- current.firstChild,
510- inlineCodeBg = inlineCodeBg,
511- inlineCodeColor = inlineCodeColor,
512- linkColor = linkColor,
513- )
514- }
505+ appendLinkNode(
506+ link = current,
507+ inlineCodeBg = inlineCodeBg,
508+ inlineCodeColor = inlineCodeColor,
509+ linkColor = linkColor,
510+ )
515511 }
516512is MarkdownImage -> {
517513val alt = buildPlainText(current.firstChild)
@@ -527,13 +523,69 @@ private fun AnnotatedString.Builder.appendInlineNode(
527523 }
528524 }
529525else -> {
530- appendInlineNode(current.firstChild, inlineCodeBg = inlineCodeBg, inlineCodeColor = inlineCodeColor, linkColor = linkColor)
526+ appendInlineNode(
527+ current.firstChild,
528+ inlineCodeBg = inlineCodeBg,
529+ inlineCodeColor = inlineCodeColor,
530+ linkColor = linkColor,
531+ )
531532 }
532533 }
533534 current = current.next
534535 }
535536}
536537538+private fun AnnotatedString.Builder.appendLinkNode(
539+link: Link,
540+inlineCodeBg: Color,
541+inlineCodeColor: Color,
542+linkColor: Color,
543+) {
544+val destination = link.destination?.trim().orEmpty()
545+val linkStyle =
546+SpanStyle(
547+ color = linkColor,
548+ textDecoration = TextDecoration.Underline,
549+ )
550+if (destination.isEmpty()) {
551+ withStyle(linkStyle) {
552+ appendInlineNode(
553+ link.firstChild,
554+ inlineCodeBg = inlineCodeBg,
555+ inlineCodeColor = inlineCodeColor,
556+ linkColor = linkColor,
557+ )
558+ }
559+return
560+ }
561+562+ withLink(LinkAnnotation.Url(url = destination, styles = TextLinkStyles(style = linkStyle))) {
563+ appendInlineNode(
564+ link.firstChild,
565+ inlineCodeBg = inlineCodeBg,
566+ inlineCodeColor = inlineCodeColor,
567+ linkColor = linkColor,
568+ )
569+ }
570+}
571+572+internal fun buildChatInlineMarkdown(
573+text: String,
574+linkColor: Color = Color.Blue,
575+): AnnotatedString {
576+val document = markdownParser.parse(text) as Document
577+val paragraph = document.firstChild as? Paragraph ?: return AnnotatedString("")
578+return buildInlineMarkdown(
579+ paragraph.firstChild,
580+InlineStyles(
581+ inlineCodeBg = Color.Transparent,
582+ inlineCodeColor = Color.Unspecified,
583+ linkColor = linkColor,
584+ baseCallout = TextStyle.Default,
585+ ),
586+ )
587+}
588+537589private fun buildPlainText(start: Node?): String {
538590val sb = StringBuilder()
539591var node = start
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。