Catalog npm
Back to packageRELEASE DETAILS

6.1.0

npm

@03balogun/react-native-marked

Markdown renderer for React Native powered by marked.js

markdownreact-nativereact-native markdown
Published30 May 2024
Direct dependencies36
Transitive dependenciesNot calculated

README

Current package documentation

@03balogun/react-native-marked

react-native-marked without FlatList

GitHub license CI Coverage Status npm npm

Markdown renderer for React Native powered by marked.js with built-in theming support

Installation

yarn add react-native-marked react-native-svg

Usage

Using Component

import * as React from "react";
import Markdown from "react-native-marked";

const ExampleComponent = () => {
  return (
    <Markdown
      value={`# Hello world`}
      flatListProps={{
        initialNumToRender: 8,
      }}
    />
  );
};

export default ExampleComponent;

Props

PropDescriptionTypeOptional?
valueMarkdown valuestringfalse
flatListPropsProps for customizing the underlying FlatList usedOmit<FlatListProps<ReactNode>, 'data' | 'renderItem' | 'horizontal'>


('data', 'renderItem', and 'horizontal' props are omitted and cannot be overridden.)
true
stylesStyles for parsed componentsMarkedStylestrue
themeProps for customizing colors and spacing for all components,and it will get overridden with custom component style applied via 'styles' propUserThemetrue
baseUrlA prefix url for any relative linkstringtrue
rendererCustom component RendererRendererInterfacetrue

Using hook

useMarkdown hook will return list of elements that can be rendered using a list component of your choice.

import React, { Fragment } from "react";
import { ScrollView, useColorScheme } from "react-native";
import { useMarkdown, type useMarkdownHookOptions } from "react-native-marked";

const CustomComponent = () => {
  const colorScheme = useColorScheme();
  const options: useMarkdownHookOptions = {
    colorScheme
  }
  const elements = useMarkdown("# Hello world", options);
  return (
    <ScrollView>
      {elements.map((element, index) => {
        return <Fragment key={`demo_${index}`}>{element}</Fragment>
      })}
    </ScrollView>
  );
};

Options

OptionDescriptionTypeOptional?
colorSchemeDevice color scheme ("dark" or "light")ColorSchemeNamefalse
stylesStyles for parsed componentsMarkedStylestrue
themeProps for customizing colors and spacing for all components,and it will get overridden with custom component style applied via 'styles' propUserThemetrue
baseUrlA prefix url for any relative linkstringtrue
rendererCustom component RendererRendererInterfacetrue
tokenizerGenerate custom tokensMarkedTokenizertrue

Examples

Supported elements

  • Headings (1 to 6)
  • Paragraph
  • Emphasis (bold, italic, and strikethrough)
  • Link
  • Image
  • Blockquote
  • Inline Code
  • Code Block
  • List (ordered, unordered)
  • Horizontal Rule
  • Table
  • HTML

Ref: CommonMark

HTML will be treated as plain text. Please refer issue#290 for a potential solution

Advanced

Using custom components

Custom components can be used to override elements, i.e. Code Highlighting, Fast Image integration

Example

import React, { ReactNode, Fragment } from "react";
import { Text, ScrollView } from "react-native";
import type { ImageStyle, TextStyle } from "react-native";
import Markdown, { Renderer, useMarkdown } from "react-native-marked";
import type { RendererInterface } from "react-native-marked";
import FastImage from "react-native-fast-image";

class CustomRenderer extends Renderer implements RendererInterface {
  constructor() {
    super();
  }

  codespan(text: string, _styles?: TextStyle): ReactNode {
    return (
      <Text key={this.getKey()} style={{ backgroundColor: "#ff0000" }}>
        {text}
      </Text>
    );
  }

  image(uri: string, _alt?: string, _style?: ImageStyle): ReactNode {
    return (
      <FastImage
        key={this.getKey()}
        style={{ width: 200, height: 200 }}
        source={{ uri: uri }}
        resizeMode={FastImage.resizeMode.contain}
      />
    );
  }
}

const renderer = new CustomRenderer();

const ExampleComponent = () => {
  return (
    <Markdown
      value={"`Hello world`"}
      flatListProps={{
        initialNumToRender: 8,
      }}
      renderer={renderer}
    />
  );
};

// Alternate using hook
const ExampleComponentWithHook = () => {
  const elements = useMarkdown("`Hello world`", { renderer });

  return (
    <ScrollView>
      {elements.map((element, index) => {
        return <Fragment key={`demo_${index}`}>{element}</Fragment>
      })}
    </ScrollView>
  )
}

export default ExampleComponent;

Please refer to RendererInterface for all the overrides

Note:

For key property for a component, you can use the getKey method from Renderer class.

Using tokenizer with custom components

Refer marked

