|
| 1 | +import {memo} from 'react'; |
| 2 | +import {useStyles} from 'design/styles'; |
| 3 | +import {ScrollView, Platform} from 'react-native'; |
| 4 | +import Render from 'react-native-markdown-display'; |
| 5 | + |
| 6 | +export const Markdown = memo(({text}: {text: string}) => { |
| 7 | + const {theme} = useStyles(); |
| 8 | + |
| 9 | + if (!text) { |
| 10 | + return null; |
| 11 | + } |
| 12 | + |
| 13 | + return ( |
| 14 | + <ScrollView |
| 15 | + contentInsetAdjustmentBehavior="automatic" |
| 16 | + style={{height: '100%'}}> |
| 17 | + <Render |
| 18 | + mergeStyle={false} |
| 19 | + style={{ |
| 20 | + body: { |
| 21 | + gap: theme.display.space2, |
| 22 | + fontFamily: theme.font.family, |
| 23 | + letterSpacing: theme.font.spacing, |
| 24 | + lineHeight: theme.typography.lineHeight2, |
| 25 | + color: theme.colors.foreground, |
| 26 | + }, |
| 27 | + // Headings |
| 28 | + heading1: { |
| 29 | + flexDirection: 'row', |
| 30 | + fontSize: theme.typography.size8, |
| 31 | + lineHeight: theme.typography.size8, |
| 32 | + letterSpacing: theme.typography.letterSpacing8, |
| 33 | + }, |
| 34 | + heading2: { |
| 35 | + flexDirection: 'row', |
| 36 | + fontSize: theme.typography.size6, |
| 37 | + lineHeight: theme.typography.size6, |
| 38 | + letterSpacing: theme.typography.letterSpacing6, |
| 39 | + }, |
| 40 | + heading3: { |
| 41 | + flexDirection: 'row', |
| 42 | + fontSize: theme.typography.size4, |
| 43 | + lineHeight: theme.typography.size4, |
| 44 | + letterSpacing: theme.typography.letterSpacing4, |
| 45 | + }, |
| 46 | + heading4: { |
| 47 | + flexDirection: 'row', |
| 48 | + fontSize: theme.typography.size3, |
| 49 | + lineHeight: theme.typography.size3, |
| 50 | + letterSpacing: theme.typography.letterSpacing3, |
| 51 | + }, |
| 52 | + heading5: { |
| 53 | + flexDirection: 'row', |
| 54 | + fontSize: theme.typography.size2, |
| 55 | + lineHeight: theme.typography.size2, |
| 56 | + letterSpacing: theme.typography.letterSpacing2, |
| 57 | + }, |
| 58 | + heading6: { |
| 59 | + flexDirection: 'row', |
| 60 | + fontSize: theme.typography.size1, |
| 61 | + lineHeight: theme.typography.size1, |
| 62 | + letterSpacing: theme.typography.letterSpacing1, |
| 63 | + }, |
| 64 | + // Horizontal Rule |
| 65 | + hr: { |
| 66 | + height: 1, |
| 67 | + backgroundColor: theme.colors.border, |
| 68 | + }, |
| 69 | + // Emphasis |
| 70 | + strong: { |
| 71 | + fontWeight: 'bold', |
| 72 | + }, |
| 73 | + em: { |
| 74 | + fontStyle: 'italic', |
| 75 | + }, |
| 76 | + s: { |
| 77 | + textDecorationLine: 'line-through', |
| 78 | + }, |
| 79 | + // Blockquotes |
| 80 | + blockquote: { |
| 81 | + backgroundColor: theme.colors.background, |
| 82 | + borderColor: theme.colors.border, |
| 83 | + borderLeftWidth: 4, |
| 84 | + marginLeft: theme.display.space2, |
| 85 | + paddingHorizontal: theme.display.space2, |
| 86 | + borderRadius: theme.display.radius3, |
| 87 | + }, |
| 88 | + // Lists |
| 89 | + bullet_list: {}, |
| 90 | + ordered_list: {}, |
| 91 | + list_item: { |
| 92 | + flexDirection: 'row', |
| 93 | + justifyContent: 'flex-start', |
| 94 | + }, |
| 95 | + // @pseudo class, does not have a unique render rule |
| 96 | + bullet_list_icon: { |
| 97 | + marginLeft: theme.display.space2, |
| 98 | + marginRight: theme.display.space2, |
| 99 | + }, |
| 100 | + // @pseudo class, does not have a unique render rule |
| 101 | + bullet_list_content: { |
| 102 | + flex: 1, |
| 103 | + }, |
| 104 | + // @pseudo class, does not have a unique render rule |
| 105 | + ordered_list_icon: { |
| 106 | + marginLeft: theme.display.space2, |
| 107 | + marginRight: theme.display.space2, |
| 108 | + }, |
| 109 | + // @pseudo class, does not have a unique render rule |
| 110 | + ordered_list_content: { |
| 111 | + flex: 1, |
| 112 | + }, |
| 113 | + // Code |
| 114 | + code_inline: { |
| 115 | + borderWidth: 1, |
| 116 | + borderColor: theme.colors.border, |
| 117 | + lineHeight: theme.font.labelHeight, |
| 118 | + paddingVertical: theme.display.space1, |
| 119 | + paddingHorizontal: theme.display.space2, |
| 120 | + borderRadius: theme.display.radius3, |
| 121 | + backgroundColor: theme.colors.card, |
| 122 | + color: theme.colors.cardForeground, |
| 123 | + ...Platform.select({ |
| 124 | + ios: { |
| 125 | + fontFamily: 'Courier', |
| 126 | + }, |
| 127 | + default: { |
| 128 | + fontFamily: 'monospace', |
| 129 | + }, |
| 130 | + }), |
| 131 | + }, |
| 132 | + code_block: { |
| 133 | + borderWidth: 1, |
| 134 | + borderColor: theme.colors.border, |
| 135 | + borderRadius: theme.display.radius3, |
| 136 | + padding: theme.display.space3, |
| 137 | + backgroundColor: theme.colors.card, |
| 138 | + color: theme.colors.cardForeground, |
| 139 | + ...Platform.select({ |
| 140 | + ios: { |
| 141 | + fontFamily: 'Courier', |
| 142 | + }, |
| 143 | + default: { |
| 144 | + fontFamily: 'monospace', |
| 145 | + }, |
| 146 | + }), |
| 147 | + }, |
| 148 | + fence: { |
| 149 | + borderWidth: 1, |
| 150 | + borderColor: theme.colors.border, |
| 151 | + borderRadius: theme.display.radius3, |
| 152 | + padding: theme.display.space3, |
| 153 | + backgroundColor: theme.colors.card, |
| 154 | + color: theme.colors.cardForeground, |
| 155 | + ...Platform.select({ |
| 156 | + ios: { |
| 157 | + fontFamily: 'Courier', |
| 158 | + }, |
| 159 | + default: { |
| 160 | + fontFamily: 'monospace', |
| 161 | + }, |
| 162 | + }), |
| 163 | + }, |
| 164 | + // Tables |
| 165 | + table: { |
| 166 | + borderWidth: 1, |
| 167 | + borderColor: theme.colors.border, |
| 168 | + borderRadius: theme.display.radius3, |
| 169 | + }, |
| 170 | + thead: {}, |
| 171 | + tbody: {}, |
| 172 | + th: { |
| 173 | + flex: 1, |
| 174 | + padding: theme.display.space2, |
| 175 | + }, |
| 176 | + tr: { |
| 177 | + borderBottomWidth: 1, |
| 178 | + borderColor: theme.colors.border, |
| 179 | + flexDirection: 'row', |
| 180 | + }, |
| 181 | + td: { |
| 182 | + flex: 1, |
| 183 | + padding: theme.display.space2, |
| 184 | + }, |
| 185 | + // Links |
| 186 | + link: { |
| 187 | + textDecorationLine: 'underline', |
| 188 | + }, |
| 189 | + blocklink: { |
| 190 | + flex: 1, |
| 191 | + borderColor: theme.colors.border, |
| 192 | + borderBottomWidth: 1, |
| 193 | + }, |
| 194 | + // Images |
| 195 | + image: { |
| 196 | + flex: 1, |
| 197 | + }, |
| 198 | + // Text Output |
| 199 | + text: {}, |
| 200 | + textgroup: {}, |
| 201 | + paragraph: { |
| 202 | + width: '100%', |
| 203 | + flexWrap: 'wrap', |
| 204 | + flexDirection: 'row', |
| 205 | + alignItems: 'flex-start', |
| 206 | + justifyContent: 'flex-start', |
| 207 | + marginVertical: theme.display.space3, |
| 208 | + fontSize: theme.font.contentSize, |
| 209 | + fontWeight: theme.font.contentWeight, |
| 210 | + lineHeight: theme.font.contentHeight, |
| 211 | + letterSpacing: theme.font.contentSpacing, |
| 212 | + }, |
| 213 | + hardbreak: { |
| 214 | + width: '100%', |
| 215 | + height: 1, |
| 216 | + }, |
| 217 | + softbreak: {}, |
| 218 | + // Never used but retained for completeness |
| 219 | + pre: {}, |
| 220 | + inline: {}, |
| 221 | + span: {}, |
| 222 | + }}> |
| 223 | + {text} |
| 224 | + </Render> |
| 225 | + </ScrollView> |
| 226 | + ); |
| 227 | +}) |
0 commit comments