import React, { useMemo } from 'react'; import { Linking, StyleSheet, View } from 'react-native'; import Markdown from 'react-native-markdown-display'; import { EnxChatTheme } from '../types'; export type ChatMarkdownVariant = | 'body' | 'subtitle' | 'title' | 'linkTitle' | 'linkDesc' | 'button' | 'compact'; type Props = { content: string; theme: EnxChatTheme; variant: ChatMarkdownVariant; /** Extra wrapper style (e.g. horizontal padding) */ containerStyle?: object; /** Overrides default text/link color (e.g. choice chips). */ textColor?: string; }; const openLink = (url: string): boolean => { if (url) void Linking.openURL(url); return false; }; export function ChatMarkdown({ content, theme, variant, containerStyle, textColor }: Props): React.JSX.Element | null { const trimmed = content?.trim() ?? ''; if (!trimmed) return null; const style = useMemo(() => { const primary = theme.primaryColor; const baseText = { color: '#111827' as const }; const compactParagraph = { marginTop: 0, marginBottom: 0, flexWrap: 'wrap' as const, flexDirection: 'row' as const, }; switch (variant) { case 'body': return { body: { ...baseText, paddingHorizontal: 12, paddingTop: 10, flexShrink: 1 }, paragraph: { ...compactParagraph, marginTop: 4, marginBottom: 4 }, text: { ...baseText, fontSize: 15, lineHeight: 22 }, link: { color: primary, textDecorationLine: 'underline' as const }, strong: { fontWeight: '700' as const }, bullet_list: { marginVertical: 4 }, ordered_list: { marginVertical: 4 }, heading1: { fontSize: 22, fontWeight: '700' as const, ...baseText, marginTop: 6, marginBottom: 4 }, heading2: { fontSize: 19, fontWeight: '700' as const, ...baseText, marginTop: 6, marginBottom: 4 }, heading3: { fontSize: 17, fontWeight: '700' as const, ...baseText, marginTop: 4, marginBottom: 2 }, heading4: { fontSize: 16, fontWeight: '600' as const, ...baseText }, heading5: { fontSize: 15, fontWeight: '600' as const, ...baseText }, heading6: { fontSize: 14, fontWeight: '600' as const, ...baseText }, blockquote: { marginVertical: 6, paddingHorizontal: 8 }, code_inline: { fontSize: 14, padding: 4 }, fence: { marginVertical: 6, fontSize: 13 }, code_block: { marginVertical: 6, fontSize: 13 }, image: { marginVertical: 6, borderRadius: 8, overflow: 'hidden' as const }, }; case 'subtitle': return { body: { ...baseText, paddingHorizontal: 12, paddingTop: 4, flexShrink: 1 }, paragraph: { ...compactParagraph, marginTop: 2, marginBottom: 2 }, text: { ...baseText, color: '#4b5563', fontSize: 13, lineHeight: 18 }, link: { color: primary, textDecorationLine: 'underline' as const }, strong: { fontWeight: '700' as const }, bullet_list: { marginVertical: 2 }, ordered_list: { marginVertical: 2 }, heading1: { fontSize: 18, ...baseText, color: '#4b5563' }, heading2: { fontSize: 16, ...baseText, color: '#4b5563' }, heading3: { fontSize: 15, ...baseText, color: '#4b5563' }, image: { marginVertical: 4 }, }; case 'title': return { body: { flexShrink: 1 }, paragraph: compactParagraph, text: { ...baseText, fontSize: 16, lineHeight: 22, fontWeight: '700' as const }, link: { color: primary, textDecorationLine: 'underline' as const }, strong: { fontWeight: '700' as const }, }; case 'linkTitle': return { body: { marginBottom: 6, flexShrink: 1 }, paragraph: compactParagraph, text: { ...baseText, fontSize: 16, lineHeight: 22, fontWeight: '700' as const }, link: { color: primary, textDecorationLine: 'underline' as const }, strong: { fontWeight: '700' as const }, }; case 'linkDesc': return { body: { marginBottom: 8, flexShrink: 1 }, paragraph: { ...compactParagraph, marginTop: 2, marginBottom: 2 }, text: { ...baseText, color: '#4b5563', fontSize: 14, lineHeight: 20 }, link: { color: primary, textDecorationLine: 'underline' as const }, strong: { fontWeight: '700' as const }, bullet_list: { marginVertical: 4 }, ordered_list: { marginVertical: 4 }, image: { marginVertical: 4 }, }; case 'button': { const c = textColor ?? primary; return { body: { width: '100%', alignItems: 'center' as const }, paragraph: { ...compactParagraph, marginTop: 0, marginBottom: 0, justifyContent: 'center' as const, alignSelf: 'center' as const, maxWidth: '100%', }, text: { fontSize: 15, textAlign: 'center' as const, color: c }, link: { color: c, textDecorationLine: 'underline' as const }, strong: { fontWeight: '700' as const }, }; } case 'compact': return { body: { flex: 1, flexShrink: 1 }, paragraph: compactParagraph, text: { ...baseText, fontSize: 14, fontWeight: '600' as const, ...(textColor ? { color: textColor } : {}) }, link: { color: textColor ?? primary, textDecorationLine: 'underline' as const }, strong: { fontWeight: '700' as const }, }; default: return {}; } }, [theme.primaryColor, variant, textColor]); return ( ['style']} mergeStyle onLinkPress={openLink}> {trimmed} ); } const styles = StyleSheet.create({ wrap: { alignSelf: 'stretch', maxWidth: '100%' }, });