How this site works.md
This is a static site generated by a minimal build system I wrote in Node.js. My goal was to have it be evergreen enough so that I can pick it back up again a year later and not have to pull my hair out upgrading a bunch of outdated libraries to new major versions with entirely different APIs.
Intentionally by design, there's absolutely no framework, just a simple custom builder, a few standard plugins, and some Nunjucks templates.
The stack:
- Content is written in Contentful
- Pages are rendered with Nunjucks
- Markdown is rendered with markdown-it
- Comments are powered by giscus
- Typeset in Fira Mono
- Hosted on Render
- Served through Cloudflare
The source is on GitHub.
File tree
.
├── dist/ # build output
├── lib/
│ ├── plugins/ # clean, contentful, copy, watch, …
│ ├── builder.js # runs plugins, renders targets
│ ├── markdown.js # markdown renderer
│ └── nunjucks.js # nunjucks environment
├── scripts/
│ ├── build.js # npm run build
│ └── serve.js # npm run server
├── src/
│ ├── parts/ # nav, comments, …
│ ├── static/ # css, js, img
│ ├── layout.njk # common layout
│ └── page.njk # dynamic page template
└── config.js # build configuration
Build config
A build is defined in config.js. It exports only three things: a render function, a set of plugins, and an array of targets.
export default {
render,
plugins: [
clean('dist'),
contentful({ client, sources }),
copy({
from: 'src/static',
to: 'dist',
}),
watch({
dir: 'src',
enabled: isDevelopment,
async handler(ctx, builder) {
await builder.build();
},
}),
],
targets: [/* … */],
};
Every build runs the same three steps, in order:
run(ctx) → render(targets) → done(ctx)
run: each plugin'srunhook is called in sequence. Plugins all share a singlectxobject and modify it.render: each target renders a template with data fromctxand writes it todist/.done: once all targets are rendered, each plugin'sdonehook is called (if it has one).
Plugins
A plugin is a function that returns an object with up to two hooks. Most only export run:
const posts = () => ({
async run(ctx) {
ctx.posts = await fetchPosts();
},
});
Because plugins run in order, later ones can leverage the context modified by earlier ones. The ones this site uses (in order):
cleandeletesdist/so every build starts fresh.contentfulfetches every entry of each content type and adds them toctx(ctx.pages,ctx.blurbs). In development it caches the response in.cache/for a day, so rebuilding doesn't clobber the API and eat into my free tier limits.copycopiessrc/static/(CSS, JS, images) intodist/.watchis the only plugin that usesdone. After all targets have rendered, it watchessrc/and rebuilds when something changes.
Targets
A target renders one template to one file. It can only access the properties of ctx it specifies in include (or '*' for everything), merged with extraContext (if defined):
{
template: 'journal.njk',
dest: dist('journal/index.html'),
include: ['pages', 'blurbs'],
}
A target can also be a function of ctx that return a target or array of targets. That's how one template becomes one page per post:
(ctx) => ctx.pages.map((page) => [
{
template: 'page.njk',
dest: dist(`${page.fields.url}/index.html`),
include: '*',
extraContext: page.fields,
},
])
Templates
Pages are rendered with Nunjucks templates. Each page extends a common layout and inserts content into its blocks:
{% extends "layout.njk" %}
{% block content %}
{{ content | markdown | safe }}
{% endblock %}
Post content is written in Contentful as markdown. The markdown filter runs it through markdown-it with plugins for heading anchors, footnotes, superscript, KaTeX for math, a table of contents, and highlight.js for code.
Scripts
There are two npm scripts:
npm run build # run the build once
npm run server # serve dist/ on http://localhost:3000
build takes two flags:
npm run build -- --watch # rebuild when src/ changes
npm run build -- --no-cache # always fetch fresh data from Contentful
Deployment
I just push to origin master.
To deploy, Render runs npm run build and serves dist/.
There are two ways a deploy gets triggered:
- Code changes: Render is connected to GitHub, so any pushes to
masterdeploy automatically. - Content changes: When CMS content is published, Contentful calls a Render deploy hook which rebuilds the site with fresh data.
Comments