shadowfacts.net/lib/generate/redirects.ts

29 lines
794 B
TypeScript
Raw Normal View History

2019-01-04 18:14:53 +00:00
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");
2019-01-05 02:59:52 +00:00
page.text = util.render(page.text, {
2019-01-04 18:14:53 +00:00
metadata: page.metadata,
newPermalink
2019-01-05 02:59:52 +00:00
}, "site/redirect.html.ejs");
2019-01-04 18:14:53 +00:00
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);
}
}
}