Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

draft: improved article body output #26

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

niccofyren
Copy link
Member

@niccofyren niccofyren commented Oct 8, 2024

A collection of image related things for later reference for anyone that wants to take a stab at it. Links to relevant code/docs is included as comment on top of files

from wagtail.images.rich_text import ImageEmbedHandler


class CustomImageEmbedHandler(ImageEmbedHandler):
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A handler is how Wagtail converts internal xml/html-like format into frontend html. The current example works but should ideally be expanded to include srcset of all relevant sizes based on output of https://github.com/gathering/tgno-backend/blob/main/aktuelt/serializers.py#L11 (which is used for other news article images)

from wagtail.images.formats import Format, register_image_format


class InlineNewsImageFormat(Format):
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This formatter is what Wagtail uses to display different image "formats" that can be selected when inserting an image in editor UI. This example does nothing useful, but outlines how we could use this to define a new format. This could for example be

  • A fullscreen image
  • An image with or without caption
  • An image that includes custom field data
  • ...etc

@@ -99,7 +100,7 @@ def schedule(self):

api_fields = [
APIField("intro"),
APIField("body"),
APIField("body", serializer=NewsBodySerializer()),
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is also the option of going with https://docs.wagtail.org/en/stable/topics/streamfield.html which seems to allow a few more advanced options. Mainly I think it could be useful if we wanted to transition to JSON array output rather than html and leave more of the body rendering logic to frontend (and remove the need for extending/hacking into wagtail frontend related code).


class NewsBodySerializer(Field):
def to_representation(self, value):
return expand_db_html(value)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is what Wagtail uses in own frontend when rendering this type of data, mainly via a richtext template filter

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Ideas / Inbox
Development

Successfully merging this pull request may close these issues.

1 participant