Over the years, I have made several types of blog engines, and sites, and I have never really been happy with anything, but this guide is one of the best attempts so far!
Table of Contents
I have created my own blog engines with php, cakephp, static with macromedia dreamweaver (if you remember that..), jekyll and even tried to move my blogging to medium.com. But each attempt has left a tangy and bitter taste in my mouth.
So, what did I do? I created a new blog engine.. Well, one that I really like!
Features
These are the features I required:
- Using markdown for quick writing
- Low impact on the environment
- Frontmatter
- Category pages (not covered here)
- Tag pages (not covered here)
- Article pages
- Create static content
- Minimal usage of html tags in markdown files (not covered here)
Prerequisites
Knowledge of:
- NodeJS
- npm
- Github
- rollup
- yaml
- JavaScript
Tools:
- terminal
- editor
- web browser
Setup
First we need to set up a repository. Head on over to https://github.com and create a new repository. Call it whatever you want, and clone it into your workspace. For the sake of this guide, we are referring to the repository as static-blog
;
Then cd
into your project:
Looks empty, right? Let us proceed.
npm
Initialize npm, change the stuff you want with the interactive tool:
After you have done that, you will have a directory something like this:
Dependencies
After you've done that, install the required dependencies:
After you have done that, you will have a directory something like this:
npm scripts
Open up your package.json
, and update the scripts
property to something like this:
As you can see, we have added some scripts that will build this site:
- clean: A helper function that will clean the
dist
folder, because you really dont want the produced files in your repository - assets: Copies over the assets to the
dist
folder - prebuild: Runs before the
build
script, making sure we have assets copied and posts created. (We will come back to the creation of posts) - postbuild: A helper script to actually move the produced rollup artifacts into their respective folder, to make it look cleaner
- build:watch: A simple watcher to build on every save.
- dev: I use
browser-sync
for dev, you can use whatever setup you are used to, but this is what I use.
Your package.json
should look something like this:
Configuration files
If you are like me and love to fine granulate options for the different modules used in this setup, you might want to add some configuration files:
.browserslistrc
.editorconfig
.postcssrc.cjs
.stylintrc
Rollup
To be able to process *.styl
files and produced bundled JavaScript, we are using rollup
. Start creating your rollup.config.js
:
And it should look something like this:
Folder structure
Now, let us add some more folders to this:
src/assets/js
Remember earlier, from the npm scripts, that we copy over the assets
folder into dist
? Well, we need a dummy file for the copying of js to work:
Go into the src/assets/js
directory and create a dummy.js
file:
src/assets/css
In the css
folder, you can put any css file you want, for example a custom styling for prismjs
, or any other library you would use.
src/styles
Then go to the styles
folder to create a index.styl
file, for all your styles.
Then in src/main.js
, add the import of your styles, rollup will handle this automatically, converting the *.styl
file into css:
Allrighty then. We've now setup, and ready to write the /scripts/posts/create.js
file, to be able to convert markdown files to static files!
Convert posts
Create a directory named scripts
, and make a file named create.js
inside:
Open up create.js
in your editor and add this:
The article template
Now, let us create the template for all of our posts! Create a article.html
file in src/assets/templates
:
And it could look something like this:
Did you notice the Create a static, sustainable website or blog
, Over the years, I have made several types of blog engines, and sites, and I have never really been happy with anything, but this guide is one of the best attempts so far!
and {{CONTENT}}
strings? We are using them to put the data into it.
We will cover the post path in the next chapter.
Create your first post
We're ready to go! Go into src/assets/posts
and create your first post:
Where <slug-of-your-post>
is the slug for your post. Don't worry, you can change the file name later.
Now, open the post in your editor, and add these frontmatter properties:
And now you are ready to write your post! Write something down, and we will proceed! Eventually, your posts
-folder would look something like this:
Build and view your first post
Now, we're ready to see it in action.
From the root of your project, do this:
These commands will build your files, convert your posts and copy your assets, and then open up a browser to view your blog.
Since we have not setup an index.html
-file, there's nothing to see. However, go to http://localhost:3000/2023/09/30/static-blog.html
and see your first post!
From here you can add posts, add the styling or even JavaScript to your blog.
After you have run npm run build
, the dist
-folder should look something like this:
Summary
You have now learned how to build a static blog site! What can be done further? Styling, interactivity, add missing pages and publish on Github Pages!. I will cover more of these features in separate articles. Stay tuned!