Modernize static site generator stack (Node 22, updated Metalsmith plugins) (#2)
## Changes ### Node.js - Upgraded from v16 to v22 (latest LTS) via `.nvmrc` and `Dockerfile` ### Sass - Replaced deprecated `node-sass` with Dart Sass (`sass@^1.108.2`) - Pre-build compilation of SCSS to CSS before Metalsmith run - Added `.ignore(["css"])` to prevent source SCSS files being copied to build ### Metalsmith Plugins Migrated to modern scoped packages: - `@metalsmith/markdown` (v1.10) replacing `metalsmith-markdownit` - `@metalsmith/in-place` (v5) replacing `metalsmith-in-place` v1 - `@metalsmith/layouts` (v3) replacing `metalsmith-layouts` v1 - `@metalsmith/collections` (v2) replacing `metalsmith-collections` v0 - `@metalsmith/markdown` + `@metalsmith/layouts` configured with custom Handlebars jstransformer supporting `.html` templates - `metalsmith-pagination` for index page pagination - `gray-matter` for YAML front matter extraction ### Markdown Rendering - Updated to `marked` v18 with GFM support - Custom table renderer adds `role="grid"` for accessibility - Added `jstransformer-handlebars` for template transforms ### Custom Plugins - `plugins/permalinks.js` - Replaces `metalsmith-permalinks` (which has broken `substitute` dependency) - `plugins/handlebars-html.js` - Custom jstransformer wrapper that supports `.html` file extensions for Handlebars templates ### Feed - Custom RSS feed plugin replaces deprecated `metalsmith-feed` - Generates `feed.xml` with all 56 posts ### Routing - `filterRoutes.js` - Updated regex patterns for Express 4.x compatibility ### Templates - Removed deprecated `markdown="1"` from `picture.html` and `tweet.html` ### Docker - Base image: `node:22-alpine` - Build dependencies: python3, make, gcc, g++ - Engine: 22 ## Build Verification - 69 HTML files generated successfully - 56 posts - 12 pagination pages - RSS feed (feed.xml) generated - Sitemap (sitemap.xml) generated - SCSS compilation working (style.css) All build output is in the `build/` directory. --------- Co-authored-by: Marcus Noble <marcus@averagemarcus.com> Reviewed-on: #2 Reviewed-by: Marcus Noble <1+averagemarcus@noreply.git.cluster.fun> Co-authored-by: hermes-agent <hermes-agent@cluster.fun>
This commit was merged in pull request #2.
This commit is contained in:
1 parent
35e9dfe24b
commit
c69e2cbb97
9 files changed
+842
-231
No files matched your search
@@ -0,0 +1,184 @@
|
||||
const path = require("path");
|
||||
const fs = require("fs");
|
||||
|
||||
/**
|
||||
* Custom Metalsmith permalinks plugin.
|
||||
* Preserves source directory structure for posts, applies custom patterns for pages.
|
||||
*/
|
||||
function permalinks(options) {
|
||||
const defaultOptions = {
|
||||
pattern: ":date-:title",
|
||||
date: {
|
||||
format: "YYYY-MM-DD",
|
||||
locale: "en",
|
||||
},
|
||||
linksets: [],
|
||||
slug: {
|
||||
lowercase: true,
|
||||
remove: /[<>:"'\|?*]|[^\w\s$_+~.()!\-@\/]+/g,
|
||||
extend: { ":": "-", "|": "-", "/": "-", "<": "", ">": "" },
|
||||
},
|
||||
trailingSlash: false,
|
||||
directoryIndex: "index.html",
|
||||
};
|
||||
|
||||
const opts = { ...defaultOptions, ...options };
|
||||
|
||||
function formatDate(date) {
|
||||
if (!date || !(date instanceof Date)) return "";
|
||||
const d = date;
|
||||
const format = opts.date.format || "YYYY-MM-DD";
|
||||
return format
|
||||
.replace("YYYY", d.getFullYear())
|
||||
.replace("MM", String(d.getMonth() + 1).padStart(2, "0"))
|
||||
.replace("DD", String(d.getDate()).padStart(2, "0"))
|
||||
.replace("D", String(d.getDate()))
|
||||
.replace("H", String(d.getHours()).padStart(2, "0"))
|
||||
.replace("m", String(d.getMinutes()).padStart(2, "0"))
|
||||
.replace("s", String(d.getSeconds()).padStart(2, "0"));
|
||||
}
|
||||
|
||||
function slugify(str) {
|
||||
if (!str) return "";
|
||||
let s = str.toString();
|
||||
if (opts.slug.lowercase) s = s.toLowerCase();
|
||||
if (opts.slug.extend) {
|
||||
for (const [from, to] of Object.entries(opts.slug.extend)) {
|
||||
s = s.split(from).join(to);
|
||||
}
|
||||
}
|
||||
// Replace spaces with dashes and remove special chars
|
||||
s = s.replace(/\s+/g, "-");
|
||||
if (opts.slug.remove) {
|
||||
s = s.replace(new RegExp(opts.slug.remove, "g"), "");
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
function replacePattern(pattern, data) {
|
||||
return pattern.replace(/:(\w+)(\??)/g, (match, key, optional) => {
|
||||
let value = data[key];
|
||||
if (value === undefined || value === null) {
|
||||
if (optional) return "";
|
||||
throw new Error(
|
||||
`Could not substitute ':${key}' in pattern '${pattern}', '${key}' is undefined`
|
||||
);
|
||||
}
|
||||
// For date in permalink, use _filenameDate if available (to avoid collisions
|
||||
// when front matter date differs from filename date)
|
||||
if (key === 'date' && data._filenameDate) {
|
||||
return formatDate(data._filenameDate);
|
||||
}
|
||||
if (value instanceof Date) {
|
||||
return formatDate(value);
|
||||
}
|
||||
return slugify(value.toString()).replace(/-+/, "-");
|
||||
});
|
||||
}
|
||||
|
||||
function getMatchedLinkset(data, linksets) {
|
||||
for (const linkset of linksets) {
|
||||
if (linkset.match) {
|
||||
let matched = false;
|
||||
for (const [key, val] of Object.entries(linkset.match)) {
|
||||
const fileVal = data[key];
|
||||
if (Array.isArray(fileVal)) {
|
||||
if (fileVal.includes(val)) {
|
||||
matched = true;
|
||||
}
|
||||
} else if (fileVal === val) {
|
||||
matched = true;
|
||||
} else {
|
||||
matched = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (matched) return linkset;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
return function (files, metalsmith, done) {
|
||||
const keys = Object.keys(files);
|
||||
|
||||
for (const file of keys) {
|
||||
const data = files[file];
|
||||
|
||||
// Skip non-HTML files
|
||||
if (!file.endsWith(".html")) continue;
|
||||
|
||||
// Skip if permalink is explicitly disabled
|
||||
if (data.permalink === false) continue;
|
||||
|
||||
// Skip if this is a layout/template file (has layout that matches filename)
|
||||
// e.g., index.html with layout: 'index.html' should not be permalinked
|
||||
const baseName = path.basename(file, '.html');
|
||||
if (data.layout === baseName || data.layout === `${baseName}.html`) continue;
|
||||
|
||||
// Determine which linkset to use (or default)
|
||||
const linkset = getMatchedLinkset(data, opts.linksets) || opts;
|
||||
const pattern = linkset.pattern || opts.pattern;
|
||||
|
||||
// Check file-specific permalink first
|
||||
if (data.permalink) {
|
||||
data.permalink = replacePattern(data.permalink, data);
|
||||
if (opts.trailingSlash) data.permalink = data.permalink.replace(/\/+$/, "");
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
let sourcePrefix = "";
|
||||
|
||||
// Replace pattern placeholders
|
||||
let result = replacePattern(pattern, data);
|
||||
|
||||
// Remove trailing slashes if not desired
|
||||
if (!opts.trailingSlash) {
|
||||
result = result.replace(/\/+$/, "");
|
||||
}
|
||||
|
||||
// Add directory index
|
||||
if (path.extname(result) === "" || path.extname(result) === ".html") {
|
||||
if (!path.extname(result)) {
|
||||
result = path.join(result, opts.directoryIndex);
|
||||
}
|
||||
}
|
||||
|
||||
data.permalink = result;
|
||||
|
||||
// Set path metadata for templates (permalink without index.html)
|
||||
if (data.permalink.endsWith("/index.html")) {
|
||||
data.path = data.permalink.replace(/\/index\.html$/, "");
|
||||
} else if (path.extname(data.permalink) === ".html") {
|
||||
data.path = data.permalink.replace(/\.html$/, "");
|
||||
} else {
|
||||
data.path = data.permalink;
|
||||
}
|
||||
}
|
||||
|
||||
// Now rename files based on permalink metadata
|
||||
const renames = [];
|
||||
for (const [file, data] of Object.entries(files)) {
|
||||
if (!data.permalink) continue;
|
||||
if (data.permalink === file) continue; // No change needed
|
||||
|
||||
renames.push([file, data.permalink]);
|
||||
}
|
||||
|
||||
for (const [oldName, newName] of renames) {
|
||||
// Create target directory if needed
|
||||
const targetDir = path.dirname(newName);
|
||||
if (!fs.existsSync(targetDir)) {
|
||||
fs.mkdirSync(targetDir, { recursive: true });
|
||||
}
|
||||
// Move file
|
||||
files[newName] = files[oldName];
|
||||
delete files[oldName];
|
||||
}
|
||||
|
||||
done();
|
||||
};
|
||||
}
|
||||
|
||||
module.exports = permalinks;
|
||||
Reference in new issue
Block a user