Files
davideisinger.com/content/journal/spellcheck-your-hugo-site-with-cspell/index.md
2024-11-20 16:21:29 -05:00

3.2 KiB

title, date, draft, tags
title date draft tags
Spellcheck Your Hugo Site With CSpell 2024-11-20T09:49:51-05:00 false
meta

Bla bla bla

1. Install CSpell

Assuming a modern version of Node.js (>= 18), you can use npx to download and run CSpell in a single command:

npx cspell content/**/*.md

You'll see a ton of spelling errors -- ignore them for now.

2. Add config file

Next, let's create a basic config file. In the root of your site, put the following in .cspell.json:

{
  "$schema": "https://raw.githubusercontent.com/streetsidesoftware/cspell/main/cspell.schema.json",
  "version": "0.2",
  "dictionaries": [
    "english"
  ]
}

3. Add additional languages

My site (especially the stuff in /elsewhere that I've mirrored from my company's website) has code snippets that the English dictionary doesn't recognize. Fortunately, CSpell ships with a bunch of additional dictionaries. Adding "ruby", "golang", and "java" to the "dictionaries" array makes a bunch of misspellings go away.

4. Ignore front matter

"patterns": [
  {
    "name": "front_matter",
    "pattern": "/^(-{3}|[+]{3})$(\\s|\\S)*?^\\1$/gm"
  }
],
"languageSettings": [
  {
    "languageId": "markdown",
    "ignoreRegExpList": [
      "front_matter",
    ]
  }
]

5. Ignore proper nouns

{
  "name": "proper_nouns",
  "pattern": "/[\\W_][A-Z][\\S]+/g"
}
"languageSettings": [
  {
    "languageId": "markdown",
    "ignoreRegExpList": [
      "front_matter",
      "proper_nouns"
    ]
  }
]

6. Fix spelling

7. Create custom dictionary

npx cspell --words-only --unique content/**/*.md >> .dictionary
"dictionaryDefinitions": [
  {
    "name": "exceptions",
    "path": ".dictionary",
    "addWords": true
  }
],
"dictionaries": [
  "english",
  "ruby",
  "golang",
  "exceptions"
]
npx cspell --words-only --unique content/**/*.md >> .dictionary
sort -o .dictionary .dictionary

8. Add to build pipeline


Here's the final .cspell.json config file.


{
  "$schema": "https://raw.githubusercontent.com/streetsidesoftware/cspell/main/cspell.schema.json",
  "version": "0.2",
  "dictionaryDefinitions": [
    {
      "name": "exceptions",
      "path": ".dictionary",
      "addWords": true
    }
  ],
  "dictionaries": [
    "english",
    "ruby",
    "golang",
    "exceptions"
  ],
  "patterns": [
    {
      "name": "front_matter",
      "pattern": "/^(-{3}|[+]{3})$(\\s|\\S)*?^\\1$/gm"
    },
    {
      "name": "proper_nouns",
      "pattern": "/[\\W_][A-Z][\\S]+/g"
    }
  ],
  "languageSettings": [
    {
      "languageId": "markdown",
      "ignoreRegExpList": [
        "front_matter",
        "proper_nouns"
      ]
    }
  ]
}