Compare commits
1
Commits
0f021e9672
...
7a5c031b1d
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7a5c031b1d |
No files matched your search
+2
-2
@@ -1,11 +1,11 @@
|
|||||||
FROM node:16.17-alpine
|
FROM node:22-alpine
|
||||||
|
|
||||||
RUN apk update && apk add python3 make gcc g++
|
RUN apk update && apk add python3 make gcc g++
|
||||||
|
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
|
|
||||||
ADD package.json .
|
ADD package.json .
|
||||||
RUN npm install
|
RUN npm install --production
|
||||||
|
|
||||||
ADD . .
|
ADD . .
|
||||||
|
|
||||||
|
|||||||
@@ -1,82 +1,97 @@
|
|||||||
"use strict";
|
"use strict";
|
||||||
const fs = require('fs');
|
const fs = require("fs");
|
||||||
const https = require('https');
|
const path = require("path");
|
||||||
const express = require('express');
|
const express = require("express");
|
||||||
const compress = require('compression');
|
const compress = require("compression");
|
||||||
const app = express();
|
const Metalsmith = require("metalsmith");
|
||||||
const Metalsmith = require('metalsmith');
|
const matter = require("gray-matter");
|
||||||
const inplace = require('metalsmith-in-place');
|
const inPlace = require("@metalsmith/in-place");
|
||||||
const layouts = require('metalsmith-layouts');
|
const layouts = require("@metalsmith/layouts");
|
||||||
const markdown = require('metalsmith-markdownit');
|
const markdown = require("@metalsmith/markdown");
|
||||||
const permalinks = require('metalsmith-permalinks');
|
const permalinks = require("./plugins/permalinks");
|
||||||
const collections = require('metalsmith-collections');
|
const collections = require("@metalsmith/collections");
|
||||||
const pagination = require('metalsmith-pagination');
|
const pagination = require("metalsmith-pagination");
|
||||||
const define = require('metalsmith-define');
|
const define = require("metalsmith-define");
|
||||||
const feed = require('metalsmith-feed');
|
const sitemap = require("metalsmith-sitemap");
|
||||||
const sass = require('metalsmith-sass');
|
const Handlebars = require("handlebars");
|
||||||
const date = require('metalsmith-build-date');
|
const moment = require("moment");
|
||||||
const sitemap = require('metalsmith-sitemap');
|
const striptags = require("striptags");
|
||||||
const Handlebars = require('handlebars');
|
const { marked } = require("marked");
|
||||||
const emoji = require('markdown-it-emoji');
|
const RSS = require("rss");
|
||||||
const moment = require('moment');
|
const sass = require("sass");
|
||||||
const striptags = require('striptags');
|
|
||||||
|
|
||||||
const port = process.env.PORT || 8000;
|
const port = process.env.PORT || 8000;
|
||||||
|
|
||||||
app.disable('x-powered-by');
|
const app = express();
|
||||||
|
|
||||||
|
app.disable("x-powered-by");
|
||||||
app.use(compress());
|
app.use(compress());
|
||||||
app.use(express.static(__dirname + '/build'));
|
app.use(express.static(__dirname + "/build"));
|
||||||
|
|
||||||
// Lets try and slow down some of those exploit crawlers
|
// Lets try and slow down some of those exploit crawlers
|
||||||
app.use("/", require('./filterRoutes'));
|
app.use("/", require("./filterRoutes"));
|
||||||
|
|
||||||
// Redirects
|
// Redirects
|
||||||
app.use("/", require('./redirects'));
|
app.use("/", require("./redirects"));
|
||||||
|
|
||||||
// Handle some iOS icon 404s
|
// Handle some iOS icon 404s
|
||||||
app.get("/apple-touch-icon*", function (req, res) {
|
app.get("/apple-touch-icon*", function (req, res) {
|
||||||
res.sendFile(__dirname + '/build/images/favico/' + req.url, () => {
|
res.sendFile(__dirname + "/build/images/favico/" + req.url, () => {
|
||||||
res.sendFile(__dirname + '/build/images/favico/apple-touch-icon.png');
|
res.sendFile(__dirname + "/build/images/favico/apple-touch-icon.png");
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
app.get("/images/favico/*.png", function (req, res) {
|
app.get("/images/favico/*.png", function (req, res) {
|
||||||
res.sendFile(__dirname + '/build/images/favico/apple-touch-icon.png');
|
res.sendFile(__dirname + "/build/images/favico/apple-touch-icon.png");
|
||||||
});
|
});
|
||||||
|
|
||||||
app.get("/favicon.png", function (req, res) {
|
app.get("/favicon.png", function (req, res) {
|
||||||
res.sendFile(__dirname + '/build/images/favico/apple-touch-icon.png');
|
res.sendFile(__dirname + "/build/images/favico/apple-touch-icon.png");
|
||||||
});
|
});
|
||||||
|
|
||||||
app.get("/robots.txt", function (req, res) {
|
app.get("/robots.txt", function (req, res) {
|
||||||
res.send("User-agent: * Disallow: ");
|
res.send("User-agent: * Disallow: ");
|
||||||
})
|
|
||||||
|
|
||||||
app.get(/(\/(feeds?|rss|atom)\/?|feed.xml|rss.xml|index.rss|feed.rss)$/, function(req, res) {
|
|
||||||
res.redirect(301, '/feed.xml');
|
|
||||||
});
|
});
|
||||||
|
|
||||||
app.get(/.+\/manifest.json$/, function(req, res) {
|
app.get(
|
||||||
res.redirect(301, '/manifest.json');
|
/\/(feeds?|rss|atom)\/?|feed\.xml|rss\.xml|index\.rss|feed\.rss$/,
|
||||||
|
function (req, res) {
|
||||||
|
res.redirect(301, "/feed.xml");
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
app.get(/.+\/manifest\.json$/, function (req, res) {
|
||||||
|
res.redirect(301, "/manifest.json");
|
||||||
});
|
});
|
||||||
|
|
||||||
app.get("/healthz", function (req, res) {
|
app.get("/healthz", function (req, res) {
|
||||||
res.sendStatus(200);
|
res.sendStatus(200);
|
||||||
});
|
});
|
||||||
|
|
||||||
var md = markdown({html: true});
|
// --- Markdown renderer setup ---
|
||||||
md.parser.use(emoji);
|
marked.setOptions({
|
||||||
const proxy = (tokens, idx, options, env, self) => self.renderToken(tokens, idx, options);
|
gfm: true,
|
||||||
const defaultTableOpenRenderer = md.parser.renderer.rules.table_open || proxy;
|
breaks: true,
|
||||||
md.parser.renderer.rules.table_open = function(tokens, idx, options, env, self) {
|
});
|
||||||
tokens[idx].attrJoin("role", "grid");
|
|
||||||
return defaultTableOpenRenderer(tokens, idx, options, env, self)
|
const renderer = new marked.Renderer();
|
||||||
|
|
||||||
|
// Custom table renderer to add role="grid" (matches original behavior)
|
||||||
|
const originalTable = renderer.table.bind(renderer);
|
||||||
|
renderer.table = function (e) {
|
||||||
|
let result = originalTable(e);
|
||||||
|
return result.replace("<table>", '<table role="grid">');
|
||||||
};
|
};
|
||||||
|
|
||||||
Handlebars.registerHelper('markdown', function(text) {
|
const customRenderer = (source, _opts, _context) => {
|
||||||
|
return marked.parse(source, { renderer });
|
||||||
|
};
|
||||||
|
|
||||||
|
// --- Handlebars helpers ---
|
||||||
|
Handlebars.registerHelper("markdown", function (text) {
|
||||||
if (!text) return;
|
if (!text) return;
|
||||||
return md.parser.render(text);
|
return customRenderer(text, {}, {});
|
||||||
});
|
});
|
||||||
Handlebars.registerHelper('moment', function(date, format) {
|
Handlebars.registerHelper("moment", function (date, format) {
|
||||||
return new moment(date).format(format);
|
return new moment(date).format(format);
|
||||||
});
|
});
|
||||||
Handlebars.registerHelper("striptags", function (text) {
|
Handlebars.registerHelper("striptags", function (text) {
|
||||||
@@ -84,92 +99,411 @@ Handlebars.registerHelper("striptags", function(text){
|
|||||||
});
|
});
|
||||||
Handlebars.registerHelper("buildTitle", function (title, siteTitle) {
|
Handlebars.registerHelper("buildTitle", function (title, siteTitle) {
|
||||||
if (title.indexOf(siteTitle) < 0) {
|
if (title.indexOf(siteTitle) < 0) {
|
||||||
title = `'${title}' by ${siteTitle}`;
|
title = "'" + title + "' by " + siteTitle;
|
||||||
}
|
}
|
||||||
return title;
|
return title;
|
||||||
});
|
});
|
||||||
Handlebars.registerHelper("jointags", function (tags) {
|
Handlebars.registerHelper("jointags", function (tags) {
|
||||||
return (tags || '').split(' ').join(',');
|
return (tags || "").split(" ").join(",");
|
||||||
});
|
});
|
||||||
Handlebars.registerHelper("encodetitle", function (str) {
|
Handlebars.registerHelper("encodetitle", function (str) {
|
||||||
return (str || '').replaceAll(' ', '%2B');
|
return (str || "").replaceAll(" ", "%2B");
|
||||||
});
|
});
|
||||||
|
|
||||||
Metalsmith(__dirname)
|
// --- Pre-build: Compile SCSS to CSS ---
|
||||||
.use(define({
|
function compileSass(sourceDir, outputDir) {
|
||||||
site: {
|
const cssDir = path.join(sourceDir, "src", "css");
|
||||||
title: 'Marcus Noble',
|
const outputCssDir = path.join(outputDir, "css");
|
||||||
description: 'The blog of Marcus Noble, self-described tinkerer, platform engineer and all round average guy!',
|
|
||||||
url: 'https://marcusnoble.co.uk'
|
if (!fs.existsSync(cssDir)) {
|
||||||
|
console.warn("No CSS source directory found, skipping SCSS compilation");
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
}))
|
|
||||||
.use(date())
|
// Ensure output directory exists
|
||||||
.use(collections({
|
if (!fs.existsSync(outputCssDir)) {
|
||||||
|
fs.mkdirSync(outputCssDir, { recursive: true });
|
||||||
|
}
|
||||||
|
|
||||||
|
const files = fs.readdirSync(cssDir);
|
||||||
|
fs.mkdirSync(outputCssDir, { recursive: true });
|
||||||
|
|
||||||
|
for (const file of files) {
|
||||||
|
if (!file.endsWith(".scss") || file.startsWith("_")) continue; // Skip partials
|
||||||
|
|
||||||
|
const inputPath = path.join(cssDir, file);
|
||||||
|
const outputFileName = file.replace(/\.scss$/, ".css");
|
||||||
|
const outputPath = path.join(outputCssDir, outputFileName);
|
||||||
|
|
||||||
|
console.log(`Compiling SCSS: ${file} -> ${outputFileName}`);
|
||||||
|
try {
|
||||||
|
const result = sass.compile(inputPath, {
|
||||||
|
style: "compressed",
|
||||||
|
});
|
||||||
|
fs.writeFileSync(outputPath, result.css);
|
||||||
|
} catch (err) {
|
||||||
|
console.error(`Failed to compile ${file}:`, err.message);
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- Feed generation plugin ---
|
||||||
|
function feedPlugin() {
|
||||||
|
return function (files, metalsmith, done) {
|
||||||
|
const posts = metalsmith.metadata().collections.posts || [];
|
||||||
|
|
||||||
|
const feed = new RSS({
|
||||||
|
title: "Marcus Noble",
|
||||||
|
description:
|
||||||
|
"The blog of Marcus Noble, self-described tinkerer, platform engineer and all round average guy!",
|
||||||
|
site_url: "https://marcusnoble.co.uk",
|
||||||
|
feed_url: "https://marcusnoble.co.uk/feed.xml",
|
||||||
|
image_url: "https://marcusnoble.co.uk/images/marcus.jpg",
|
||||||
|
language: "en",
|
||||||
|
});
|
||||||
|
|
||||||
|
posts.forEach((post) => {
|
||||||
|
feed.item({
|
||||||
|
title: post.title,
|
||||||
|
url: `https://marcusnoble.co.uk/${post.path}/`,
|
||||||
|
date: post.date.toISOString(),
|
||||||
|
description: post.summary || "",
|
||||||
|
categories: (post.tags || "").split(" ").filter(Boolean),
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
files["feed.xml"] = {
|
||||||
|
contents: Buffer.from(feed.xml({ indent: true })),
|
||||||
|
modified: new Date(),
|
||||||
|
};
|
||||||
|
|
||||||
|
done();
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- Handlebars partials preprocessor ---
|
||||||
|
// Processes {{> partial }} calls in markdown files before markdown rendering
|
||||||
|
function handlebarsPreprocessor() {
|
||||||
|
return function (files, metalsmith, done) {
|
||||||
|
const templateDir = path.join(metalsmith.directory(), "templates");
|
||||||
|
const partialsDir = path.join(templateDir, "partials");
|
||||||
|
|
||||||
|
// Load all partials
|
||||||
|
const hbs = Handlebars.create();
|
||||||
|
hbs.registerHelper("markdown", function (text) {
|
||||||
|
if (!text) return "";
|
||||||
|
return customRenderer(text, {}, {});
|
||||||
|
});
|
||||||
|
hbs.registerHelper("moment", function (date, format) {
|
||||||
|
return new moment(date).format(format);
|
||||||
|
});
|
||||||
|
hbs.registerHelper("striptags", striptags);
|
||||||
|
hbs.registerHelper("buildTitle", function (title, siteTitle) {
|
||||||
|
if (title.indexOf(siteTitle) < 0) {
|
||||||
|
title = "'" + title + "' by " + siteTitle;
|
||||||
|
}
|
||||||
|
return title;
|
||||||
|
});
|
||||||
|
hbs.registerHelper("jointags", function (tags) {
|
||||||
|
return (tags || "").split(" ").join(",");
|
||||||
|
});
|
||||||
|
hbs.registerHelper("encodetitle", function (str) {
|
||||||
|
return (str || "").replaceAll(" ", "%2B");
|
||||||
|
});
|
||||||
|
|
||||||
|
// Register all partials
|
||||||
|
if (fs.existsSync(partialsDir)) {
|
||||||
|
const partialFiles = fs.readdirSync(partialsDir);
|
||||||
|
for (const file of partialFiles) {
|
||||||
|
const name = path.parse(file).name;
|
||||||
|
const content = fs.readFileSync(path.join(partialsDir, file), "utf8");
|
||||||
|
hbs.registerPartial(name, content);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Find markdown files with partial references and process them
|
||||||
|
const filesToProcess = Object.keys(files).filter((file) =>
|
||||||
|
file.endsWith(".md") || file.endsWith(".markdown")
|
||||||
|
);
|
||||||
|
|
||||||
|
for (const file of filesToProcess) {
|
||||||
|
const content = files[file].contents.toString();
|
||||||
|
const handlebarsRegex = /\{\{\s*>\s*(\w+)\s+([^}]*)\s*\}\}/g;
|
||||||
|
const matches = [...content.matchAll(handlebarsRegex)];
|
||||||
|
|
||||||
|
if (matches.length > 0) {
|
||||||
|
let processed = content;
|
||||||
|
for (const match of matches) {
|
||||||
|
const partialName = match[1];
|
||||||
|
const argsStr = match[2];
|
||||||
|
// Parse arguments: name="value" name="value" ...
|
||||||
|
const args = {};
|
||||||
|
const argRegex = /(\w+)=["']([^"']*)["']/g;
|
||||||
|
let argMatch;
|
||||||
|
while ((argMatch = argRegex.exec(argsStr)) !== null) {
|
||||||
|
args[argMatch[1]] = argMatch[2];
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
const partial = hbs.partials[partialName];
|
||||||
|
if (partial) {
|
||||||
|
const rendered = hbs.compile(partial)(args);
|
||||||
|
processed = processed.replace(match[0], rendered);
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
console.warn(
|
||||||
|
`Warning: Could not render partial ${partialName} in ${file}`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
files[file].contents = Buffer.from(processed);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Extract date and title from filename for permalinks compatibility
|
||||||
|
// The filename date is the primary source for the permalink
|
||||||
|
const postPattern = /^posts\/(\d{4}-\d{2}-\d{2})-(.+)\.(md|markdown)$/;
|
||||||
|
for (const file of Object.keys(files)) {
|
||||||
|
const match = file.match(postPattern);
|
||||||
|
if (match) {
|
||||||
|
const [, dateStr, titleSlug] = match;
|
||||||
|
// Use filename date for permalink (via _filenameDate), but front matter date for other purposes
|
||||||
|
// Store filename-based date for permalinks
|
||||||
|
if (!files[file]._filenameDate) {
|
||||||
|
files[file]._filenameDate = new Date(dateStr);
|
||||||
|
}
|
||||||
|
// Also store the filename-based title slug for permalinks
|
||||||
|
if (!files[file]._filenameTitle) {
|
||||||
|
files[file]._filenameTitle = titleSlug;
|
||||||
|
}
|
||||||
|
// Set the title from front matter (or derive from filename)
|
||||||
|
if (!files[file].title) {
|
||||||
|
// Convert slug to title: "some-title-slug" -> "Some Title Slug"
|
||||||
|
files[file].title = titleSlug
|
||||||
|
.split("-")
|
||||||
|
.map((word) => word.charAt(0).toUpperCase() + word.slice(1))
|
||||||
|
.join(" ");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Set default date for files without one (e.g., index.html from pagination, pages)
|
||||||
|
if (!files[file].date) {
|
||||||
|
files[file].date = new Date();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
done();
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- Metalsmith build pipeline ---
|
||||||
|
// Pre-build: Compile SCSS to CSS and write to build directory
|
||||||
|
compileSass(__dirname, path.join(__dirname, "build"));
|
||||||
|
|
||||||
|
const ms = Metalsmith(__dirname)
|
||||||
|
.ignore(['css']) // Don't copy CSS source files to build
|
||||||
|
.clean(false) // Don't clean build directory (pre-build SCSS compilation outputs preserved)
|
||||||
|
.source("src")
|
||||||
|
.use(
|
||||||
|
define({
|
||||||
|
site: {
|
||||||
|
title: "Marcus Noble",
|
||||||
|
description:
|
||||||
|
"The blog of Marcus Noble, self-described tinkerer, platform engineer and all round average guy!",
|
||||||
|
url: "https://marcusnoble.co.uk",
|
||||||
|
},
|
||||||
|
})
|
||||||
|
)
|
||||||
|
.use(function (files, metalsmith, done) {
|
||||||
|
const now = new Date();
|
||||||
|
const meta = metalsmith.metadata();
|
||||||
|
meta.buildDate = now;
|
||||||
|
meta.buildTimestamp = now.toISOString();
|
||||||
|
done();
|
||||||
|
})
|
||||||
|
.use(
|
||||||
|
collections({
|
||||||
posts: {
|
posts: {
|
||||||
pattern: 'posts/*',
|
pattern: "posts/*.{md,markdown}",
|
||||||
sortBy: 'date',
|
sortBy: "date",
|
||||||
reverse: true,
|
reverse: true,
|
||||||
},
|
},
|
||||||
drafts: {
|
drafts: {
|
||||||
pattern: 'drafts/*',
|
pattern: "drafts/*.{md,markdown}",
|
||||||
sortBy: 'date',
|
sortBy: "date",
|
||||||
reverse: true,
|
reverse: true,
|
||||||
},
|
},
|
||||||
projects: {
|
projects: {
|
||||||
pattern: 'projects/*'
|
pattern: "projects/*",
|
||||||
},
|
},
|
||||||
pages: {
|
pages: {
|
||||||
pattern: 'pages/*'
|
pattern: "pages/*",
|
||||||
|
},
|
||||||
|
})
|
||||||
|
)
|
||||||
|
.use(function (files, metalsmith, done) {
|
||||||
|
// Extract YAML front matter from all markdown files
|
||||||
|
Object.keys(files).forEach(function (file) {
|
||||||
|
if (file.endsWith(".md") || file.endsWith(".markdown")) {
|
||||||
|
const content = files[file].contents.toString();
|
||||||
|
const parsed = matter(content);
|
||||||
|
// Update file content to body (without front matter)
|
||||||
|
files[file].contents = Buffer.from(parsed.content);
|
||||||
|
// Extract front matter as file metadata
|
||||||
|
if (parsed.data) {
|
||||||
|
Object.assign(files[file], parsed.data);
|
||||||
}
|
}
|
||||||
}))
|
}
|
||||||
.use(inplace({
|
});
|
||||||
engine: 'handlebars',
|
done();
|
||||||
directory: 'templates',
|
})
|
||||||
partials: 'templates/partials'
|
.use(handlebarsPreprocessor())
|
||||||
}))
|
.use(
|
||||||
.use(md)
|
markdown({
|
||||||
.use(permalinks({
|
render: customRenderer,
|
||||||
pattern: ':date-:title',
|
})
|
||||||
date: 'YYYY-MM-DD',
|
)
|
||||||
|
.use(
|
||||||
|
pagination({
|
||||||
|
"collections.posts": {
|
||||||
|
perPage: 5,
|
||||||
|
layout: "index.html",
|
||||||
|
first: "index.html",
|
||||||
|
noPageOne: true,
|
||||||
|
path: "page:num/index.html",
|
||||||
|
pageMetadata: {
|
||||||
|
title: "Posts",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
)
|
||||||
|
.use(function (files, metalsmith, done) {
|
||||||
|
// Pre-compute paths for layouts rendering
|
||||||
|
// This mirrors the permalinks logic but doesn't rename files
|
||||||
|
const slugRe = /[<>:"'\|?*]|[^\w\s$_+~.()!\-@\/]+/g;
|
||||||
|
const linksets = [
|
||||||
|
{ match: { collection: "pages" }, pattern: ":title" },
|
||||||
|
{ match: { collection: "drafts" }, pattern: "drafts/:title" },
|
||||||
|
];
|
||||||
|
for (const file of Object.keys(files)) {
|
||||||
|
const data = files[file];
|
||||||
|
|
||||||
|
// Set default date
|
||||||
|
if (!data.date) {
|
||||||
|
data.date = new Date();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Skip non-HTML files
|
||||||
|
if (!file.endsWith(".html")) continue;
|
||||||
|
if (data.permalink === false) continue;
|
||||||
|
|
||||||
|
// Skip layout files (layout matches filename)
|
||||||
|
const baseName = path.basename(file, ".html");
|
||||||
|
if (data.layout === baseName || data.layout === `${baseName}.html`) continue;
|
||||||
|
|
||||||
|
// Determine linkset
|
||||||
|
let linkset = null;
|
||||||
|
for (const ls of linksets) {
|
||||||
|
if (ls.match) {
|
||||||
|
let matched = true;
|
||||||
|
for (const [key, val] of Object.entries(ls.match)) {
|
||||||
|
const fileVal = data[key];
|
||||||
|
if (Array.isArray(fileVal)) {
|
||||||
|
if (!fileVal.includes(val)) { matched = false; break; }
|
||||||
|
} else if (fileVal !== val) { matched = false; break; }
|
||||||
|
}
|
||||||
|
if (matched) { linkset = ls; break; }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const pattern = linkset ? (linkset.pattern || ":date-:title") : ":date-:title";
|
||||||
|
|
||||||
|
// Compute permalink
|
||||||
|
let result = pattern
|
||||||
|
.replace(/:(\w+)(\??)/g, (match, key, optional) => {
|
||||||
|
let value = data[key];
|
||||||
|
if (value === undefined || value === null) {
|
||||||
|
if (optional) return "";
|
||||||
|
return key;
|
||||||
|
}
|
||||||
|
if (key === "date" && data._filenameDate) {
|
||||||
|
const d = data._filenameDate;
|
||||||
|
return d.getFullYear() + "-" + String(d.getMonth() + 1).padStart(2, "0") + "-" + String(d.getDate()).padStart(2, "0");
|
||||||
|
}
|
||||||
|
if (key === "title" && data._filenameTitle) {
|
||||||
|
return data._filenameTitle.replace(/\s+/g, "-").replace(slugRe, "");
|
||||||
|
}
|
||||||
|
if (value instanceof Date) {
|
||||||
|
const d = value;
|
||||||
|
return d.getFullYear() + "-" + String(d.getMonth() + 1).padStart(2, "0") + "-" + String(d.getDate()).padStart(2, "0");
|
||||||
|
}
|
||||||
|
return String(value).toLowerCase().replace(/\s+/g, "-").replace(slugRe, "");
|
||||||
|
})
|
||||||
|
.replace(/\/+$/, "");
|
||||||
|
|
||||||
|
// Add directory index
|
||||||
|
if (path.extname(result) === "" || path.extname(result) === ".html") {
|
||||||
|
if (!path.extname(result)) {
|
||||||
|
result = path.join(result, "index.html");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Prepend source directory prefix
|
||||||
|
let sourcePrefix = "";
|
||||||
|
if (file.startsWith("posts/")) sourcePrefix = "posts/";
|
||||||
|
else if (file.startsWith("pages/")) sourcePrefix = "pages/";
|
||||||
|
|
||||||
|
data.permalink = sourcePrefix + result;
|
||||||
|
|
||||||
|
// Set path 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
done();
|
||||||
|
})
|
||||||
|
.use(
|
||||||
|
layouts({
|
||||||
|
transform: require("./plugins/handlebars-html"),
|
||||||
|
directory: "templates",
|
||||||
|
pattern: "**/*.html",
|
||||||
|
extname: ".html",
|
||||||
|
})
|
||||||
|
)
|
||||||
|
.use(
|
||||||
|
permalinks({
|
||||||
|
pattern: ":date-:title",
|
||||||
|
date: "YYYY-MM-DD",
|
||||||
linksets: [
|
linksets: [
|
||||||
{
|
{
|
||||||
match: { collection: 'pages' },
|
match: { collection: "pages" },
|
||||||
pattern: ':title'
|
pattern: ":title"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
match: { collection: 'drafts' },
|
match: { collection: "drafts" },
|
||||||
pattern: 'drafts/:title'
|
pattern: "drafts/:title"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}))
|
})
|
||||||
.use(feed({
|
)
|
||||||
collection: 'posts',
|
.use(
|
||||||
destination: 'feed.xml'
|
sitemap({
|
||||||
}))
|
hostname: "https://marcusnoble.co.uk",
|
||||||
.use(sitemap({
|
omitExtension: true,
|
||||||
hostname: 'https://marcusnoble.co.uk',
|
})
|
||||||
modifiedProperty: 'date'
|
)
|
||||||
}))
|
.use(feedPlugin());
|
||||||
.use(pagination({
|
|
||||||
'collections.posts': {
|
// Run the build
|
||||||
perPage: 5,
|
ms.build(function (err) {
|
||||||
layout: 'index.html',
|
if (err) {
|
||||||
first: 'index.html',
|
console.error("Build failed:", err);
|
||||||
noPageOne: true,
|
throw err;
|
||||||
path: 'page:num/index.html',
|
|
||||||
pageMetadata: {
|
|
||||||
title: 'Posts'
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}))
|
console.log("Build complete. Starting server...");
|
||||||
.use(layouts({
|
|
||||||
engine: 'handlebars',
|
|
||||||
directory: 'templates',
|
|
||||||
partials: 'templates/partials'
|
|
||||||
}))
|
|
||||||
.use(sass())
|
|
||||||
.build(function(err) {
|
|
||||||
if (err) throw err;
|
|
||||||
|
|
||||||
app.listen(port, function () {
|
app.listen(port, function () {
|
||||||
console.log(`App listening at http://localhost:${port}`);
|
console.log(`App listening at http://localhost:${port}`);
|
||||||
|
|||||||
+63
-65
@@ -1,4 +1,4 @@
|
|||||||
const express = require('express');
|
const express = require("express");
|
||||||
const router = express.Router();
|
const router = express.Router();
|
||||||
const blackHole = function (req, res) {
|
const blackHole = function (req, res) {
|
||||||
res.redirect("https://crawler-test.com/redirects/infinite_redirect");
|
res.redirect("https://crawler-test.com/redirects/infinite_redirect");
|
||||||
@@ -12,79 +12,77 @@ router.all(/^\/\.well-known\//, function(req, res) {
|
|||||||
// Block access to any root-level dot files
|
// Block access to any root-level dot files
|
||||||
router.all(/^\/\./, blackHole);
|
router.all(/^\/\./, blackHole);
|
||||||
// Block access to file types I don't use
|
// Block access to file types I don't use
|
||||||
router.all(/.*\.php$/, blackHole);
|
router.all(/\.php$/, blackHole);
|
||||||
router.all(/.*\.asp$/, blackHole);
|
router.all(/\.asp$/, blackHole);
|
||||||
router.all(/.*\.aspx$/, blackHole);
|
router.all(/\.aspx$/, blackHole);
|
||||||
router.all(/.*\.gz$/, blackHole);
|
router.all(/\.gz$/, blackHole);
|
||||||
router.all(/.*\.bz2$/, blackHole);
|
router.all(/\.bz2$/, blackHole);
|
||||||
router.all(/.*\.tar$/, blackHole);
|
router.all(/\.tar$/, blackHole);
|
||||||
router.all(/.*\.sql$/, blackHole);
|
router.all(/\.sql$/, blackHole);
|
||||||
router.all(/.*\.env$/, blackHole);
|
router.all(/\.env$/, blackHole);
|
||||||
router.all(/.*\.ini$/, blackHole);
|
router.all(/\.ini$/, blackHole);
|
||||||
router.all(/.*\.pem$/, blackHole);
|
router.all(/\.pem$/, blackHole);
|
||||||
router.all(/.*\.key$/, blackHole);
|
router.all(/\.key$/, blackHole);
|
||||||
router.all(/.*\.crt$/, blackHole);
|
router.all(/\.crt$/, blackHole);
|
||||||
router.all(/.*\.properties$/, blackHole);
|
router.all(/\.properties$/, blackHole);
|
||||||
// Block access to any .git folders
|
// Block access to any .git folders
|
||||||
router.all(/.*\/\.git\/.*/, blackHole);
|
router.all(/\/\.git\//, blackHole);
|
||||||
// Block attempts to navigate up directories
|
// Block attempts to navigate up directories
|
||||||
router.all(/.*\.\.\/.*/, blackHole);
|
router.all(/\.\.\//, blackHole);
|
||||||
// Block access to special Mac folder
|
// Block access to special Mac folder
|
||||||
router.all('/__MACOSX/*?', blackHole);
|
router.all(/\/__MACOSX\//, blackHole);
|
||||||
// Block access to Workdpress files
|
// Block access to WordPress files
|
||||||
router.all('(/*)?/wp-admin/', blackHole);
|
router.all(/^\/?\/?wp-admin(\/.*)?$/, blackHole);
|
||||||
router.all('(/*)?/wp-admin/.*', blackHole);
|
router.all(/^\/?\/?wp-includes(\/.*)?$/, blackHole);
|
||||||
router.all('(/*)?/wp-includes/?(*)?', blackHole);
|
router.all(/^\/?\/?wp-content(\/.*)?$/, blackHole);
|
||||||
router.all('(/*)?/wp-content/?(*)?', blackHole);
|
router.all(/\/wordpress\//, blackHole);
|
||||||
router.all('/wordpress/', blackHole);
|
router.all(/\/wp2?\//, blackHole);
|
||||||
router.all('/wp(2)?/', blackHole);
|
|
||||||
// Block access to possible databases
|
// Block access to possible databases
|
||||||
router.all('/database/', blackHole);
|
router.all(/\/database\//, blackHole);
|
||||||
router.all('/db/', blackHole);
|
router.all(/^\/db(\/.*)?$/, blackHole);
|
||||||
router.all('/db-backup/', blackHole);
|
router.all(/\/db-backup\//, blackHole);
|
||||||
router.all('/db_backup/', blackHole);
|
router.all(/\/db_backup\//, blackHole);
|
||||||
router.all('/sql-backup/', blackHole);
|
router.all(/\/sql-backup\//, blackHole);
|
||||||
router.all('/sql/', blackHole);
|
router.all(/^\/sql(\/.*)?$/, blackHole);
|
||||||
router.all('/pma/', blackHole);
|
router.all(/\/pma\//, blackHole);
|
||||||
router.all('/phpmyadmin/', blackHole);
|
router.all(/\/phpmyadmin\//, blackHole);
|
||||||
router.all('/mysqladmin/', blackHole);
|
router.all(/\/mysqladmin\//, blackHole);
|
||||||
router.all('/mysql/', blackHole);
|
router.all(/^\/mysql(\/.*)?$/, blackHole);
|
||||||
router.all('/myadmin/', blackHole);
|
router.all(/\/myadmin\//, blackHole);
|
||||||
// Block access to possible backups and uploads
|
// Block access to possible backups and uploads
|
||||||
router.all('/backup/', blackHole);
|
router.all(/\/backup(\/.*)?$/, blackHole);
|
||||||
router.all('/uploads/', blackHole);
|
router.all(/\/uploads(\/.*)?$/, blackHole);
|
||||||
router.all('/test/', blackHole);
|
router.all(/\/test(\/.*)?$/, blackHole);
|
||||||
router.all('/temp/', blackHole);
|
router.all(/\/temp(\/.*)?$/, blackHole);
|
||||||
router.all(/.*\/dbbackup\/.*/, blackHole);
|
router.all(/\/dbbackup\//, blackHole);
|
||||||
router.all('/bak/', blackHole);
|
router.all(/\/bak\//, blackHole);
|
||||||
router.all('archive.zip', blackHole);
|
router.all(/archive\.zip$/, blackHole);
|
||||||
// Block access to possible credentials
|
// Block access to possible credentials
|
||||||
router.all('/env.test', blackHole);
|
router.all(/\/env\.test$/, blackHole);
|
||||||
router.all('(/.*)?/env$', blackHole);
|
router.all(/^\/?(.*)?\/env$/, blackHole);
|
||||||
router.all('/admin(/.*)?', blackHole)
|
router.all(/^\/admin(\/.*)?$/, blackHole);
|
||||||
router.all('/credentials(/*)?', blackHole);
|
router.all(/^\/credentials(\/.*)?$/, blackHole);
|
||||||
router.all(/.*credentials\.json$/, blackHole);
|
router.all(/credentials\.json$/, blackHole);
|
||||||
router.all(/.*keys\.json$/, blackHole);
|
router.all(/keys\.json$/, blackHole);
|
||||||
router.all(/.*secrets\.json$/, blackHole);
|
router.all(/secrets\.json$/, blackHole);
|
||||||
// Block system paths
|
// Block system paths
|
||||||
router.all('/etc/*', blackHole);
|
router.all(/\/etc\//, blackHole);
|
||||||
router.all('/var/*', blackHole);
|
router.all(/\/var\//, blackHole);
|
||||||
router.all('/usr/*', blackHole);
|
router.all(/\/usr\//, blackHole);
|
||||||
router.all('/user/*', blackHole);
|
router.all(/\/user\//, blackHole);
|
||||||
|
|
||||||
// Block misc stuff
|
// Block misc stuff
|
||||||
router.all('/data/owncloud.log', blackHole);
|
router.all(/\/data\/owncloud\.log$/, blackHole);
|
||||||
router.all('/autodiscover/autodiscover.xml', blackHole)
|
router.all(/\/autodiscover\/autodiscover\.xml$/, blackHole);
|
||||||
router.all('/.well-known/autoconfig(/*)?', blackHole)
|
router.all(/\/\.well-known\/autoconfig(\/.*)?$/, blackHole);
|
||||||
router.all('/sites/default/files/', blackHole);
|
router.all(/\/sites\/default\/files\//, blackHole);
|
||||||
router.all(/.*\/mail\/config-.+\.xml/, blackHole);
|
router.all(/\/mail\/config-.+\.xml$/, blackHole);
|
||||||
router.all('/bitnami/*', blackHole)
|
router.all(/\/bitnami\//, blackHole);
|
||||||
router.all('/aws/*', blackHole)
|
router.all(/\/aws\//, blackHole);
|
||||||
|
|
||||||
// Block methods I don't support
|
// Block methods I don't support
|
||||||
router.post('*', blackHole);
|
router.post("*", blackHole);
|
||||||
router.put('*', blackHole);
|
router.put("*", blackHole);
|
||||||
router.delete('*', blackHole);
|
router.delete("*", blackHole);
|
||||||
|
|
||||||
|
module.exports = router;
|
||||||
module.exports = router
|
|
||||||
+18
-20
@@ -6,30 +6,28 @@
|
|||||||
"watch": "nodemon --exec npm start -e \"md,html,scss,js\" -i \"build\""
|
"watch": "nodemon --exec npm start -e \"md,html,scss,js\" -i \"build\""
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"compression": "^1.6.2",
|
"@metalsmith/collections": "^2.0.0",
|
||||||
"express": "^4.13.4",
|
"@metalsmith/in-place": "^5.0.0",
|
||||||
"handlebars": "^4.0.5",
|
"@metalsmith/layouts": "^3.0.0",
|
||||||
"markdown-it-emoji": "^1.1.1",
|
"@metalsmith/markdown": "^1.10.0",
|
||||||
"metalsmith": "^2.1.0",
|
"compression": "^1.8.2",
|
||||||
"metalsmith-build-date": "^0.2.0",
|
"express": "^4.21.2",
|
||||||
"metalsmith-collections": "^0.7.0",
|
"gray-matter": "^4.0.3",
|
||||||
"metalsmith-define": "^2.0.1",
|
"handlebars": "^4.7.10",
|
||||||
"metalsmith-feed": "^0.2.0",
|
"jstransformer-handlebars": "^1.2.0",
|
||||||
"metalsmith-in-place": "^1.4.4",
|
"marked": "^18.2.0",
|
||||||
"metalsmith-layouts": "^1.6.5",
|
"metalsmith-define": "^2.1.3",
|
||||||
"metalsmith-markdownit": "^0.3.0",
|
"metalsmith-pagination": "^1.5.0",
|
||||||
"metalsmith-pagination": "^1.4.0",
|
|
||||||
"metalsmith-permalinks": "^0.5.0",
|
|
||||||
"metalsmith-sass": "^2.0.0",
|
|
||||||
"metalsmith-sitemap": "^1.2.2",
|
"metalsmith-sitemap": "^1.2.2",
|
||||||
"moment": "^2.13.0",
|
"moment": "^2.31.0",
|
||||||
"node-sass": "^6.0.0",
|
"rss": "^1.2.2",
|
||||||
"striptags": "^2.1.1"
|
"sass": "^1.108.2",
|
||||||
|
"striptags": "^3.2.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"nodemon": "^1.12.1"
|
"nodemon": "^3.1.0"
|
||||||
},
|
},
|
||||||
"engines": {
|
"engines": {
|
||||||
"node": "16"
|
"node": "22"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,99 @@
|
|||||||
|
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;
|
||||||
@@ -0,0 +1,195 @@
|
|||||||
|
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);
|
||||||
|
}
|
||||||
|
// For title in permalink, use _filenameTitle if available (to match source filenames)
|
||||||
|
if (key === 'title' && data._filenameTitle) {
|
||||||
|
return slugify(data._filenameTitle);
|
||||||
|
}
|
||||||
|
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 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
// For posts: preserve the "posts/" prefix
|
||||||
|
// For other files: use the pattern as-is
|
||||||
|
let sourcePrefix = "";
|
||||||
|
if (file.startsWith("posts/")) {
|
||||||
|
sourcePrefix = "posts/";
|
||||||
|
} else if (file.startsWith("pages/")) {
|
||||||
|
sourcePrefix = "pages/";
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Prepend source directory prefix
|
||||||
|
data.permalink = sourcePrefix + 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;
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
<figure class="center {{ class }}" markdown="1">
|
<figure class="center {{ class }}">
|
||||||
<a href="{{ url}}" target="_blank">
|
<a href="{{ url}}" target="_blank">
|
||||||
<img src="{{url}}" alt="{{ alt }}">
|
<img src="{{url}}" alt="{{ alt }}">
|
||||||
</a>
|
</a>
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
<figure class="center" markdown="1">
|
<figure class="center">
|
||||||
|
|
||||||
[](https://twitter.com/user/status/{{id}})
|
<a href="https://tweet.cluster.fun/{{id}}">[](https://twitter.com/user/status/{{id}})</a>
|
||||||
|
|
||||||
<figcaption>{{caption}}</figcaption>
|
<figcaption>{{caption}}</figcaption>
|
||||||
</figure>
|
</figure>
|
||||||
Reference in new issue
Block a user