Open History to see the last commit
1import { useCallback, useMemo, useState } from "react";
2import {
3 Markdown,
4 type CustomRenderers,
5 type NodeStyleOverrides,
6 type PartialMarkdownTheme,
7} from "react-native-nitro-markdown";
8import { RefreshControl, ScrollView, Text as NativeText, View } from "react-native";
9
10import { tryOpenExternalUrl } from "../../lib/openExternalUrl";
11import {
12 resolveMarkdownFontSizes,
13 resolveNativeMarkdownTypography,
14} from "../../lib/appearancePreferences";
15import { useThemeColor } from "../../lib/useThemeColor";
16import { useAppearancePreferences } from "../settings/appearance/AppearancePreferencesProvider";
17import {
18 hasNativeSelectableMarkdownText,
19 SelectableMarkdownText,
20 type NativeMarkdownTextStyle,
21} from "../../native/SelectableMarkdownText";
22
23interface MarkdownPreviewStyles {
24 readonly theme: PartialMarkdownTheme;
25 readonly styles: NodeStyleOverrides;
26 readonly renderers: CustomRenderers;
27 readonly nativeTextStyle: NativeMarkdownTextStyle;
28}
29
30function useMarkdownPreviewStyles(): MarkdownPreviewStyles {
31 const { appearance } = useAppearancePreferences();
32 const markdownFontSizes = useMemo(
33 () => resolveMarkdownFontSizes(appearance.baseFontSize),
34 [appearance.baseFontSize],
35 );
36 const nativeMarkdownTypography = useMemo(
37 () => resolveNativeMarkdownTypography(appearance.baseFontSize),
38 [appearance.baseFontSize],
39 );
40 const body = String(useThemeColor("--color-md-body"));
41 const strong = String(useThemeColor("--color-md-strong"));
42 const link = String(useThemeColor("--color-md-link"));
43 const blockquoteBorder = String(useThemeColor("--color-md-blockquote-border"));
44 const blockquoteBackground = String(useThemeColor("--color-md-blockquote-bg"));
45 const codeBackground = String(useThemeColor("--color-md-code-bg"));
46 const codeText = String(useThemeColor("--color-md-code-text"));
47 const horizontalRule = String(useThemeColor("--color-md-hr"));
48
49 return useMemo(() => {
50 const renderers: CustomRenderers = {
51 link: ({ href, children }) => (
52 <NativeText
53 onPress={() => {
54 if (href) {
55 void tryOpenExternalUrl(href, "markdown-link");
56 }
57 }}
58 style={{
59 color: link,
60 fontFamily: "DMSans_500Medium",
61 textDecorationLine: "none",
62 }}
63 >
64 {children}
65 </NativeText>
66 ),
67 };
68
69 return {
70 theme: {
71 colors: {
72 text: body,
73 heading: strong,
74 link,
75 blockquote: blockquoteBorder,
76 border: horizontalRule,
77 surfaceLight: blockquoteBackground,
78 accent: link,
79 tableBorder: horizontalRule,
80 tableHeader: blockquoteBackground,
81 tableHeaderText: strong,
82 code: codeText,
83 codeBackground,
84 },
85 },
86 styles: {
87 text: {
88 color: body,
89 fontFamily: "DMSans_400Regular",
90 fontSize: markdownFontSizes.m,
91 lineHeight: markdownFontSizes.bodyLineHeight,
92 },
93 heading: {
94 color: strong,
95 fontFamily: "DMSans_700Bold",
96 },
97 strong: {
98 color: strong,
99 fontFamily: "DMSans_700Bold",
100 },
101 link: {
102 color: link,
103 fontFamily: "DMSans_500Medium",
104 },
105 blockquote: {
106 backgroundColor: blockquoteBackground,
107 borderLeftColor: blockquoteBorder,
108 borderLeftWidth: 3,
109 paddingLeft: 12,
110 },
111 code: {
112 backgroundColor: codeBackground,
113 color: codeText,
114 fontFamily: "ui-monospace",
115 },
116 codeBlock: {
117 backgroundColor: codeBackground,
118 borderRadius: 12,
119 color: codeText,
120 fontFamily: "ui-monospace",
121 padding: 12,
122 },
123 hr: {
124 backgroundColor: horizontalRule,
125 },
126 },
127 renderers,
128 nativeTextStyle: {
129 color: body,
130 strongColor: strong,
131 mutedColor: body,
132 linkColor: link,
133 inlineCodeColor: codeText,
134 codeColor: codeText,
135 codeBackgroundColor: codeBackground,
136 codeBlockBackgroundColor: codeBackground,
137 fileTextColor: codeText,
138 skillTextColor: codeText,
139 quoteMarkerColor: blockquoteBorder,
140 dividerColor: horizontalRule,
141 fontSize: nativeMarkdownTypography.fontSize,
142 lineHeight: nativeMarkdownTypography.lineHeight,
143 headingFontSizes: nativeMarkdownTypography.headingFontSizes,
144 fontFamily: "DMSans_400Regular",
145 headingFontFamily: "DMSans_700Bold",
146 boldFontFamily: "DMSans_700Bold",
147 },
148 };
149 }, [
150 blockquoteBackground,
151 blockquoteBorder,
152 body,
153 codeBackground,
154 codeText,
155 horizontalRule,
156 link,
157 markdownFontSizes,
158 nativeMarkdownTypography,
159 strong,
160 ]);
161}
162
163export function FileMarkdownPreview(props: {
164 readonly markdown: string;
165 readonly onRefresh?: () => Promise<void> | void;
166}) {
167 const [isPullRefreshing, setIsPullRefreshing] = useState(false);
168 const handlePullToRefresh = useCallback(async () => {
169 if (!props.onRefresh) {
170 return;
171 }
172 setIsPullRefreshing(true);
173 try {
174 await props.onRefresh();
175 } finally {
176 setIsPullRefreshing(false);
177 }
178 }, [props.onRefresh]);
179 const styles = useMarkdownPreviewStyles();
180 const onLinkPress = useCallback((href: string) => {
181 void tryOpenExternalUrl(href, "markdown-link");
182 }, []);
183
184 return (
185 <ScrollView
186 className="flex-1 bg-sheet"
187 contentContainerStyle={{ padding: 18 }}
188 refreshControl={
189 props.onRefresh ? (
190 <RefreshControl
191 refreshing={isPullRefreshing}
192 onRefresh={() => void handlePullToRefresh()}
193 />
194 ) : undefined
195 }
196 >
197 <View className="mx-auto w-full max-w-[760px]">
198 {hasNativeSelectableMarkdownText() ? (
199 <SelectableMarkdownText
200 markdown={props.markdown}
201 onLinkPress={onLinkPress}
202 textStyle={styles.nativeTextStyle}
203 />
204 ) : (
205 <Markdown
206 options={{ gfm: true }}
207 renderers={styles.renderers}
208 styles={styles.styles}
209 theme={styles.theme}
210 >
211 {props.markdown}
212 </Markdown>
213 )}
214 </View>
215 </ScrollView>
216 );
217}