![Maven Central Adds Sigstore Signature Validation](https://cdn.sanity.io/images/cgdhsj6q/production/7da3bc8a946cfb5df15d7fcf49767faedc72b483-1024x1024.webp?w=400&fit=max&auto=format)
Security News
Maven Central Adds Sigstore Signature Validation
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.
draftjs-to-html
Advanced tools
The draftjs-to-html npm package is a utility that converts Draft.js editor content to HTML. It is particularly useful for applications that use Draft.js for rich text editing and need to render the content as HTML for display or storage.
Convert Draft.js ContentState to HTML
This feature allows you to convert the Draft.js ContentState to HTML. The code sample demonstrates how to get the current content from the editor state, convert it to a raw content state, and then convert it to HTML using the draftjs-to-html package.
const draftToHtml = require('draftjs-to-html');
const { convertToRaw } = require('draft-js');
const contentState = editorState.getCurrentContent();
const rawContentState = convertToRaw(contentState);
const html = draftToHtml(rawContentState);
console.log(html);
Custom Block Rendering
This feature allows you to define custom rendering for specific block types. The code sample shows how to render 'atomic' blocks with a custom HTML structure.
const draftToHtml = require('draftjs-to-html');
const { convertToRaw } = require('draft-js');
const contentState = editorState.getCurrentContent();
const rawContentState = convertToRaw(contentState);
const html = draftToHtml(rawContentState, {
blockRenderers: {
'atomic': (block) => `<div class='custom-atomic'>${block.text}</div>`
}
});
console.log(html);
Custom Inline Style Rendering
This feature allows you to define custom rendering for inline styles. The code sample demonstrates how to render 'BOLD' styles as <strong> tags.
const draftToHtml = require('draftjs-to-html');
const { convertToRaw } = require('draft-js');
const contentState = editorState.getCurrentContent();
const rawContentState = convertToRaw(contentState);
const html = draftToHtml(rawContentState, {
styleToHTML: (style) => {
if (style === 'BOLD') {
return <strong />;
}
return null;
}
});
console.log(html);
The draft-js-export-html package provides similar functionality to draftjs-to-html by converting Draft.js ContentState to HTML. It offers more customization options for block and inline style rendering, making it a robust alternative.
The draft-convert package is another alternative that provides utilities for converting Draft.js ContentState to and from HTML. It offers a flexible API for defining custom block and inline style conversions, making it a versatile choice for developers.
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.
npm install draftjs-to-html
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:
contentState: Its instance of RawDraftContentState
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.
directional: Boolean, if directional is true text is aligned according to bidi algorithm. This is also optional.
customEntityTransform: Its function to render custom defined entities by user, its also optional.
editorState is instance of DraftJS EditorState.
Following is the list of conversions it supports:
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:
\n
with <br>
<
with <
>
with >
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 in JSON object 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, targetOption for target: <a href="url" target="_self">text</a>
. Default target is _self
.
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>
.
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"/>
.
Converts embedded links to iFrames, using width, height and src from entity data. <iframe width="width" height="height" src="src" frameBorder="0"></iframe>
Converts hashtags to anchor tag: <a href="#tag" class="wysiwyg-hashtag">#tag</a>
.
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:
entity
( object with { type, mutalibity, data})text
text present in the block.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.
MIT.
FAQs
A library for draftjs to html conversion.
The npm package draftjs-to-html receives a total of 0 weekly downloads. As such, draftjs-to-html popularity was classified as not popular.
We found that draftjs-to-html demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.
Security News
CISOs are racing to adopt AI for cybersecurity, but hurdles in budgets and governance may leave some falling behind in the fight against cyber threats.
Research
Security News
Socket researchers uncovered a backdoored typosquat of BoltDB in the Go ecosystem, exploiting Go Module Proxy caching to persist undetected for years.