Spell-checking in a Bitbucket pipeline

Spell-checking has been a thing for at least 50 years so spelling mistakes really shouldn’t be making their way onto a live website.

But things like running a spell-check are easily forgotten if we don’t make them happen automatically.

At Codeface we already have a standard mechanism on our projects for making sure a tool does get run automatically - we use Bitbucket Pipelines to ensure that certain processes are run every time changes are pushed to git.

Until recently we used this exclusively to run rails tests with rake or rspec, but one day it occurred to me: why not use Bitbucket Pipelines to run a spell-check?

A two-step pipeline with both steps passing

Here’s what we ended up with…

bitbucket-pipelines.yml

First we need to add a step to our Pipeline. After exploring a few options I ended up using a Python base image and installed pyspelling in it. pyspelling is a friendly wrapper around the slightly lower-level aspell. The pipeline step is pretty simple:

    ...
    - step:
        name: Spellcheck HTML Files
        image: python:3.8
        script:
          - apt-get update
          - apt-get install -y aspell
          - pip install pyspelling
          - pyspelling -c ./.spellcheck.yml

.spellcheck.yml

The last line of the pipeline step above references a file called .spellcheck.yml.

The format for the pyspell config file is documented here

Here’s my .spellcheck.yml:

matrix:
- name: Markdown
  aspell:
    lang: en
    d: en_GB
  dictionary:
    encoding: utf-8
    wordlists:
    - .dictionary.txt
  pipeline:
  - pyspelling.filters.html:
      comments: false
      ignores:
      - code
      - pre
      - sup
      - a
      - span.postcode
  sources:
  - '_site/**/*.html'
  default_encoding: utf-8

The ignores block enables us to easily skip spell-checking things like postcodes; we can wrap a postcode in a <span class="postcode">, and it won’t throw up spelling errors.

.dictionary.txt

And finally, I have a custom wordlist file called .dictionary.txt which looks like this:

# Names of People
McGregor
Poskitt

# Addresses
Kingsway
Hove
BN3

This custom wordlist allows us to add words that aren’t in the dictionary and have them pass the spell-test.

Once it’s all put together, it works pretty well.

There were a few oddities with the spell-checking - despite selecting a British dictionary there seemed to be a few words missing that were definitely valid words. I ended up adding them to my custom dictionary.

Once the spell-check passes, you get a nice green tick in Bitbucket, and next time you introduce a typo, Bitbucket Pipelines will complain.

Valid HTML This page was handcrafted in Brighton by Codeface