Skip to content

A prototype of rendering Wagtail's richtext on the frontend using ContentState json

Notifications You must be signed in to change notification settings

JNaftali/wagtail-richtext-prototype

Repository files navigation

This project intends to be able to render ContentState json (as produced by the Wagtail cms) with React. It expects rich text data serialized as in the following serializer:

from rest_framework.serializers import BaseSerializer
from wagtail.admin.rich_text.converters.contentstate import ContentstateConverter
from wagtail.embeds.models import Embed
import json

class RichTextFieldSerializer(BaseSerializer):
    def to_representation(self, instance):
        converter = ContentstateConverter(features=features)
        content = json.loads(converter.from_database_format(instance))
        for key in content["entityMap"]:
            entity = content["entityMap"][key]
            if entity["type"] != "EMBED":
                continue

            entity["data"]["html"] = Embed.objects.get(url=entity["data"]["url"]).html
        return content

In the above code, features refers to the list of features that the RichText field that you are trying to serialize supports. This project aims to work with all built in wagtail RichText features (h#, bold, italic, ordered/unordered lists, hrs, links, document links, images, embeds, inline code, super/subscript, strikethrough, and blockquotes) with the ability to easily add support for any custom blocks/styles/entities you care to define.

We are not there yet.

About

A prototype of rendering Wagtail's richtext on the frontend using ContentState json

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published