Files
blog/plugins/permalinks.js
T
Marcus Noble 0f021e9672 feat: modernize static site generator stack
- Upgrade from Node 16 to Node 22
- Replace deprecated node-sass with Dart Sass (sass)
- Migrate to @metalsmith/* scoped packages for markdown, layouts, in-place, collections
- Replace deprecated metalsmith-markdownit with @metalsmith/markdown + Marked
- Replace deprecated metalsmith-in-place v1 with @metalsmith/in-place v5
- Replace deprecated metalsmith-layouts v1 with @metalsmith/layouts v3
- Update @metalsmith/collections to v2
- Use custom permalinks plugin (upstream incompatible with new regexparam)
- Migrate filterRoutes.js to Express 4 compatibility
- Add Handlebars partial preprocessing for inline {{> partial }} in markdown
- Add custom feed generation (replaces deprecated metalsmith-feed)
- Update all other dependencies to latest versions
- Update Dockerfile for Node 22 Alpine
- Update .nvmrc to 22
2026-11-16 09:01:33 +00:00

139 lines
3.8 KiB
JavaScript

const path = require("path");
/**
* Custom Metalsmith permalinks plugin.
* Replaces `:date-:title` style patterns using file metadata.
*/
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);
}
}
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) => {
const value = data[key];
if (value === undefined || value === null) {
if (optional) return "";
throw new Error(
`Could not substitute ':${key}' in pattern '${pattern}', '${key}' is undefined`
);
}
if (value instanceof Date) {
return formatDate(value);
}
return slugify(value.toString());
});
}
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 entries = Object.entries(files);
for (const [file, data] of entries) {
// Skip non-HTML files
if (!file.endsWith(".html")) continue;
// Skip if permalink is explicitly disabled
if (data.permalink === false) continue;
// Check file-specific permalink first
if (data.permalink) {
let result = replacePattern(data.permalink, data);
if (opts.trailingSlash) result = result.replace(/\/+$/, "");
data.permalink = result;
continue;
}
// Determine which linkset to use (or default)
const linkset = getMatchedLinkset(data, opts.linksets) || opts;
const pattern = linkset.pattern || opts.pattern;
// 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;
}
done();
};
}
module.exports = permalinks;