# @mathieumg/draft-js-export-html

> DraftJS: Export ContentState to HTML

Latest version **0.5.3** (published 2017-06-28) · ISC license · 0 weekly downloads

## Install

```sh
npm install @mathieumg/draft-js-export-html
pnpm add @mathieumg/draft-js-export-html
yarn add @mathieumg/draft-js-export-html
bun add @mathieumg/draft-js-export-html
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.5.3 |
| Published | 2017-06-28 |
| First published | 2017-06-28 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 1 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 881 |
| Author | sstur@me.com |
| Maintainers | mathieumg |
| Keywords | draft-js, export-html |

## Links

- npm: https://www.npmjs.com/package/@mathieumg/draft-js-export-html
- Repository: https://github.com/sstur/draft-js-export-html
- Homepage: https://github.com/sstur/draft-js-export-html#readme
- Issues: https://github.com/sstur/draft-js-export-html/issues
- npm.io page: https://npm.io/package/@mathieumg/draft-js-export-html

## Dependencies (1)

- [draft-js-utils](https://npm.io/package/draft-js-utils.md) ^0.1.5

## Alternatives

- [@tsparticles/shape-image](https://npm.io/package/@tsparticles/shape-image.md) — 303.7K weekly downloads
- [@tsparticles/shape-line](https://npm.io/package/@tsparticles/shape-line.md) — 233.7K weekly downloads
- [stringify-attributes](https://npm.io/package/stringify-attributes.md) — 58.6K weekly downloads
- [mobile-drag-drop](https://npm.io/package/mobile-drag-drop.md) — 46.3K weekly downloads
- [@comunica/actor-rdf-parse-html](https://npm.io/package/@comunica/actor-rdf-parse-html.md) — 29.2K weekly downloads

## Recent versions

- 0.5.3 (latest) — 2017-06-28

## README

# DraftJS: Export ContentState to HTML

This is a module for [DraftJS](https://github.com/facebook/draft-js) that will export your editor content to semantic HTML.

It was extracted from [React-RTE](https://react-rte.org) and placed into a separate module for more general use. Hopefully it can be helpful in your projects.

## Installation

    npm install --save draft-js-export-html

## How to Use

```javascript
import {stateToHTML} from 'draft-js-export-html';
let html = stateToHTML(contentState);
```

## Options

You can optionally pass a second "options" argument to `stateToHTML` which should be an object with one or more of the following properties:

### `inlineStyles`

You can define rendering options for inline styles. This applies to built-in inline styles (e.g. `BOLD`) or your own custom inline styles (e.g. `RED`). You can specify which element/tag name will be used (e.g. use `<b>` instead of `<strong>` for `BOLD`). You can add custom attributes (e.g. `class="foo"`) or add some styling (e.g. `color: red`).

Example:

```javascript
let options = {
  inlineStyles: {
    // Override default element (`strong`).
    BOLD: {element: 'b'},
    ITALIC: {
      // Add custom attributes. You can also use React-style `className`.
      attributes: {class: 'foo'},
      // Use camel-case. Units (`px`) will be added where necessary.
      style: {fontSize: 12}
    },
    // Use a custom inline style. Default element is `span`.
    RED: {style: {color: '#900'}},
  },
};
let html = stateToHTML(contentState, options);
```

### `blockRenderers`

You can define a custom renderer for any block type. Pass a function that accepts `block` as an argument. You can return a string to render this block yourself, or return nothing (null or undefined) to defer to the default renderer.

Example:

```javascript
let options = {
  blockRenderers: {
    atomic: (block) => {
      let data = block.getData();
      if (data.get('foo') === 'bar') {
        return '<div>' + escape(block.getText()) + '</div>';
      }
    },
  },
};
let html = stateToHTML(contentState, options);
```

### `blockStyleFn`

You can define custom styles and attributes for your block, utilizing the underlying built-in rendering logic of the tags, but adding your own attributes or styles on top. The `blockStyleFn` option takes a block and returns an Object similar to `inlineStyles` of the following signature or null:

```js
{
  attributes: {}
  style: {}
}
```

Example:
```js
let options = {
  blockStyleFn(block) => {
    if (block.getData().get('color')) {
      return {
        style: {
          color: block.getData().get('color')
        }
      }
    }
  }
}
let html = stateToHTML(contentState, options);
```

## Contributing

If you want to help out, please open an issue to discuss or join us on [Slack](https://draftjs.herokuapp.com/).

## License

This software is [BSD Licensed](/LICENSE).

---
_Source: https://npm.io/package/@mathieumg/draft-js-export-html · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
