• Stars
    star
    146
  • Rank 252,769 (Top 5 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 8 years ago
  • Updated about 3 years ago

Reviews

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

Repository Details

Converts content from Draft.js blocks to Markdown and vice versa.

npm version

Draft.js to Markdown to Draft.js converter

Converts rich text content between Draft.js blocks and Markdown.

Reasoning and background

This library exists because I needed a highly customisable rich text editor which posts to an external API in Markdown. Draft.js to the rescue! It provides the editor state but, alas, doesn't ship with any sort of conversion to or from markdown. So, I've written my own.

Installation

npm install draftjs-md-converter

Support

The following inline styles are supported:

  • bold
  • italic
  • H1 - H6

The following block styles are supported:

  • ordered list
  • unordered list
  • block quote

The following media types are supported:

  • images
  • videos (with draft-js-video-plugin, parsing can be done using remark-shortcodes)

Usage

Converting from Markdown to Draft.js

mdToDraftjs(markdown: String): RawDraftContentState

Use convertToRaw from the draft-js library to convert the resulting RawDraftContentState into a draft-js ContentState.

Custom inline styles and block styles

The default supported inline styles:

{
  Strong: {
    type: 'BOLD',
    symbol: '__'
  },
  Emphasis: {
    type: 'ITALIC',
    symbol: '*'
  }
}

The default supported block styles:

{
  List: 'unordered-list-item',
  Header1: 'header-one',
  Header2: 'header-two',
  Header3: 'header-three',
  Header4: 'header-four',
  Header5: 'header-five',
  Header6: 'header-six',
  CodeBlock: 'code-block',
  BlockQuote: 'blockquote'
}

Inline styles and block styles can be extended or overridden by passing a custom styles object as a second optional argument to mdToDraftjs, e.g.

const markdown = "Some `markdown` with ~~deleted~~ text";

const myCustomStyles = {
  inlineStyles: {
    Delete: {
      type: "STRIKETHROUGH",
      symbol: "~~",
    },
    Code: {
      type: "CODE",
      symbol: "`",
    },
  },
  blockStyles: {
    CustomBlock: "custom-block",
  },
};

const content = mdToDraftjs(markdown, myCustomStyles);

The keys to the inlineStyles object should be valid AST node types.

Converting from Draft.js to Markdown

draftjsToMd(rawData: RawDraftContentState): String

Use convertFromRaw from the draft-js library to get the raw RawDraftContentState to then pass into the converter.

Custom dictionaries

The default Markdown dictionary is

{
  BOLD: '__',
  ITALIC: '*'
};

The inline styles can be extended or overridden by passing a custom dictionary object as a second optional argument to draftjsToMd, e.g.

const myMdDict = {
  BOLD: "**",
  STRIKETHROUGH: "~~",
};
const markdown = draftjsToMd(blocks, myMdDict);

NOTE: at this point you cannot override block styles!

Example

[---]

import { mdToDraftjs, draftjsToMd } from 'draftjs-md-converter';
import { EditorState, ContentState, convertToRaw, convertFromRaw } from 'draft-js';

[---]

constructor(props) {
  super(props);

  // some default value in markdown
  const defaultValue = this.props.defaultValue;
  const rawData = mdToDraftjs(defaultValue);
  const contentState = convertFromRaw(rawData);
  const newEditorState = EditorState.createWithContent(contentState);

  this.state = {
    editorState: newEditorState,
  };
  this.onChange = (editorState) => {
    this.props.onChange(this.getMarkdown());
    this.setState({ editorState });
  };
}

[---]

getMarkdown() {
  const content = this.state.editorState.getCurrentContent();
  return draftjsToMd(convertToRaw(content));
}

[---]

Run tests

npm test

Run tests with a watcher

npm run test-dev

Lint

npm run lint

More Repositories

1

react-native-v2

React Native v2 course material for Frontend Masters
JavaScript
191
star
2

react-native-template

A template for a new React Native project
Java
66
star
3

react-native-beyond-basics

Intermediate React Native course material
JavaScript
58
star
4

offline-first-mobile-example

JavaScript
29
star
5

AwesomeProjectRN

React Native solutions to my React Native course for Frontend Masters
JavaScript
29
star
6

news-flash

TypeScript
17
star
7

news-flash-api

JavaScript
13
star
8

AwesomeProjectExpo

Expo solutions to my React Native course for Frontend Masters
JavaScript
12
star
9

color-palette-api

JavaScript
12
star
10

MoodTracker

Solutions repo for my Intermediate React Native course material on Frontend Masters
TypeScript
12
star
11

skeleton-loader

JavaScript
11
star
12

react-native-workshop

React Native Workshop for HackConf 2019
JavaScript
10
star
13

how-to-router

Small projects to demonstrate various functionality in Expo Router
TypeScript
10
star
14

react-native-v3-course-app

Solutions for React Native v3 course on Frontend Masters
TypeScript
9
star
15

threadstarter

Lambda for function for a slackbot that starts threads
JavaScript
7
star
16

UrqlTest

React Native urql Demo app
Java
6
star
17

intermediate-react-native-v2-course-app

TypeScript
6
star
18

mounting-unmounting-lab

JavaScript
5
star
19

mood-tracker

TypeScript
5
star
20

rendering-lab

JavaScript
5
star
21

kadikraman

3
star
22

sorting

A collection of sorting algorithms in JavaScript
JavaScript
3
star
23

react-native-v3-course-app-js

JS solutions for React Native v3 course on Frontend Masters
JavaScript
3
star
24

fix-me-app

Practice using Expo Doctor to find and resolve problems in your project.
JavaScript
2
star
25

happy-campers

A game about camping and trees
JavaScript
2
star
26

linkify-jira

Chrome extension that forces all links in JIRA to open in a new tab
JavaScript
2
star
27

expo-image-transition-demo

TypeScript
2
star
28

EveningMeetingTalkJune2014

My talk about Data visualization in D3 made using reveal.js and obviously D3
JavaScript
1
star
29

presentation-react-native

My slides for React to React Native talk
JavaScript
1
star
30

staff-swipe

An example of a swipecard UI
JavaScript
1
star
31

RobotsOnMars

Command line game for observing the behaviour of some robots in an alternate universe Mars
Python
1
star
32

CarExpensesAPI

API to access and update my car expenses data
JavaScript
1
star
33

new-arch-text-input-bug

TypeScript
1
star