Skip to content

Commit 78dcc30

Browse files
committed
only include gist and codehilite css once if the code block (markdown) or raw html blocks have been used
1 parent 2f5a822 commit 78dcc30

File tree

4 files changed

+30
-7
lines changed

4 files changed

+30
-7
lines changed

tbx/core/templatetags/util_tags.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33

44
from tbx.core.utils.models import SocialMediaSettings
55

6+
from wagtail import blocks
7+
from wagtail.blocks import StreamValue
8+
from wagtailmarkdown.blocks import MarkdownBlock
9+
610
register = template.Library()
711

812

@@ -88,3 +92,17 @@ def ifinlist(value, list):
8892
# cast to strings before testing as this is used for heading levels 2, 3, 4 etc
8993
stringList = [str(x) for x in list]
9094
return str(value) in stringList
95+
96+
97+
@register.filter(name="has_raw_html_block")
98+
def has_raw_html_block(value):
99+
if isinstance(value, StreamValue):
100+
return any(type(item.block) is blocks.RawHTMLBlock for item in value)
101+
return False
102+
103+
104+
@register.filter(name="has_markdown_block")
105+
def has_markdown_block(value):
106+
if isinstance(value, StreamValue):
107+
return any(type(item.block) is MarkdownBlock for item in value)
108+
return False

tbx/project_styleguide/templates/patterns/molecules/streamfield/blocks/markdown_block.html

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
{% load wagtailmarkdown static %}
22

33
<div class="grid__markdown markdown-block">
4-
<link rel="stylesheet" type="text/css" href="{% static 'css/codehilite.css' %}">
5-
64
{# tabindex needed because in order to scroll using the keyboard #}
75
<div class="markdown-block__scroller" tabindex="0">
86
{{ value|markdown }}
Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
{% load static %}
22

33
<div class="grid__raw-html">
4-
{% if 'gist' in value %}
5-
<link rel="stylesheet" type="text/css" href="{% static 'css/gist.css' %}">
6-
{% endif %}
7-
84
{{ value }}
95
</div>

tbx/project_styleguide/templates/patterns/pages/blog/blog_detail.html

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
{% extends "patterns/base_page.html" %}
2-
{% load wagtailcore_tags wagtailimages_tags static %}
2+
{% load wagtailcore_tags wagtailimages_tags util_tags static %}
3+
4+
{% block extra_css %}
5+
{{ block.super }}
6+
{% if page.body|has_raw_html_block %}
7+
<link rel="stylesheet" type="text/css" href="{% static 'css/gist.css' %}">
8+
{% endif %}
9+
10+
{% if page.body|has_markdown_block %}
11+
<link rel="stylesheet" type="text/css" href="{% static 'css/codehilite.css' %}">
12+
{% endif %}
13+
{% endblock %}
314

415
{% block meta_tags %}
516
<script>

0 commit comments

Comments
 (0)