Registry / web-framework / draftjs-exporter

draftjs-exporter

JSON →
library7.1.0pypypi✓ verified 25d ago

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.

pip install draftjs-exporter
INSTALL
IMPORT
SIG · DRAFTJS-EXPORTER
D
draftjs-exporter
web-frameworkpythonv7.1.0
Install
1.9s avg
Import
55ms
Disk
28MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v7.1.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.915 runs
installs and imports cleanly · install 0.0s · import 0.057s · 30.3MB
glibc
py 3.103.915 runs
installs and imports cleanly · install 1.9s · import 0.053s · 31MB
28MB installed
● package 28MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

HTML
from draftjs_exporter.html import HTML
The main class for configuring and rendering Draft.js ContentState to HTML.
DOM
from draftjs_exporter.dom import DOM
Used for accessing DOM engine constants (e.g., DOM.HTML5LIB, DOM.LXML, DOM.STRING_COMPAT) and creating elements in custom components.

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>'
Debug
Known issues
breakingRemoved support for older Python versions. v5.0.0+ requires Python 3.6+, v4.0.0+ requires Python 3.5+, v3.0.0+ requires Python 3.4+.
fix
Upgrade your Python environment to 3.6+ (preferably 3.10+) or pin `draftjs-exporter` to a compatible version.
affects: < 5.0.0, < 4.0.0, < 3.0.0
breakingThe default string engine in v4.0.0 removed HTML attributes alphabetical sorting and disabled single/double quotes escaping outside of attributes.
fix
If your application relied on the exact output format of the string engine prior to v4.0.0, you might need to adjust expectations or consider the `string_compat` engine introduced in v5.0.0 for maximum output stability.
affects: 4.0.0+
gotchaThe `parse_html` method (available via `DOMEngine` implementations) does not sanitize its input. Directly using user-provided HTML with this method can lead to XSS vulnerabilities.
fix
Always sanitize any HTML passed to `parse_html` from untrusted sources before using it with `draftjs-exporter`. The `html5lib` and `lxml` engines offer better parsing and sanitization capabilities than the default `string` engine, but `parse_html` remains a direct HTML parsing function.
affects: All
gotchaThe default 'string' engine is fast and dependency-free but offers no HTML sanitization. For robust handling of potentially unsafe HTML, especially when dealing with user-generated content or arbitrary HTML, consider using the `html5lib` or `lxml` engines.
fix
Install `draftjs-exporter[html5lib]` or `draftjs-exporter[lxml]` and set the `engine` configuration property to `DOM.HTML5LIB` or `DOM.LXML` respectively. Note that `lxml` requires system libraries `libxml2` and `libxslt`.
affects: All
gotchaVersion 5.0.0 introduced a new `string_compat` engine. If precise, backward-compatible HTML output is critical, this engine is recommended.
fix
Set the exporter's `engine` property to `DOM.STRING_COMPAT` in your configuration: `config = {'engine': DOM.STRING_COMPAT}`. This ensures consistent output matching its first release.
affects: 5.0.0+
Errors
Common errors & fixes
KeyError: '<block_type_or_entity_type_name>'
The Draft.js ContentState contains a block type (e.g., 'header-one', 'atomic') or an entity type (e.g., 'IMAGE', 'LINK') that has not been defined or mapped in the exporter's configuration (block_map or entity_decorators).
fix
Ensure all block types and entity types present in your Draft.js ContentState are explicitly defined in the `block_map` or `entity_decorators` when initializing the `HTML` exporter. For example, if you have 'header-one' blocks, add `'header-one': {'element': 'h1'}` to your `block_map`.
AttributeError: 'NoneType' object has no attribute 'get'
This typically occurs when a part of the input Draft.js ContentState JSON is missing or `None`, and the exporter, or a custom component/decorator, attempts to access an attribute or key from it as if it were a dictionary or object.
fix
Validate that the `contentState` JSON passed to the exporter is complete and well-formed. When writing custom components or accessing nested data, use `dict.get('key_name', default_value)` to safely handle potentially missing keys or `None` values.
ModuleNotFoundError: No module named 'draftjs_exporter'
The `draftjs-exporter` library has not been installed in your Python environment, or there is a typo in the import statement, or your Python environment's paths are not correctly configured.
fix
Install the library using pip: `pip install draftjs_exporter`. Verify that your import statements are correct, for example: `from draftjs_exporter.html import HTML` and `from draftjs_exporter.dom import DOM`.
TypeError: '<component_function_name>' object is not callable
A custom component function provided in the exporter's configuration for blocks, inline styles, or entities is either not defined as a callable Python function, or it's incorrectly structured.
fix
Ensure that any custom component passed to the exporter (e.g., in `block_map`, `style_map`, `entity_decorators`) is a properly defined Python function that accepts `props` as its argument and returns a DOM element created using `DOM.create_element`.
Upgrade
Version history
7.1.0latest on PyPI · released Aug 13, 2026
Audit
Dependencies
html5liboptionalOptional backend for HTML parsing/rendering with better sanitization.
lxmloptionalOptional backend for HTML parsing/rendering, typically faster. Requires system libraries `libxml2` and `libxslt`.
Agent activity
11 hits · last 30 days
node
10
Resources
draftjs-exporter — pip install draftjs-exporter · libregistry