0.1.12 • Published 3 years ago

braft-pure-convert v0.1.12

Weekly downloads
2
License
MIT
Repository
github
Last release
3 years ago

Braft Pure Convert

A library for converting Braft Editor content(also DraftJs Raw Content) to plain HTML with Braft styles.

No dependencies, easy to under any javascript frameworks, not limited at React.

This project is inspired by draftjs-to-html, I forked it to support braft styles.

Installation

npm install braft-pure-convert

Usage

import braftPureConvert from 'braft-pure-convert';

const rawContentState = this.editorState.getContent();

const markup = draftToHtml({
  rawState: rawContentState,
  hashtagConfig: 
  directional: false,
  extendAtomicTransform: function (atomic) {}
});

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.

0.1.11

3 years ago

0.1.12

3 years ago

0.1.10

3 years ago

0.1.9

5 years ago

0.1.8

5 years ago

0.1.7

6 years ago

0.1.6

6 years ago

0.1.5

6 years ago

0.1.4

6 years ago

0.1.3

6 years ago

0.1.2

6 years ago

0.1.1

6 years ago

0.1.0

6 years ago