API reference¶
HTML exporter¶
Combine entity, wrapper, and style state to render Draft.js content.
This is the main entry point for converting a Draft.js content state into an HTML string.
Source code in draftjs_exporter/html.py
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 | |
__init__(config=None)
¶
Initialize the exporter with the given configuration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
ExporterConfig | None
|
Exporter options. Missing values use the default block and style maps and the string DOM engine. |
None
|
Source code in draftjs_exporter/html.py
build_command_groups(block)
¶
Group block modification commands by start index.
Each group is paired with the slice of text the commands apply to.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
block
|
Block
|
The Draft.js block whose commands are grouped. |
required |
Returns:
| Type | Description |
|---|---|
list[tuple[str, list[Command]]]
|
Tuples of |
Source code in draftjs_exporter/html.py
build_commands(block)
¶
Build all manipulation commands for a block.
The returned commands include a start and stop text marker plus one command for each inline style and entity range.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
block
|
Block
|
The Draft.js block to build commands from. |
required |
Returns:
| Type | Description |
|---|---|
list[Command]
|
The ordered list of manipulation commands. |
Source code in draftjs_exporter/html.py
render(content_state=None)
¶
Render the given Draft.js content state as HTML.
Source code in draftjs_exporter/html.py
render_block(block, entity_map, wrapper_state, dom)
¶
Render a single block to an element.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
block
|
Block
|
The Draft.js block to render. |
required |
entity_map
|
EntityMap
|
Map of entity keys to entity definitions. |
required |
wrapper_state
|
WrapperState
|
Stateful wrapper that handles nesting of list items. |
required |
dom
|
type[DOMEngine]
|
Active DOM engine used to create elements. |
required |
Returns:
| Type | Description |
|---|---|
Element
|
The rendered block element. |
Source code in draftjs_exporter/html.py
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 | |
DOM¶
Provide a DOM-building API that abstracts the active engine.
Source code in draftjs_exporter/dom.py
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 | |
HTML5LIB = 'draftjs_exporter.engines.html5lib.DOM_HTML5LIB'
class-attribute
instance-attribute
¶
Identifier for the html5lib DOM engine.
LXML = 'draftjs_exporter.engines.lxml.DOM_LXML'
class-attribute
instance-attribute
¶
Identifier for the lxml DOM engine.
MARKDOWN = 'draftjs_exporter.engines.markdown.DOMMarkdown'
class-attribute
instance-attribute
¶
Identifier for the Markdown DOM engine.
STRING = 'draftjs_exporter.engines.string.DOMString'
class-attribute
instance-attribute
¶
Identifier for the string DOM engine.
STRING_COMPAT = 'draftjs_exporter.engines.string_compat.DOMStringCompat'
class-attribute
instance-attribute
¶
Identifier for the string compatibility DOM engine.
append_child(elt, child)
classmethod
¶
camel_to_dash(camel_cased_str)
staticmethod
¶
Convert a camelCase string to a dashed-case attribute name.
Source code in draftjs_exporter/dom.py
create_element(type_=None, props=None, *elt_children)
classmethod
¶
Create an element or document fragment using the current engine.
The signature mirrors React.createElement: a type, an optional props dictionary, and zero or more children.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
type_
|
RenderableType
|
The tag name, component, or |
None
|
props
|
Props | None
|
Attributes and metadata to pass to the element. |
None
|
*elt_children
|
Element | None
|
Child nodes to append to the element. |
()
|
Returns:
| Type | Description |
|---|---|
Element
|
The constructed element. |
Source code in draftjs_exporter/dom.py
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 | |
engine(engine)
staticmethod
¶
Temporarily set the DOM engine for the current context.
Source code in draftjs_exporter/dom.py
parse_html(markup)
classmethod
¶
render(elt)
classmethod
¶
render_debug(elt)
classmethod
¶
use(engine)
classmethod
¶
Select the DOM implementation for the current context.
Source code in draftjs_exporter/dom.py
Constants¶
Draft.js-compatible constants used by the exporter.
Defines the block types, inline styles, and entity categories supported by Draft.js, plus a small enum helper.
ENTITY_TYPES = Enum('LINK', 'DOCUMENT', 'IMAGE', 'EMBED', 'HORIZONTAL_RULE', 'FALLBACK')
module-attribute
¶
Draft.js entity categories.
INLINE_STYLES = Enum('BOLD', 'CODE', 'ITALIC', 'UNDERLINE', 'STRIKETHROUGH', 'SUPERSCRIPT', 'SUBSCRIPT', 'MARK', 'QUOTATION', 'SMALL', 'SAMPLE', 'INSERT', 'DELETE', 'KEYBOARD', 'FALLBACK')
module-attribute
¶
Draft.js inline style names.
BLOCK_TYPES
¶
Draft.js block types mapped to HTML elements.
Source code in draftjs_exporter/constants.py
Enum
¶
Minimal enum-like container exposing a fixed set of string attributes.
Accessing a registered attribute returns its name. Accessing any other
attribute raises AttributeError.
Source code in draftjs_exporter/constants.py
__getattr__(name)
¶
Return the attribute name if it is a registered enum element.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Attribute name being accessed. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The requested attribute name. |
Raises:
| Type | Description |
|---|---|
AttributeError
|
If the name is not a registered element. |
Source code in draftjs_exporter/constants.py
Default maps¶
Default block and style maps for the exporter.
The mappings connect Draft.js block types and inline styles to HTML tags or component functions.
BLOCK_MAP = {BLOCK_TYPES.UNSTYLED: 'p', BLOCK_TYPES.HEADER_ONE: 'h1', BLOCK_TYPES.HEADER_TWO: 'h2', BLOCK_TYPES.HEADER_THREE: 'h3', BLOCK_TYPES.HEADER_FOUR: 'h4', BLOCK_TYPES.HEADER_FIVE: 'h5', BLOCK_TYPES.HEADER_SIX: 'h6', BLOCK_TYPES.UNORDERED_LIST_ITEM: {'element': 'li', 'wrapper': 'ul'}, BLOCK_TYPES.ORDERED_LIST_ITEM: {'element': 'li', 'wrapper': 'ol'}, BLOCK_TYPES.BLOCKQUOTE: 'blockquote', BLOCK_TYPES.PRE: 'pre', BLOCK_TYPES.CODE: code_block, BLOCK_TYPES.ATOMIC: render_children}
module-attribute
¶
Default mapping from block types to renderable tags or components.
STYLE_MAP = {INLINE_STYLES.BOLD: 'strong', INLINE_STYLES.CODE: 'code', INLINE_STYLES.ITALIC: 'em', INLINE_STYLES.UNDERLINE: 'u', INLINE_STYLES.STRIKETHROUGH: 's', INLINE_STYLES.SUPERSCRIPT: 'sup', INLINE_STYLES.SUBSCRIPT: 'sub', INLINE_STYLES.MARK: 'mark', INLINE_STYLES.QUOTATION: 'q', INLINE_STYLES.SMALL: 'small', INLINE_STYLES.SAMPLE: 'samp', INLINE_STYLES.INSERT: 'ins', INLINE_STYLES.DELETE: 'del', INLINE_STYLES.KEYBOARD: 'kbd'}
module-attribute
¶
Default mapping from inline styles to HTML tags.
code_block(props)
¶
Render a code block inside a pre > code element tree.
Types¶
Shared type aliases and TypedDict structures for Draft.js data.
Use these types when annotating custom components, decorators, or configuration maps consumed by the exporter.
Component = Callable[[Props], Element]
module-attribute
¶
Render a component from props and return an element.
CompositeDecorators = list[Decorator]
module-attribute
¶
List composite decorators applied while rendering blocks.
ConfigMap = dict[str, RenderableConfig | RenderableType]
module-attribute
¶
Map string keys to renderable configurations or values.
Element = Any
module-attribute
¶
Engine-specific element produced by a renderable.
EntityKey = str
module-attribute
¶
Key used to look up an entity in the entity map.
EntityMap = dict[EntityKey, Entity]
module-attribute
¶
Map entity keys to their definitions.
HTML = str
module-attribute
¶
Final rendered output of the exporter.
Mutability = Literal['MUTABLE', 'IMMUTABLE', 'SEGMENTED']
module-attribute
¶
Draft.js entity mutability setting.
Props = dict[str, Any]
module-attribute
¶
Dictionary of string attribute keys to arbitrary values.
RenderableType = Component | Tag | None
module-attribute
¶
A tag name, component function, or None for a fragment.
Tag = str
module-attribute
¶
HTML tag name.
Block
¶
Bases: TypedDict
Single Draft.js block within a content state.
Source code in draftjs_exporter/types.py
ContentState
¶
Decorator
¶
Entity
¶
EntityRange
¶
InlineStyleRange
¶
RenderableConfig
¶
Bases: TypedDict
Configuration describing how to render a single renderable.
Source code in draftjs_exporter/types.py
Options¶
Normalize renderer configuration maps into internal option objects.
Options
¶
Store and query normalized configuration for a single renderable type.
Source code in draftjs_exporter/options.py
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 | |
__eq__(other)
¶
Compare options for equality.
This comparison is intended for test assertions only and should not be relied on by the exporter at runtime.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Any
|
The other object to compare with. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True when the string representations match. |
Source code in draftjs_exporter/options.py
__hash__()
¶
__init__(type_, element, props=None, wrapper=None, wrapper_props=None)
¶
Initialize options for a renderable type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
type_
|
str
|
The Draft.js type being configured. |
required |
element
|
RenderableType
|
The element or component used to render this type. |
required |
props
|
Props | None
|
Default props to pass to the element. |
None
|
wrapper
|
RenderableType
|
Optional wrapper element used for nested blocks. |
None
|
wrapper_props
|
Props | None
|
Props to pass to the wrapper element. |
None
|
Source code in draftjs_exporter/options.py
__ne__(other)
¶
__repr__()
¶
__str__()
¶
create(kind_map, type_, fallback_key)
staticmethod
¶
Create an Options object from a config map.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
kind_map
|
ConfigMap
|
The user-provided configuration map. |
required |
type_
|
str
|
The type to look up in the map. |
required |
fallback_key
|
str
|
The key to use when |
required |
Returns:
| Type | Description |
|---|---|
Options
|
The normalized options. |
Raises:
| Type | Description |
|---|---|
ConfigException
|
If the type and fallback are both missing or the config defines no element. |
Source code in draftjs_exporter/options.py
get(options, type_, fallback_key)
staticmethod
¶
Return existing options from a map, falling back when needed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
options
|
OptionsMap
|
An existing normalized options map. |
required |
type_
|
str
|
The type to look up. |
required |
fallback_key
|
str
|
The key to use when |
required |
Returns:
| Type | Description |
|---|---|
Options
|
The matching options. |
Raises:
| Type | Description |
|---|---|
ConfigException
|
If neither the type nor the fallback is present. |
Source code in draftjs_exporter/options.py
map(kind_map, fallback_key)
staticmethod
¶
Create an OptionsMap from each entry in a config map.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
kind_map
|
ConfigMap
|
The user-provided configuration map. |
required |
fallback_key
|
str
|
The fallback key passed to |
required |
Returns:
| Type | Description |
|---|---|
OptionsMap
|
A mapping from type to normalized options. |
Source code in draftjs_exporter/options.py
map_blocks(block_map)
staticmethod
¶
Create an OptionsMap from a block configuration map.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
block_map
|
ConfigMap
|
The user-provided block map. |
required |
Returns:
| Type | Description |
|---|---|
OptionsMap
|
A mapping from block type to normalized options. |
Source code in draftjs_exporter/options.py
map_entities(entity_map)
staticmethod
¶
Create an OptionsMap from an entity configuration map.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
entity_map
|
ConfigMap
|
The user-provided entity decorators map. |
required |
Returns:
| Type | Description |
|---|---|
OptionsMap
|
A mapping from entity type to normalized options. |
Source code in draftjs_exporter/options.py
map_styles(style_map)
staticmethod
¶
Create an OptionsMap from a style configuration map.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
style_map
|
ConfigMap
|
The user-provided style map. |
required |
Returns:
| Type | Description |
|---|---|
OptionsMap
|
A mapping from style name to normalized options. |
Source code in draftjs_exporter/options.py
Error¶
Custom exceptions raised by the exporter.
ConfigException
¶
Bases: ExporterException
Raised when the exporter configuration is invalid or unsupported.