An open API service indexing awesome lists of open source software.

https://github.com/amr258144/draft-js-html-converter

A lightweight, zero-dependency library for converting between Draft.js rich text format and HTML
https://github.com/amr258144/draft-js-html-converter

bidirectional draft-js draftjs-to-html editor html javascript npm-package rich-text typescript zero-dependencies

Last synced: 16 days ago
JSON representation

A lightweight, zero-dependency library for converting between Draft.js rich text format and HTML

Awesome Lists containing this project

README

          

# Draft.js HTML Converter

[![npm version](https://badge.fury.io/js/draft-js-html-converter.svg)](https://www.npmjs.com/package/draft-js-html-converter)
[![GitHub license](https://img.shields.io/github/license/amr258144/draft-js-html-converter.svg)](https://github.com/amr258144/draft-js-html-converter/blob/main/LICENSE)
[![GitHub issues](https://img.shields.io/github/issues/amr258144/draft-js-html-converter.svg)](https://github.com/amr258144/draft-js-html-converter/issues)
[![GitHub stars](https://img.shields.io/github/stars/amr258144/draft-js-html-converter.svg)](https://github.com/amr258144/draft-js-html-converter/stargazers)

A lightweight, zero-dependency library for converting between Draft.js rich text format and HTML.

## Table of Contents

- [Why Choose This Library?](#why-choose-this-library)
- [Installation](#installation)
- [Quick Start](#quick-start)
- [Features](#features)
- [Usage](#usage)
- [API Reference](#api-reference)
- [Supported Features](#supported-features)
- [Examples](#examples)
- [Error Handling](#error-handling)
- [Browser Support](#browser-support)
- [Testing](#testing)
- [Troubleshooting](#troubleshooting)
- [Contributing](#contributing)
- [Related Projects](#related-projects)
- [Changelog](#changelog)
- [License](#license)

## Why Choose This Library?

- ๐Ÿš€ **Zero dependencies** - No bloat, just pure JavaScript
- ๐Ÿ“ฆ **Lightweight** - Less than 15KB minified
- โšก **Fast** - Optimized for performance
- ๐Ÿ”„ **Bidirectional** - Perfect round-trip conversion
- ๐Ÿงช **Well tested** - 95%+ test coverage
- ๐Ÿ“˜ **TypeScript ready** - Full type definitions included

## Installation

```bash
# npm
npm install draft-js-html-converter

# yarn
yarn add draft-js-html-converter

# pnpm
pnpm add draft-js-html-converter
```

## Quick Start

```javascript
const { convertRichTextToHtml, convertHtmlToDraftJs } = require('draft-js-html-converter');

// Draft.js to HTML
const html = convertRichTextToHtml(draftContent);

// HTML to Draft.js
const draftContent = convertHtmlToDraftJs('

Hello World

');
```

## Features

- โœ… Convert Draft.js content to clean HTML
- โœ… Convert HTML back to Draft.js format
- โœ… Support for all common formatting (bold, italic, underline, headers, lists, links, colors, font sizes)
- โœ… Proper handling of nested styles and entities
- โœ… Text alignment support
- โœ… Zero dependencies
- โœ… TypeScript support
- โœ… Comprehensive test coverage
- โœ… Round-trip conversion support

## Usage

### Converting Draft.js to HTML

```javascript
const { convertRichTextToHtml } = require('draft-js-html-converter');

const draftContent = {
blocks: [
{
key: 'abc123',
data: {},
text: 'Hello World',
type: 'unstyled',
depth: 0,
entityRanges: [],
inlineStyleRanges: [
{ style: 'BOLD', offset: 0, length: 5 }
]
}
],
entityMap: {}
};

const html = convertRichTextToHtml(draftContent);
console.log(html); // '

Hello World

'
```

### Converting HTML to Draft.js

```javascript
const { convertHtmlToDraftJs } = require('draft-js-html-converter');

const html = '

Hello World

';
const draftContent = convertHtmlToDraftJs(html);

console.log(draftContent);
// {
// blocks: [
// {
// key: 'generated-key',
// data: {},
// text: 'Hello World',
// type: 'unstyled',
// depth: 0,
// entityRanges: [],
// inlineStyleRanges: [
// { style: 'BOLD', offset: 0, length: 5 }
// ]
// }
// ],
// entityMap: {}
// }
```

### ES6 Imports

```javascript
import { convertRichTextToHtml, convertHtmlToDraftJs } from 'draft-js-html-converter';

// or

import converter from 'draft-js-html-converter';
const html = converter.convertRichTextToHtml(draftContent);
```

## API Reference

### convertRichTextToHtml(richTextContent)

Converts Draft.js content to HTML string.

**Parameters:**
- `richTextContent` (Object): Draft.js content state object

**Returns:**
- `string`: HTML representation of the content

### convertHtmlToDraftJs(htmlContent)

Converts HTML string to Draft.js content format.

**Parameters:**
- `htmlContent` (string): HTML content to convert

**Returns:**
- `Object`: Draft.js content state object with `blocks` and `entityMap`

## Supported Features

### Block Types
- `unstyled` โ†’ `

`
- `header-one` โ†’ `

`
- `header-two` โ†’ `

`
- `header-three` โ†’ `

`
- `unordered-list-item` โ†’ `