Skip to main content
https://travis-ci.org/springload/draftjs_exporter.svg?branch=master https://img.shields.io/pypi/v/draftjs_exporter.svg https://coveralls.io/repos/github/springload/draftjs_exporter/badge.svg?branch=master https://codeclimate.com/github/springload/draftjs_exporter/badges/gpa.svg

draftjs_exporter 🐍

Python library to convert the Facebook Draft.js editor’s raw ContentState to HTML.

It is intended to be used with Draftail and integrated into Wagtail CMS.

Usage

Understanding DraftJS contentState

Unlike traditional rich text editors, DraftJS stores data in a JSON representation.

There are two main parts:

  • blocks - lines of data and inline style attributes (without newlines).

  • entityMap – collection of Entities

For more information, this article covers the concepts in depth.

Using the exporter

pip install draftjs_exporter

The library requires you to explicity define mappings for the types of blocks and entities you wish to render. We may provide some defaults in the future.

from draftjs_exporter.html import HTML

# The Link decorator will be used to render LINK entities.
class Link:
    def render(self, props):
        return DOM.create_element('a', { 'href': data['url'] }, props['children'])

# Initialise the exporter with your configuration
exporter = HTML({
    'entity_decorators': {
        'LINK': Link()
    },
    # Define how each block should be transformed to HTML.
    'block_map': {
        'header-two': {'element': 'h2'},
        'blockquote': {'element': 'blockquote'},
        'unstyled': {'element': 'p'}
    },
    # Define how each style is rendered – either inline styles or elements.
    'style_map': {
        'ITALIC': {'fontStyle': 'italic'},
        'BOLD': {'element': 'strong'}
    }
})

# Render a Draft.js `contentState`
markup = exporter.render({
    'entityMap': {},
    'blocks': [
        {
            'key': '6mgfh',
            'text': 'User experience (UX) design',
            'type': 'header-two',
            'depth': 0,
            'inlineStyleRanges': [
                {
                    'offset': 16,
                    'length': 4,
                    'style': 'BOLD'
                }
            ],
            'entityRanges': []
        }
    ]
})

Running the example

You can run an executable example as follows:

python example.py

Feature list

This project adheres to Semantic Versioning and measures code coverage.

  • Extensive configuration of the generated HTML.

  • Default, extensible block & inline style maps for common HTML elements.

  • Define any attribute in the block map – custom class names for elements.

  • React-like API to create custom entity decorators.

  • Automatic conversion of entity data to HTML attributes (int & boolean to string, className to class).

  • Wrapped blocks (<li> elements go inside <ul>).

  • Nested wrapped blocks (multiple list levels, arbitrary type and depth).

  • Output inline styles as inline elements (<em>, <strong>, pick and choose).

  • Overlapping inline style ranges.

Development

Installation

Requirements: virtualenv, pyenv, twine

git clone git@github.com:springload/draftjs_exporter.git
cd draftjs_exporter/
virtualenv .venv
source ./.venv/bin/activate
make init
# Optionally, install the git hooks
./.githooks/deploy
# Optionally, install all tested python versions
pyenv install 2.7.11 && pyenv install 3.3.6 && pyenv install 3.4.4 && pyenv install 3.5.1
pyenv global system 2.7.11 3.3.6 3.4.4 3.5.1

Commands

make help            # See what commands are available.
make init            # Install dependencies and initialise for development.
make lint            # Lint the project.
make test            # Test the project.
make test-watch      # Restarts the tests whenever a file changes.
make test-coverage   # Run the tests while generating test coverage data.
make test-ci         # Continuous integration test suite.
make dev             # Restarts the example whenever a file changes.
make clean-pyc       # Remove Python file artifacts.
make publish         # Publishes a new version to pypi.

Debugging

  • Always run the tests. npm install -g nodemon, then make test-watch.

  • Use a debugger. pip install ipdb, then import ipdb; ipdb.set_trace().

Releases

Documentation

See the docs/ folder

Release files for draftjs_exporter 0.5.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for draftjs_exporter 0.5.1
File Size Uploaded
draftjs_exporter-0.5.1.tar.gz 20.1 kB Details

Release files / draftjs_exporter-0.5.1.tar.gz

Download URL draftjs_exporter-0.5.1.tar.gz
Size 20.1 kB
Tags Source
SHA-256 checksum
How to use checksums
caf19099ca3c05a2bfe2dbb4920be98c014b61041d237933b84b3f4a4d0deac2
BLAKE2b-256 checksum
How to use checksums
123be45ffd0eb87ac0dcba66da6f7479820264325234819e477e390b441f1f83
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

7.1.0

2 release files

7.0.0

1 release file

6.0.0

2 release files

5.2.0

2 release files

5.1.0

2 release files

5.0.0

2 release files

4.1.2

2 release files

4.1.1

2 release files

4.1.0

2 release files

4.0.0

1 release file

3.0.1

1 release file

3.0.0

1 release file

2.1.7

2 release files

2.1.6

2 release files

2.1.5

2 release files

2.1.4

1 release file

2.1.3

1 release file

2.1.2

1 release file

2.1.1

1 release file

2.1.0

1 release file

2.0.0

1 release file

1.1.1

1 release file

1.1.0

1 release file

1.0.0

1 release file

0.9.0

1 release file

0.8.1

1 release file

0.8.0

1 release file

0.7.0

1 release file

0.6.2

1 release file

0.6.1

1 release file

0.6.0

1 release file

0.5.2

1 release file

This release

0.5.1 This release

1 release file

0.5.0

1 release file

0.4.0

1 release file

0.3.3

1 release file

0.3.2

1 release file

0.3.1

1 release file

0.3.0

1 release file

0.2.0

1 release file

0.1.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page