Mastodon hachyterm.io

Every programming language needs its static site generator. Rust has 3 main contenders, one of them is Zola.

I wanted to try out a Rust static site generator, so let’s walk through getting started with Zola.

Installation

Zola offers several ways of installing, even a Dockerfile.

For MacOs, you can use Brew:

brew install zola

For Arch Linux (yay!):

pacman -S zola

That was pretty painless.

Getting started

The docs are very straight-forward. If I want to make a blog, I can follow the outlined steps.

zola init myblog

Now you’ll need to answer a few questions that are self-explanatory.

Zola offers the option to add search functionality to your blog! That’s something that I’m missing from Hugo.

The Getting Started Guide now goes on to explain how to create templates and a folder structure.

The templating language looks like dozen others — which is a good thing!

But we want to add a theme and deploy the blog to Netlify, so let’s try that.

Install a Theme

I want to put my blog on a free Netlify domain via GitHub.

Let’s initialize a new Git repository:

cd zolablog
git init

(You may need to configure your username and email address, if you haven’t done so before.)

Although the documentation does not spell it out, I’ll add a theme as a Git submodule.

Why submodules?

Submodules is, in most cases, your best choice. You can easily pull in updated theme(s) when needed.

The feather theme looks pretty nice.

git submodule add https://github.com/piedoom/feather themes/feather

(If you want to update a git submodule, run git submodule update --rebase --remote.)

Now, we need to edit the config.toml to add the theme. Add this line:

theme = "feather"

Start the zola development server:

zola serve

You’ll see your blog on https://localhost:1111.

Adding Blog Posts

Luckily, the theme works out of the box (that wasn’t the case with all themes).

I’ll add a new blog post with some dummy text. For that to work, I need to add a new markdown file into the content folder.

We need to add some necessary frontmatter in .toml format:

Example post (content/first-post.md):

+++
title = "My First Blog Post"
date = 2021-08-08
+++

Bla bla bla

That works!

Deploy to Netlify

We’ll want to put that wonder of a blog on Netlify. Everyone should be able to see it!

I’ll use GitHub for source control and deployment.

Create a new repository.

Follow the instructions to add a new remote.

We also need a .gitignore file which will ignore the public folder. The public folder is where the finished (built) blog will live, we don’t need it in source control.

echo "public" > .gitignore

Then we’ll add our work to the Git staging area and push it to GitHub:

git add -A
git commit -m "Initial commit"
git push origin main

Zola has a section in the documentation that deals with Netlify.

We’ll need a netlify.toml file:

# You do need the `publish` and `command` variables.
[build]
publish = "public"
command = "zola build"

[build.environment]
# Set the version name that you want to use and Netlify will automatically use it.
ZOLA_VERSION = "0.14.0"

# The magic for deploying previews of branches.
# We need to override the base url with whatever url Netlify assigns to our
# preview site.  We do this using the Netlify environment variable
# `$DEPLOY_PRIME_URL`.

[context.deploy-preview]
command = "zola build --base-url $DEPLOY_PRIME_URL"

If you run zola build locally, the tool will create a public folder which contains the blog. That’s why we are using publish = "public".

The Zola documentation does not explain how to setup Netlify and how to connect your GitHub repository to Netlify.

But we can follow the guide on the Hugo documentation which also comes with a few screenshots. Please ignore everything that’s specific to Hugo.

When you use the Netlify webpage to import your GitHub repository, Netlify will automatically read the necessary settings from netlify.toml. That means that you can keep to the default settings.

Here’s the result of our walkthrough.

Thoughts

The documentation is pretty solid. If you’ve worked with static site generators before, the site explains what you need to know.

For total beginners, the documentation is too sparse. But newbies probably won’t use Zola anyway as it’s unlikely that a new programmer will learn Rust as a first language.

I like the sensible templating language and structure of a Zola project. Some of the extra features (link checker, search index) are neat.

All in all, I can see Zola as an alternative to Hugo.