Compare commits
1
Commits
0f021e9672
..
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c69e2cbb97 |
No files matched your search
@@ -4,6 +4,7 @@ const path = require("path");
|
|||||||
const express = require("express");
|
const express = require("express");
|
||||||
const compress = require("compression");
|
const compress = require("compression");
|
||||||
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/markdown");
|
const markdown = require("@metalsmith/markdown");
|
||||||
@@ -163,9 +164,11 @@ function feedPlugin() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
posts.forEach((post) => {
|
posts.forEach((post) => {
|
||||||
|
const postUrl = `https://marcusnoble.co.uk/${post.path}/`;
|
||||||
feed.item({
|
feed.item({
|
||||||
title: post.title,
|
title: post.title,
|
||||||
url: `https://marcusnoble.co.uk/${post.path}/`,
|
url: postUrl,
|
||||||
|
guid: postUrl,
|
||||||
date: post.date.toISOString(),
|
date: post.date.toISOString(),
|
||||||
description: post.summary || "",
|
description: post.summary || "",
|
||||||
categories: (post.tags || "").split(" ").filter(Boolean),
|
categories: (post.tags || "").split(" ").filter(Boolean),
|
||||||
@@ -260,16 +263,22 @@ function handlebarsPreprocessor() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Extract date and title from filename for permalinks compatibility
|
// Extract date and title from filename for permalinks compatibility
|
||||||
// Also set default values for files without them
|
// The filename date is the primary source for the permalink
|
||||||
const postPattern = /^posts\/(\d{4}-\d{2}-\d{2})-(.+)\.(md|markdown)$/;
|
const postPattern = /^posts\/(\d{4}-\d{2}-\d{2})-(.+)\.(md|markdown)$/;
|
||||||
for (const file of Object.keys(files)) {
|
for (const file of Object.keys(files)) {
|
||||||
const match = file.match(postPattern);
|
const match = file.match(postPattern);
|
||||||
if (match) {
|
if (match) {
|
||||||
const [, dateStr, titleSlug] = match;
|
const [, dateStr, titleSlug] = match;
|
||||||
// Only set if not already set in front matter
|
// Use filename date for permalink (via _filenameDate), but front matter date for other purposes
|
||||||
if (!files[file].date) {
|
// Store filename-based date for permalinks
|
||||||
files[file].date = new Date(dateStr);
|
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) {
|
if (!files[file].title) {
|
||||||
// Convert slug to title: "some-title-slug" -> "Some Title Slug"
|
// Convert slug to title: "some-title-slug" -> "Some Title Slug"
|
||||||
files[file].title = titleSlug
|
files[file].title = titleSlug
|
||||||
@@ -278,7 +287,7 @@ function handlebarsPreprocessor() {
|
|||||||
.join(" ");
|
.join(" ");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Set default date for files without one (e.g., pages)
|
// Set default date for files without one (e.g., index.html from pagination, pages)
|
||||||
if (!files[file].date) {
|
if (!files[file].date) {
|
||||||
files[file].date = new Date();
|
files[file].date = new Date();
|
||||||
}
|
}
|
||||||
@@ -293,7 +302,7 @@ function handlebarsPreprocessor() {
|
|||||||
compileSass(__dirname, path.join(__dirname, "build"));
|
compileSass(__dirname, path.join(__dirname, "build"));
|
||||||
|
|
||||||
const ms = Metalsmith(__dirname)
|
const ms = Metalsmith(__dirname)
|
||||||
.ignore(['css']) // Don't copy CSS source files to build
|
.ignore(['**/*.scss']) // Don't copy SCSS source files to build
|
||||||
.clean(false) // Don't clean build directory (pre-build SCSS compilation outputs preserved)
|
.clean(false) // Don't clean build directory (pre-build SCSS compilation outputs preserved)
|
||||||
.source("src")
|
.source("src")
|
||||||
.use(
|
.use(
|
||||||
@@ -316,14 +325,12 @@ const ms = Metalsmith(__dirname)
|
|||||||
.use(
|
.use(
|
||||||
collections({
|
collections({
|
||||||
posts: {
|
posts: {
|
||||||
pattern: "posts/*",
|
pattern: "posts/*.{md,markdown}",
|
||||||
sortBy: "date",
|
sort: "date:desc",
|
||||||
reverse: true,
|
|
||||||
},
|
},
|
||||||
drafts: {
|
drafts: {
|
||||||
pattern: "drafts/*",
|
pattern: "drafts/*.{md,markdown}",
|
||||||
sortBy: "date",
|
sort: "date:desc",
|
||||||
reverse: true,
|
|
||||||
},
|
},
|
||||||
projects: {
|
projects: {
|
||||||
pattern: "projects/*",
|
pattern: "projects/*",
|
||||||
@@ -333,6 +340,22 @@ const ms = Metalsmith(__dirname)
|
|||||||
},
|
},
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
|
.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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
done();
|
||||||
|
})
|
||||||
.use(handlebarsPreprocessor())
|
.use(handlebarsPreprocessor())
|
||||||
.use(
|
.use(
|
||||||
markdown({
|
markdown({
|
||||||
@@ -340,14 +363,108 @@ const ms = Metalsmith(__dirname)
|
|||||||
})
|
})
|
||||||
)
|
)
|
||||||
.use(
|
.use(
|
||||||
inPlace({
|
pagination({
|
||||||
transform: "handlebars",
|
"collections.posts": {
|
||||||
pattern: "**/*.html",
|
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");
|
||||||
|
// Don't skip index.html - it already has permalink from pagination
|
||||||
|
if (file === "index.html") {
|
||||||
|
// Set path for index page (root URL)
|
||||||
|
data.path = "";
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
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 (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(/-+/g, "-");
|
||||||
|
})
|
||||||
|
.replace(/\/+$/, "");
|
||||||
|
|
||||||
|
// Add directory index - force index.html for permalinks without .html extension
|
||||||
|
// Dots in titles (like T.I.L.) confuse path.extname() so we check the actual extension
|
||||||
|
if (!result.endsWith(".html")) {
|
||||||
|
result = path.join(result, "index.html");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Post files: no prefix - URLs match production format /YYYY-MM-DD-slug/
|
||||||
|
data.permalink = 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(
|
.use(
|
||||||
layouts({
|
layouts({
|
||||||
transform: "handlebars",
|
transform: require("./plugins/handlebars-html"),
|
||||||
directory: "templates",
|
directory: "templates",
|
||||||
pattern: "**/*.html",
|
pattern: "**/*.html",
|
||||||
extname: ".html",
|
extname: ".html",
|
||||||
@@ -372,20 +489,7 @@ const ms = Metalsmith(__dirname)
|
|||||||
.use(
|
.use(
|
||||||
sitemap({
|
sitemap({
|
||||||
hostname: "https://marcusnoble.co.uk",
|
hostname: "https://marcusnoble.co.uk",
|
||||||
})
|
omitExtension: true,
|
||||||
)
|
|
||||||
.use(
|
|
||||||
pagination({
|
|
||||||
"collections.posts": {
|
|
||||||
perPage: 5,
|
|
||||||
layout: "index.html",
|
|
||||||
first: "index.html",
|
|
||||||
noPageOne: true,
|
|
||||||
path: "page:num/index.html",
|
|
||||||
pageMetadata: {
|
|
||||||
title: "Posts",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
.use(feedPlugin());
|
.use(feedPlugin());
|
||||||
|
|||||||
+2
-1
@@ -10,14 +10,15 @@
|
|||||||
"@metalsmith/in-place": "^5.0.0",
|
"@metalsmith/in-place": "^5.0.0",
|
||||||
"@metalsmith/layouts": "^3.0.0",
|
"@metalsmith/layouts": "^3.0.0",
|
||||||
"@metalsmith/markdown": "^1.10.0",
|
"@metalsmith/markdown": "^1.10.0",
|
||||||
"metalsmith-sitemap": "^1.2.2",
|
|
||||||
"compression": "^1.8.2",
|
"compression": "^1.8.2",
|
||||||
"express": "^4.21.2",
|
"express": "^4.21.2",
|
||||||
|
"gray-matter": "^4.0.3",
|
||||||
"handlebars": "^4.7.10",
|
"handlebars": "^4.7.10",
|
||||||
"jstransformer-handlebars": "^1.2.0",
|
"jstransformer-handlebars": "^1.2.0",
|
||||||
"marked": "^18.2.0",
|
"marked": "^18.2.0",
|
||||||
"metalsmith-define": "^2.1.3",
|
"metalsmith-define": "^2.1.3",
|
||||||
"metalsmith-pagination": "^1.5.0",
|
"metalsmith-pagination": "^1.5.0",
|
||||||
|
"metalsmith-sitemap": "^1.2.2",
|
||||||
"moment": "^2.31.0",
|
"moment": "^2.31.0",
|
||||||
"rss": "^1.2.2",
|
"rss": "^1.2.2",
|
||||||
"sass": "^1.108.2",
|
"sass": "^1.108.2",
|
||||||
|
|||||||
@@ -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;
|
||||||
+58
-12
@@ -1,8 +1,9 @@
|
|||||||
const path = require("path");
|
const path = require("path");
|
||||||
|
const fs = require("fs");
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Custom Metalsmith permalinks plugin.
|
* Custom Metalsmith permalinks plugin.
|
||||||
* Replaces `:date-:title` style patterns using file metadata.
|
* Preserves source directory structure for posts, applies custom patterns for pages.
|
||||||
*/
|
*/
|
||||||
function permalinks(options) {
|
function permalinks(options) {
|
||||||
const defaultOptions = {
|
const defaultOptions = {
|
||||||
@@ -46,6 +47,8 @@ function permalinks(options) {
|
|||||||
s = s.split(from).join(to);
|
s = s.split(from).join(to);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// Replace spaces with dashes and remove special chars
|
||||||
|
s = s.replace(/\s+/g, "-");
|
||||||
if (opts.slug.remove) {
|
if (opts.slug.remove) {
|
||||||
s = s.replace(new RegExp(opts.slug.remove, "g"), "");
|
s = s.replace(new RegExp(opts.slug.remove, "g"), "");
|
||||||
}
|
}
|
||||||
@@ -54,17 +57,22 @@ function permalinks(options) {
|
|||||||
|
|
||||||
function replacePattern(pattern, data) {
|
function replacePattern(pattern, data) {
|
||||||
return pattern.replace(/:(\w+)(\??)/g, (match, key, optional) => {
|
return pattern.replace(/:(\w+)(\??)/g, (match, key, optional) => {
|
||||||
const value = data[key];
|
let value = data[key];
|
||||||
if (value === undefined || value === null) {
|
if (value === undefined || value === null) {
|
||||||
if (optional) return "";
|
if (optional) return "";
|
||||||
throw new Error(
|
throw new Error(
|
||||||
`Could not substitute ':${key}' in pattern '${pattern}', '${key}' is undefined`
|
`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) {
|
if (value instanceof Date) {
|
||||||
return formatDate(value);
|
return formatDate(value);
|
||||||
}
|
}
|
||||||
return slugify(value.toString());
|
return slugify(value.toString()).replace(/-+/, "-");
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -92,27 +100,36 @@ function permalinks(options) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return function (files, metalsmith, done) {
|
return function (files, metalsmith, done) {
|
||||||
const entries = Object.entries(files);
|
const keys = Object.keys(files);
|
||||||
|
|
||||||
|
for (const file of keys) {
|
||||||
|
const data = files[file];
|
||||||
|
|
||||||
for (const [file, data] of entries) {
|
|
||||||
// Skip non-HTML files
|
// Skip non-HTML files
|
||||||
if (!file.endsWith(".html")) continue;
|
if (!file.endsWith(".html")) continue;
|
||||||
|
|
||||||
// Skip if permalink is explicitly disabled
|
// Skip if permalink is explicitly disabled
|
||||||
if (data.permalink === false) continue;
|
if (data.permalink === false) continue;
|
||||||
|
|
||||||
// Check file-specific permalink first
|
// Skip if this is a layout/template file (has layout that matches filename)
|
||||||
if (data.permalink) {
|
// e.g., index.html with layout: 'index.html' should not be permalinked
|
||||||
let result = replacePattern(data.permalink, data);
|
const baseName = path.basename(file, '.html');
|
||||||
if (opts.trailingSlash) result = result.replace(/\/+$/, "");
|
if (data.layout === baseName || data.layout === `${baseName}.html`) continue;
|
||||||
data.permalink = result;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Determine which linkset to use (or default)
|
// Determine which linkset to use (or default)
|
||||||
const linkset = getMatchedLinkset(data, opts.linksets) || opts;
|
const linkset = getMatchedLinkset(data, opts.linksets) || opts;
|
||||||
const pattern = linkset.pattern || opts.pattern;
|
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
|
// Replace pattern placeholders
|
||||||
let result = replacePattern(pattern, data);
|
let result = replacePattern(pattern, data);
|
||||||
|
|
||||||
@@ -129,6 +146,35 @@ function permalinks(options) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
data.permalink = result;
|
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();
|
done();
|
||||||
|
|||||||
Reference in new issue
Block a user