DEV Community

K-Sato
K-Sato

Posted on

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

Top comments (7)

Collapse
 
developerbishwas profile image
Bishwas Bhandari

cool work dude

Collapse
 
dcc profile image
Dario Castaรฑรฉ • Edited

Thanks for mentioning Zas!

Collapse
 
joemama123 profile image
Your Average Roblox Game Dev

it doesn't work

Collapse
 
k_penguin_sato profile image
K-Sato • Edited

could u share the error msgs and stuff?

Collapse
 
joemama123 profile image
Your Average Roblox Game Dev

im just joking i just wanted to be the first viewer and commenter

Thread Thread
 
k_penguin_sato profile image
K-Sato • Edited

dude....๐Ÿ˜…
thanks for commenting anyways!

Collapse
 
allen_douglas_8296ca1a6d1 profile image
Allen Douglas

Iโ€™m grateful for the effort you put into these posts, they are always a great read. BUY NCR Ranger Armor Coat

Some comments may only be visible to logged-in visitors. Sign in to view all comments.