@docren/react-native-markdown

中文文档
0.8.6发布于 2026/11/04MITTypeScriptCommonJS构建来源已验证近 30 天下载 4995

Markdown renderer for React Native

原始使用文档
文档目录14 节
  1. Features
  2. Acknowledgements
  3. Installation
  4. Base Usage
  5. Props and Methods
  6. Syntax Support
  7. Rules and Styles
  8. Styling Elements
  9. Roadmap
  10. Related
  11. Contributing
  12. License
  13. Authors
  14. Built with ❤️

React Native Markdown Renderer

NPM License NPM Version runs with expo npm

A flexible and performant Markdown renderer for React Native, powered by MDAST. Supports custom styling, nested elements, and extendable render rules for full control over output.

https://github.com/user-attachments/assets/7e07a865-bccd-4961-b696-0b17935bbcc9

Features

  • 📄 CommonMark-Compliant – Parses and renders Markdown based on the CommonMark spec
  • 🧩 GFM Support – Extend Markdown with GitHub Flavored Markdown features via pluggable extensions.
  • ⚛️ React Native First – Designed specifically for rendering Markdown in React Native environments
  • 🎯 AST-Based Rendering – Fine-grained control using custom render rules powered by MDAST nodes
  • 🛠️ Fully Customizable – Customize how each Markdown element is rendered with flexible render rules
  • 🧠 Lightweight & Tree-Shakeable – Minimal footprint with no bundled syntax highlighters
  • 🔐 TypeScript First – Strong typing throughout for safer and more predictable code

Markdown – Render Markdown with customizable components, AST access, and full control over rendering behavior.

Acknowledgements

This package is inspired by react-native-markdown-display — many thanks to the original author for laying the groundwork for Markdown rendering in React Native.

We’ve built on this foundation with a fully type-safe architecture, swapped in the lighter and more modern mdast-util-from-markdown parser for better performance and maintainability, and added extended support for more Markdown elements, custom rendering, and advanced features.

Installation

NPM

npm install @docren/react-native-markdown

Yarn

yarn add @docren/react-native-markdown

PNPM

pnpm add @docren/react-native-markdown

Base Usage

import { View, StyleSheet, ScrollView } from 'react-native';

import { Markdown } from '@docren/react-native-markdown';

export default function App() {
  return (
    <View style={styles.container}>
      <ScrollView contentContainerStyle={styles.content}>
        <Markdown
          markdown={`#Hello world`}
          }
        />
      </ScrollView>
    </View>
  );
}

export default App;

For more detailed usage ⬇️

Props and Methods

The <Markdown> component takes the following props.

PropTypeDefaultDescription
markdonstring | Uint8ArrayN/Arequired The Markdown content to render. Accepts plain strings or .md file contents as Uint8Array.
styleStyleMap | nullsourceStyles applied to Markdown elements. Pass a map of styles keyed by Markdown element types.
mergeStylebooleantrueWhether to merge default styles with the user-provided style map. If false, only your styles are used.
renderRulesRenderRulessourceCustomize how specific Markdown elements are rendered using an object of render functions. Merged by default.
listBulletStyle'disc' | 'dash''disc'Defines the default bullet style for unordered lists (• or –).
customBulletElementReactElement | nullN/AOverride the default list bullet with a custom React element. Applies to all unordered list items.
onLinkPress(url: string) => voidN/ACallback invoked when a link is pressed. When provided, it replaces the default Linking.openURL behaviour.
extensionsExtension[][]Extend or modify Markdown parsing by providing custom MDAST extensions. Useful for GFM or custom syntax. To know more
debugbooleanfalseEnable debug logs during parsing and rendering for development purposes.

Syntax Support

  • blockquote
  • break
  • code
  • emphasis
  • definition
  • heading
    • heading1
    • heading3
    • heading4
    • heading2
    • heading5
    • heading6
  • image
  • imageReference
  • inlineCode
  • link
  • linkReference
  • list
  • listItem
  • paragraph
  • root
  • strong
  • text
  • thematicBreak
  • strikethrough (Planned)
  • table (Planned)
    • tableCell
    • tableRow
  • html (Not planned)
  • yaml (Not planned)

Rules and Styles

Styling Elements

Text styles follow a cascading approach, similar to how CSS works. Global styles can be set using the root style, which will apply across most Markdown elements, while still allowing you to override specific styles on individual elements when needed.

Keep in mind: the text style does not affect all text nodes—especially things like list bullets. If you want to apply a consistent style (e.g. color or font) to all text, it’s better to define it in the root style instead.

Styles

Styles allow you to customize the appearance of specific Markdown elements, overriding the default styling behavior, Default styling.

By default, your styles are merged with the built-in styles. To override them completely, refer to the mergeStyle prop.

Styling example

export default function App() {
  return (
    <View style={styles.container}>
      <ScrollView contentContainerStyle={styles.content}>
        <Markdown
          markdown={mdxString}
          styles={{
            blockquote: {
              backgroundColor: '#cad8ee',
              borderRadius: 12,
            },
          }}
        />
      </ScrollView>
    </View>
  );
}

Rules

Rules let you define how specific Markdown elements should be rendered. You can find the default implementation here.

Note: When defining rules, make sure to assign a unique key to each component. You can use a custom value or the key provided in the node property.

ℹ️ You don’t need to define rules for definition, linkReference, or imageReference, as these are internally resolved to link and image. You can still use them in your Markdown content. If you need customization, override the render rules for link and image.

If you’re using this package with Expo, it’s recommended to use expo-image for rendering images.

