DEV Community

a-parris21
a-parris21

Posted on

Refactoring my SSG

In refactoring my code, I focussed on removing global variables, reducing code duplication, adjusting my IF and ELSE statements for clearer logic, and shortening the body of my functions.

Change List

  • Removed outputPath global constant
  • Removed htmlLangAttribute global variable
  • Renamed parseFile to parseDir
  • Adjusted the IF and ELSE blocks in parseDir to make logic easier to understand
  • Edited main.js such that --lang CLI option is only parsed if an --input option is provided
  • Extracted function generateHtmlBody from writeHtmlFile
  • Extracted function parseOneFile from parseDir

Improvements

Previously, my code used 2 global variables and 1 global constant. I removed the need for the constant (outputPath) and 1 of the variables (htmlLangAttribute).
The outputPath constant stored the default filepath './dist' for the output directory (where generated HTML files would be saved). This constant has been removed and the default filepath is defined internally with a function (setOutputFolder).
The htmlLangAttribute was used to save, as a string, the value for the 'lang' attribute when generating HTML files. This information is now passed in to the relevant functions as a parameter instead of existing as a global variable.

The parseFile function was renamed to parseDir to better reflect its intended purpose. parseDir reads the files inside a folder, and if any of them are folders, it will recursively parse those sub-folders. If the files are single-files, that file is read and an HTML file is generated from its text content.

A new function generateHtmlBody was extracted from writeHtmlFile which handles converting an array of strings into HTML paragraphs and an HTML title element.

A new function parseOneFile was extracted from parseDir. I moved the code which handles the reading and writing of a single file from parseDir into a function parseOneFile and then invoke parseOneFile as needed.

The Interactive Rebase

The interactive rebase was very easy to do. I squashed all of the commits into the oldest one and edited its commit message to reflect the later changes.

Bugs

When I started refactoring my code I realized that, after implementing the recursive folder parsing feature, the index.html file was no longer being generated correctly.
I was not able to resolve this issue as yet though I've ruled out some of the possible solutions I came up with previously.

Changing history with Git

This was my first experience with using the rebase feature to change the history of commits on a repo. It turned out to be fairly easy, since I had kept each of my commits limited to a single change so squashing and renaming them was not difficult.

Top comments (0)