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

FileMarkdownPreview.tsx

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