• Stars
    star
    315
  • Rank 132,951 (Top 3 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 8 years ago
  • Updated 10 months ago

Reviews

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

Repository Details

Library for converting Draftjs editor content state to HTML

DraftJS TO HTML

A library for converting DraftJS Editor content to plain HTML.

This is draft to HTML library I wrote for one of my projects. I am open-sourcing it so that others can also be benefitted from my work.

Installation

npm install draftjs-to-html

Usage

import { convertToRaw } from 'draft-js';
import draftToHtml from 'draftjs-to-html';

const rawContentState = convertToRaw(editorState.getCurrentContent());

const markup = draftToHtml(
  rawContentState, 
  hashtagConfig, 
  directional, 
  customEntityTransform
);

The function parameters are:

  1. contentState: Its instance of RawDraftContentState

  2. hashConfig: Its configuration object for hashtag, its required only if hashtags are used. If the object is not defined hashtags will be output as simple text in the markdown.

    hashConfig = {
      trigger: '#',
      separator: ' ',
    }

    Here trigger is character that marks starting of hashtag (default '#') and separator is character that separates characters (default ' '). These fields in hastag object are optional.

  3. directional: Boolean, if directional is true text is aligned according to bidi algorithm. This is also optional.

  4. customEntityTransform: Its function to render custom defined entities by user, its also optional.

    editorState is instance of DraftJS EditorState.

Supported conversions

Following is the list of conversions it supports:

  1. Convert block types to corresponding HTML tags:

    Block Type HTML Tag
    1 header-one h1
    2 header-two h2
    3 header-three h3
    4 header-four h4
    5 header-five h5
    6 header-six h6
    7 unordered-list-item ul
    8 ordered-list-item ol
    9 blockquote blockquote
    10 code pre
    11 unstyled p

    It performs these additional changes to text of blocks:

    • replace blank space in beginning and end of block with  
    • replace \n with <br>
    • replace < with &lt;
    • replace > with &gt;
  2. Converts ordered and unordered list blocks with depths to nested structure of <ul>, <ol> and <li>.

  3. Converts inline styles BOLD, ITALIC, UNDERLINE, STRIKETHROUGH, CODE, SUPERSCRIPT, SUBSCRIPT to corresponding HTML tags: <strong>, <em>, <ins>, <code>, <sup>, <sub>.

  4. Converts inline styles color, background-color, font-size, font-family to a span tag with inline style details: <span style="color:xyz;font-size:xx">. (The inline styles in JSON object should start with strings color or font-size like color-red, color-green or fontsize-12, fontsize-20).

  5. Converts entity range of type link to anchor tag using entity data url for href, targetOption for target: <a href="url" target="_self">text</a>. Default target is _self.

  6. Converts entity range of type mention to anchor tag using entity data url for href and value for data-value, it also adds class to it: <a href="url" class="wysiwyg-mention" data-mention data-value="value">text</a>.

  7. Converts atomic entity image to image tag using entity data src for image source, and if present alt, alignment, height, width also: <img src="src" alt="alt_text" style="float: left, height: 50px; width: 50px"/>.

  8. Converts embedded links to iFrames, using width, height and src from entity data. <iframe width="width" height="height" src="src" frameBorder="0"></iframe>

  9. Converts hashtags to anchor tag: <a href="#tag" class="wysiwyg-hashtag">#tag</a>.

  10. customEntityTransform can be used for transformation of a custom entity block to html. If present its call to generate html for entity. It can take 2 parameter:

    1. entity ( object with { type, mutalibity, data})
    2. text text present in the block.
  11. Adding style property to block tag for block level styles like text-align: <p style="text-align: right">text</p>.

  12. RTL, if directional function parameter is true, generated blocks have property dir = "auto" thus they get aligned according to bidi algorithm.

License

MIT.

More Repositories

1

react-draft-wysiwyg

A Wysiwyg editor build on top of ReactJS and DraftJS. https://jpuri.github.io/react-draft-wysiwyg
JavaScript
6,358
star
2

draftjs-utils

An collection of useful utility functions for DraftJS.
JavaScript
285
star
3

html-to-draftjs

HTML
158
star
4

react-range-slider

React range slider component.
JavaScript
115
star
5

draftjs-to-markdown

JavaScript
62
star
6

GraphQL-Typescript-Starter

React Node GraphQL TypeScript - Starter Kit
TypeScript
44
star
7

react-component-stores

A simplified and powerful state management library for React applications.
JavaScript
21
star
8

react-simplified-boilerplate

A simplified boilerplate for my React projects
JavaScript
7
star
9

rustoli

Programming problems solved using Rust programming language
Rust
5
star
10

defi_yield_farming

DEFI yield farming dapp using chainlink price feeds
JavaScript
5
star
11

reason-places

A single page web application build using ReasonML
JavaScript
5
star
12

Solidity-Examples

Example solidity contracts
Solidity
3
star
13

solom

Articles about solidity
3
star
14

reason-tictactoe

Simple game of Tic Tac Toe made using ReasonML
OCaml
3
star
15

Election-DAPP

Election is simple DAPP which allow users to cast votes. It has some rules built on top of it like a user can cast only 1 vote, etc.
JavaScript
2
star
16

Runup

Runup token is implementation of ERC-20 token ground up (without using any library).
JavaScript
2
star
17

DaddaNFT

NFT minting
JavaScript
2
star
18

token_price_link

Obtaining latest token prices using chainlink blockchain
JavaScript
2
star
19

VR

VR and 2D Experiments
JavaScript
2
star
20

hub-poc

HTML
1
star
21

Learning-Reason

1
star
22

react-prosemirror

JavaScript
1
star
23

sanity-gatsby-blog

Blog with Gatsby
JavaScript
1
star
24

humble-site

CSS
1
star