Render rule example

import {Image} from 'expo-image'

export default function App() {

  const mdxString = `
          ![Alt text](https://fastly.picsum.photos/id/70/200/200.jpg?hmac=hRU7tEHltyLUTf0bCrAWFXlPRXOBTsvCcvL-dIUG2CE "Image title")
          `

  return (
    <View style={styles.container}>
      <ScrollView contentContainerStyle={styles.content}>
        <Markdown
          markdown={mdxString}
          renderRules={{
            image: ({ node }) => (
              <Image
                key={(node as any).key}
                source={{ uri: node.url }}
                alt={node.alt ?? ""}
                style={{ width: "100%", height: 300, borderRadius: 8 }}
              />
            ),
          }}
        />
      </ScrollView>
    </View>
  );
}

Syntax Highlighing

This package does not include built-in syntax highlighting, as it aims to remain lightweight. However, you can integrate external libraries like react-native-syntax-highlighter or react-native-code-highlighter for that functionality.

Once integrated, you can apply syntax highlighting by overriding the renderRule.code function.

An example implementation can be found in the included example app.

Syntax highlighting render rule


import CodeHighlighter from 'some-syntax-highlighting-package'

<Markdown
  markdown={mdxString}
  renderRules={{
    code: ({ node }) => (
      <CodeHighlighter
        key={(node as any).key}
        hljsStyle={atomOneDarkReasonable} //highlight.js theme
        language={node.lang || 'plaintext'}
        scrollViewProps={{
          bounces: false,
          contentContainerStyle: styles.codeContentContainer,
        }}
        textStyle={styles.codeTextStyle}
      >
        {node.value}
      </CodeHighlighter>
    ),
  }}
/>

By default, links are handled by Linking from react-native. ie,

import { Linking } from 'react-native';

Linking.openURL(url);

You can override this behavior in one of two ways:

Using the onLinkPress callback


export default function App() {
    const mdxString = `[Link to Google](https://www.google.com)`

    const onLinkPress = (url: string) => {
        // Handle link press, e.g., open in a web browser or navigate to a screen
        console.log('Link pressed:', url);
    };

  return (
    <View style={styles.container}>
      <ScrollView contentContainerStyle={styles.content}>
        <Markdown markdown={mdxString} onLinkPress={onLinkPress} />
      </ScrollView>
    </View>
  );
}

Providing a custom renderRule.link implementation

To override the default link behavior, you can use a custom renderRule.link. If you’re using Expo, it’s recommended to use expo-router for internal links and expo-linking for external links.

export default function Screen() {
  const markdown = `
[Explore](/explore)
        
[Google](https://google.com)
  `;

  const router = useRouter();

  const handleLinkPress = (url: string) => {
    if (!url.startsWith('/')) {
      // If the URL does not start with '/', treat it as an external link
      // and open it in the default browser.
      return Linking.openURL(url);
    }

    router.push(url as Href);
  };

  const rules: RenderRules = {
    link: ({ node, children }) => {
      return (
        <Text
          key={(node as any).key}
          style={{ color: 'blue', textDecorationLine: 'underline' }}
          onPress={() => handleLinkPress(node.url)}
        >
          {children}
        </Text>
      );
    },
  };

  return (
    <View style={styles.container}>
      <ScrollView contentContainerStyle={styles.content}>
        <Markdown markdown={markdown} renderRules={rules} />
      </ScrollView>
    </View>
  );
}

All rules and their associated styles

Most rules and styles share the same names, so refer to the Syntax Support section for the complete list of available rules. Any exceptions with different naming are listed below.

When in doubt check the default styling and default render rules

No rules or styles are provided for definition, linkReference, and imageReference, as they are handled internally as link and image, as mentioned above.

Render RuleStyleNote
headingheading1 | heading2 | heading3 | heading4 | heading5 | heading6For a single heading render rule, there are six style variations corresponding to the different heading levels (h1 through h6).
listItemlistItem, listBullet, listItemContentlistItem – Style for the container that wraps both the bullet and the content. listBullet – Style for the default bullet used in unordered lists ( • or –). [Not applicablefor custom bullet element] listItemContent – Style for the content portion of the list item.

Roadmap

  • Themed Styling (Dark / light Mode)
  • Table Support

Contributing

See the contributing guide to learn how to contribute to the repository and the development workflow.

License

This project is licensed under the MIT License.

MIT

Authors

Built with ❤️

版本与发布记录

@docren/react-native-markdown 共发布 4 个版本,首次发布于 2025/10/14;过去 12 个月发布了 3 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.8.6latest2026/11/049 天前
0.8.52026/01/159 个月前
0.8.42026/01/0210 个月前
0.8.32025/10/141.1 年前

关于 @docren/react-native-markdown 的常见问题

@docren/react-native-markdown 是什么?

Markdown renderer for React Native

如何安装 @docren/react-native-markdown?

在项目目录运行 npm install @docren/react-native-markdown,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:react(*)、react-native(*)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@docren/react-native-markdown 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@docren/react-native-markdown 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@docren/react-native-markdown 可以商用吗?

@docren/react-native-markdown 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@docren/react-native-markdown 的下载量有多少?

根据 npm 官方下载统计,@docren/react-native-markdown 在 2026/10/13 – 2026/11/11 这 30 天内被下载 4,995 次,日均约 167 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@docren/react-native-markdown 还在维护吗?

最新版本 0.8.6 发布于 2026/11/04(9 天前);至今共发布 4 个版本,其中过去 12 个月发布了 3 个;首次发布于 2025/10/14。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。