teeny

A super simple static site generator

87
3
JavaScript

Teeny: A simple static site generator

โš ๏ธ Disclaimer: This is a tool I built in a couple of hours to generate my personal blog yakkomajuri.github.io. It doesnโ€™t do much right now and probably wonโ€™t ever.

Buy Me a Coffee at ko-fi.com

๐Ÿƒ Quick start

npm i -g teeny-cli # yarn global add teeny-cli
teeny init && teeny develop

For an example of a project using Teeny, check out my personal blogโ€™s repo.

๐Ÿ“– Backstory

You can read about my motivation for building Teeny on this blog post titled โ€œWhy I built my own static site generatorโ€.

๐Ÿ’ป Supported commands

Initialize a Teeny project in the current directory

teeny init

Build the static HTML files and add them to public/

teeny build

Start a local Teeny server that listens for file changes

teeny develop

๐Ÿ“„ How it works

Teeny is a super simple static site generator built to suit my needs and my needs only.

All it does is generate pages based on HTML templates and Markdown content.

It does very little and is strongly opinionated (read: I was too lazy to build customization/conditional handlers), but has allowed me to build a blog Iโ€™m happy with extremely quickly.

Essentially, there are really only 2 concepts you need to think about: templates and pages.

Templates

Templates are plain HTML and should be added to a templates/ subdirectory.

They can contain an element with the id page-content, which is where Teeny adds the HTML generated by parsing the Markdown content.

Pages

Markdown is a first-class citizen in Teeny, so all of your websiteโ€™s pages are defined by a Markdown file.

The file need not have any actual content though, so if you want a page to be defined purely in HTML you just need to create a template that is referenced from a page file.

To specify what template a page should use, you can specify it in the frontmatter of the page, like so:

---
template: blog
---

In the above example, Teeny will look for a template called blog.html. If no template is specified, Teeny looks for a default.html file in templates/ and uses that.

๐Ÿ’ก Example usage

Hereโ€™s an example of Teeny at work.

To start a Teeny project, run teeny init. This will create the following in your current directory:

.
โ”œโ”€โ”€ pages
โ”‚ย ย  โ””โ”€โ”€ index.md
โ”œโ”€โ”€ static
โ”‚ย ย  โ””โ”€โ”€ main.js
โ””โ”€โ”€ templates
    โ”œโ”€โ”€ default.html
    โ””โ”€โ”€ homepage.html

If you then run teeny build, youโ€™ll end up with this:

.
โ”œโ”€โ”€ pages
โ”‚ย ย  โ””โ”€โ”€ index.md
โ”œโ”€โ”€ public
โ”‚ย ย  โ”œโ”€โ”€ index.html
โ”‚ย ย  โ””โ”€โ”€ main.js
โ”œโ”€โ”€ static
โ”‚ย ย  โ””โ”€โ”€ main.js
โ””โ”€โ”€ templates
    โ”œโ”€โ”€ default.html
    โ””โ”€โ”€ homepage.html

index.md uses the homepage template, and together they generate index.html. As is standard with other SSGs, static files are served from public/.

Youโ€™ll also notice main.js got moved to public/ too. Teeny will actually take all non-template and non-page files from pages/, templates/, and static/ and copy them to public/, following the same structure from the origin directory.

The reason for this is that I actually didnโ€™t want to have โ€œmagicโ€ imports, where you have to import static assets from paths that do not correspond to the actual directory structure. As a result, I decided that static files would just live inside templates/ and pages/ as necessary.

Later I did surrender to the static/ directory approach though, as there may be assets both pages and templates want to use. Imports from static/ are โ€œmagicโ€, meaning you need to think about the output of teeny build for them to work.

The last command that Teeny supports is teeny develop. This creates an HTTP server to server files from the public/ subdirectory.

It listens for changes to the files and updates the static files on the fly (naively, by just rebuilding everything each time it detects a change).

๐Ÿ”ฎ Potential future improvements

I want to keep Teeny as tiny as possible. I deliberately put all the code in one file as a reminder to myself that this is supposed to just be a simple tool for me to build simple static blogs quickly.

However, it could use a few โ€œdeveloper experienceโ€ upgrades, like an optimized approach to teeny develop instead of naively rebuilding everything, as well as some better customization options.