go |ssg |html

Writing a SSG in Go

k_penguin_sato

K-Sato

Posted on January 26, 2024

Writing a SSG in Go

Overview

Ever since I read this article Writing a Static Blog Generator in Go by @zupzup a few years ago, I've been wanting to do the same.

After not doing it for a few years just like a lot of things in my life rn, I finally gave it a try.

What I Created

Here is the core package(SSG)

GitHub logo K-Sato1995 / go-simple-ssg

Minimalistic SSG(Static Site Generator) in Go

What this is

Yet another SSG in Go. It's not intended to replace any existing SSGs. I created it as a learning exercise and really enjoyed the process.πŸ€—

Heavily inspired by this blog post(Writing a Static Blog Generator in Go) by @zupzup.

In case you are wondering how to use this, check out the Initialization process below.

Initialization

Install the cli.

go install github.com/K-Sato1995/go-simple-ssg/simple-ssg-cli@latest

run

simple-ssg-cli init

would create a new project that looks somethi ng like this.

./
β”œβ”€β”€ contents/ (Stores markdown files)
β”œβ”€β”€ templates/ (Stores template html files and style files)
β”œβ”€β”€ generated/ (Stores generated files)
β”œβ”€β”€ go.mod
β”œβ”€β”€ go.sum
└── main.go

cd your project and just run

go run main.go

You should be able to see the site running on http://localhost:3001

Todo

  • CLI To run generating static contents based on the config file
    • put all the code in one…

and here is a static site I created using the ssg.

https://go-simple-ssg.vercel.app

Structure

The core package mainly consists of two parts

  • Generator: Generate static files based on Markdown and Templates(HTML) files.
  • CLI: Allows anyone to bootstrap a project.

The core packages looks like this

./
β”œβ”€β”€ parser/ (Part that parses Markdown into HTML)
β”‚ β”œβ”€β”€ mdToHTML.go
β”‚ └── parseMetaData.go
β”œβ”€β”€ builder/ (Part that generates static files based on the given md and templates)
β”‚ β”œβ”€β”€ bundleCss.go
β”‚ β”œβ”€β”€ copyStaticFiles.go
β”‚ β”œβ”€β”€ generateDetail.go
β”‚ └── generateList.go
β”œβ”€β”€ simple-ssg-cli/ (CLI that allows anyone to bootstrap a project)
β”œβ”€ example/
β”œβ”€β”€ config/ (Part that makes the project custamizable for users)
└── engine.go 
Enter fullscreen mode Exit fullscreen mode

To use it, install the simple-ssg-cli and execute the init command.

$ go install github.com/K-Sato1995/go-simple-ssg/simple-ssg-cli@latest
$ simple-ssg-cli init
Enter fullscreen mode Exit fullscreen mode

Doing this will create a new project with the following structure:

./
β”œβ”€β”€ contents/ (Stores markdown files)
β”œβ”€β”€ templates/ (Stores template html files and style files)
β”œβ”€β”€ generated/ (Stores generated files)
β”œβ”€β”€ go.mod
β”œβ”€β”€ go.sum
└── main.go
Enter fullscreen mode Exit fullscreen mode

Lastly, move to the newly created project and run it to build static contents and stuff.

$ cd your_project && go run main.go
Enter fullscreen mode Exit fullscreen mode

Random Thoughts

Doing this made me appreciate existing SSGs like Hugo and Next.js even moreπŸ‘πŸ‘

Cool existing SSGs written in Go

πŸ’– πŸ’ͺ πŸ™… 🚩
k_penguin_sato
K-Sato

Posted on January 26, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Writing a SSG in Go
go Writing a SSG in Go

January 26, 2024