question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

API should provide access to the HTML-converted versions of rich text fields

See original GitHub issue

Hello, I’m working with a Wagtail 1.5 site and we have a rich text field. When we embed a video using the included functionality, the HTML comes out looking like:

<embed embedtype=\"media\" url=\"https://www.youtube.com/watch?v=1TQ0gZJbWP8\"/>

This is not a valid embed tag, it should look like:

<embed type=\"media\" src=\"https://www.youtube.com/watch?v=1TQ0gZJbWP8\"/>

Notice, embedtype->type and url->src.

This is based on this spec: http://www.w3schools.com/tags/tag_embed.asp

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:5
  • Comments:14 (2 by maintainers)

github_iconTop GitHub Comments

30reactions
richtiercommented, Apr 10, 2019
from wagtail.wagtailcore.rich_text import expand_db_html


class APIRichTextField(APIField):
    def __init__(self, name):
        serializer = serializers.APIRichTextSerializer()
        super().__init__(name=name, serializer=serializer)


class APIRichTextSerializer(fields.CharField):
    def to_representation(self, instance):
        representation = super().to_representation(instance)
        return expand_db_html(representation)

then we can do the following depending on taste:

class MyModel(Page):
    body = RichTextField()
    api_fields = [
        APIRichTextField('body'),
    ]

or

class MyModel(Page):
    body = RichTextField()
    api_fields = [
        APIField('body', serializer=APIRichTextSerializer()),
    ]
6reactions
peteybcommented, May 12, 2020

For anyone using a RichTextBlock inside of a StreamField who would like the Wagtail API to generate the HTML for just the RichText block of the StreamField whilst preserving the other block structure in the API output:

# blocks.py
from wagtail.core import blocks
from wagtail.core.rich_text import expand_db_html


class APIRichTextBlock(blocks.RichTextBlock):
    def get_api_representation(self, value, context=None):
        representation = super().get_api_representation(value, context)
        return expand_db_html(representation)



# models.py
from wagtail.core.fields import StreamField
from wagtail.core.models import Page

from .blocks import APIRichTextBlock


class YourPage(Page):
 ...
    your_field = StreamField([
        ('text', APIRichTextBlock()),
    ])

    api_fields = [
        APIField('your_field'),
    ]

NB. This works on Django 2.2 Wagtail 2.7

Read more comments on GitHub >

github_iconTop Results From Across the Web

What is rich text? - Contentful
Rich Text is a field type that enables authors to create rich text content, similar to traditional "What you see is what you...
Read more >
Please fix markdown output with "rich" API - Developers Central
I know that normal text fields and canvas fields generate “Markdown” output if fetched from the API using valueFormat=rich.
Read more >
Rich text field storage in the API | Adobe Workfront
Requesting text information from a project object that contains rich text can be done using the field parameterValues.
Read more >
Cannot access images in rich text fields coming from REST API
I have a big problem that I cannot get my head around: We created a REST API that returns an object with a...
Read more >
Rich Text Fields and Types | Documentation - Review Board
New in version 2.0. Several resources in the API provide text fields that can store either plain text or Markdown text. These text...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found