Open History to see the last commit
1import { Image, Linking, type TextStyle, useColorScheme } from "react-native";
2
3import { MarkdownTextPrimitive } from "./MarkdownTextPrimitive";
4import { markdownFileIconSource } from "./markdownFileIcons";
5import type { NativeMarkdownTextRun } from "./nativeMarkdownText";
6import type { NativeMarkdownTextStyle } from "./SelectableMarkdownText.types";
7
8const EXTERNAL_LINK_PREFIX = "◉ ";
9const INLINE_ATTACHMENT_PREFIX = "\uFFFC\u00A0";
10const SKILL_ICON_PLACEHOLDER = "\uFFFC";
11const PARAGRAPH_STYLE_ENCODING_OFFSET = 1000;
12
13function runKeySignature(run: NativeMarkdownTextRun): string {
14 return [
15 run.text,
16 run.bold,
17 run.italic,
18 run.strikethrough,
19 run.code,
20 run.href,
21 run.externalHost,
22 run.fileIcon,
23 run.skillName,
24 run.skillLabel,
25 run.role,
26 run.headingLevel,
27 run.depth,
28 run.spacing,
29 run.firstLineHeadIndent,
30 run.headIndent,
31 run.paragraphSpacing,
32 ].join(":");
33}
34
35const DEFAULT_BODY_FONT_SIZE = 15;
36const DEFAULT_HEADING_FONT_SIZES = [22, 19, 17, 16, 15, 15] as const;
37
38function resolveHeadingFontSize(textStyle: NativeMarkdownTextStyle, headingLevel: number): number {
39 const index = Math.max(0, Math.min(5, headingLevel - 1));
40 const configured = textStyle.headingFontSizes?.[index];
41 if (typeof configured === "number" && Number.isFinite(configured)) {
42 return configured;
43 }
44
45 const scale = textStyle.fontSize / DEFAULT_BODY_FONT_SIZE;
46 return Math.max(12, Math.round(DEFAULT_HEADING_FONT_SIZES[index] * scale));
47}
48
49function runStyle(run: NativeMarkdownTextRun, textStyle: NativeMarkdownTextStyle): TextStyle {
50 const isFile = run.fileIcon != null;
51 const isSkill = run.skillName != null;
52 const headingLevel = Math.max(1, Math.min(6, run.headingLevel ?? 1));
53 const headingFontSize = resolveHeadingFontSize(textStyle, headingLevel);
54 const isHeading = run.role === "heading";
55 const isCodeBlock = run.role === "code-block" || run.role === "code-language";
56 const hasParagraphStyle = run.headIndent !== undefined;
57 const textDecorationLine = run.strikethrough
58 ? "line-through"
59 : run.href && !isFile
60 ? "underline"
61 : "none";
62
63 return {
64 color: isFile
65 ? textStyle.fileTextColor
66 : isSkill
67 ? textStyle.skillTextColor
68 : run.href
69 ? textStyle.linkColor
70 : isHeading
71 ? textStyle.strongColor
72 : run.role === "quote-marker"
73 ? textStyle.quoteMarkerColor
74 : run.role === "divider"
75 ? textStyle.dividerColor
76 : run.role === "code-language"
77 ? textStyle.mutedColor
78 : run.role === "list-marker"
79 ? textStyle.mutedColor
80 : isCodeBlock
81 ? textStyle.codeColor
82 : run.code
83 ? textStyle.inlineCodeColor
84 : run.bold
85 ? textStyle.strongColor
86 : textStyle.color,
87 fontFamily:
88 isFile || isSkill
89 ? textStyle.boldFontFamily
90 : run.code || isCodeBlock
91 ? "ui-monospace"
92 : isHeading
93 ? textStyle.headingFontFamily
94 : run.bold
95 ? textStyle.boldFontFamily
96 : textStyle.fontFamily,
97 fontSize:
98 run.role === "spacer"
99 ? (run.spacing ?? 10)
100 : run.role === "list-break"
101 ? textStyle.fontSize
102 : isHeading
103 ? headingFontSize
104 : run.role === "code-language"
105 ? Math.max(10, Math.round(textStyle.fontSize * 0.73))
106 : run.code || isCodeBlock
107 ? Math.max(12, textStyle.fontSize - 2)
108 : textStyle.fontSize,
109 lineHeight:
110 run.role === "spacer"
111 ? (run.spacing ?? 10)
112 : run.role === "list-break"
113 ? textStyle.lineHeight + (run.spacing ?? 0)
114 : isHeading
115 ? Math.max(headingFontSize + 6, textStyle.lineHeight + 2)
116 : isCodeBlock
117 ? Math.max(16, textStyle.lineHeight - 2)
118 : textStyle.lineHeight,
119 fontStyle: run.italic ? "italic" : "normal",
120 fontWeight: isHeading || run.bold || isFile || isSkill ? "700" : "400",
121 textDecorationLine,
122 backgroundColor: isCodeBlock ? textStyle.codeBlockBackgroundColor : undefined,
123 ...(hasParagraphStyle
124 ? {
125 shadowColor: "transparent",
126 shadowOffset: {
127 width: run.firstLineHeadIndent ?? 0,
128 height: run.headIndent,
129 },
130 shadowRadius: PARAGRAPH_STYLE_ENCODING_OFFSET + (run.paragraphSpacing ?? 0),
131 }
132 : {}),
133 };
134}
135
136export function NativeMarkdownSelectableText(props: {
137 readonly runs: ReadonlyArray<NativeMarkdownTextRun>;
138 readonly textStyle: NativeMarkdownTextStyle;
139 readonly onLinkPress?: (href: string) => void;
140}) {
141 const colorScheme = useColorScheme();
142 const occurrences = new Map<string, number>();
143 const prefixedExternalLinks = new Set<string>();
144 const keyedRuns = props.runs.map((run) => {
145 const signature = runKeySignature(run);
146 const occurrence = occurrences.get(signature) ?? 0;
147 occurrences.set(signature, occurrence + 1);
148
149 let text = run.text;
150 if (run.fileIcon) {
151 text = `${INLINE_ATTACHMENT_PREFIX}${text}`;
152 } else if (run.skillName && run.skillLabel) {
153 text = `${SKILL_ICON_PLACEHOLDER}\u00A0${run.skillLabel}`;
154 } else if (run.externalHost && run.href && !prefixedExternalLinks.has(run.href)) {
155 prefixedExternalLinks.add(run.href);
156 text = `${EXTERNAL_LINK_PREFIX}${text}`;
157 }
158
159 return { key: `${signature}:${occurrence}`, run, text };
160 });
161 // T3MarkdownText only rebuilds its attributed string during native layout. A
162 // color-only child update can otherwise leave the previous appearance cached.
163 const appearanceKey = [
164 colorScheme ?? "unspecified",
165 props.textStyle.fontSize,
166 props.textStyle.lineHeight,
167 props.textStyle.headingFontSizes?.join(","),
168 props.textStyle.color,
169 props.textStyle.strongColor,
170 props.textStyle.mutedColor,
171 props.textStyle.linkColor,
172 props.textStyle.inlineCodeColor,
173 props.textStyle.codeColor,
174 props.textStyle.codeBackgroundColor,
175 props.textStyle.codeBlockBackgroundColor,
176 props.textStyle.fileTextColor,
177 props.textStyle.skillTextColor,
178 props.textStyle.quoteMarkerColor,
179 props.textStyle.dividerColor,
180 ].join(":");
181
182 return (
183 <MarkdownTextPrimitive
184 key={appearanceKey}
185 uiTextView
186 selectable
187 style={{
188 flexShrink: 1,
189 minWidth: 0,
190 color: props.textStyle.color,
191 fontFamily: props.textStyle.fontFamily,
192 fontSize: props.textStyle.fontSize,
193 lineHeight: props.textStyle.lineHeight,
194 }}
195 >
196 {keyedRuns.map(({ key, run, text }) => {
197 const href = run.href;
198 return (
199 <MarkdownTextPrimitive
200 key={key}
201 nativeID={
202 run.fileIcon
203 ? `t3-file:${Image.resolveAssetSource(markdownFileIconSource(run.fileIcon)).uri}`
204 : run.skillName
205 ? "t3-skill:sf:cube"
206 : undefined
207 }
208 style={runStyle(run, props.textStyle)}
209 onPress={
210 href
211 ? () => {
212 if (props.onLinkPress) {
213 props.onLinkPress(href);
214 } else {
215 void Linking.openURL(href);
216 }
217 }
218 : undefined
219 }
220 >
221 {text}
222 </MarkdownTextPrimitive>
223 );
224 })}
225 </MarkdownTextPrimitive>
226 );
227}