Files
blog/plugins/handlebars-html.js
T
Marcus Noble 7a5c031b1d feat: modernize static site generator stack
- Node.js: Upgraded from v16 to v22 (latest LTS)
- SASS: Replaced deprecated node-sass with Dart Sass (sass package)
- 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
  - Added metalsmith-define for site metadata
  - Added gray-matter for YAML front matter extraction
- Markdown rendering: Updated to marked v18 for GFM support
- Express: Kept at v4.x (stable, compatible)
- Handlebars: Updated to v4.7 (latest)
- Custom plugins: Created local permalinks and handlebars-html plugins
  - Custom permalinks plugin replaces metalsmith-permalinks (broken substitute dep)
  - Custom handlebars jstransformer wrapper supports .html extension
- Feed generation: Custom RSS plugin (replaces deprecated metalsmith-feed)
- filterRoutes: Updated regex patterns for Express compatibility
- Template changes: Removed deprecated markdown="1" from partials
- Docker: Updated Dockerfile to use Node 22 Alpine
- Collection patterns: Updated to support both .md and .markdown extensions
- Pipeline: Reordered for correct metadata flow (front matter → markdown → layouts → permalinks)
2026-11-16 10:39:12 +00:00

100 lines
3.1 KiB
JavaScript

const fs = require("fs");
const path = require("path");
const Handlebars = require("handlebars");
const moment = require("moment");
const striptags = require("striptags");
const { marked } = require("marked");
// Setup marked renderer
marked.setOptions({ gfm: true, breaks: true });
const renderer = new marked.Renderer();
const originalTable = renderer.table.bind(renderer);
renderer.table = function (e) {
let result = originalTable(e);
return result.replace("<table>", '<table role="grid">');
};
// Register helpers on Handlebars
Handlebars.registerHelper("markdown", function (text) {
if (!text) return "";
return marked.parse(text, { renderer });
});
Handlebars.registerHelper("moment", function (date, format) {
return new moment(date).format(format);
});
Handlebars.registerHelper("striptags", function (text) {
return striptags(text);
});
Handlebars.registerHelper("buildTitle", function (title, siteTitle) {
if (title.indexOf(siteTitle) < 0) {
title = "'" + title + "' by " + siteTitle;
}
return title;
});
Handlebars.registerHelper("jointags", function (tags) {
return (tags || "").split(" ").join(",");
});
Handlebars.registerHelper("encodetitle", function (str) {
return (str || "").replaceAll(" ", "%2B");
});
// Load all partials from templates/partials directory
const partialsDir = path.join(__dirname, "..", "templates", "partials");
if (fs.existsSync(partialsDir)) {
fs.readdirSync(partialsDir).forEach((file) => {
const name = path.parse(file).name;
const content = fs.readFileSync(path.join(partialsDir, file), "utf8");
Handlebars.registerPartial(name, content);
});
}
// Compile a template file (used by renderFileAsync)
function compile(filePath) {
const template = fs.readFileSync(filePath, "utf8");
return {
fn: Handlebars.compile(template),
dependencies: [],
};
}
// Render a template string with locals
function render(templateStr, opts, locals) {
const template = Handlebars.compile(templateStr);
const output = template(locals);
return { body: output, dependencies: [] };
}
// Render a template file with locals
async function renderFileAsync(filePath, opts, locals) {
const template = fs.readFileSync(filePath, "utf8");
const compiled = Handlebars.compile(template);
const output = compiled(locals);
return { body: output, dependencies: [] };
}
// Assemble the jstransformer interface
const jstransformer = {
name: "handlebars",
inputFormats: ["hbs", "handlebars", "html"],
outputFormat: "html",
compile: function (templateStr) {
const compiled = Handlebars.compile(templateStr);
return { fn: compiled, dependencies: [] };
},
render: function (templateStr, opts, locals) {
return render(templateStr, opts, locals);
},
renderAsync: async function (templateStr, opts, locals) {
return render(templateStr, opts, locals);
},
renderFileAsync: async function (filePath, opts, locals) {
return renderFileAsync(filePath, opts, locals);
},
registerPartial: Handlebars.registerPartial.bind(Handlebars),
registerHelper: Handlebars.registerHelper.bind(Handlebars),
partials: Handlebars.partials,
helpers: Handlebars.helpers,
};
module.exports = jstransformer;