refactor: merge markdown parser

- Replaced button with a div in FeedListItem for better accessibility.
- Introduced MarkdownNative component for improved markdown rendering in mobile.
- Updated RsshubFormScreen to utilize MarkdownNative for displaying route descriptions.
- Refactored renderMarkdown function to use parseMarkdown for better performance and added support for text alignment in table cells.

Signed-off-by: Innei <tukon479@gmail.com>
This commit is contained in:
Innei
2025-05-29 15:48:31 +08:00
parent ee0ec4422c
commit 9af7dccf33
5 changed files with 52 additions and 35 deletions
@@ -453,9 +453,9 @@ const FeedListItem = memo(
if (!subscription) return null
return (
<button
<div
data-id={id}
type="button"
role="button"
tabIndex={-1}
className={clsx(
"hover:bg-material-medium grid h-10 w-full items-center gap-4 rounded px-1",
@@ -551,7 +551,7 @@ const FeedListItem = memo(
<div className="text-text-secondary">--</div>
)}
</div>
</button>
</div>
)
},
)
@@ -0,0 +1,11 @@
import { useMemo } from "react"
import { renderMarkdown } from "@/src/lib/markdown"
export const MarkdownNative: WebComponent<{
value: string
}> = ({ value }) => {
return useMemo(() => {
return renderMarkdown(value)
}, [value])
}
+34 -26
View File
@@ -1,8 +1,5 @@
import { toJsxRuntime } from "hast-util-to-jsx-runtime"
import { fromMarkdown } from "mdast-util-from-markdown"
import { toHast } from "mdast-util-to-hast"
import { parseMarkdown } from "@follow/components/utils/parse-markdown.tsx"
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
@@ -72,11 +69,6 @@ const renderTextChildren = (
}
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
@@ -235,7 +227,7 @@ export const renderMarkdown = (markdown: string) => {
{renderTextChildren(children)}
</Text>
),
// Table elements (simplified for mobile) - GitHub style
// Table elements (GFM table support) - GitHub style with improved mobile layout
table: ({ children, node, ...props }: any) => (
<View
className="border-non-opaque-separator mb-4 overflow-hidden rounded-md border"
@@ -255,16 +247,36 @@ export const renderMarkdown = (markdown: string) => {
{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>
),
th: ({ children, node, ...props }: any) => {
// Get text alignment from node properties if available
const align = node?.properties?.align || "left"
const textAlignStyle =
align === "center" ? "text-center" : align === "right" ? "text-right" : "text-left"
return (
<Text
className={`text-label flex-1 px-3 py-2 font-semibold leading-[20px] ${textAlignStyle}`}
{...props}
>
{renderTextChildren(children)}
</Text>
)
},
td: ({ children, node, ...props }: any) => {
// Get text alignment from node properties if available
const align = node?.properties?.align || "left"
const textAlignStyle =
align === "center" ? "text-center" : align === "right" ? "text-right" : "text-left"
return (
<Text
className={`text-label flex-1 px-3 py-2 text-[14px] leading-[20px] ${textAlignStyle}`}
{...props}
>
{renderTextChildren(children)}
</Text>
)
},
// Not implemented
img: ({ src, alt, node, ..._props }: any) => null,
@@ -282,13 +294,9 @@ export const renderMarkdown = (markdown: string) => {
)
// 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,
const result = parseMarkdown(markdown, {
components,
})
}).content
return {
...result,
@@ -22,7 +22,7 @@ import {
import { FormProvider, useFormContext } from "@/src/components/ui/form/FormProvider"
import { Select } from "@/src/components/ui/form/Select"
import { TextField } from "@/src/components/ui/form/TextField"
import { Markdown } from "@/src/components/ui/typography/Markdown"
import { MarkdownNative } from "@/src/components/ui/typography/MarkdownNative"
import { useNavigation } from "@/src/lib/navigation/hooks"
import { useSetModalScreenOptions } from "@/src/lib/navigation/ScreenOptionsContext"
import type { NavigationControllerView } from "@/src/lib/navigation/types"
@@ -202,11 +202,7 @@ function FormImpl({ route, routePrefix, name }: RsshubFormParams) {
{!!route.description && (
<View className="bg-system-background border-t-hairline border-opaque-separator mt-4 flex-1 px-4">
<Markdown
className="bg-system-background py-4"
value={route.description.replaceAll("::: ", ":::")}
webViewProps={{ matchContents: true, scrollEnabled: false }}
/>
<MarkdownNative value={route.description.replaceAll("::: ", ":::")} />
</View>
)}
</SafeNavigationScrollView>
@@ -62,6 +62,7 @@ function Checkbox({ className, onCheckedChange, indeterminate, ...props }: Check
opacity: 1,
transition: {
duration: 0.2,
delay: 0.1,
},
},
hidden: {
@@ -96,6 +97,7 @@ function Checkbox({ className, onCheckedChange, indeterminate, ...props }: Check
opacity: 1,
transition: {
duration: 0.2,
delay: 0.1,
},
},
unchecked: {