From 9af7dccf33c7fa10cc423c1f1e0ea628e54e19bc Mon Sep 17 00:00:00 2001 From: Innei Date: Thu, 29 May 2025 15:48:31 +0800 Subject: [PATCH] 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 --- .../src/modules/settings/tabs/feeds.tsx | 6 +- .../ui/typography/MarkdownNative.tsx | 11 ++++ apps/mobile/src/lib/markdown.tsx | 60 +++++++++++-------- .../src/screens/(modal)/RsshubFormScreen.tsx | 8 +-- .../components/src/ui/checkbox/index.tsx | 2 + 5 files changed, 52 insertions(+), 35 deletions(-) create mode 100644 apps/mobile/src/components/ui/typography/MarkdownNative.tsx diff --git a/apps/desktop/layer/renderer/src/modules/settings/tabs/feeds.tsx b/apps/desktop/layer/renderer/src/modules/settings/tabs/feeds.tsx index 24cf514df..539806bd4 100644 --- a/apps/desktop/layer/renderer/src/modules/settings/tabs/feeds.tsx +++ b/apps/desktop/layer/renderer/src/modules/settings/tabs/feeds.tsx @@ -453,9 +453,9 @@ const FeedListItem = memo( if (!subscription) return null return ( - + ) }, ) diff --git a/apps/mobile/src/components/ui/typography/MarkdownNative.tsx b/apps/mobile/src/components/ui/typography/MarkdownNative.tsx new file mode 100644 index 000000000..fcec882db --- /dev/null +++ b/apps/mobile/src/components/ui/typography/MarkdownNative.tsx @@ -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]) +} diff --git a/apps/mobile/src/lib/markdown.tsx b/apps/mobile/src/lib/markdown.tsx index 67ec16095..45dcfc711 100644 --- a/apps/mobile/src/lib/markdown.tsx +++ b/apps/mobile/src/lib/markdown.tsx @@ -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)} ), - // Table elements (simplified for mobile) - GitHub style + // Table elements (GFM table support) - GitHub style with improved mobile layout table: ({ children, node, ...props }: any) => ( { {wrapText(children)} ), - th: ({ children, node, ...props }: any) => ( - - {renderTextChildren(children)} - - ), - td: ({ children, node, ...props }: any) => ( - - {renderTextChildren(children)} - - ), + 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 ( + + {renderTextChildren(children)} + + ) + }, + 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 ( + + {renderTextChildren(children)} + + ) + }, // 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, diff --git a/apps/mobile/src/screens/(modal)/RsshubFormScreen.tsx b/apps/mobile/src/screens/(modal)/RsshubFormScreen.tsx index 780d83386..f7c9a218e 100644 --- a/apps/mobile/src/screens/(modal)/RsshubFormScreen.tsx +++ b/apps/mobile/src/screens/(modal)/RsshubFormScreen.tsx @@ -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 && ( - + )} diff --git a/packages/internal/components/src/ui/checkbox/index.tsx b/packages/internal/components/src/ui/checkbox/index.tsx index 302ce8e6d..6ea171c08 100644 --- a/packages/internal/components/src/ui/checkbox/index.tsx +++ b/packages/internal/components/src/ui/checkbox/index.tsx @@ -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: {