From 85afe6c41bf09b4bee650fb8c664e6c8f7ff512d Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Sat, 16 Jul 2022 16:04:06 +0100 Subject: [PATCH] Add alt tags to image blocks --- .../common/templates/common/blocks/image-caption.html | 4 ++-- website/common/templatetags/util_tags.py | 9 +++++++++ .../mermaid_block/templates/contrib/blocks/mermaid.html | 4 ++-- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/website/common/templates/common/blocks/image-caption.html b/website/common/templates/common/blocks/image-caption.html index b547cb6..001cec3 100644 --- a/website/common/templates/common/blocks/image-caption.html +++ b/website/common/templates/common/blocks/image-caption.html @@ -1,8 +1,8 @@ -{% load wagtailimages_tags wagtailcore_tags %} +{% load wagtailimages_tags wagtailcore_tags util_tags %}
- + {{ value.caption|richtext|extract_text }}
{{ value.caption|richtext }} diff --git a/website/common/templatetags/util_tags.py b/website/common/templatetags/util_tags.py index bb721aa..bc7e1cd 100644 --- a/website/common/templatetags/util_tags.py +++ b/website/common/templatetags/util_tags.py @@ -1,5 +1,9 @@ from django.template import Library +from django.utils.encoding import force_str from wagtail.models import Page +from wagtail.rich_text import RichText + +from website.common import utils register = Library() @@ -12,3 +16,8 @@ def do_range(stop: int) -> range: @register.simple_tag(takes_context=True) def pagefullurl(context: dict, page: Page) -> str: return page.get_full_url(context["request"]) + + +@register.filter() +def extract_text(html: str | RichText) -> str: + return utils.extract_text(force_str(html)) diff --git a/website/contrib/mermaid_block/templates/contrib/blocks/mermaid.html b/website/contrib/mermaid_block/templates/contrib/blocks/mermaid.html index dea81b7..5e76c2e 100644 --- a/website/contrib/mermaid_block/templates/contrib/blocks/mermaid.html +++ b/website/contrib/mermaid_block/templates/contrib/blocks/mermaid.html @@ -1,8 +1,8 @@ -{% load wagtailcore_tags %} +{% load wagtailcore_tags util_tags %}
- + {{ value.caption|richtext|extract_text }}
{{ value.caption|richtext }}