Socket
Socket
Sign inDemoInstall

draft-js-html

Package Overview
Dependencies
23
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    draft-js-html

This repository is used to convert draft-js RawDraftContentState's into html content


Version published
Weekly downloads
114
decreased by-23.49%
Maintainers
1
Install size
12.3 kB
Created
Weekly downloads
 

Readme

Source

draft-js-html

This repository is used to convert draft-js RawDraftContentState's into html content

It supports custom inline styling, custom block styling and custom multi block style (multiple elements of the same type, for example custom 'ul', 'ol', codeblock of multiple blocks)

Function:

// Element interface:
export interface IElementStyle {
    element: string;
    properties?: {[key: string]: string};
}

// It will append 'properties' to the element
// Eg. '{element: "span", properties: {class: "color-red"}}'

// Types used as module parameters:
type InlineStyleCallback = (type: DraftInlineStyleType) => IElementStyle | void;
type BlockStyleCallback = (type: DraftBlockType) => IElementStyle | void;
type MultiBlockStyleCallback = (type: DraftBlockType) => IElementStyle | void;

// Exported function from this module:
convertDraftToHtml(rawContent: RawDraftContentState, customInlineStyleFn?: InlineStyleCallback, customBlockStyleFn?: BlockStyleCallback, customMultiBlockStyleFn?: MultiBlockStyleCallback)

Example usage:

convertDraftToHtml(rawContent); // Convert with default styles

convertDraftToHtml(rawContent, type => {
            if(type == "BOLD") return {element: "strong", properties: {class: "example", placeholder: "example", customProp: "this is a custom prop"}};
        }, type => {
            if(type == "unstyled") return {element: "span", properties: {class: "custom-unstyled"}};
        }, type => {
            if(type == "unordered-list-item") return {element: "ul"};
            if(type == "code-block") return {element: "code"};
        });

Defaults

  • default applied styles without having to provide them yourself with custom styles, if you do not like them you can override them by returning something with customStyles
    • InlineStyles:
      • BOLD => {element: 'strong'},
      • ITALIC => {element: 'i'},
      • UNDERLINE => {element: 'u'}
    • BlockStyles:
      • unstyled / paragraph => {element: 'p'},
      • header-one => {element: 'h1'},
      • header-two => {element: 'h2'},
      • header-three => {element: 'h3'},
      • header-four => {element: 'h4'},
      • header-five => {element: 'h5'},
      • header-six => {element: 'h6'},
      • ordered-list-item / unordered-list-item => {element: 'li'},
      • blockquote => {element: 'blockquote'},
      • code-block => {element: 'pre'}
    • MultiBlockStyles:
      • ordered-list-item => {element: 'ol'},
      • unordered-list-item => {element: 'ul'}

Coming soon: Conversion from HTML to RawDraftContentState

Keywords

FAQs

Last updated on 05 Mar 2020

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc