shadowfacts.net/lib/generate/redirects.ts

29 lines
794 B
TypeScript

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 = util.render(page.text, {
metadata: page.metadata,
newPermalink
}, "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);
}
}
}