#django #python #wagtail

When I created this blog, I wanted to be able to use Markdown for writing the blog posts.

I used the package wagtail-markdown for getting the editor up-and-running. It's working great for me, but some of the CSS used in the editor wasn't really what I liked. The font wasn't my preferred code font and the colors lacked contrast.

To fix this, I figured out that the best option would be to override the admin CSS instead of tinkering inside the wagtail-markdown module itself. This makes it easier to update the package in the future and keeps the customisations nicely separated. Again, Wagtail surprised me again in how flexible it is to allow customisations like these.

The trick is to use a Wagtail hook to inject a custom CSS into the admin which allows you to define the overrides. The hook we are going to use is called insert_global_admin_css.

We first start with creating a file called wagtail_hooks.py in one our apps. In my setup, I've made this part of the blog app as it is the only app using Markdown. In there, we can define which hooks should be registered and what they are supposed to be doing:

blog/wagtail_hooks.py

 1from django.utils.html import format_html
 2from django.templatetags.static import static
 3
 4from wagtail.core import hooks
 5
 6@hooks.register("insert_global_admin_css")
 7def insert_global_admin_css():
 8    return format_html(
 9        '<link rel="stylesheet" type="text/css" href="{}">',
10        static("css/admin.css"),
11    )

This hook is telling Wagtail to load the css/admin.css file in addition to the stock admin CSS file.

In there, after some fiddling around, I defined the following overrides:

mysite/static/css/admin.css

 1.CodeMirror {
 2    font-family: Menlo, monospace !important;
 3    font-size: 1.0em !important;
 4}
 5
 6.CodeMirror .CodeMirror-code .cm-url, .CodeMirror .CodeMirror-code .cm-link {
 7    color: #00676a !important;
 8}
 9
10.CodeMirror .CodeMirror-code .cm-comment {
11    background: rgba(0, 103, 106, .15) !important;
12}
13
14.editor-toolbar.fullscreen, .CodeMirror-fullscreen {
15    left: 200px !important;
16    bottom: 40px !important;
17}
18
19@media screen and (min-width: 50em) {
20    .markdown_textarea .field-content {
21        width: 100% !important;
22    }
23}

The selector .CodeMirror changes the font of the editor to a monospaced font which I find easier to read.

The cm.-url and .cm-link selector change the color of the hyperlinks to the green used in the Wagtail admin. I found that to be easier to read and to have a lot more contrast than the default gray.

The .cm-comment adds a greenish backgroud to the code snippets, making them more visible. This is especially handy when you forget to close a code block. If you do so, a big part of your post will now show up in green making it really visible.

.CodeMirror-fullscreen fixes a bug where the fullscreen mode of the editor was a bit too enthousiastic. It didn't take into account that on bigger screens, the sidebar is always visible. Without the customization, the left 200 pixels of the editor would be covered by the sidebar. It also changed the bottom so that the text wouldn't go underneath the actions, "Publish" and "Preview".

Last but not least, the .markdown_textarea .field-content was made slightly bigger to make it align with the default fields. We're doing this only for markdown textarea instances so that we don't resize the other fields.