|
| 1 | +import type { PropsWithChildren } from 'react'; |
| 2 | +import { Linking, type StyleProp, type TextStyle } from 'react-native'; |
| 3 | +import Markdown, { |
| 4 | + MarkdownIt, |
| 5 | + type RenderRules, |
| 6 | +} from 'react-native-markdown-display'; |
| 7 | +import { type ColorVars, useTheme } from '../theme'; |
| 8 | +import { confirmLinkTransition } from '../utils'; |
| 9 | +import { Text } from './Text'; |
| 10 | + |
| 11 | +interface Props { |
| 12 | + text: string; |
| 13 | + baseTextVariant?: 'caption' | 'text'; |
| 14 | + baseColor?: keyof ColorVars; |
| 15 | +} |
| 16 | + |
| 17 | +const LinkWithConfirm = ({ |
| 18 | + href, |
| 19 | + children, |
| 20 | + style, |
| 21 | +}: PropsWithChildren<{ href: string; style?: StyleProp<TextStyle> }>) => { |
| 22 | + return ( |
| 23 | + <Text |
| 24 | + style={style} |
| 25 | + onPress={async () => { |
| 26 | + const isOpenable = await Linking.canOpenURL(href); |
| 27 | + |
| 28 | + if (!isOpenable) { |
| 29 | + return; |
| 30 | + } |
| 31 | + |
| 32 | + confirmLinkTransition(href, { |
| 33 | + onOk: () => { |
| 34 | + Linking.openURL(href); |
| 35 | + }, |
| 36 | + onCancel: () => {}, |
| 37 | + }); |
| 38 | + }} |
| 39 | + > |
| 40 | + {children} |
| 41 | + </Text> |
| 42 | + ); |
| 43 | +}; |
| 44 | + |
| 45 | +const rules: RenderRules = { |
| 46 | + image: () => null, |
| 47 | + link: (node, children, _, styles) => { |
| 48 | + const href = node.attributes.href; |
| 49 | + |
| 50 | + return ( |
| 51 | + <LinkWithConfirm key={node.key} href={href} style={styles.link}> |
| 52 | + {children} |
| 53 | + </LinkWithConfirm> |
| 54 | + ); |
| 55 | + }, |
| 56 | +}; |
| 57 | + |
| 58 | +export const SimpleMarkdown = ({ |
| 59 | + text, |
| 60 | + baseTextVariant = 'text', |
| 61 | + baseColor = 'textPrimary', |
| 62 | +}: Props) => { |
| 63 | + const theme = useTheme(); |
| 64 | + const baseTextStyles = { |
| 65 | + ...theme.textVariants[baseTextVariant], |
| 66 | + color: theme.colors[baseColor], |
| 67 | + }; |
| 68 | + |
| 69 | + const commonHeadingParagraphStyle = { |
| 70 | + ...baseTextStyles, |
| 71 | + marginTop: 0, |
| 72 | + marginBottom: theme.spacing['1'], |
| 73 | + }; |
| 74 | + |
| 75 | + const commonCodeBlockStyle = { |
| 76 | + ...baseTextStyles, |
| 77 | + borderWidth: 0, |
| 78 | + backgroundColor: 'transparent', |
| 79 | + marginLeft: 0, |
| 80 | + paddingLeft: 0, |
| 81 | + borderRadius: 0, |
| 82 | + }; |
| 83 | + |
| 84 | + return ( |
| 85 | + <Markdown |
| 86 | + rules={rules} |
| 87 | + markdownit={MarkdownIt({ |
| 88 | + typographer: true, |
| 89 | + linkify: true, |
| 90 | + })} |
| 91 | + style={{ |
| 92 | + text: baseTextStyles, |
| 93 | + heading1: commonHeadingParagraphStyle, |
| 94 | + heading2: commonHeadingParagraphStyle, |
| 95 | + heading3: commonHeadingParagraphStyle, |
| 96 | + heading4: commonHeadingParagraphStyle, |
| 97 | + heading5: commonHeadingParagraphStyle, |
| 98 | + heading6: commonHeadingParagraphStyle, |
| 99 | + paragraph: commonHeadingParagraphStyle, |
| 100 | + code_block: commonCodeBlockStyle, |
| 101 | + code_inline: commonCodeBlockStyle, |
| 102 | + fence: commonCodeBlockStyle, |
| 103 | + blockquote: { |
| 104 | + backgroundColor: 'transparent', |
| 105 | + borderLeftWidth: 0, |
| 106 | + paddingHorizontal: 0, |
| 107 | + paddingVertical: 0, |
| 108 | + marginLeft: 0, |
| 109 | + }, |
| 110 | + table: { |
| 111 | + borderWidth: 0, |
| 112 | + borderRadius: 0, |
| 113 | + }, |
| 114 | + td: { |
| 115 | + padding: 0, |
| 116 | + paddingHorizontal: theme.spacing['0.5'], |
| 117 | + paddingVertical: theme.spacing['1'], |
| 118 | + }, |
| 119 | + th: { |
| 120 | + padding: 0, |
| 121 | + paddingHorizontal: theme.spacing['0.5'], |
| 122 | + paddingVertical: theme.spacing['1'], |
| 123 | + }, |
| 124 | + tr: { |
| 125 | + borderBottomWidth: 0, |
| 126 | + }, |
| 127 | + ordered_list_icon: baseTextStyles, |
| 128 | + hr: { |
| 129 | + marginVertical: theme.spacing['1'], |
| 130 | + }, |
| 131 | + }} |
| 132 | + > |
| 133 | + {text} |
| 134 | + </Markdown> |
| 135 | + ); |
| 136 | +}; |
0 commit comments