Draft.js Exporter

5.2.0 · active · verified Thu Apr 09

draftjs-exporter is a Python library designed to convert rich text from Draft.js raw ContentState, a JSON representation used by the React-based rich text editor, into HTML. It is actively maintained by Springload, currently at version 5.2.0, and regularly releases updates to support newer Python versions and introduce features or performance improvements.

Warnings

Install

Imports

Quickstart

Initialise the `HTML` exporter with a configuration dictionary (often using `DOM` constants for the engine), then call its `render` method with a Draft.js `ContentState` object. The `ContentState` is a JSON-like dictionary describing the rich text structure.

from draftjs_exporter.dom import DOM
from draftjs_exporter.html import HTML

# Example Draft.js ContentState
content_state = {
    'entityMap': {},
    'blocks': [
        {
            'key': '6mgfh',
            'text': 'Hello, world!',
            'type': 'unstyled',
            'depth': 0,
            'inlineStyleRanges': [],
            'entityRanges': []
        }
    ]
}

# Configuration options (can be customized)
config = {
    # Example: use the string_compat engine for consistent output
    'engine': DOM.STRING_COMPAT
}

# Initialize the exporter
exporter = HTML(config)

# Render the ContentState to HTML
html_output = exporter.render(content_state)
print(html_output)
# Expected output: '<p>Hello, world!</p>'

view raw JSON →