The tokenizer defines how to turn markdown text into tokens. If you supply a tokenizer object to the Marked options, it will be merged with the built-in tokenizer and any functions inside will override the default handling of that token type.


The implementation requires you to return a token of type 'custom' (ref: CustomToken) and the same needs to be implemented in the Renderer

Example

Overriding default codespan tokenizer to include LaTeX.


import React, { ReactNode } from "react";
import Markdown, { Renderer, MarkedTokenizer, MarkedLexer } from "react-native-marked";
import type { RendererInterface, type CustomToken, } from "react-native-marked";

class CustomTokenizer extends MarkedTokenizer<CustomToken> {
  // Override
  codespan(this: MarkedTokenizer<CustomToken>, src: string) {
    const match = src.match(/^\$+([^\$\n]+?)\$+/);
    if (match?.[1]) {
      const text = match[1].trim();
      const token: CustomToken = {
        type: 'custom',
        raw: match[0], // should be the exact regex pattern match
        identifier: "latex", // Uniq identifier for the token
        tokens: MarkedLexer(text), // optional, can be used if the markdown contains children
        args: { // optional, can be used to send more information to the renderer
          text: text,
        }
      };
      return token;
    }

    return super.codespan(src)
  }
}

class CustomRenderer extends Renderer implements RendererInterface {
  // Custom Token implementation
  custom(identifier: string, _raw: string, _children?: ReactNode[], args?: Record<string, unknown>): ReactNode {
    if (identifier === "latex") {
      const styles = {
        padding: 16,
        minWidth: "100%",
        backgroundColor: "#f6f8fa"
      };
      return this.code(text.trim(), "latex", styles);
    }
    return null;
  }
}

const renderer = new CustomRenderer();
const tokenizer = new CustomTokenizer();

const ExampleComponent = () => {
  return (
    <Markdown
      value={"$ latex code $\n\n` other code `"}
      flatListProps={{
        initialNumToRender: 8,
      }}
      renderer={renderer}
      tokenizer={tokenizer}
    />
  );
};

Example

Screenshots

Dark ThemeLight Theme
Dark themeLight theme

Contributing

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

License

MIT


Made with create-react-native-library

Built using

Buy Me A Coffee

Downloads & popularity

Awaiting observations

No metric observations yet

Download counts and popularity will appear here as the collector receives them.

Declared dependencies 38

