Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

FileMarkdownPreview.tsx

227 lines6.6 KB

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