0.9.1 • Published 4 years ago

draftjs-to-html v0.9.1

Weekly downloads
110,662
License
MIT
Repository
github
Last release
4 years ago

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.

Installation

npm install draftjs-to-html

Usage

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:

  1. contentState: Its instance of RawDraftContentState

  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 DraftJS EditorState.

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;
  1. Converts ordered and unordered list blocks with depths to nested structure of <ul>, <ol> and <li>.

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

  3. 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).

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

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

  6. 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"/>.

  7. Converts embedded links to iFrames, using width, height and src from entity data. <iframe width="width" height="height" src="src" frameBorder="0"></iframe>

  8. Converts hashtags to anchor tag: <a href="#tag" class="wysiwyg-hashtag">#tag</a>.

  9. 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.
  10. Adding style property to block tag for block level styles like text-align: <p style="text-align: right">text</p>.

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

License

MIT.

nawadata-web-testing@mailupinc.2012/bee-plugin-bundleddependenciestarnet-jumbo-reactk2.n3osmt-form-builder@cepres/uikitagent-interfacejl-react-formspcx-react-controlsscm-uivib-libraryclass-note-editor-moduletalent-app-shellbitsy-form-buildercaec-admin-webluna-formstxstate-react@xanda/react-componentshotee-adminquickbuilderreact-ui-form-buildershubham-form-builderpc_esignatureui@cloudhub-ux/coredemandscience-editorcognitiveleap-corecognitiveleap-core-usarms_v2.8_webui@infinitebrahmanuniverse/nolb-drafsupport-worksuiterect-components-007@everything-registry/sub-chunk-1513react-components-007@devphenomenon/react-form-builder-2ui-food2cdogma-gantt-uivv-react-formsweb-ui-depsvuexy-react-admin-dashboardwordpress-rest-adminwso2_apim_publisherth-react-formbuilderth-react-formbuilder1th-react-formbuilder2th-react-frombuilder2test_hamar_01test_ui_kittestmodule-carlosyangtruthy-react-cmsttk-app-corettk-componentuse-editor-draftjsunravelauunravelau_webpackv5gl-form-builderubidy_ui_kitveskos-tree@innovaccer/rich-text-editorsyed.hussainta-formbuildertarnet-uitcfw-showcase-editshowcase-editshopism-emails-messagessmedia-react-crudsmt-buildersm-react-crudsolution-form-buildersourcenet_schoolsp-react-formfieldssp_widget_editorstaple-form-buildersterlingdatatable@moamen.youssef/my-react-form-builder@olegrumiancev/sp-react-formfields@mintlab/ui@mirrormedia/html-draft-editor@mkljczk/react-form-builder@onextech/react-hocs@mapgear/geoapps-ui-framework@microduino/micdesign@ornio/uwork-app-core@modernsoftwares/adhi-research-platform@modernsoftwares/kids-study-research-platformz-panel@jvenusaur/react-form-builder2-tailwindcss@jujubes/ui-react@majoo-ui/react@mahdiaskary/react-form-builder2@newageerp/v3.form.rich-editor-draftreact-cssd900@odecloud/ui@saber2pr/react-richtext@react-form-fields/editor@santiliaudat/react-form-builder@plugandwork/mails@plugandwork/notes@notsanchez/notsanchez-react-form-builder2@tabtabgo/web@verevinds/ui-kit
0.9.1

4 years ago

0.9.0

4 years ago

0.8.4

6 years ago

0.8.3

6 years ago

0.8.2

6 years ago

0.8.1

6 years ago

0.8.0

6 years ago

0.7.6

7 years ago

0.7.5

7 years ago

0.7.4

7 years ago

0.7.3

7 years ago

0.7.2

7 years ago

0.7.1

7 years ago

0.7.0

7 years ago

0.6.3

7 years ago

0.6.2

7 years ago

0.6.1

7 years ago

0.6.0

7 years ago

0.5.1

7 years ago

0.5.0

7 years ago

0.4.5

7 years ago

0.4.4

7 years ago

0.4.3

7 years ago

0.4.2

7 years ago

0.4.1

7 years ago

0.4.0

7 years ago

0.3.9

7 years ago

0.3.8

7 years ago

0.3.7

7 years ago

0.3.6

7 years ago

0.3.5

7 years ago

0.3.4

7 years ago

0.3.3

7 years ago

0.3.2

7 years ago

0.3.1

8 years ago

0.3.0

8 years ago

0.2.8

8 years ago

0.2.7

8 years ago

0.2.6

8 years ago

0.2.5

8 years ago

0.2.4

8 years ago

0.2.3

8 years ago

0.2.2

8 years ago

0.2.1

8 years ago

0.2.0

8 years ago

0.1.9

8 years ago

0.1.8

8 years ago

0.1.7

8 years ago

0.1.6

8 years ago

0.1.5

8 years ago

0.1.4

8 years ago

0.1.3

8 years ago

0.1.2

8 years ago

0.1.1

8 years ago

0.1.0

8 years ago