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 <tukon479@gmail.com>
This commit is contained in:
Innei
2025-05-29 14:18:56 +08:00
parent 91306e3158
commit 017387db2c
11 changed files with 376 additions and 40 deletions
+307
View File
@@ -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 <Text className="text-label text-[16px] leading-[24px]">{children}</Text>
}
if (Array.isArray(children)) {
return children.map((child, index) => {
if (typeof child === "string") {
if (child.trim() === "") {
return null
}
return (
<Text key={index} className="text-label text-[16px] leading-[24px]">
{child}
</Text>
)
}
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 <Text className="text-label text-[16px] leading-[24px]">{String(children)}</Text>
}
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 <React.Fragment key={index}>{child}</React.Fragment>
}
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 (
<Text className="text-label text-[16px] leading-[24px]">
{renderTextChildren(children)}
</Text>
)
}
// For container-like elements, use View and wrap any text children
return <View>{wrapText(children)}</View>
}
// Create components object with fallback for unknown elements
const components = new Proxy(
{
// React Native compatible components - GitHub markdown style
p: ({ children, node, ...props }: any) => (
<Text className="text-label mb-4 leading-[24px]" {...props}>
{renderTextChildren(children)}
</Text>
),
h1: ({ children, node, ...props }: any) => (
<Text
className="text-label border-non-opaque-separator mb-4 mt-6 border-b pb-2 text-[32px] font-semibold leading-[40px]"
{...props}
>
{renderTextChildren(children)}
</Text>
),
h2: ({ children, node, ...props }: any) => (
<Text
className="text-label border-non-opaque-separator mb-4 mt-6 border-b pb-2 text-[24px] font-semibold leading-[30px]"
{...props}
>
{renderTextChildren(children)}
</Text>
),
h3: ({ children, node, ...props }: any) => (
<Text className="text-label mb-4 mt-6 text-[20px] font-semibold leading-[25px]" {...props}>
{renderTextChildren(children)}
</Text>
),
h4: ({ children, node, ...props }: any) => (
<Text className="text-label mb-4 mt-6 text-[16px] font-semibold leading-[20px]" {...props}>
{renderTextChildren(children)}
</Text>
),
h5: ({ children, node, ...props }: any) => (
<Text className="text-label mb-4 mt-6 text-[14px] font-semibold leading-[18px]" {...props}>
{renderTextChildren(children)}
</Text>
),
h6: ({ children, node, ...props }: any) => (
<Text
className="text-secondary-label mb-4 mt-6 text-[13px] font-semibold leading-[16px]"
{...props}
>
{renderTextChildren(children)}
</Text>
),
ul: ({ children, node, ...props }: any) => (
<View className="mb-4 pl-4" {...props}>
{wrapText(children)}
</View>
),
ol: ({ children, node, ...props }: any) => (
<View className="mb-4 pl-4" {...props}>
{wrapText(children)}
</View>
),
li: ({ children, node, ordered, index, ...props }: any) => {
const bullet = ordered ? `${(index || 0) + 1}.` : "•"
return (
<View className="mb-1 flex-row items-start" {...props}>
<Text className="text-label mt-0 min-w-[24px] text-[16px] leading-[24px]">
{bullet}
</Text>
<View className="flex-1">
<Text className="text-label text-[16px] leading-[24px]">
{renderTextChildren(children)}
</Text>
</View>
</View>
)
},
strong: ({ children, node, ...props }: any) => (
<Text className="font-semibold" {...props}>
{renderTextChildren(children)}
</Text>
),
b: ({ children, node, ...props }: any) => (
<Text className="font-semibold" {...props}>
{renderTextChildren(children)}
</Text>
),
em: ({ children, node, ...props }: any) => (
<Text className="italic" {...props}>
{renderTextChildren(children)}
</Text>
),
i: ({ children, node, ...props }: any) => (
<Text className="italic" {...props}>
{renderTextChildren(children)}
</Text>
),
code: ({ children, node, ...props }: any) => (
<Text
className="bg-quaternary-system-fill text-label rounded-md px-2 py-1 font-mono text-[14px]"
{...props}
>
{renderTextChildren(children)}
</Text>
),
pre: ({ children, node, ...props }: any) => (
<View
className="bg-secondary-system-background border-non-opaque-separator mb-4 rounded-lg border p-4"
{...props}
>
<Text className="text-label font-mono text-[14px] leading-[20px]">
{renderTextChildren(children)}
</Text>
</View>
),
blockquote: ({ children, node, ...props }: any) => (
<View
className="border-non-opaque-separator bg-secondary-system-background border-l-accent mb-4 border-l-4 py-2 pl-4"
{...props}
>
<Text className="text-secondary-label italic leading-[24px]">
{renderTextChildren(children)}
</Text>
</View>
),
a: ({ children, href, node, ...props }: any) => (
<Text
className="text-accent font-medium underline"
onPress={() => href && Linking.openURL(href)}
{...props}
>
{renderTextChildren(children)}
</Text>
),
hr: ({ node, ..._props }: any) => (
<View className="border-non-opaque-separator my-6 h-px border-t" />
),
br: ({ node, ..._props }: any) => <Text>{"\n"}</Text>,
// Common HTML elements that might appear
div: ({ children, node, ...props }: any) => <View {...props}>{wrapText(children)}</View>,
span: ({ children, node, ...props }: any) => (
<Text className="text-label leading-[24px]" {...props}>
{renderTextChildren(children)}
</Text>
),
// Table elements (simplified for mobile) - GitHub style
table: ({ children, node, ...props }: any) => (
<View
className="border-non-opaque-separator mb-4 overflow-hidden rounded-md border"
{...props}
>
{wrapText(children)}
</View>
),
thead: ({ children, node, ...props }: any) => (
<View className="bg-secondary-system-background" {...props}>
{wrapText(children)}
</View>
),
tbody: ({ children, node, ...props }: any) => <View {...props}>{wrapText(children)}</View>,
tr: ({ children, node, ...props }: any) => (
<View className="border-non-opaque-separator flex-row border-b last:border-b-0" {...props}>
{wrapText(children)}
</View>
),
th: ({ children, node, ...props }: any) => (
<Text className="text-label flex-1 px-3 py-2 font-semibold leading-[20px]" {...props}>
{renderTextChildren(children)}
</Text>
),
td: ({ children, node, ...props }: any) => (
<Text className="text-label flex-1 px-3 py-2 text-[14px] leading-[20px]" {...props}>
{renderTextChildren(children)}
</Text>
),
// 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,
},
}
}
+14 -5
View File
@@ -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 (
<Animated.View
className={cn(
"border-opaque-separator/50 mx-4 my-2 rounded-xl p-4",
"border-opaque-separator/50 mx-4 my-2 rounded-xl",
isReactElement ? "px-4 pt-4" : "p-4",
// Opacity modifier style incorrectly applied in Android
isAndroid ? "bg-secondary-system-background" : "bg-secondary-system-background/30",
className,
@@ -100,12 +105,14 @@ export const AISummary: FC<{
</Pressable>
)}
</View>
) : isReactElement ? (
<View className="mt-2">{summary}</View>
) : (
<TextInput
readOnly
multiline
className="text-label text-[14px] leading-[22px]"
value={summary?.trim()}
value={summaryText?.trim()}
/>
)}
</View>
@@ -124,9 +131,11 @@ export const AISummary: FC<{
</View>
)}
</View>
) : isReactElement ? (
<View className="mt-2">{summary}</View>
) : (
<Text className="text-label mt-2 text-[14px] leading-[22px]" selectable>
{summary?.trim()}
{summaryText?.trim()}
</Text>
)}
</View>
+1 -1
View File
@@ -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(
@@ -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 (
<AISummary
className="my-3"
summary={summaryToShow}
pending={status === SummaryGeneratingStatus.Pending}
error={status === SummaryGeneratingStatus.Error ? "Failed to generate summary" : undefined}
/>
<ErrorBoundary
fallbackRender={() => (
<Text className="text-label text-[16px] leading-[24px]">
Failed to generate summary. Rendering error.
</Text>
)}
>
<AISummary
className="my-3"
summary={summaryToShow}
pending={status === SummaryGeneratingStatus.Pending}
error={status === SummaryGeneratingStatus.Error ? "Failed to generate summary" : undefined}
/>
</ErrorBoundary>
)
}
+2 -2
View File
@@ -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"
@@ -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")
@@ -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 <ScrollView contentContainerClassName="p-4">{element}</ScrollView>
}
@@ -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)
},
},
],
},
]
@@ -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 (
<Markdown
value={legalMarkdown.privacy}
webViewProps={{ scrollEnabled: false, matchContents: true }}
style={{ padding: 16, flex: 1 }}
/>
)
const PrivacyMarkdown = () => {
return useMemo(() => renderMarkdown(legalMarkdown.privacy), [])
}
export const PrivacyPolicyScreen: NavigationControllerView = () => {
@@ -22,6 +17,7 @@ export const PrivacyPolicyScreen: NavigationControllerView = () => {
<SafeNavigationScrollView
className="bg-system-background"
contentInsetAdjustmentBehavior="never"
contentContainerClassName="px-4"
Header={<NavigationBlurEffectHeaderView title="Privacy Policy" />}
>
<PrivacyMarkdown />
@@ -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 (
<Markdown
value={legalMarkdown.tos}
webViewProps={{ scrollEnabled: false, matchContents: true }}
style={{ padding: 16, flex: 1 }}
/>
)
return useMemo(() => renderMarkdown(legalMarkdown.tos), [])
}
export const TermsScreen: NavigationControllerView = () => {
@@ -22,6 +17,7 @@ export const TermsScreen: NavigationControllerView = () => {
<SafeNavigationScrollView
className="bg-system-background"
contentInsetAdjustmentBehavior="never"
contentContainerClassName="px-4"
Header={<NavigationBlurEffectHeaderView title="Terms of Service" />}
>
<TermsMarkdown />
+2 -2
View File
@@ -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"