Skip to content

Commit ce00cad

Browse files
authored
Merge pull request #295 from torchbox/fix/code-embed-colour-contrast
Add styles for gists
2 parents eef7db1 + c038dec commit ce00cad

File tree

12 files changed

+723
-11
lines changed

12 files changed

+723
-11
lines changed

docs/front-end/markdown-codehilite.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
1-
# Markdown block and codehilite
1+
# Markdown block, Raw HTML block, Gists and Codehilite
2+
3+
## Markdown block and Codehilite
24

35
We make use of the [wagtailmarkdown](https://github.com/torchbox/wagtail-markdown) package to provide a markdown block, whose only purpose is to provide a block to add code with syntax highlighting. We could have opted to use [wagtailcodeblock](https://github.com/FlipperPA/wagtailcodeblock) but using `wagtailmarkdown` allowed us to import existing blog posts already using it.
46

57
Using headings, text and other formatting in the markdown block will mean pages are not styled correctly. For this reason we have updated the toolbar options in this block to only show the 'code' block. This is done via a custom `admin.js` script which can be used for any admin JavaScript customisations in the future. It is compiled separately to the main site JavaScript in webpack, and called via `core/wagtail_hooks.py`.
68

7-
`wagtailmarkdown` allows the use of `codehilite` styles for syntax highlighting, from [pygments](https://pygments.org/styles/). We have made use of two themes from pygments - `monokai` for darkmode, and the `default` styles for light mode. Pygments styles aren't available to install via npm, so they are added in a `vendor` folder inside `static_src/sass`, with `stylelint` entirely disabled. There's a simple nesting rule in the CSS to load the `monokai` styles for `.dark-mode` and the `default` styles for `.light-mode`.
9+
`wagtailmarkdown` allows the use of `codehilite` styles for syntax highlighting, from [pygments](https://pygments.org/styles/). We have made use of two themes from pygments - `monokai` for darkmode, and the `default` styles for light mode. Pygments styles aren't available to install via npm, so they are added in a `vendor` folder inside `static_src/sass`, with `stylelint` entirely disabled. There's a simple nesting rule in the CSS to load the `monokai` styles for `.dark-mode` and the `default` styles for `.light-mode`. The file has it's own entrypoint in the webpack config and the bundled css is output to `static_compiled/css/codehilite.css`. It is then conditionally loaded in via a custom `has_markdown_block` filter that checks for the presence of the markdown block in the page body.
10+
11+
## Raw HTML block and Gists
12+
13+
In some cases Github Gists are used to display code snippets - we use the RAW HTML block to embed these.
14+
15+
Gists do not use codehilite for their styles, so we have a separate `_gist.scss` file in the `static_src/sass/vendor` folder. This is also conditionally loaded in via a custom `has_gist_block` filter that checks for the presence of the RAW HTML block in the page that that includes 'https://gist.github.com' in the content.

tbx/core/templatetags/util_tags.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from django.utils.text import camel_case_to_spaces, slugify
33

44
from tbx.core.utils.models import SocialMediaSettings
5+
from wagtail.blocks import StreamValue
56

67
register = template.Library()
78

@@ -88,3 +89,41 @@ def ifinlist(value, list):
8889
# cast to strings before testing as this is used for heading levels 2, 3, 4 etc
8990
stringList = [str(x) for x in list]
9091
return str(value) in stringList
92+
93+
94+
@register.filter(name="has_gist_block")
95+
def has_gist_block(value):
96+
if not isinstance(value, StreamValue):
97+
return False
98+
99+
for block in value.blocks_by_name(block_name="raw_html"):
100+
if "https://gist.github.com" in block.value:
101+
return True
102+
103+
# Special case for work page section block as the StreamField blockss are nested within sections
104+
for block in value.blocks_by_name(block_name="section"):
105+
if "content" not in block.value:
106+
continue
107+
for sub_block in block.value["content"].blocks_by_name("raw_html"):
108+
if "https://gist.github.com" in sub_block.value:
109+
return True
110+
111+
return False
112+
113+
114+
@register.filter(name="has_markdown_block")
115+
def has_markdown_block(value):
116+
if not isinstance(value, StreamValue):
117+
return False
118+
119+
if len(value.blocks_by_name(block_name="markdown")):
120+
return True
121+
122+
# Special case for work page section block as the StreamField blockss are nested within sections
123+
for block in value.blocks_by_name(block_name="section"):
124+
if "content" not in block.value:
125+
continue
126+
if len(block.value["content"].blocks_by_name("markdown")):
127+
return True
128+
129+
return False

tbx/project_styleguide/templates/patterns/base.html

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,16 @@
3131

3232
<link rel="stylesheet" type="text/css" href="{% static 'css/main.css' %}">
3333

34+
{# Add syntax highlighting for gists if a gist exists within a raw html streamfield #}
35+
{% if page.body|has_gist_block %}
36+
<link rel="stylesheet" type="text/css" href="{% static 'css/gist.css' %}">
37+
{% endif %}
38+
39+
{# Add syntax highlighting for code snippets within a code block streamfield (aka markdown block) #}
40+
{% if page.body|has_markdown_block %}
41+
<link rel="stylesheet" type="text/css" href="{% static 'css/codehilite.css' %}">
42+
{% endif %}
43+
3444
{% block extra_css %}{% endblock %}
3545
</head>
3646

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{% load wagtailmarkdown %}
2+
23
<div class="grid__markdown markdown-block">
34
{# tabindex needed because in order to scroll using the keyboard #}
45
<div class="markdown-block__scroller" tabindex="0">

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{% extends "patterns/base_page.html" %}
2-
{% load wagtailcore_tags wagtailimages_tags static %}
2+
{% load wagtailcore_tags wagtailimages_tags util_tags static %}
33

44
{% block meta_tags %}
55
<script>

tbx/project_styleguide/templates/patterns/pages/service/service_page.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{% extends "patterns/base_page.html" %}
2-
{% load wagtailcore_tags wagtailimages_tags static %}
2+
{% load wagtailcore_tags wagtailimages_tags %}
33

44
{% block content %}
55
<div class="grid grid--spacer-large streamfield">

tbx/project_styleguide/templates/patterns/pages/standard/standard_page.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{% extends "patterns/base_page.html" %}
2-
{% load wagtailcore_tags wagtailimages_tags static %}
2+
{% load wagtailcore_tags wagtailimages_tags %}
33

44
{% block content %}
55
<div class="grid grid--spacer-large streamfield">

tbx/project_styleguide/templates/patterns/pages/work/historical_work_page.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{% extends "patterns/pages/work/work_page_base.html" %}
2-
{% load wagtailcore_tags wagtailimages_tags static %}
2+
{% load wagtailcore_tags wagtailimages_tags %}
33

44
{# The historical work pages did not have a separate intro - so we just loop over the body streamfield to find the intro block(s) #}
55
{% block intro %}

tbx/static_src/sass/main.scss

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,3 @@
8282
@tailwind base;
8383
@tailwind components;
8484
@tailwind utilities;
85-
86-
// CSS from external vendors (not available as npm)
87-
@import 'vendor/codehilite';

tbx/static_src/sass/vendor/_codehilite.scss renamed to tbx/static_src/sass/vendor/codehilite.scss

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// This file has it's own entry point in webpack and is only included in markdown_block.html
2+
13
/*!
24
Code highlighting generated from Pygments | https://pygments.org/
35
| Copyright 2006-2023 by the Pygments team
@@ -8,8 +10,6 @@
810

911
/* stylelint-disable */
1012

11-
@use 'config' as *;
12-
1313
.mode-dark {
1414
pre {
1515
line-height: 125%;

0 commit comments

Comments
 (0)