Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

draft-js-export-html

Package Overview
Dependencies
Maintainers
4
Versions
34
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

draft-js-export-html

DraftJS: Export ContentState to HTML

  • 1.4.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
35K
decreased by-76.69%
Maintainers
4
Weekly downloads
 
Created

What is draft-js-export-html?

The draft-js-export-html package is a utility for converting Draft.js editor content to HTML. It provides a straightforward way to transform the content state of a Draft.js editor into an HTML string, making it useful for rendering rich text content in web applications.

What are draft-js-export-html's main functionalities?

Basic Conversion

This feature allows you to convert the content state of a Draft.js editor to an HTML string. The code sample demonstrates how to convert a simple raw content state to HTML.

const { stateToHTML } = require('draft-js-export-html');
const { EditorState, ContentState, convertFromRaw } = require('draft-js');

const rawContent = {
  blocks: [
    {
      text: 'Hello, world!',
      type: 'unstyled'
    }
  ],
  entityMap: {}
};

const contentState = convertFromRaw(rawContent);
const editorState = EditorState.createWithContent(contentState);
const html = stateToHTML(editorState.getCurrentContent());
console.log(html); // Outputs: <p>Hello, world!</p>

Custom Style Map

This feature allows you to define a custom style map for inline styles. The code sample shows how to convert content with bold and italic styles to HTML using custom HTML elements.

const { stateToHTML } = require('draft-js-export-html');
const { EditorState, ContentState, convertFromRaw } = require('draft-js');

const rawContent = {
  blocks: [
    {
      text: 'Bold and Italic text',
      type: 'unstyled',
      inlineStyleRanges: [
        { offset: 0, length: 4, style: 'BOLD' },
        { offset: 9, length: 6, style: 'ITALIC' }
      ]
    }
  ],
  entityMap: {}
};

const contentState = convertFromRaw(rawContent);
const editorState = EditorState.createWithContent(contentState);
const customStyleMap = {
  BOLD: { element: 'strong' },
  ITALIC: { element: 'em' }
};
const html = stateToHTML(editorState.getCurrentContent(), { inlineStyles: customStyleMap });
console.log(html); // Outputs: <p><strong>Bold</strong> and <em>Italic</em> text</p>

Custom Block Rendering

This feature allows you to define custom rendering for different block types. The code sample demonstrates how to render a header block as an <h1> element.

const { stateToHTML } = require('draft-js-export-html');
const { EditorState, ContentState, convertFromRaw } = require('draft-js');

const rawContent = {
  blocks: [
    {
      text: 'Header text',
      type: 'header-one'
    }
  ],
  entityMap: {}
};

const contentState = convertFromRaw(rawContent);
const editorState = EditorState.createWithContent(contentState);
const blockRenderers = {
  'header-one': (block) => `<h1>${block.getText()}</h1>`
};
const html = stateToHTML(editorState.getCurrentContent(), { blockRenderers });
console.log(html); // Outputs: <h1>Header text</h1>

Other packages similar to draft-js-export-html

Keywords

FAQs

Package last updated on 30 Jul 2019

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc