A blog with one Bun script
How this site gets a blog without a framework: markdown in, static HTML out, build-time highlighting, RSS, and one decision about where the output lives.
This site is plain HTML, one stylesheet and a small script. It runs as static assets on Cloudflare Workers. It used to be a React single-page app, and I stripped it down to what a personal page needs. When I wanted a blog, I set one rule: the blog must not bring back what I had removed.
Requirements
I wrote four requirements. They ruled out most options.
- I write a post as one markdown file.
- A build step turns the file into static HTML. Nothing runs on the Worker.
- The blog looks like the rest of the site, with the same fonts, palette, header and footer.
- Post pages load no client-side JavaScript.
A framework, a hosted CMS and a renderer at request time all fail this list. One script passes it.
The script
The build is one Bun script of about 300 lines with no dependencies. It reads every markdown file in a posts folder, parses a small front matter block and writes three things:
- an index page, newest first
- one page for each post at
/blog/<slug>/ - an RSS feed
The file name is the slug. The script rejects any name that is not lowercase letters, digits and hyphens, so a URL cannot surprise me. The front matter has five flat fields: title, date, description, tags and a draft flag. I wrote a small parser for it instead of adding a YAML library, because five flat fields do not need one. A missing required field stops the build and names the file.
Markdown comes with the runtime
Bun ships a markdown renderer. It turns markdown into HTML. One option adds an id and an anchor link to each heading, and another turns bare URLs into links. Tables, lists and fenced code come out as I expect them.
Highlighting at build time
Code highlighting is where a blog usually gains a script tag. I wanted no script, so the build does the work. After the markdown becomes HTML, a function finds each fenced code block and reads its language. It wraps tokens in span elements with classes for comments, strings, numbers and keywords. The stylesheet colors those classes with the palette of the site.
The tokenizer is one regular expression with named groups, tried from left to right. It is not a parser and does not claim to be. It handles comments, strings, numbers and common keywords for the languages I write. It leaves the rest as plain text. A mistake costs one color and never breaks a page.
One bug showed up in testing. A function parameter named fn appeared as a keyword, because fn is a keyword in Rust. The fix applies that keyword only to Rust blocks.
What every page carries
A post page has what a link needs to look right and to be found. It has a canonical URL on the same host as the home page. It also has Open Graph and Twitter tags, the date, the tags, a reading time and a link to the feed. The reading time is the word count outside code blocks, divided by 220 and rounded, with a minimum of one minute.
The layout reuses the stylesheet of the site and adds a second, hand-written stylesheet for the blog. The text column is about 62 characters wide, the same measure as the home page. Long code lines scroll sideways and do not wrap.
Drafts
A post with draft: true is not built. The --drafts flag includes drafts, and --out writes to another folder. Together they let me preview unfinished work in a scratch folder. A draft never reaches the real output. After a build, I serve the folder with a throwaway local server and request each page, the index and the feed.
Where the output lives
The Worker has no build step. It serves a folder as it is. So either the deploy command must run my script, or I must commit the generated HTML. I chose to commit the output. The deploy stays one command. Every diff shows the exact pages I publish. The cost is a few generated files in the history, which is small.
One habit keeps this safe. I run the plain build before every commit, so a draft cannot slip in.
What it gives me
Two readable files decide how a post looks. I can write a post anywhere as a single file. The blog has no dependencies to update. It also has no plugins, no image pipeline and no search. For a personal blog with a handful of posts, I would rather own 300 lines than rent a framework.