|
1 | 1 | # react-native-streamdown |
2 | 2 |
|
3 | | -Markdown Streaming |
| 3 | +A streaming-ready markdown component for React Native built on top of [`react-native-enriched-markdown`](https://github.com/Expensify/react-native-enriched-markdown) and [`remend`](https://www.npmjs.com/package/remend). |
| 4 | + |
| 5 | +It processes raw, incomplete markdown (as it streams token-by-token from an LLM) in the background using [`react-native-worklets`](https://docs.swmansion.com/react-native-worklets/docs/) powerful concurrency feature - the Bundle Mode - keeping the JS thread free at all times. |
| 6 | + |
| 7 | +## Features |
| 8 | + |
| 9 | +- Renders incomplete streaming markdown correctly — no visual glitches mid-stream |
| 10 | +- Background thread processing via `react-native-worklets` Bundle Mode |
| 11 | +- Inline LaTeX support (`$...$`) with streaming completion — applied automatically, no configuration needed (we've also opened a [PR to add this directly to remend](https://github.com/vercel/streamdown/pull/446)) |
| 12 | +- CommonMark rendering (headers, bold, italic, inline code, fenced code blocks, links, images) powered by `react-native-enriched-markdown` with built-in `streamingAnimation` |
| 13 | +- Customizable via `remendConfig` |
| 14 | + |
| 15 | +--- |
4 | 16 |
|
5 | 17 | ## Installation |
6 | 18 |
|
| 19 | +```sh |
| 20 | +yarn add react-native-streamdown |
| 21 | +``` |
| 22 | + |
| 23 | +### Peer dependencies |
7 | 24 |
|
8 | 25 | ```sh |
9 | | -npm install react-native-streamdown |
| 26 | +yarn add react-native-enriched-markdown react-native-worklets remend |
10 | 27 | ``` |
11 | 28 |
|
| 29 | +| Package | Version | |
| 30 | +| -------------------------------- | ----------------------------- | |
| 31 | +| `react-native-enriched-markdown` | `0.4.0` | |
| 32 | +| `react-native-worklets` | `0.8.0-bundle-mode-preview-2` | |
| 33 | +| `remend` | `1.2.2` | |
12 | 34 |
|
13 | | -## Usage |
| 35 | +--- |
| 36 | + |
| 37 | +## Required setup — Bundle Mode |
14 | 38 |
|
| 39 | +`react-native-streamdown` runs markdown processing on a worklet thread using **Bundle Mode** from `react-native-worklets`. This requires extra configuration steps from the [official Bundle Mode setup guide](https://docs.swmansion.com/react-native-worklets/docs/bundleMode/setup/). Make sure to complete these steps before continuing. For a real-world reference of an app configured with Bundle Mode, check out the [Bundle Mode Showcase App](https://github.com/software-mansion-labs/Bundle-Mode-showcase-app). |
| 40 | + |
| 41 | +### 1. `babel.config.js` — configure Worklets Babel plugin |
| 42 | + |
| 43 | +`react-native-streamdown` requires special options to be added to the Worklets Babel plugin config in `babel.config.js`: |
15 | 44 |
|
16 | 45 | ```js |
17 | | -import { multiply } from 'react-native-streamdown'; |
| 46 | +const workletsPluginOptions = { |
| 47 | + bundleMode: true, |
| 48 | + // other options... |
| 49 | + workletizableModules: ['remend'], // add this line |
| 50 | +}; |
| 51 | +``` |
| 52 | + |
| 53 | +`workletizableModules: ['remend']` tells the Babel plugin to pre-bundle `remend` for the worklet runtime so it can be called off the JS thread. |
| 54 | + |
| 55 | +### 2. `metro.config.js` — configure Metro for monorepos |
18 | 56 |
|
19 | | -// ... |
| 57 | +`react-native-worklets` Bundle Mode generates files on the fly that might not be tracked by Metro in some monorepo setups. It might also shadow your resolving function. If you're running into issues with module resolution, you need to add the following to your `metro.config.js`: |
20 | 58 |
|
21 | | -const result = await multiply(3, 7); |
| 59 | +```js |
| 60 | +const { getDefaultConfig, mergeConfig } = require('@react-native/metro-config'); |
| 61 | +const { bundleModeMetroConfig } = require('react-native-worklets/bundleMode'); |
| 62 | + |
| 63 | +let config = getDefaultConfig(__dirname); |
| 64 | + |
| 65 | +// Watch the .worklets/ output directory |
| 66 | +config.watchFolders.push( |
| 67 | + require('path').resolve( |
| 68 | + __dirname, |
| 69 | + 'node_modules/react-native-worklets/.worklets' |
| 70 | + ) |
| 71 | +); |
| 72 | + |
| 73 | +// Resolve react-native-worklets/.worklets/* via the Bundle Mode resolver |
| 74 | +const defaultResolver = config.resolver.resolveRequest; |
| 75 | + |
| 76 | +config = mergeConfig(config, bundleModeMetroConfig); |
| 77 | + |
| 78 | +config.resolver.resolveRequest = (context, moduleName, platform) => { |
| 79 | + if (moduleName.startsWith('react-native-worklets/.worklets/')) { |
| 80 | + return bundleModeMetroConfig.resolver.resolveRequest( |
| 81 | + context, |
| 82 | + moduleName, |
| 83 | + platform |
| 84 | + ); |
| 85 | + } |
| 86 | + return defaultResolver(context, moduleName, platform); |
| 87 | +}; |
| 88 | + |
| 89 | +module.exports = config; |
22 | 90 | ``` |
23 | 91 |
|
| 92 | +--- |
24 | 93 |
|
25 | | -## Contributing |
| 94 | +## Usage |
26 | 95 |
|
27 | | -- [Development workflow](CONTRIBUTING.md#development-workflow) |
28 | | -- [Sending a pull request](CONTRIBUTING.md#sending-a-pull-request) |
29 | | -- [Code of conduct](CODE_OF_CONDUCT.md) |
| 96 | +```tsx |
| 97 | +import { StreamdownText } from 'react-native-streamdown'; |
30 | 98 |
|
31 | | -## License |
| 99 | +// rawMarkdown can be updated token-by-token as the LLM streams |
| 100 | +<StreamdownText rawMarkdown={partialMarkdown} />; |
| 101 | +``` |
32 | 102 |
|
33 | | -MIT |
| 103 | +### Props |
| 104 | + |
| 105 | +`StreamdownText` accepts all props from `EnrichedMarkdownText` (except `flavor`, which is hardcoded to `commonmark`) plus one additional prop: |
| 106 | + |
| 107 | +| Prop | Type | Description | |
| 108 | +| -------------- | --------------- | ------------------------------------------------------------------------------------------------------------------------------------------- | |
| 109 | +| `remendConfig` | `RemendOptions` | Optional. Override the default remend processing config. See [remend docs](https://www.npmjs.com/package/remend) for all available options. | |
| 110 | + |
| 111 | +--- |
| 112 | + |
| 113 | +## Example app |
| 114 | + |
| 115 | +The `example/` directory in this repository contains a fully working demo app that shows: |
| 116 | + |
| 117 | +- **Streaming Markdown Simulator** — streams a sample markdown document token-by-token to demonstrate rendering quality and the `streamingAnimation` effect |
| 118 | +- **LLM Streaming Demo** — connects to the OpenAI Chat Completions API via SSE and renders the response live using `StreamdownText` |
| 119 | + |
| 120 | +It is a practical reference for the full Bundle Mode setup (Babel, Metro, `package.json` flags) and for how to wire `StreamdownText` into a real streaming UI. |
| 121 | + |
| 122 | +--- |
| 123 | + |
| 124 | +## Limitations |
| 125 | + |
| 126 | +- **CommonMark only** — `StreamdownText` currently renders using the `commonmark` flavour of `react-native-enriched-markdown`. GitHub Flavored Markdown (GFM) support is planned for a future release. |
| 127 | + |
| 128 | +--- |
| 129 | + |
| 130 | +Built by [Software Mansion](https://swmansion.com/). |
| 131 | + |
| 132 | +[<img width="128" height="69" alt="Software Mansion Logo" src="https://github.com/user-attachments/assets/f0e18471-a7aa-4e80-86ac-87686a86fe56" />](https://swmansion.com/) |
34 | 133 |
|
35 | 134 | --- |
36 | 135 |
|
37 | | -Made with [create-react-native-library](https://github.com/callstack/react-native-builder-bob) |
| 136 | +## License |
| 137 | + |
| 138 | +MIT |
0 commit comments