shadowfacts.net/lib/generate/redirects.ts

32 lines
833 B
TypeScript

import ejs from "ejs";
import * as metadata from "../metadata";
import { Page } from "../metadata";
import layout from "../layout";
import * as util from "../util";
async function generateRedirect(oldPermalink: string, newPermalink: string) {
const page = await metadata.get("site/redirect.html.ejs");
page.text = ejs.render(page.text, {
metadata: page.metadata,
newPermalink
}, {
filename: "site/redirect.html.ejs"
});
page.text = await layout(page.text, page.metadata, page.metadata.layout!);
if (oldPermalink.endsWith("/")) {
oldPermalink += "index.html";
}
util.write(oldPermalink, page.text);
}
export default async function redirects(posts: Page[]) {
for (const post of posts) {
if (post.metadata.oldPermalink) {
await generateRedirect(post.metadata.oldPermalink, post.metadata.permalink);
}
}
}