• Stars
    star
    190
  • Rank 197,418 (Top 4 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created about 7 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

MarkdownView for React Native

react-native-markdown-view

npm version

A straight-forward React Native component for rendering Markdown as native views.

This library is backed by simple-markdown.

Library Goals

Compared to the current alternative Markdown components available for React Native this library has a strong focus on the following:

  • Minimalistic React Native specific API i.e. Doesn't expose unnecessary details of simple-markdown.
  • Rendering correctness e.g. Tables cells align correctly thanks to react-native-tabular-grid
  • Avoiding unnecessary dependencies e.g. No using lodash simply for merge/map.
  • Best effort default styling e.g. Use of translucent grey backgrounds should work well in either light or dark themed apps.
  • Simple customisability/extensibility i.e. Support for providing your own styles and rules.
  • Is written using modern ES6/7 React Native syntax.

Install

Install the node module:

yarn add react-native-markdown-view

or with npm:

npm install --save react-native-markdown-view

Usage

Import MarkdownView from react-native-markdown-view:

import { MarkdownView } from 'react-native-markdown-view'

In your Component's render() method you can then render markdown via JSX e.g.

<MarkdownView>
  # MarkdownView{'\n'}
  {'\n'}
  **React Native** is even better with Markdown!{'\n'}
  {'\n'}
  ![RN Logo](https://reactjs.org/logo-og.png =120x63){'\n'}
  {'\n'}
  `react-native-markdown-view` is:{'\n'}
  {'\n'}
  * Easy to use{'\n'}
  * Looks good by default{'\n'}
  * Is __extensible__{'\n'}
</MarkdownView>

which renders like:

example

MarkdownView

A React Native component which renders Markdown as a hierarchy of React Native views.

Props

rules

An object overriding or providing additional rules for parsing and rendering Markdown. Keys are rule names (you can define your own, or override existing rules), and values are an object of the form:

  {
    match: (source, state, lookbehind) => RegExp,
    parse: (match, nestedParse, state) => Node
    render: (node, output, state, styles) => Component
  }

match: A function returning a RegExp to be executed against the MarkdownView's content.

  • source: Upcoming content of MarkdownView. i.e. A string beginning at the current position of parsing (source[0] is the next character).
  • state: Matcher state object, you can attach your own state properties if desirable.
  • lookbehind: The string most recently captured at this parsing level, to allow for look-behind parsing.

parse: A function that returns an AST 'node' object to pass to the rules' render method. If the object returned has a 'type' key, rendering will be deferred to the rule matching the value of 'type'.

  • match: Return value of match.exec() (match i.e. the rule property, not the parameter)
  • nestedParse: (string, state) => object, call this to parse nested nodes.
  • state: Parser state object, you can attach your own state properties if desirable.

render: A function that returns the rendered node (and its children). Typically you'll return a React Native view component.

  • node: An AST node. Please refer to the Flow types in types.js.
  • output: A function that can be used to render nested/children nodes. Typically you'll want call output(node.children) and use that as the content of the component you're returning.
  • state: Renderer state object. You can attach your own state to this object and use it, for example, to render nodes differently depending on their parents/ancestors.
  • styles: An object containing React Native styles that you can use for rendering components.

Default rendering rules have keys:

heading, hr, codeBlock, blockQuote, list, table, newline, paragraph, link, image, em, strong, u, del, inlineCode, br, text

Default parse-only rules (which defer rendering to another rule) have keys:

nptable, lheading, fence, def, escape, autolink, mailto, url, reflink, refimage,

styles

An object providing styles to be passed to a corresponding rule render method. Keys are rule/node names and values are React Native style objects. If a style is defined here and a default style exists, they will me merged, with style properties defined here taking precedence.

Please refer to styles.js for a complete list of styles that can be overwritten.

e.g.

{
  tableHeaderCell: {
    color: '#f00'
  },
  paragraph: {
    marginLeft: 8
  }
}

onLinkPress

Callback function for when a link is pressed. The callback receives the URL of the link as a string (first and only argument).

Here's an example using Linking from react-native to open link in default browser app:

Import

import { Linking } from 'react-native'

Usage

<MarkdownView
  onLinkPress={(url) => {
    Linking.openURL(url).catch(error =>
      console.warn('An error occurred: ', error),
    )
  }}
>
Check out: [react-native-markdown-view](https://github.com/Benjamin-Dobell/react-native-markdown-view/)
</MarkdownView>

More Repositories

1

Heimdall

Heimdall is a cross-platform open-source tool suite used to flash firmware (aka ROMs) onto Samsung Galaxy devices.
C++
2,291
star
2

nvidia-update

Install nVidia drivers on macOS the easy way.
Shell
1,284
star
3

IntelliJ-Luanalysis

Type-safe Lua IDE Plugin for IntelliJ IDEA
Kotlin
145
star
4

VelocityViewPager

Custom Android ViewPager with support for flinging/velocity based scrolling.
Java
80
star
5

s3tc-dxt-decompression

S3TC DXT1/DXT5 texture decompression routines.
C++
44
star
6

luabundle

A library for bundling several Lua files into a single file.
TypeScript
34
star
7

luabundler

CLI tool for bundling several Lua files into a single file.
TypeScript
27
star
8

humble-bundle-key-redeemer

Bulk redemption/activation of Humble Bundle Steam keys.
Ruby
25
star
9

ge_tts

Lua
21
star
10

kogan-tv-gpl

C
20
star
11

Fookoff

Prevent OS X apps stealing focus.
Objective-C
19
star
12

GERazerKit

Unofficial SDK for Razer hardware on macOS.
C
16
star
13

tts-types

Tabletop Simulator EmmyLua types.
Lua
8
star
14

docker-tts

Dockerized Tabletop Simulator
ASP.NET
8
star
15

react-native-android-slidingtabstrip

React Native sliding tab strip implementation for Android.
Java
5
star
16

react-native-android-drawables

React Native support for custom Android Drawables
Java
3
star
17

LuanalysisTypesDemo

Demo of the advanced type functionality added to my EmmyLua fork.
Lua
3
star
18

libopenddl

Several parsers & composers for OpenDDL in different programming languages.
Java
3
star
19

RazerBuildLight

OS X app to turn Razer Chroma accessories into a build light for Github projects
Objective-C
2
star
20

ReactNativeNotificationsExample

Integration of react-native-notifications with the Android Overhaul and iOS Firebase integration.
Objective-C
2
star
21

ge_tts_demo

ge_tts (Tabletop Simulator) demonstration project.
Lua
2
star
22

libpit--Java-

Java library for reading/writing Samsung PIT files, a part of the Heimdall project.
Java
2
star
23

Samsung-Read-Only-FSR

Samsung Flex Sector Remapper (Read-Only)
C
2
star
24

react-native-android-toolbar-badge

React Native support for displaying badges in ToolbarAndroid
Java
2
star
25

SteamKitIntegrationTests

Integration tests for SteamKit
C#
2
star
26

shc

shc creates a stripped binary executable version a shell script.
C
2
star
27

steam-redeem

Steam (in particular Humble Bundle) key redemption extension for Chrome.
JavaScript
2
star
28

pdbtomdb

CLI program to convert C# debug symbols from PDB to MDB (Mono).
C#
1
star
29

pymdown-toc-ext

Python-Markdown Table of Contents (Extended) Extension
Python
1
star
30

react-native-tabular-grid-markdown-view

react-native-tabular-grid fork to be used with react-native-markdown-view
JavaScript
1
star