{"id":2011,"library":"draftjs-exporter","title":"Draft.js Exporter","description":"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.","status":"active","version":"5.2.0","language":"en","source_language":"en","source_url":"https://github.com/springload/draftjs_exporter","tags":["draftjs","html","richtext","editor","frontend","backend"],"install":[{"cmd":"pip install draftjs-exporter","lang":"bash","label":"Basic Install"},{"cmd":"pip install draftjs-exporter[html5lib]","lang":"bash","label":"Install with html5lib engine"},{"cmd":"pip install draftjs-exporter[lxml]","lang":"bash","label":"Install with lxml engine (requires libxml2/libxslt)"}],"dependencies":[{"reason":"Optional backend for HTML parsing/rendering with better sanitization.","package":"html5lib","optional":true},{"reason":"Optional backend for HTML parsing/rendering, typically faster. Requires system libraries `libxml2` and `libxslt`.","package":"lxml","optional":true}],"imports":[{"note":"The main class for configuring and rendering Draft.js ContentState to HTML.","symbol":"HTML","correct":"from draftjs_exporter.html import HTML"},{"note":"Used for accessing DOM engine constants (e.g., DOM.HTML5LIB, DOM.LXML, DOM.STRING_COMPAT) and creating elements in custom components.","symbol":"DOM","correct":"from draftjs_exporter.dom import DOM"}],"quickstart":{"code":"from draftjs_exporter.dom import DOM\nfrom draftjs_exporter.html import HTML\n\n# Example Draft.js ContentState\ncontent_state = {\n    'entityMap': {},\n    'blocks': [\n        {\n            'key': '6mgfh',\n            'text': 'Hello, world!',\n            'type': 'unstyled',\n            'depth': 0,\n            'inlineStyleRanges': [],\n            'entityRanges': []\n        }\n    ]\n}\n\n# Configuration options (can be customized)\nconfig = {\n    # Example: use the string_compat engine for consistent output\n    'engine': DOM.STRING_COMPAT\n}\n\n# Initialize the exporter\nexporter = HTML(config)\n\n# Render the ContentState to HTML\nhtml_output = exporter.render(content_state)\nprint(html_output)\n# Expected output: '<p>Hello, world!</p>'","lang":"python","description":"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."},"warnings":[{"fix":"Upgrade your Python environment to 3.6+ (preferably 3.10+) or pin `draftjs-exporter` to a compatible version.","message":"Removed 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+.","severity":"breaking","affected_versions":"< 5.0.0, < 4.0.0, < 3.0.0"},{"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.","message":"The default string engine in v4.0.0 removed HTML attributes alphabetical sorting and disabled single/double quotes escaping outside of attributes.","severity":"breaking","affected_versions":"4.0.0+"},{"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.","message":"The `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.","severity":"gotcha","affected_versions":"All"},{"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`.","message":"The 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.","severity":"gotcha","affected_versions":"All"},{"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.","message":"Version 5.0.0 introduced a new `string_compat` engine. If precise, backward-compatible HTML output is critical, this engine is recommended.","severity":"gotcha","affected_versions":"5.0.0+"}],"env_vars":null,"last_verified":"2026-04-09T00:00:00.000Z","next_check":"2026-07-08T00:00:00.000Z"}