Open History to see the last commit
1import { useCallback, useMemo } from "react";
2import {
3 Markdown,
4 type CustomRenderers,
5 type NodeStyleOverrides,
6 type PartialMarkdownTheme,
7} from "react-native-nitro-markdown";
8import { ScrollView, Text as NativeText, View } from "react-native";
9
10import { tryOpenExternalUrl } from "../../lib/openExternalUrl";
11import { MOBILE_TYPOGRAPHY } from "../../lib/typography";
12import { useThemeColor } from "../../lib/useThemeColor";
13import {
14 hasNativeSelectableMarkdownText,
15 SelectableMarkdownText,
16 type NativeMarkdownTextStyle,
17} from "../../native/SelectableMarkdownText";
18
19interface MarkdownPreviewStyles {
20 readonly theme: PartialMarkdownTheme;
21 readonly styles: NodeStyleOverrides;
22 readonly renderers: CustomRenderers;
23 readonly nativeTextStyle: NativeMarkdownTextStyle;
24}
25
26function useMarkdownPreviewStyles(): MarkdownPreviewStyles {
27 const body = String(useThemeColor("--color-md-body"));
28 const strong = String(useThemeColor("--color-md-strong"));
29 const link = String(useThemeColor("--color-md-link"));
30 const blockquoteBorder = String(useThemeColor("--color-md-blockquote-border"));
31 const blockquoteBackground = String(useThemeColor("--color-md-blockquote-bg"));
32 const codeBackground = String(useThemeColor("--color-md-code-bg"));
33 const codeText = String(useThemeColor("--color-md-code-text"));
34 const horizontalRule = String(useThemeColor("--color-md-hr"));
35
36 return useMemo(() => {
37 const renderers: CustomRenderers = {
38 link: ({ href, children }) => (
39 <NativeText
40 onPress={() => {
41 if (href) {
42 void tryOpenExternalUrl(href, "markdown-link");
43 }
44 }}
45 style={{
46 color: link,
47 fontFamily: "DMSans_500Medium",
48 textDecorationLine: "none",
49 }}
50 >
51 {children}
52 </NativeText>
53 ),
54 };
55
56 return {
57 theme: {
58 colors: {
59 text: body,
60 heading: strong,
61 link,
62 blockquote: blockquoteBorder,
63 border: horizontalRule,
64 surfaceLight: blockquoteBackground,
65 accent: link,
66 tableBorder: horizontalRule,
67 tableHeader: blockquoteBackground,
68 tableHeaderText: strong,
69 code: codeText,
70 codeBackground,
71 },
72 },
73 styles: {
74 text: {
75 color: body,
76 fontFamily: "DMSans_400Regular",
77 ...MOBILE_TYPOGRAPHY.body,
78 },
79 heading: {
80 color: strong,
81 fontFamily: "DMSans_700Bold",
82 },
83 strong: {
84 color: strong,
85 fontFamily: "DMSans_700Bold",
86 },
87 link: {
88 color: link,
89 fontFamily: "DMSans_500Medium",
90 },
91 blockquote: {
92 backgroundColor: blockquoteBackground,
93 borderLeftColor: blockquoteBorder,
94 borderLeftWidth: 3,
95 paddingLeft: 12,
96 },
97 code: {
98 backgroundColor: codeBackground,
99 color: codeText,
100 fontFamily: "ui-monospace",
101 },
102 codeBlock: {
103 backgroundColor: codeBackground,
104 borderRadius: 12,
105 color: codeText,
106 fontFamily: "ui-monospace",
107 padding: 12,
108 },
109 hr: {
110 backgroundColor: horizontalRule,
111 },
112 },
113 renderers,
114 nativeTextStyle: {
115 color: body,
116 strongColor: strong,
117 mutedColor: body,
118 linkColor: link,
119 inlineCodeColor: codeText,
120 codeColor: codeText,
121 codeBackgroundColor: codeBackground,
122 codeBlockBackgroundColor: codeBackground,
123 fileTextColor: codeText,
124 skillTextColor: codeText,
125 quoteMarkerColor: blockquoteBorder,
126 dividerColor: horizontalRule,
127 ...MOBILE_TYPOGRAPHY.body,
128 fontFamily: "DMSans_400Regular",
129 headingFontFamily: "DMSans_700Bold",
130 boldFontFamily: "DMSans_700Bold",
131 },
132 };
133 }, [
134 blockquoteBackground,
135 blockquoteBorder,
136 body,
137 codeBackground,
138 codeText,
139 horizontalRule,
140 link,
141 strong,
142 ]);
143}
144
145export function FileMarkdownPreview(props: { readonly markdown: string }) {
146 const styles = useMarkdownPreviewStyles();
147 const onLinkPress = useCallback((href: string) => {
148 void tryOpenExternalUrl(href, "markdown-link");
149 }, []);
150
151 return (
152 <ScrollView className="flex-1 bg-card" contentContainerStyle={{ padding: 18 }}>
153 <View className="mx-auto w-full max-w-[760px]">
154 {hasNativeSelectableMarkdownText() ? (
155 <SelectableMarkdownText
156 markdown={props.markdown}
157 onLinkPress={onLinkPress}
158 textStyle={styles.nativeTextStyle}
159 />
160 ) : (
161 <Markdown
162 options={{ gfm: true }}
163 renderers={styles.renderers}
164 styles={styles.styles}
165 theme={styles.theme}
166 >
167 {props.markdown}
168 </Markdown>
169 )}
170 </View>
171 </ScrollView>
172 );
173}