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
 
dcc profile image
Dario CastaΓ±Γ© • Edited

Thanks for mentioning Zas!

Collapse
 
developerbishwas profile image
Bishwas Bhandari

cool work dude

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.