diff --git a/lib/generate/index.ts b/lib/generate/index.ts index fa55e87..bb25ce1 100644 --- a/lib/generate/index.ts +++ b/lib/generate/index.ts @@ -4,7 +4,6 @@ import css from "./css"; import errors from "./errors"; import homepage from "./homepage"; import posts from "./posts"; -import redirects from "./redirects"; import rss from "./rss"; import tutorials from "./tutorials"; @@ -15,7 +14,6 @@ export = { errors, homepage, posts, - redirects, rss, tutorials }; diff --git a/lib/generate/redirects.ts b/lib/generate/redirects.ts deleted file mode 100644 index 74c3c75..0000000 --- a/lib/generate/redirects.ts +++ /dev/null @@ -1,29 +0,0 @@ -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); - } - } -} \ No newline at end of file diff --git a/lib/index.ts b/lib/index.ts index 52c919b..8359921 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -20,7 +20,6 @@ async function generate(): Promise { const posts = await generators.posts(); generators.homepage(posts); - generators.redirects(posts); const categories = await generators.categories(posts); await generators.rss(posts, categories, tutorials); @@ -74,6 +73,15 @@ async function generate(): Promise { app.use(apRouter); app.use(express.static("out")); + // redirect posts with changed permalinks + for (const post of posts) { + if (post.metadata.oldPermalink) { + app.get(post.metadata.oldPermalink, (req, res) => { + res.status(301).redirect(post.metadata.permalink); + }); + } + } + app.use((err, req, res, next) => { console.error("Unhandled error:", err); res.status(500).sendFile("500.html", { diff --git a/site/redirect.html.ejs b/site/redirect.html.ejs deleted file mode 100644 index 241a971..0000000 --- a/site/redirect.html.ejs +++ /dev/null @@ -1,16 +0,0 @@ -``` -metadata.title = "Redirect" -metadata.layout = "default.html.ejs" -``` - - - -
-
-

- This page has moved to a new URL. If you are not automatically redirected, click here. -

-
-
\ No newline at end of file