Spell Checking Markdown Documents

Spell Checking Markdown Documents

by John Vincent


Posted on January 30, 2021


This is a discussion about spell checking Markdown documents.

Overview

The requirements are

  • have a dictionary and rules than I can add to or override.
  • be able to spell check folders and subfolders, with some stated omissions.
  • be able to run a test markdown files.

I chose to use the package markdown-spellcheck

npm i markdown-spellcheck --save-dev

Notice

Dictionary (-d, --dictionary)

Specify a custom Hunspell dictionary to load. The passed filename should not include a file extension and markdown-spellcheck will attempt to load the file with .aff and .dic extensions.

Find a dictionary

English-US and English-AU Dictionaries, which mention Official Hunspell Dictionaries for en_US

Download

cd /Users/jv/Desktop/MyDevelopment/github/website/gatsby-website
mkdir -p dictionary/hunspell
  • Downloaded from en_US
  • Unzip
cp /Users/jv/Downloads/hunspell-en_US-2020.12.07/en_US* dictionary/hunspell

which copies two files

  • en_US.dic
  • en_US.aff

Create Additional Dictionary

cd /Users/jv/Desktop/MyDevelopment/github/website/gatsby-website/dictionary/my-dictionary

and create file list.dic

Npm/X
npm/X

with additional words.

Create word rules file list.aff

KEEPCASE X

which adds the rule that X means ignore case.

Word Rules

A helpful reference is hunspell - format of Hunspell dictionaries and affix files

Word rules are stored in dictionary/hunspell/en_US.aff, for example M - noun, name

Unix Script

Create file md-spell-checker

#!/bin/sh
#
# script to spell check markdown files
#
PARAM="$1"
# echo "PARAM :$PARAM:"
#
JVTMP=/tmp/dictionary-$$
TEST_MD=./dictionary/my-dictionary/test.md
#
prepareDictionary() {
	cat ./dictionary/hunspell/en_US.dic ./dictionary/my-dictionary/list.dic > ${JVTMP}.dic;
	cat ./dictionary/hunspell/en_US.aff ./dictionary/my-dictionary/list.aff > ${JVTMP}.aff;
}
#
spellChecker() {
	NAME=$1;
	# echo "NAME $NAME";
	if [ -z "$NAME" ]; then
		return;
	fi;
	./node_modules/markdown-spellcheck/bin/mdspell -r -n -a --en-us --dictionary $JVTMP $NAME;
}
#

prepareDictionary			# prepare the combined dictionary

if [ -z "$PARAM" ]; then
	echo "Spell Checker for all Markdown Files"
	for FILE in `find . \( -path ./node_modules -o -path ./destination -o -path ./src/markdown/content -o -path ./Not-in-use \) -prune -o -name '*.md' -print`
	do
		echo "Spell Checking Markdown file $FILE"
		spellChecker $FILE
		echo "Completed Spell Checking Markdown file $FILE"
		echo " "
	done
else
	if [ "$PARAM" = "tester" ]; then
		echo "Spell Checking TEST WORDS Markdown file $TEST_MD"
		spellChecker $TEST_MD		# check words in this file, ensure they pass.
		echo "Completed Spell Checking TEST WORDS Markdown file $FILE"
		echo " "
	else
		echo "Spell Checking Markdown file $PARAM"
		spellChecker $PARAM
		echo " "
	fi
fi

Tester Markdown File

./dictionary/my-dictionary/test.md is used to test words that should be added to the dictionary.

Add a word to this file and test with

npm run spelling-tester

Execute

package.json

"spelling": "./md-spell-checker",
"spelling-tester": "./md-spell-checker tester",
"spelling-clean": "rm /tmp/dictionary-*",

To check the spelling of all markdown files

npm run spelling

To run the test markdown file test.md

npm run spelling-tester

To spell check only one file

./md-spell-checker {your-file}

Cleanup

See package.json

"spelling-clean": "rm /tmp/dictionary-*",

Files are created in /tmp. To remove them

npm run spelling-clean