Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Library for converting Draftjs editor content state to HTML

License

NotificationsYou must be signed in to change notification settings

jpuri/draftjs-to-html

Repository files navigation

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';importdraftToHtmlfrom'draftjs-to-html';constrawContentState=convertToRaw(editorState.getCurrentContent());constmarkup=draftToHtml(rawContentState,hashtagConfig,directional,customEntityTransform);

The function parameters are:

  1. contentState: Its instance ofRawDraftContentState

  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 DraftJSEditorState.

Supported conversions

Following is the list of conversions it supports:

  1. 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
    10codepre
    11unstyledp

    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>. (The inline styles in JSON object should start with stringscolor orfont-size likecolor-red,color-green orfontsize-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">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" 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"/>.

  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">#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>text</p>.

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

License

MIT.

About

Library for converting Draftjs editor content state to HTML

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors16


[8]ページ先頭

©2009-2025 Movatter.jp