SYS_PORTFOLIO_V1.0

"Behind You, a Syntax Error..."

?
Open this help
H / Home
Home
J
Journal
P
Projects
A
About
M
Toggle the navigation menu
← / →
Move between focused menu links
Ctrl + ← / →
Previous or next journal entry
Esc
Close this help or the navigation menu
───[ JOURNAL_ENTRY ]─── BACK_TO_ARCHIVE

How This Blog Engine Works Now with C# and Forge

───[ DM_READS ]───

You return to the engine room. The Markdown manuscripts are still on the same shelf. Beside them are two C# files. One builds the journal. The other tells Forge where to take it.

The reader gets a page. The build gets the complicated bit. This arrangement seems fair.

───[ ON_THIS_PAGE ]───

In March I wrote How This Blog Engine Works. The short version was that the browser fetched the Markdown, parsed it, and put the article on the screen. GitHub Pages served the files, and a small Node script kept the journal index up to date. It was a reasonable way to run a personal site.

Now the browser gets an HTML article. There are still Markdown files in _posts/, and I still write the journal the same way. The difference is a C# file that does the rendering before I publish. There's a second C# file that tells Forge how to get the result onto Cloudflare Pages.

This started as a change to the blog. It became a good example of why I think everyone who writes software should give C# a try, especially for small tools.

Start with a file

With the .NET 10 SDK, you can put this in count-posts.cs at the root of a Markdown blog.

var folder = args.Length > 0 ? args[0] : "_posts";
var count = Directory.EnumerateFiles(folder, "*.md").Count();
Console.WriteLine($"{count} Markdown files in {folder}");

Run it from that same directory.

dotnet run --file count-posts.cs -- _posts

That's the entire program. The SDK compiles it and runs it, with build artifacts kept in a temporary directory by default. When you need a NuGet package, you can declare it with #:package at the top of the file. #:sdk lets you pick a different SDK. If the program grows into something you'd rather maintain as a project, dotnet project convert gives you a .csproj to work from. Microsoft’s file-based app documentation covers the commands and directives.

My generator lives in scripts/blog.cs. It uses Markdig for Markdown, System.CommandLine for the CLI, and the ASP.NET Core SDK for local preview. Those are ordinary .NET libraries, available from one source file. The generator has commands for scaffolding a site, creating a post, building the output, and serving it locally. The little counting script and the generator use the same runtime and standard library.

What happens when I build the journal

I build and preview from the root of the site repository.

dotnet run --file scripts/blog.cs -- build .
dotnet run --file scripts/blog.cs -- serve dist

The portfolio build reads the dated files in _posts/ and their front matter. It leaves future-dated posts out of the build, using the UTC date. For each published post, Markdig turns the body into HTML. The generator puts it into the article template and adds the section links, metadata, category links, and previous and next entries. That same set of posts supplies the homepage's latest entries and the journal index.

Each post gets its own HTML file under dist/journal/. The old journal/post.html?slug=... URLs still open the corresponding published article. That keeps the links in older posts working.

The part I care about is where the work happens. The build has the Markdown and all the post metadata in front of it at once. It can produce the article, the archive entry, and the share metadata together. The browser still handles things like category filtering and keyboard shortcuts. Reading an article starts with the HTML page itself. The local server and the public host both serve the files that come out of dist/.

Forge does the publishing

The other file, forge-delivery.cs, is the delivery contract. Forge runs three steps in order.

Build the site ──► npm ci ──► Upload dist/ to Cloudflare Pages

The build runs the same C# command I use locally. npm ci installs the version of Wrangler pinned in the lockfile. The upload step reads the Cloudflare credentials from the local Forge vault and passes them to the deploy process through its environment. The credentials live on the machine running the contract.

With the Forge packages and vault set up, I use draft to inspect the plan and weave to run it.

dotnet run --file forge-delivery.cs -- draft
dotnet run --file forge-delivery.cs -- weave

For the last publish, I ran weave from a clean checkout of the commit I wanted to ship. Forge built the site, installed Wrangler, and uploaded dist/ using Cloudflare Pages Direct Upload. Then I checked the Pages URL and ioni.solarz.me. Cloudflare serves the result as static files. C# runs on the build machine, where it has something to do.

I like being able to read the deployment in the same language as the tool it runs. When I wonder which command produces dist/, or where the upload gets its credentials, I can open the contract and follow it from top to bottom.

Give C# one of your small jobs

I've used C# for systems that look nothing like this journal. A desktop tool spends its time on layout and input. A terminal app cares about which cells it has to redraw. A blog generator walks files and produces HTML. The interesting parts change, and I get to bring the same language, libraries, debugger, and tools with me.

For me, that's what makes C# good at a surprising amount of everyday work. I can start with a file that renames things, checks a folder, calls an API, or generates some HTML. The first version can be one file. If it turns into a tool I run every day, the language and the SDK have somewhere for it to go.

If you haven't tried C# in a while, give it one of the little jobs on your list. My blog needed to turn a directory of Markdown into a directory of HTML. That turned out to be a pretty good place to start.

Licensed under CC BY-NC-ND 4.0. You may share with attribution under the license terms.