`:
```python
BLOCK_TYPES.UNORDERED_LIST_ITEM: {
'element': 'li',
'wrapper': 'ul',
'wrapper_props': {'class': 'bullet-list'},
},
```
For more flexibility — reading block data or depth — point the `element` at a [custom component](https://wagtail.github.io/draftjs_exporter/guides/custom-components/index.md) instead of a tag name:
```python
BLOCK_TYPES.BLOCKQUOTE: blockquote,
BLOCK_TYPES.ORDERED_LIST_ITEM: {
'element': list_item,
'wrapper': ordered_list,
},
```
## Style map
`style_map` defines the HTML representation of inline styles. It uses the same mapping format as `block_map`: a string for the simplest case, a dict to add props.
```python
config = {
'style_map': {
**STYLE_MAP,
'KBD': 'kbd',
'HIGHLIGHT': {'element': 'strong', 'props': {'style': {'textDecoration': 'underline'}}},
},
}
```
The `style` prop can be a string (rendered as-is) or a dict, in which case its properties are converted to a CSS string using `camel_to_dash`.
## Entity decorators
`entity_decorators` maps entity types to components so they can be rendered with their data. Each value is a [custom component](https://wagtail.github.io/draftjs_exporter/guides/custom-components/index.md) function, a lambda, or `None` to discard that entity type entirely.
```python
config = {
'entity_decorators': {
ENTITY_TYPES.IMAGE: image,
ENTITY_TYPES.LINK: link,
ENTITY_TYPES.HORIZONTAL_RULE: lambda props: DOM.create_element('hr'),
ENTITY_TYPES.EMBED: None,
},
}
```
## Composite decorators
Composite decorators replace text based on a regular expression. Use them for line breaks, mentions, linkification, or any text-level transformation that isn't an entity in the Draft.js data.
Each entry is a dict with a `strategy` (compiled regex) and a `component` (a [custom component](https://wagtail.github.io/draftjs_exporter/guides/custom-components/index.md) function):
```python
config = {
'composite_decorators': [
{'strategy': re.compile(r'\n'), 'component': br},
{'strategy': re.compile(r'#\w+'), 'component': hashtag},
{'strategy': LINKIFY_RE, 'component': linkify},
],
}
```
## Fallbacks
Each map accepts a special fallback key — `BLOCK_TYPES.FALLBACK`, `INLINE_STYLES.FALLBACK`, or `ENTITY_TYPES.FALLBACK` — that is triggered when the exporter encounters a type with no explicit mapping. See [Fallback components](https://wagtail.github.io/draftjs_exporter/guides/fallback-components/index.md) for how to write one.
## Putting it all together
See [`example.py`](https://github.com/wagtail/draftjs_exporter/blob/main/example.py) as a fully-fledged demo of the configuration. For the full list of block types, inline styles, and entity types the exporter recognizes, see the [API reference](https://wagtail.github.io/draftjs_exporter/reference/api/index.md).
# Example output (generated by `example.py`)
## HTML
```html
draftjs_exporter is an HTML exporter for
Draft.js
content
Try it out by running this file!
Features 📝🍸
The exporter aims to provide sensible defaults from basic block types and inline styles to HTML, that can easily be customized when required. For more advanced scenarios, an API is provided (mimicking React's
createElement
) to create custom rendering components of arbitrary complexity.
Here are some features worth highlighting:
-
Convert line breaks to
<br>
elements.
-
Automatic conversion of entity data to HTML attributes (int & boolean to string,
style object
to
style string
).
-
Wrapped blocks (
<li>
elements go inside
<ul>
or
<ol>
).
-
With arbitrary nesting.
-
Common text styles:
Bold
,
Italic
,
Underline
,
Monospace
,
Strikethrough.
cmd + b
-
Overlapping
te
xt
styles.
Custom styles
too!
-
Depth can go back and forth, it works fiiine (1)
-
Depth can go back and forth, it works fiiine (2)
-
Depth can go back and forth, it works fiiine (3)
-
Depth can go back and forth, it works fiiine (4)
-
Depth can go back and forth, it works fiiine (5)
For developers 🚀
-
Import the library
-
Define your configuration
-
Go!
-
Optionally, define your custom components.
def blockquote(props):
block_data = props['block']['data']
return DOM.create_element('blockquote', {
'cite': block_data.get('cite')
}, props['children'])
Discarded block but the content stays.
Render as
div
Voilà!
```
## Markdown
## draftjs_exporter is an HTML exporter for [Draft.js](https://github.com/facebook/draft-js) content
> Try it out by running this file!
### Features 📝🍸
The exporter aims to provide sensible defaults from basic block types and inline styles to HTML, that can easily be customized when required. For more advanced scenarios, an API is provided (mimicking React's [`createElement`](https://facebook.github.io/react/docs/top-level-api.html#react.createelement)) to create custom rendering components of arbitrary complexity.
______________________________________________________________________
Here are some features worth highlighting:
- Convert line breaks to `
` elements.
- Automatic conversion of entity data to HTML attributes (int & boolean to string, [`style object` to `style string`](https://facebook.github.io/react/docs/jsx-in-depth.html)).
- Wrapped blocks (`- ` elements go inside `
` or ``).
- With arbitrary nesting.
- Common text styles: **Bold**, *Italic*, Underline, `Monospace`, Strikethrough. cmd + b
- ~Overlapping \*\*te~**\*\**xt***\_ styles. \_Custom styles too!
- # hashtag support via [#CompositeDecorators](https://github.com/wagtail/draftjs_exporter/pull/17).
- Linkify URLs too!
- Depth can go back and forth, it works fiiine (1)
- Depth can go back and forth, it works fiiine (2)
- Depth can go back and forth, it works fiiine (3)
- Depth can go back and forth, it works fiiine (4)
- Depth can go back and forth, it works fiiine (5)
### For developers 🚀
1. Import the library
1. Define your configuration
1. Go!
1. Optionally, define your custom components.
```text
def blockquote(props):
block_data = props['block']['data']
return DOM.create_element('blockquote', {
'cite': block_data.get('cite')
}, props['children'])
```
Discarded block but the content stays.
Render as div
Voilà!