From 017387db2ccc769ba72a478d0b95e0324f3d793b Mon Sep 17 00:00:00 2001 From: Innei Date: Thu, 29 May 2025 14:18:56 +0800 Subject: [PATCH] feat: add rn native markdown rendering - Introduced PrivacyPolicyScreen and TermsScreen components to display legal documents. - Implemented markdown rendering for both screens using the renderMarkdown utility. - Updated imports in various modules to reference the new screen components. - Enhanced AISummary to accept ReactNode as summary content for better flexibility. Signed-off-by: Innei --- apps/mobile/src/lib/markdown.tsx | 307 ++++++++++++++++++ apps/mobile/src/modules/ai/summary.tsx | 19 +- apps/mobile/src/modules/debug/index.tsx | 2 +- .../modules/entry-content/EntryAISummary.tsx | 31 +- apps/mobile/src/modules/login/index.tsx | 4 +- .../src/modules/settings/routes/Privacy.tsx | 4 +- .../screens/(headless)/(debug)/markdown.tsx | 11 + .../(headless)/{debug.tsx => DebugScreen.tsx} | 8 + .../{privacy.tsx => PrivacyPolicyScreen.tsx} | 14 +- .../(headless)/{terms.tsx => TermsScreen.tsx} | 12 +- apps/mobile/src/sitemap.tsx | 4 +- 11 files changed, 376 insertions(+), 40 deletions(-) create mode 100644 apps/mobile/src/lib/markdown.tsx create mode 100644 apps/mobile/src/screens/(headless)/(debug)/markdown.tsx rename apps/mobile/src/screens/(headless)/{debug.tsx => DebugScreen.tsx} (97%) rename apps/mobile/src/screens/(headless)/{privacy.tsx => PrivacyPolicyScreen.tsx} (67%) rename apps/mobile/src/screens/(headless)/{terms.tsx => TermsScreen.tsx} (72%) diff --git a/apps/mobile/src/lib/markdown.tsx b/apps/mobile/src/lib/markdown.tsx new file mode 100644 index 000000000..67ec16095 --- /dev/null +++ b/apps/mobile/src/lib/markdown.tsx @@ -0,0 +1,307 @@ +import { toJsxRuntime } from "hast-util-to-jsx-runtime" +import { fromMarkdown } from "mdast-util-from-markdown" +import { toHast } from "mdast-util-to-hast" +import * as React from "react" +import { Fragment, jsx, jsxs } from "react/jsx-runtime" +import { Linking, Text, View } from "react-native" + +// Helper function to ensure text is wrapped in Text component +const wrapText = (children: any): any => { + if (typeof children === "string") { + if (children.trim() === "") { + return null + } + return {children} + } + if (Array.isArray(children)) { + return children.map((child, index) => { + if (typeof child === "string") { + if (child.trim() === "") { + return null + } + return ( + + {child} + + ) + } + if (React.isValidElement(child)) { + return child + } + // Handle other types recursively + return wrapText(child) + }) + } + if (React.isValidElement(children)) { + return children + } + // For any other type, try to convert to string and wrap + if (children != null) { + if (children.trim() === "") { + return null + } + return {String(children)} + } + return children +} + +// Helper function to safely render children in Text components +const renderTextChildren = ( + children: any, + _className = "text-label text-[16px] leading-[24px]", +): any => { + if (typeof children === "string") { + return children + } + if (Array.isArray(children)) { + return children.map((child, index) => { + if (typeof child === "string") { + return child + } + if (React.isValidElement(child)) { + // If it's already a React element, render it as is + return {child} + } + return String(child) + }) + } + if (React.isValidElement(children)) { + return children + } + return String(children || "") +} + +export const renderMarkdown = (markdown: string) => { + const mdastTree = fromMarkdown(markdown) + + // Convert mdast to hast + const hastTree = toHast(mdastTree) + + // Fallback component for unknown HTML elements + const FallbackComponent = ({ children, node, ..._props }: any) => { + // For text-like elements, use Text + if ( + typeof children === "string" || + (Array.isArray(children) && children.every((child) => typeof child === "string")) + ) { + return ( + + {renderTextChildren(children)} + + ) + } + // For container-like elements, use View and wrap any text children + return {wrapText(children)} + } + + // Create components object with fallback for unknown elements + const components = new Proxy( + { + // React Native compatible components - GitHub markdown style + p: ({ children, node, ...props }: any) => ( + + {renderTextChildren(children)} + + ), + h1: ({ children, node, ...props }: any) => ( + + {renderTextChildren(children)} + + ), + h2: ({ children, node, ...props }: any) => ( + + {renderTextChildren(children)} + + ), + h3: ({ children, node, ...props }: any) => ( + + {renderTextChildren(children)} + + ), + h4: ({ children, node, ...props }: any) => ( + + {renderTextChildren(children)} + + ), + h5: ({ children, node, ...props }: any) => ( + + {renderTextChildren(children)} + + ), + h6: ({ children, node, ...props }: any) => ( + + {renderTextChildren(children)} + + ), + ul: ({ children, node, ...props }: any) => ( + + {wrapText(children)} + + ), + ol: ({ children, node, ...props }: any) => ( + + {wrapText(children)} + + ), + li: ({ children, node, ordered, index, ...props }: any) => { + const bullet = ordered ? `${(index || 0) + 1}.` : "•" + return ( + + + {bullet} + + + + {renderTextChildren(children)} + + + + ) + }, + strong: ({ children, node, ...props }: any) => ( + + {renderTextChildren(children)} + + ), + b: ({ children, node, ...props }: any) => ( + + {renderTextChildren(children)} + + ), + em: ({ children, node, ...props }: any) => ( + + {renderTextChildren(children)} + + ), + i: ({ children, node, ...props }: any) => ( + + {renderTextChildren(children)} + + ), + code: ({ children, node, ...props }: any) => ( + + {renderTextChildren(children)} + + ), + pre: ({ children, node, ...props }: any) => ( + + + {renderTextChildren(children)} + + + ), + blockquote: ({ children, node, ...props }: any) => ( + + + {renderTextChildren(children)} + + + ), + a: ({ children, href, node, ...props }: any) => ( + href && Linking.openURL(href)} + {...props} + > + {renderTextChildren(children)} + + ), + hr: ({ node, ..._props }: any) => ( + + ), + br: ({ node, ..._props }: any) => {"\n"}, + // Common HTML elements that might appear + div: ({ children, node, ...props }: any) => {wrapText(children)}, + span: ({ children, node, ...props }: any) => ( + + {renderTextChildren(children)} + + ), + // Table elements (simplified for mobile) - GitHub style + table: ({ children, node, ...props }: any) => ( + + {wrapText(children)} + + ), + thead: ({ children, node, ...props }: any) => ( + + {wrapText(children)} + + ), + tbody: ({ children, node, ...props }: any) => {wrapText(children)}, + tr: ({ children, node, ...props }: any) => ( + + {wrapText(children)} + + ), + th: ({ children, node, ...props }: any) => ( + + {renderTextChildren(children)} + + ), + td: ({ children, node, ...props }: any) => ( + + {renderTextChildren(children)} + + ), + + // Not implemented + img: ({ src, alt, node, ..._props }: any) => null, + }, + { + get(target, prop) { + // If the component exists, return it + if (prop in target) { + return target[prop as keyof typeof target] + } + // Otherwise, return the fallback component + return FallbackComponent + }, + }, + ) + + // Convert hast to React Native components + const result = toJsxRuntime(hastTree, { + Fragment, + jsx: (type, props, key) => jsx(type as any, props, key), + jsxs: (type, props, key) => jsxs(type as any, props, key), + passNode: true, + components, + }) + + return { + ...result, + props: { + ...result.props, + children: Array.isArray(result.props?.children) + ? result.props.children.filter((child: any) => { + if (typeof child === "string") { + return child.trim() !== "" + } + return true + }) + : result.props.children, + }, + } +} diff --git a/apps/mobile/src/modules/ai/summary.tsx b/apps/mobile/src/modules/ai/summary.tsx index 9cfea088f..b1671e85b 100644 --- a/apps/mobile/src/modules/ai/summary.tsx +++ b/apps/mobile/src/modules/ai/summary.tsx @@ -1,7 +1,7 @@ import { cn } from "@follow/utils" import MaskedView from "@react-native-masked-view/masked-view" import { LinearGradient } from "expo-linear-gradient" -import type { FC } from "react" +import type { FC, ReactNode } from "react" import * as React from "react" import type { LayoutChangeEvent } from "react-native" import { Pressable, StyleSheet, Text, TextInput, View } from "react-native" @@ -20,7 +20,7 @@ import { isAndroid } from "@/src/lib/platform" export const AISummary: FC<{ className?: string - summary?: string + summary?: string | ReactNode pending?: boolean error?: string onRetry?: () => void @@ -59,11 +59,16 @@ export const AISummary: FC<{ const purpleColor = useColor("purple") + // Check if summary is a React element or string + const isReactElement = React.isValidElement(summary) + const summaryText = typeof summary === "string" ? summary : "" + if (pending || (!summary && !error)) return null return ( )} + ) : isReactElement ? ( + {summary} ) : ( )} @@ -124,9 +131,11 @@ export const AISummary: FC<{ )} + ) : isReactElement ? ( + {summary} ) : ( - {summary?.trim()} + {summaryText?.trim()} )} diff --git a/apps/mobile/src/modules/debug/index.tsx b/apps/mobile/src/modules/debug/index.tsx index 7b258e574..0996ade3d 100644 --- a/apps/mobile/src/modules/debug/index.tsx +++ b/apps/mobile/src/modules/debug/index.tsx @@ -13,7 +13,7 @@ import { BugCuteReIcon } from "@/src/icons/bug_cute_re" import { JotaiPersistSyncStorage } from "@/src/lib/jotai" import { Navigation } from "@/src/lib/navigation/Navigation" import { setEnvProfile, useEnvProfile } from "@/src/lib/proxy-env" -import { DebugScreen } from "@/src/screens/(headless)/debug" +import { DebugScreen } from "@/src/screens/(headless)/DebugScreen" export const DebugButton = () => { const cachedPositionAtom = useMemo( diff --git a/apps/mobile/src/modules/entry-content/EntryAISummary.tsx b/apps/mobile/src/modules/entry-content/EntryAISummary.tsx index 7d760317e..fc505b3ca 100644 --- a/apps/mobile/src/modules/entry-content/EntryAISummary.tsx +++ b/apps/mobile/src/modules/entry-content/EntryAISummary.tsx @@ -3,12 +3,13 @@ import { SummaryGeneratingStatus } from "@follow/store/summary/enum" import { usePrefetchSummary, useSummary } from "@follow/store/summary/hooks" import { useSummaryStore } from "@follow/store/summary/store" import { useAtomValue } from "jotai" -import { fromMarkdown } from "mdast-util-from-markdown" -import { toString } from "mdast-util-to-string" import type { FC } from "react" import { useMemo } from "react" +import { Text } from "react-native" import { useActionLanguage, useGeneralSettingKey } from "@/src/atoms/settings/general" +import { ErrorBoundary } from "@/src/components/common/ErrorBoundary" +import { renderMarkdown } from "@/src/lib/markdown" import { AISummary } from "../ai/summary" import { useEntryContentContext } from "./ctx" @@ -33,20 +34,28 @@ export const EntryAISummary: FC<{ const maybeMarkdown = showReadability ? summary?.readabilitySummary || summary?.summary : summary?.summary - if (!maybeMarkdown) return "" - const tree = fromMarkdown(maybeMarkdown) - return toString(tree) + if (!maybeMarkdown) return null + + return renderMarkdown(maybeMarkdown) }, [showReadability, summary?.readabilitySummary, summary?.summary]) const status = useSummaryStore((state) => state.generatingStatus[entryId]) if (!showAISummary) return null return ( - + ( + + Failed to generate summary. Rendering error. + + )} + > + + ) } diff --git a/apps/mobile/src/modules/login/index.tsx b/apps/mobile/src/modules/login/index.tsx index 407056d54..0e94101e9 100644 --- a/apps/mobile/src/modules/login/index.tsx +++ b/apps/mobile/src/modules/login/index.tsx @@ -10,8 +10,8 @@ import { Logo } from "@/src/components/ui/logo" import { useNavigation } from "@/src/lib/navigation/hooks" import { NavigationLink } from "@/src/lib/navigation/NavigationLink" import { useScaleHeight } from "@/src/lib/responsive" -import { PrivacyPolicyScreen } from "@/src/screens/(headless)/privacy" -import { TermsMarkdown, TermsScreen } from "@/src/screens/(headless)/terms" +import { PrivacyPolicyScreen } from "@/src/screens/(headless)/PrivacyPolicyScreen" +import { TermsMarkdown, TermsScreen } from "@/src/screens/(headless)/TermsScreen" import { EmailLogin, EmailSignUp } from "./email" import { SocialLogin } from "./social" diff --git a/apps/mobile/src/modules/settings/routes/Privacy.tsx b/apps/mobile/src/modules/settings/routes/Privacy.tsx index f280f25d1..1470e15cf 100644 --- a/apps/mobile/src/modules/settings/routes/Privacy.tsx +++ b/apps/mobile/src/modules/settings/routes/Privacy.tsx @@ -9,8 +9,8 @@ import { GroupedInsetListNavigationLink, } from "@/src/components/ui/grouped/GroupedList" import { useNavigation } from "@/src/lib/navigation/hooks" -import { PrivacyPolicyScreen } from "@/src/screens/(headless)/privacy" -import { TermsScreen } from "@/src/screens/(headless)/terms" +import { PrivacyPolicyScreen } from "@/src/screens/(headless)/PrivacyPolicyScreen" +import { TermsScreen } from "@/src/screens/(headless)/TermsScreen" export const PrivacyScreen = () => { const { t } = useTranslation("settings") diff --git a/apps/mobile/src/screens/(headless)/(debug)/markdown.tsx b/apps/mobile/src/screens/(headless)/(debug)/markdown.tsx new file mode 100644 index 000000000..efd387c9a --- /dev/null +++ b/apps/mobile/src/screens/(headless)/(debug)/markdown.tsx @@ -0,0 +1,11 @@ +import { legalMarkdown } from "@follow/legal" +import { useMemo } from "react" +import { ScrollView } from "react-native" + +import { renderMarkdown } from "@/src/lib/markdown" + +export const MarkdownScreen = () => { + const element = useMemo(() => renderMarkdown(legalMarkdown.tos), []) + + return {element} +} diff --git a/apps/mobile/src/screens/(headless)/debug.tsx b/apps/mobile/src/screens/(headless)/DebugScreen.tsx similarity index 97% rename from apps/mobile/src/screens/(headless)/debug.tsx rename to apps/mobile/src/screens/(headless)/DebugScreen.tsx index db72cb5dd..507dc244d 100644 --- a/apps/mobile/src/screens/(headless)/debug.tsx +++ b/apps/mobile/src/screens/(headless)/DebugScreen.tsx @@ -31,6 +31,8 @@ import type { NavigationControllerView } from "@/src/lib/navigation/types" import { setEnvProfile, useEnvProfile } from "@/src/lib/proxy-env" import { toast } from "@/src/lib/toast" +import { MarkdownScreen } from "./(debug)/markdown" + interface MenuSection { title: string items: (MenuItem | FC)[] @@ -151,6 +153,12 @@ export const DebugScreen: NavigationControllerView = () => { navigation.pushControllerView(DebugButtonGroup) }, }, + { + title: "Markdown", + onPress: () => { + navigation.pushControllerView(MarkdownScreen) + }, + }, ], }, ] diff --git a/apps/mobile/src/screens/(headless)/privacy.tsx b/apps/mobile/src/screens/(headless)/PrivacyPolicyScreen.tsx similarity index 67% rename from apps/mobile/src/screens/(headless)/privacy.tsx rename to apps/mobile/src/screens/(headless)/PrivacyPolicyScreen.tsx index 4d5f6c4d3..6242bc21c 100644 --- a/apps/mobile/src/screens/(headless)/privacy.tsx +++ b/apps/mobile/src/screens/(headless)/PrivacyPolicyScreen.tsx @@ -1,20 +1,15 @@ import { legalMarkdown } from "@follow/legal" +import { useMemo } from "react" import { NavigationBlurEffectHeaderView, SafeNavigationScrollView, } from "@/src/components/layouts/views/SafeNavigationScrollView" -import { Markdown } from "@/src/components/ui/typography/Markdown" +import { renderMarkdown } from "@/src/lib/markdown" import type { NavigationControllerView } from "@/src/lib/navigation/types" -export const PrivacyMarkdown = () => { - return ( - - ) +const PrivacyMarkdown = () => { + return useMemo(() => renderMarkdown(legalMarkdown.privacy), []) } export const PrivacyPolicyScreen: NavigationControllerView = () => { @@ -22,6 +17,7 @@ export const PrivacyPolicyScreen: NavigationControllerView = () => { } > diff --git a/apps/mobile/src/screens/(headless)/terms.tsx b/apps/mobile/src/screens/(headless)/TermsScreen.tsx similarity index 72% rename from apps/mobile/src/screens/(headless)/terms.tsx rename to apps/mobile/src/screens/(headless)/TermsScreen.tsx index c5c6cfe3f..5a38a4d5c 100644 --- a/apps/mobile/src/screens/(headless)/terms.tsx +++ b/apps/mobile/src/screens/(headless)/TermsScreen.tsx @@ -1,20 +1,15 @@ import { legalMarkdown } from "@follow/legal" +import { useMemo } from "react" import { NavigationBlurEffectHeaderView, SafeNavigationScrollView, } from "@/src/components/layouts/views/SafeNavigationScrollView" -import { Markdown } from "@/src/components/ui/typography/Markdown" +import { renderMarkdown } from "@/src/lib/markdown" import type { NavigationControllerView } from "@/src/lib/navigation/types" export const TermsMarkdown = () => { - return ( - - ) + return useMemo(() => renderMarkdown(legalMarkdown.tos), []) } export const TermsScreen: NavigationControllerView = () => { @@ -22,6 +17,7 @@ export const TermsScreen: NavigationControllerView = () => { } > diff --git a/apps/mobile/src/sitemap.tsx b/apps/mobile/src/sitemap.tsx index 373e2dfe8..39a2b92c6 100644 --- a/apps/mobile/src/sitemap.tsx +++ b/apps/mobile/src/sitemap.tsx @@ -6,8 +6,8 @@ import { Navigation } from "./lib/navigation/Navigation" import { NavigationSitemapRegistry } from "./lib/navigation/sitemap/registry" import type { NavigationControllerView } from "./lib/navigation/types" import { OTPWindow } from "./modules/settings/components/OTPWindow" -import { PrivacyPolicyScreen } from "./screens/(headless)/privacy" -import { TermsScreen } from "./screens/(headless)/terms" +import { PrivacyPolicyScreen } from "./screens/(headless)/PrivacyPolicyScreen" +import { TermsScreen } from "./screens/(headless)/TermsScreen" import { ForgetPasswordScreen } from "./screens/(modal)/ForgetPasswordScreen" import { InvitationScreen } from "./screens/(modal)/InvitationScreen" import { LoginScreen } from "./screens/(modal)/LoginScreen"