Add a Spellcheck Feature to Your NodeJS ProjectHannah Apuan
Hacktoberfest, a virtual hackathon that takes place the month of October, is a great opportunity to add something new to an open source project. My first Hacktoberfest pull request was to Twilio SendGrid’s docs repository, adding a spellchecking feature.
This feature is so important for large-scale projects where multiple people contribute to markdown and HTML pages, and it can be challenging to keep track of all the changes. Even on a personal scale, this feature is great for projects like writing this blog post. Adding a spellcheck feature is super easy for beginner NPM users, so keep reading to see how you can add it to your next project.
If you are editing an existing NodeJS project, skip to the next paragraph. Even if you’re not familiar with NodeJS or NPM, this shouldn’t be too hard. NodeJS is amazing out of the box. I’m not going to explain how to set up NodeJS and NPM, but you can learn how to do so here.
If you’re starting from a brand new directory, initialize the folder as a typical NodeJS project via the command line.
If it starts up properly, you should be good to move on to the next section.
For the spell checking functionality, I am using the npm package called markdown-spellcheck. This package is great because it can handle a lot of errors, spellcheck from many files/directories, and it doesn’t actually change any code. Instead, the package prints out its analysis to stdout.
The end functionality will allow the command
npm run spellcheck to spellcheck all of the HTML and markdown files and allow the
npm run spellcheck-file [filename] to spellcheck [filename] (given it is HTML or markdown).
First, we need to initialize the package by adding it as a dependency to
package.json. This can all be done on the command line by the magic of npm.
>> 16 spelling errors found in 1 file
mdspell won’t return any mistakes if you don’t have any files or mistakes in your files. Next, we need to edit the
package.json directly to add the scripts
Notice how the `
mdspell` command accepts directory formats with wildcards ‘
*‘ and can exclude certain folders and subfolders with exclamation points ‘
If you want more information on the
mdspell command or
markdown-spellcheck npm package, go to the markdown-spellcheck package documentation.
Via command line:
Guess what? You’re done! It was that easy.
Is your spellcheck coming back with a bunch of false positive errors because of tech jargon? If you want to add any custom dictionary exceptions without too much overhead, you can implement the
.spelling file which is automatically checked by the
mdspell command. After first making the
.spelling file in the project root, the syntax is pretty easy.
Tl;dr: NodeJS is awesome! Once you get the hang of NodeJS, the hardest part is finding a package for your need. This adds a useful feature in a modular way without way too much work. (Reinventing the wheel for this problem could easily become huge and complex.)
Some potential cons of this package I found are that it becomes more complicated with CI tools like Travis or Jenkins because of all the log files generated. This can be fixed by excluding those files from being checked. Otherwise, you risk mdspell freezing and timing out your build. I also noticed that the dictionary is subpar, especially when analyzing content with tech jargon or markdown files with code. However, the package documentation includes a custom dictionary option. Just make sure to watch out for those details!
I would also highly recommend getting involved in the next Hacktoberfest. Contributing to open source projects is always a good way to flex your skills. And, as an added bonus, you can get free swag from DigitalOcean from only 4 pull requests.