Open History to see the last commit
1import { useMemo } from "react";
2import { View } from "react-native";
3import { parseMarkdownWithOptions } from "react-native-nitro-markdown/headless";
4
5import {
6 nativeMarkdownChunkSpacing,
7 nativeMarkdownDocumentChunks,
8 nativeMarkdownDocumentRuns,
9 nativeMarkdownWithAuthoredWindowsPaths,
10 nativeMarkdownWithPreservedSoftBreaks,
11} from "./nativeMarkdownText";
12import { MarkdownImageRendererContext, NativeMarkdownBlock } from "./NativeMarkdownBlock";
13import {
14 MarkdownContextClipboardContext,
15 MarkdownFileContextMenuContext,
16 NativeMarkdownSelectableText,
17 type MarkdownFileContextMenuHandlers,
18} from "./NativeMarkdownSelectableText";
19import type {
20 SelectableMarkdownSkill,
21 SelectableMarkdownTextProps,
22} from "./SelectableMarkdownText.types";
23
24const EMPTY_SKILLS: ReadonlyArray<SelectableMarkdownSkill> = [];
25
26export type {
27 MarkdownCodeHighlighter,
28 MarkdownHighlightedToken,
29 MarkdownImageRenderer,
30 MarkdownImageRequest,
31 NativeMarkdownTextStyle,
32 SelectableMarkdownSkill,
33 SelectableMarkdownTextProps,
34} from "./SelectableMarkdownText.types";
35
36export function hasNativeSelectableMarkdownText(): boolean {
37 return true;
38}
39
40export function SelectableMarkdownText({
41 markdown,
42 contextClipboardFragment,
43 skills = EMPTY_SKILLS,
44 textStyle,
45 highlightCode,
46 preserveSoftBreaks = false,
47 onLinkPress,
48 fileContextMenu,
49 onFileContextMenuAction,
50 renderImage,
51 marginTop = 0,
52 marginBottom = 0,
53}: SelectableMarkdownTextProps) {
54 const chunks = useMemo(() => {
55 const parsedDocument = nativeMarkdownWithAuthoredWindowsPaths(
56 parseMarkdownWithOptions(markdown, { gfm: true, html: true, math: false }),
57 markdown,
58 );
59 const document = preserveSoftBreaks
60 ? nativeMarkdownWithPreservedSoftBreaks(parsedDocument)
61 : parsedDocument;
62 return nativeMarkdownDocumentChunks(document).map((chunk) =>
63 chunk.kind === "selectable"
64 ? {
65 ...chunk,
66 runs: nativeMarkdownDocumentRuns(chunk.node, skills),
67 }
68 : chunk,
69 );
70 }, [markdown, preserveSoftBreaks, skills]);
71
72 const fileContextMenuHandlers = useMemo<MarkdownFileContextMenuHandlers | null>(
73 () =>
74 fileContextMenu && onFileContextMenuAction
75 ? { fileContextMenu, onFileContextMenuAction }
76 : null,
77 [fileContextMenu, onFileContextMenuAction],
78 );
79
80 return (
81 <MarkdownContextClipboardContext.Provider value={contextClipboardFragment ?? ""}>
82 <MarkdownImageRendererContext.Provider value={renderImage ?? null}>
83 <MarkdownFileContextMenuContext.Provider value={fileContextMenuHandlers}>
84 {/* A percentage width here creates a cyclic intrinsic measurement inside
85 shrink-to-fit containers such as user-message bubbles. Yoga then gives
86 the native text node an unbounded second pass and the parent only clips
87 the resulting single-line width instead of reflowing it. */}
88 <View style={{ flexShrink: 1, minWidth: 0, marginTop, marginBottom }}>
89 {chunks.map((chunk, index) => {
90 const content =
91 chunk.kind === "rich" ? (
92 <NativeMarkdownBlock
93 node={chunk.node}
94 skills={skills}
95 textStyle={textStyle}
96 highlightCode={highlightCode}
97 onLinkPress={onLinkPress}
98 />
99 ) : (
100 <NativeMarkdownSelectableText
101 runs={chunk.runs}
102 textStyle={textStyle}
103 onLinkPress={onLinkPress}
104 />
105 );
106
107 return (
108 <View
109 key={chunk.key}
110 style={{ paddingTop: nativeMarkdownChunkSpacing(chunks[index - 1], chunk) }}
111 >
112 {content}
113 </View>
114 );
115 })}
116 </View>
117 </MarkdownFileContextMenuContext.Provider>
118 </MarkdownImageRendererContext.Provider>
119 </MarkdownContextClipboardContext.Provider>
120 );
121}