From ff64d84e854102e1d12a3bbe6ab6642b0586e716 Mon Sep 17 00:00:00 2001 From: Benjamin Budet Date: Tue, 16 Dec 2025 16:06:55 +0100 Subject: [PATCH] feat(markdown): add react component embedding support --- README.md | 72 ++++++++ examples/react-native-marked-sample/App.tsx | 52 ++++-- .../ReactComponentsExample.tsx | 90 ++++++++++ examples/react-native-marked-sample/const.ts | 34 +++- .../useMarkdownWithComponents.spec.tsx | 159 +++++++++++++++++ src/hooks/useMarkdownWithComponents.tsx | 89 ++++++++++ src/index.ts | 14 +- src/lib/ReactComponentRegistry.tsx | 52 ++++++ src/lib/ReactComponentTokenizer.ts | 149 ++++++++++++++++ .../__tests__/ReactComponentTokenizer.spec.ts | 165 ++++++++++++++++++ 10 files changed, 860 insertions(+), 16 deletions(-) create mode 100644 examples/react-native-marked-sample/ReactComponentsExample.tsx create mode 100644 src/hooks/__tests__/useMarkdownWithComponents.spec.tsx create mode 100644 src/hooks/useMarkdownWithComponents.tsx create mode 100644 src/lib/ReactComponentRegistry.tsx create mode 100644 src/lib/ReactComponentTokenizer.ts create mode 100644 src/lib/__tests__/ReactComponentTokenizer.spec.ts diff --git a/README.md b/README.md index cb1a5cfc..385c3a04 100644 --- a/README.md +++ b/README.md @@ -112,6 +112,7 @@ const CustomComponent = () => { - [x] List (ordered, unordered) - [x] Horizontal Rule - [x] Table +- [x] React Components (via `useMarkdownWithComponents`) - [ ] HTML Ref: [CommonMark](https://commonmark.org/help/) @@ -119,6 +120,77 @@ Ref: [CommonMark](https://commonmark.org/help/) > HTML will be treated as plain text. Please refer [issue#290](https://github.com/gmsgowtham/react-native-marked/issues/290) for a potential solution ## Advanced + +### Embedding React Components in Markdown + +You can embed React components directly in your markdown using JSX-style syntax. This is useful for adding interactive elements like buttons, custom info boxes, or any other React component. + +```tsx +import React, { Fragment } from "react"; +import { Pressable, ScrollView, Text, View } from "react-native"; +import { + ReactComponentRegistryProvider, + useMarkdownWithComponents, + type ReactComponentRegistry, +} from "react-native-marked"; + +// Define your components +const components: ReactComponentRegistry = { + Button: ({ props }) => ( + console.log("Pressed!")}> + {String(props.label ?? "Click me")} + + ), + InfoBox: ({ props, children }) => ( + + {props.title && {String(props.title)}} + {children} + + ), +}; + +const markdown = ` +# Hello World + +Click the button below: + +