Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix inline link might navigate wrong #292

Merged
merged 2 commits into from
Feb 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,17 @@ 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.*
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.MarkdownDivider
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.MarkdownTable
import com.mikepenz.markdown.compose.elements.MarkdownText
import com.mikepenz.markdown.model.MarkdownTypography
import com.mikepenz.markdown.utils.getUnescapedTextInNode
import org.intellij.markdown.IElementType
Expand Down Expand Up @@ -187,11 +197,9 @@ object CurrentComponentsBridge {
MarkdownImage(it.content, it.node)
}
val linkDefinition: MarkdownComponent = {
val linkLabel =
it.node.findChildOfType(MarkdownElementTypes.LINK_LABEL)?.getUnescapedTextInNode(it.content)
val linkLabel = it.node.findChildOfType(MarkdownElementTypes.LINK_LABEL)?.getUnescapedTextInNode(it.content)
if (linkLabel != null) {
val destination = it.node.findChildOfType(MarkdownElementTypes.LINK_DESTINATION)
?.getUnescapedTextInNode(it.content)
val destination = it.node.findChildOfType(MarkdownElementTypes.LINK_DESTINATION)?.getUnescapedTextInNode(it.content)
LocalReferenceLinkHandler.current.store(linkLabel, destination)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.buildAnnotatedString
import com.mikepenz.markdown.compose.LocalMarkdownAnnotator
import com.mikepenz.markdown.compose.LocalMarkdownTypography
import com.mikepenz.markdown.compose.LocalReferenceLinkHandler
import com.mikepenz.markdown.utils.buildMarkdownAnnotatedString
import com.mikepenz.markdown.utils.codeSpanStyle
import com.mikepenz.markdown.utils.linkTextSpanStyle
Expand All @@ -21,9 +22,17 @@ fun MarkdownParagraph(
val annotator = LocalMarkdownAnnotator.current
val linkTextSpanStyle = LocalMarkdownTypography.current.linkTextSpanStyle
val codeSpanStyle = LocalMarkdownTypography.current.codeSpanStyle
val referenceLinkHandler = LocalReferenceLinkHandler.current
val styledText = buildAnnotatedString {
pushStyle(style.toSpanStyle())
buildMarkdownAnnotatedString(content, node, linkTextSpanStyle, codeSpanStyle, annotator)
buildMarkdownAnnotatedString(
content = content,
node = node,
linkTextStyle = linkTextSpanStyle,
codeStyle = codeSpanStyle,
annotator = annotator,
referenceLinkHandler = referenceLinkHandler
)
pop()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,15 @@ package com.mikepenz.markdown.compose.elements

import androidx.compose.foundation.background
import androidx.compose.foundation.horizontalScroll
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.layout.BoxWithConstraints
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.IntrinsicSize
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.requiredWidth
import androidx.compose.foundation.layout.widthIn
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.runtime.Composable
Expand All @@ -16,10 +24,15 @@ import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.times
import com.mikepenz.markdown.compose.LocalMarkdownAnnotator
import com.mikepenz.markdown.compose.LocalMarkdownColors
import com.mikepenz.markdown.compose.LocalMarkdownDimens
import com.mikepenz.markdown.compose.LocalMarkdownTypography
import com.mikepenz.markdown.compose.LocalReferenceLinkHandler
import com.mikepenz.markdown.compose.elements.material.MarkdownBasicText
import com.mikepenz.markdown.utils.buildMarkdownAnnotatedString
import com.mikepenz.markdown.utils.codeSpanStyle
import com.mikepenz.markdown.utils.linkTextSpanStyle
import org.intellij.markdown.ast.ASTNode
import org.intellij.markdown.ast.findChildOfType
import org.intellij.markdown.flavours.gfm.GFMElementTypes.HEADER
Expand Down Expand Up @@ -90,6 +103,10 @@ fun MarkdownTableHeader(
maxLines: Int = 1,
overflow: TextOverflow = TextOverflow.Ellipsis,
) {
val annotator = LocalMarkdownAnnotator.current
val linkTextSpanStyle = LocalMarkdownTypography.current.linkTextSpanStyle
val codeSpanStyle = LocalMarkdownTypography.current.codeSpanStyle
val referenceLinkHandler = LocalReferenceLinkHandler.current
val tableCellPadding = LocalMarkdownDimens.current.tableCellPadding
Row(
verticalAlignment = verticalAlignment,
Expand All @@ -100,7 +117,12 @@ fun MarkdownTableHeader(
CELL -> {
MarkdownBasicText(
text = content.buildMarkdownAnnotatedString(
it, style.copy(fontWeight = FontWeight.Bold)
textNode = it,
style = style.copy(fontWeight = FontWeight.Bold),
linkTextSpanStyle = linkTextSpanStyle,
codeSpanStyle = codeSpanStyle,
annotator = annotator,
referenceLinkHandler = referenceLinkHandler
),
style = style.copy(fontWeight = FontWeight.Bold),
color = LocalMarkdownColors.current.tableText,
Expand All @@ -124,14 +146,25 @@ fun MarkdownTableRow(
maxLines: Int = 1,
overflow: TextOverflow = TextOverflow.Ellipsis,
) {
val annotator = LocalMarkdownAnnotator.current
val linkTextSpanStyle = LocalMarkdownTypography.current.linkTextSpanStyle
val codeSpanStyle = LocalMarkdownTypography.current.codeSpanStyle
val referenceLinkHandler = LocalReferenceLinkHandler.current
val tableCellPadding = LocalMarkdownDimens.current.tableCellPadding
Row(
verticalAlignment = verticalAlignment,
modifier = Modifier.widthIn(tableWidth)
) {
header.children.filter { it.type == CELL }.forEach { cell ->
MarkdownBasicText(
text = content.buildMarkdownAnnotatedString(cell, style),
text = content.buildMarkdownAnnotatedString(
textNode = cell,
style = style,
linkTextSpanStyle = linkTextSpanStyle,
codeSpanStyle = codeSpanStyle,
annotator = annotator,
referenceLinkHandler = referenceLinkHandler
),
style = style,
color = LocalMarkdownColors.current.tableText,
modifier = Modifier.padding(tableCellPadding).weight(1f),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,40 @@ import androidx.compose.foundation.gestures.awaitEachGesture
import androidx.compose.foundation.gestures.awaitFirstDown
import androidx.compose.foundation.gestures.waitForUpOrCancellation
import androidx.compose.foundation.text.InlineTextContent
import androidx.compose.runtime.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.input.pointer.PointerIcon
import androidx.compose.ui.input.pointer.pointerHoverIcon
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.layout.onPlaced
import androidx.compose.ui.platform.LocalUriHandler
import androidx.compose.ui.text.*
import androidx.compose.ui.text.AnnotatedString
import androidx.compose.ui.text.Placeholder
import androidx.compose.ui.text.TextLayoutResult
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.unit.sp
import com.mikepenz.markdown.compose.*
import com.mikepenz.markdown.compose.LocalImageTransformer
import com.mikepenz.markdown.compose.LocalMarkdownAnimations
import com.mikepenz.markdown.compose.LocalMarkdownAnnotator
import com.mikepenz.markdown.compose.LocalMarkdownColors
import com.mikepenz.markdown.compose.LocalMarkdownExtendedSpans
import com.mikepenz.markdown.compose.LocalMarkdownTypography
import com.mikepenz.markdown.compose.LocalReferenceLinkHandler
import com.mikepenz.markdown.compose.elements.material.MarkdownBasicText
import com.mikepenz.markdown.compose.extendedspans.ExtendedSpans
import com.mikepenz.markdown.compose.extendedspans.drawBehind
import com.mikepenz.markdown.model.rememberMarkdownImageState
import com.mikepenz.markdown.utils.*
import com.mikepenz.markdown.utils.MARKDOWN_TAG_IMAGE_URL
import com.mikepenz.markdown.utils.MARKDOWN_TAG_URL
import com.mikepenz.markdown.utils.buildMarkdownAnnotatedString
import com.mikepenz.markdown.utils.codeSpanStyle
import com.mikepenz.markdown.utils.linkTextSpanStyle
import org.intellij.markdown.IElementType
import org.intellij.markdown.ast.ASTNode
import org.intellij.markdown.ast.findChildOfType
Expand All @@ -45,11 +64,12 @@ fun MarkdownText(
val annotator = LocalMarkdownAnnotator.current
val linkTextSpanStyle = LocalMarkdownTypography.current.linkTextSpanStyle
val codeSpanStyle = LocalMarkdownTypography.current.codeSpanStyle
val referenceLinkHandler = LocalReferenceLinkHandler.current
val childNode = contentChildType?.let { node.findChildOfType(it) } ?: node

val styledText = buildAnnotatedString {
pushStyle(style.toSpanStyle())
buildMarkdownAnnotatedString(content, childNode, linkTextSpanStyle, codeSpanStyle, annotator)
buildMarkdownAnnotatedString(content, childNode, linkTextSpanStyle, codeSpanStyle, annotator, referenceLinkHandler)
pop()
}

Expand Down Expand Up @@ -111,8 +131,7 @@ fun MarkdownText(

val foundReference = layoutResult.value?.let { layoutResult ->
val position = layoutResult.getOffsetForPosition(pos)
content.getStringAnnotations(MARKDOWN_TAG_URL, position, position).reversed().firstOrNull()
?.let { referenceLinkHandler.find(it.item) }
content.getStringAnnotations(MARKDOWN_TAG_URL, position, position).reversed().firstOrNull()?.let { referenceLinkHandler.find(it.item) }
}

if (foundReference != null) {
Expand Down
Loading