Native requirements
36 direct targetsNot calculated transitive count
Package / targetRequirementRelationship
svg-parser
Environment
{
  "local_name": "svg-parser",
  "alias": false,
  "peer_metadata": null
}
2.0.4runtime
html-entities
Environment
{
  "local_name": "html-entities",
  "alias": false,
  "peer_metadata": null
}
2.5.2runtime
@03balogun/marked
Environment
{
  "local_name": "@03balogun/marked",
  "alias": false,
  "peer_metadata": null
}
^5.0.6runtime
@jsamr/counter-style
Environment
{
  "local_name": "@jsamr/counter-style",
  "alias": false,
  "peer_metadata": null
}
2.0.2runtime
@jsamr/react-native-li
Environment
{
  "local_name": "@jsamr/react-native-li",
  "alias": false,
  "peer_metadata": null
}
2.3.1runtime
react-native-table-component
Environment
{
  "local_name": "react-native-table-component",
  "alias": false,
  "peer_metadata": null
}
1.2.2runtime
jest
Environment
{
  "local_name": "jest",
  "alias": false,
  "peer_metadata": null
}
29.7.0development
react
Environment
{
  "local_name": "react",
  "alias": false,
  "peer_metadata": null
}
18.3.1development
danger
Environment
{
  "local_name": "danger",
  "alias": false,
  "peer_metadata": null
}
11.3.1development
reassure
Environment
{
  "local_name": "reassure",
  "alias": false,
  "peer_metadata": null
}
0.11.0development
commitlint
Environment
{
  "local_name": "commitlint",
  "alias": false,
  "peer_metadata": null
}
19.3.0development
release-it
Environment
{
  "local_name": "release-it",
  "alias": false,
  "peer_metadata": null
}
17.1.1development
typescript
Environment
{
  "local_name": "typescript",
  "alias": false,
  "peer_metadata": null
}
5.4.5development
@babel/core
Environment
{
  "local_name": "@babel/core",
  "alias": false,
  "peer_metadata": null
}
7.24.4development
@types/jest
Environment
{
  "local_name": "@types/jest",
  "alias": false,
  "peer_metadata": null
}
29.5.12development
@types/node
Environment
{
  "local_name": "@types/node",
  "alias": false,
  "peer_metadata": null
}
20.12.7development
pod-install
Environment
{
  "local_name": "pod-install",
  "alias": false,
  "peer_metadata": null
}
0.2.2development
@types/react
Environment
{
  "local_name": "@types/react",
  "alias": false,
  "peer_metadata": null
}
18.3.2development
react-native
Environment
{
  "local_name": "react-native",
  "alias": false,
  "peer_metadata": null
}
0.72.6development
@types/marked
Environment
{
  "local_name": "@types/marked",
  "alias": false,
  "peer_metadata": null
}
5.0.0development
@biomejs/biome
Environment
{
  "local_name": "@biomejs/biome",
  "alias": false,
  "peer_metadata": null
}
1.3.3development
@babel/preset-env
Environment
{
  "local_name": "@babel/preset-env",
  "alias": false,
  "peer_metadata": null
}
7.24.4development
@types/svg-parser
Environment
{
  "local_name": "@types/svg-parser",
  "alias": false,
  "peer_metadata": null
}
^2.0.3development
@types/react-native
Environment
{
  "local_name": "@types/react-native",
  "alias": false,
  "peer_metadata": null
}
0.72.6development
react-test-renderer
Environment
{
  "local_name": "react-test-renderer",
  "alias": false,
  "peer_metadata": null
}
18.3.1development
@evilmartians/lefthook
Environment
{
  "local_name": "@evilmartians/lefthook",
  "alias": false,
  "peer_metadata": null
}
1.6.10development
jest-environment-jsdom
Environment
{
  "local_name": "jest-environment-jsdom",
  "alias": false,
  "peer_metadata": null
}
29.7.0development
postinstall-postinstall
Environment
{
  "local_name": "postinstall-postinstall",
  "alias": false,
  "peer_metadata": null
}
2.1.0development
react-native-builder-bob
Environment
{
  "local_name": "react-native-builder-bob",
  "alias": false,
  "peer_metadata": null
}
0.23.2development
@testing-library/jest-native
Environment
{
  "local_name": "@testing-library/jest-native",
  "alias": false,
  "peer_metadata": null
}
5.4.3development
@testing-library/react-native
Environment
{
  "local_name": "@testing-library/react-native",
  "alias": false,
  "peer_metadata": null
}
12.5.0development
@commitlint/config-conventional
Environment
{
  "local_name": "@commitlint/config-conventional",
  "alias": false,
  "peer_metadata": null
}
19.2.2development
metro-react-native-babel-preset
Environment
{
  "local_name": "metro-react-native-babel-preset",
  "alias": false,
  "peer_metadata": null
}
0.77.0development
@release-it/conventional-changelog
Environment
{
  "local_name": "@release-it/conventional-changelog",
  "alias": false,
  "peer_metadata": null
}
8.0.1development
@types/react-native-table-component
Environment
{
  "local_name": "@types/react-native-table-component",
  "alias": false,
  "peer_metadata": null
}
1.2.8development
react
Environment
{
  "local_name": "react",
  "alias": false,
  "peer_metadata": null
}
^16.8.6 || ^17.0.0 || ^18.0.0peer
react-native
Environment
{
  "local_name": "react-native",
  "alias": false,
  "peer_metadata": null
}
>=0.60.0peer
react-native-svg
Environment
{
  "local_name": "react-native-svg",
  "alias": false,
  "peer_metadata": null
}
>=13.14.0peer

Distribution files

1 collected
Distribution 1

Source distribution

Source file
Checksums from source
{
  "shasum": "281ccb618c72b0af73a7a36dcd3949518ec4badc",
  "tarball": "https://registry.npmjs.org/@03balogun/react-native-marked/-/react-native-marked-6.1.0.tgz",
  "fileCount": 120,
  "integrity": "sha512-GVeh49idg9gGwP2rP0scmT51yUIEWZzixQCqujwakELlhuwl+ad5qmZHnDNk9iG10baAL6BhP6WbOuOeKOv/3g==",
  "signatures": [
    {
      "sig": "MEYCIQDtmQdIb985sSHN9CPuM8TTTu0BwsT7D2XzEWRGR49N5wIhAOLB5d63mW3Gh5aDJstG+JGhgH+Zx0iyg5MZokSEnTwB",
      "keyid": "SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"
    }
  ],
  "unpackedSize": 220075
}

Repository

PENDING
03balogun/react-native-marked

Shared repository data · GitHub

License detected in repository
Not collected

Repository

PENDING
gmsgowtham/react-native-marked

Shared repository data · GitHub

License detected in repository
Not collected

Metadata history

Meaningful changes
Current collected state Current

Observed since 20 Oct, 22:00 UTC

No superseded metadata states collected. Unchanged observations do not create new revisions.

Record identity

package_version:7ce5fd78510e6839add39b0d8a7b3e06e617ddf098b366fae29866defd39588a

Other collected versions

6.0.1217 Apr6.0.113 Jun6.0.1030 May6.0.929 May6.0.829 May6.0.729 MayView all collected versions →