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;