🚀 DAY 5 OF LAUNCH WEEK: Introducing Socket Firewall Enterprise.Learn more →
Socket
Book a DemoInstallSign in
Socket

draftjs-to-html

Package Overview
Dependencies
Maintainers
1
Versions
55
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

draftjs-to-html

A library for draftjs to html conversion.

Source
npmnpm
Version
0.6.0
Version published
Weekly downloads
214K
-2.94%
Maintainers
1
Weekly downloads
 
Created
Source

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.

Installing

npm install draftjs-to-html

Using

import draftToHtml from 'draftjs-to-html';

const rawContentState = convertToRaw(editorState.getCurrentContent());
const markup = draftToHtml(contentState, directional, customEntityTransform);

Supported conversions

Following is the list of conversions it supports:

  • Convert block types to corresponding HTML tags:
Block TypeHTML Tag
1header-oneh1
2header-twoh2
3header-threeh3
4header-fourh4
5header-fiveh5
6header-sixh6
7unordered-list-itemul
8ordered-list-itemol
9blockquoteblockquote
10unstyledp

It performs these additional changes to text of blocks: - replace blank space in beginning and end of block with &nbsp; - replace \n with <br>\n - replace < with &lt; - replace > with &gt;

  • Converts ordered and unordered list blocks with depths to nested structure of <ul>, <ol> and <li>.

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

  • 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 should start with strings color or font-size like color-red, color-green or fontsize-12, fontsize-20.

  • Converts entity range of type link to anchor tag using entity data url for href: <a href="url" />.

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

  • Converts atomic entity image to image tag using entity data src for image source: <img src="src" />.

  • Converts embedded links to iFrames.

  • customEntityTransform can be used for transformation of a custom entity block to html.

  • Adding style property to block tag for block level styles like text-align: <p style="text-align: right">text</p>.

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

License

MIT.

FAQs

Package last updated on 05 Feb 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts