@@ -7,8 +7,11 @@ import {
77import {
88 markdownWithHtml ,
99 markdownWithInlineFileLinks ,
10+ markdownWithMermaidDiagrams ,
1011 markdownWithTableBreaks ,
1112} from "../lib/markdownDocument" ;
13+ import type { MermaidPalette } from "../lib/mermaidSvg" ;
14+ import { themeColorWithAlpha } from "../lib/mobileTheme" ;
1215import { useCallback , useMemo , useState , type ReactNode } from "react" ;
1316import { Platform , Share , StyleSheet , View } from "react-native" ;
1417import {
@@ -21,6 +24,7 @@ import type { SelectableMarkdownTextProps } from "./SelectableMarkdownText.types
2124
2225import { useAppearancePreferences } from "../features/settings/appearance/AppearancePreferencesProvider" ;
2326import { Menu } from "../components/ui/Menu" ;
27+ import { MermaidDiagramView , renderMermaidDiagram } from "./MermaidDiagram" ;
2428import { useEnrichedLinkVariants } from "./useEnrichedLinkVariants" ;
2529import { copyTextWithHaptic } from "../lib/copyTextWithHaptic" ;
2630import {
@@ -152,18 +156,40 @@ function enrichedStyle(
152156}
153157
154158export function MobileEnrichedMarkdownText ( props : SelectableMarkdownTextProps ) {
155- const { renderImage, resolveImageSource } = props ;
159+ const { renderImage, resolveImageSource, textStyle } = props ;
156160 const { themeAppearance, mathRenderingEnabled } = useAppearancePreferences ( ) ;
157- const markdown = useMemo (
158- ( ) =>
159- markdownWithTableBreaks (
161+ const mermaidPalette = useMemo (
162+ ( ) : MermaidPalette => ( {
163+ background : textStyle . codeBlockBackgroundColor ,
164+ foreground : textStyle . color ,
165+ muted : textStyle . mutedColor ,
166+ surface : themeColorWithAlpha ( textStyle . color , 0.08 ) ,
167+ surfaceAlt : themeColorWithAlpha ( textStyle . color , 0.16 ) ,
168+ border : themeColorWithAlpha ( textStyle . color , 0.28 ) ,
169+ accent : textStyle . linkColor ,
170+ monoFontFamily : Platform . OS === "ios" ? "Menlo" : "monospace" ,
171+ } ) ,
172+ [
173+ textStyle . codeBlockBackgroundColor ,
174+ textStyle . color ,
175+ textStyle . mutedColor ,
176+ textStyle . linkColor ,
177+ ] ,
178+ ) ;
179+ const { markdown, mermaidDiagrams } = useMemo ( ( ) => {
180+ const withDiagrams = markdownWithMermaidDiagrams ( props . markdown , ( source ) =>
181+ renderMermaidDiagram ( source , mermaidPalette ) ,
182+ ) ;
183+ return {
184+ markdown : markdownWithTableBreaks (
160185 markdownWithInlineFileLinks (
161- markdownWithHtml ( props . markdown ) ,
186+ markdownWithHtml ( withDiagrams . markdown ) ,
162187 ENRICHED_INLINE_FILE_LINK_REGEX ,
163188 ) ,
164189 ) ,
165- [ props . markdown ] ,
166- ) ;
190+ mermaidDiagrams : withDiagrams . diagrams ,
191+ } ;
192+ } , [ props . markdown , mermaidPalette ] ) ;
167193 const [ documentAssets , setDocumentAssets ] = useState < DocumentAssetsEvent [ "assets" ] > ( [ ] ) ;
168194 const onDocumentAssets = useCallback ( ( { assets } : DocumentAssetsEvent ) => {
169195 setDocumentAssets ( ( previous ) =>
@@ -265,13 +291,25 @@ export function MobileEnrichedMarkdownText(props: SelectableMarkdownTextProps) {
265291 [ props . textStyle , themeAppearance , linkVariants ] ,
266292 ) ;
267293 const renderMedia = useCallback (
268- ( asset : MarkdownMediaAsset ) =>
269- renderImage ?.( {
270- href : asset . url ,
271- alt : asset . altText || null ,
272- title : asset . title || null ,
273- } ) ?? null ,
274- [ renderImage ] ,
294+ ( asset : MarkdownMediaAsset ) => {
295+ const diagram = mermaidDiagrams . get ( asset . url ) ;
296+ if ( diagram )
297+ return (
298+ < MermaidDiagramView
299+ diagram = { diagram }
300+ backgroundColor = { mermaidPalette . background }
301+ borderColor = { textStyle . dividerColor }
302+ />
303+ ) ;
304+ return (
305+ renderImage ?.( {
306+ href : asset . url ,
307+ alt : asset . altText || null ,
308+ title : asset . title || null ,
309+ } ) ?? null
310+ ) ;
311+ } ,
312+ [ mermaidDiagrams , mermaidPalette . background , textStyle . dividerColor , renderImage ] ,
275313 ) ;
276314 const resolveNativeImageSource = useCallback (
277315 ( asset : MarkdownMediaAsset ) =>
@@ -311,7 +349,7 @@ export function MobileEnrichedMarkdownText(props: SelectableMarkdownTextProps) {
311349 markdown = { markdown }
312350 markdownStyle = { markdownStyle }
313351 onDocumentAssets = { onDocumentAssets }
314- renderMedia = { renderImage ? renderMedia : undefined }
352+ renderMedia = { renderImage || mermaidDiagrams . size > 0 ? renderMedia : undefined }
315353 resolveImageSource = { resolveImageSource ? resolveNativeImageSource : undefined }
316354 onImagePress = {
317355 props . onImagePress || props . onLinkPress
0 commit comments