How Trio worked

A walk through Trio 6, written from the published package itself: its source, its command line help and the starter project it shipped with.

Version 6.1.0, May 2021

The idea

Most generators ask you to learn a template language. Trio did not. Its README described it as fast, simple yet powerful JavaScript driven static site generation, and the JavaScript part was the point: anything dynamic on a page was done by a small Node module working on the HTML, with cheerio doing the selecting, much as you would with jQuery in a browser.

The rest was plain files in a folder called source, and a build that wrote finished pages to public while you worked, or to release when you were ready to ship.

The project folder

A new project, made with trio new, had this shape. Each folder had one job.

my-site/
  trio.json            settings: blogFolderName, baseUrl, permalinks, ignore
  source/
    fragments/         the content, in Markdown or HTML
      articles/        blog posts, dated in the file name
    templates/         page shells
    includes/          shared pieces such as a header
    callbacks/         JavaScript modules called from tags
    filters/           modules that pick items for a collection
    data/              JSON files, available to callbacks
    sass/              stylesheets, compiled with Dart Sass
    media/             images and other files
    scripts/           browser JavaScript
    etc/               copied as is to the site root (robots.txt, favicon)

A fragment and its template

A fragment carried its settings at the top inside an HTML comment, rather than the usual triple dashes. This is the about page from Trio's own starter project:

<!--
template: about
title: "| About"
appendToTarget: true
activeHeaderItem: 3
-->

<h1 data-trio-callback="getBrand">About </h1>

The page text, in Markdown.

The template it names is ordinary HTML. The element marked data-trio-fragment receives the content, and data-trio-include pulls in a shared file from source/includes:

<body>
  <header data-trio-include="header.html" data-trio-callback="header"></header>
  <main>
    <section data-trio-fragment class="container"></section>
  </main>
  <footer data-trio-include="footer.html"></footer>
</body>

Callbacks instead of a template language

Any tag could name a callback with data-trio-callback. At build time Trio loaded that module from source/callbacks and called it with the tag, the whole page, the current file, the site's metadata and cheerio itself. The footer callback in the starter project is one line:

module.exports = ({ $tag, site }) => {
    $tag.append(site.timestamp);
};

Because it is just Node, a callback could read the JSON in source/data, build a list of related articles, or set the page title, without anything new to learn.

Blogging and collections

Trio knew about blogs. Articles went in source/fragments/articles with the date at the start of the file name, such as 2019-01-01-article1.md, and could carry a category and tags. From those Trio built the sorted catalogues behind category pages, tag pages, an archive and related article lists. A line in the article marked <!-- end --> split off the excerpt.

Collections, added in version 2, let one fragment produce many pages. A fragment named a filter module and how many items each page should hold, and Trio generated the pages, which is how the starter blog paginated its listing.

The commands

trio new pathA new, bare project. Add -s for the starter project with a blog.
trio buildBuilds into public. -i builds incrementally, -w watches for changes, -s serves it in the browser, and -I does all three.
trio releaseBuilds for release into release.
trio cachebustAdds content hashes to the release build's file names, so browsers fetch new versions.
trio serveServes public, or release with -r.

Each command also had a one letter alias: n, b, r, c and s.

Builds that only did what changed

Trio kept file stats in a .cache folder and, on an incremental build, regenerated only what had gone stale. Before building it checked every chain of dependencies, from fragment to template to include to callback, and reported anything it could not resolve rather than writing a broken page. Fragments with no front matter were skipped and reported, and from 4.3.1 a fragment whose name started with an underscore counted as a work in progress.

Stylesheets went through Dart Sass, then PostCSS with autoprefixer and cssnano, with source maps you could switch on or off per build type in trio.json. The generated HTML was tidied with js-beautify.

Could you still run it?

The package is still on npm and the code is MIT licensed on GitHub. The last dependency update was for Node 14, which reached its end of life in 2023, so a current Node may complain. If you only need the site, you do not need Trio at all: the release folder from your last build is the whole website. Here is how to put it online.

Ship the folder

Your last Trio build still works as a website. Put it on any static host in a few minutes.

Host a static site
Hosted by Clay