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

174 lines
3.2 KiB
Markdown

---
title: "Spellcheck Your Hugo Site With CSpell"
date: 2024-11-20T09:49:51-05:00
draft: false
tags:
- meta
---
Bla bla bla
[5]: https://cspell.org/
<!--more-->
### 1. Install CSpell
Assuming a modern version of Node.js (>= 18), you can use [npx][1] to download and run CSpell in a single command:
```sh
npx cspell content/**/*.md
```
You'll see a ton of spelling errors -- ignore them for now.
[1]: https://docs.npmjs.com/cli/v10/commands/npx
### 2. Add config file
Next, let's create a basic config file. In the root of your site, put the following in `.cspell.json`:
```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][2] 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][3]. Adding `"ruby"`, `"golang"`, and `"java"` to the `"dictionaries"` array makes a bunch of misspellings go away.
[2]: /elsewhere
[3]: https://github.com/streetsidesoftware/cspell-dicts/tree/main/dictionaries
### 4. Ignore front matter
```json
"patterns": [
{
"name": "front_matter",
"pattern": "/^(-{3}|[+]{3})$(\\s|\\S)*?^\\1$/gm"
}
],
"languageSettings": [
{
"languageId": "markdown",
"ignoreRegExpList": [
"front_matter",
]
}
]
```
[6]: https://gohugo.io/content-management/front-matter/
[7]: https://github.com/streetsidesoftware/cspell/discussions/3456#discussioncomment-3438647
### 5. Ignore proper nouns
```json
{
"name": "proper_nouns",
"pattern": "/[\\W_][A-Z][\\S]+/g"
}
```
```json
"languageSettings": [
{
"languageId": "markdown",
"ignoreRegExpList": [
"front_matter",
"proper_nouns"
]
}
]
```
### 6. Fix spelling
### 7. Create custom dictionary
```sh
npx cspell --words-only --unique content/**/*.md >> .dictionary
```
```json
"dictionaryDefinitions": [
{
"name": "exceptions",
"path": ".dictionary",
"addWords": true
}
],
```
```json
"dictionaries": [
"english",
"ruby",
"golang",
"exceptions"
]
```
```sh
npx cspell --words-only --unique content/**/*.md >> .dictionary
sort -o .dictionary .dictionary
```
### 8. Add to build pipeline
[8]: https://git.sr.ht/~dce/davideisinger.com/tree/main/item/.build.yml#L23-24
---
[Here's the final `.cspell.json` config file.][4]
[4]: https://git.sr.ht/~dce/davideisinger.com/tree/main/item/.cspell.json
---
```json
{
"$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"
]
}
]
}
```