diff --git a/src/lib/getRedirects.ts b/src/lib/getRedirects.ts index 810e503..c35213d 100644 --- a/src/lib/getRedirects.ts +++ b/src/lib/getRedirects.ts @@ -1,13 +1,24 @@ import getPosts from "./getPosts"; +import getTags from "./getTags"; export default async function getRedirects(): Promise> { const posts = await getPosts(); + const tags = await getTags(); - return posts.reduce>((acc, post) => { + const postRedirects = posts.reduce>((acc, post) => { if (!post.redirects.length) return acc; post.redirects.forEach((r) => { acc[`/${r}`] = `/${post.slug}`; }); return acc; }, {}); + + const tagRedirects = tags.reduce>((acc, tag) => { + if (tag.redirect) { + acc[`/tags/${tag.redirect}`] = `/tags/${tag.name}`; + } + return acc; + }, {}); + + return { ...postRedirects, ...tagRedirects }; } diff --git a/src/lib/getTags.ts b/src/lib/getTags.ts index 3a323b2..2bacb2d 100644 --- a/src/lib/getTags.ts +++ b/src/lib/getTags.ts @@ -6,6 +6,7 @@ type Tag = { name: string; posts: Post[]; count: number; + redirect: string | null; }; const getTags = memoize(makeTags); @@ -21,6 +22,7 @@ async function makeTags(): Promise { name: t, posts: matched, count: matched.length, + redirect: t.includes(" ") ? t.replace(/ /g, "+") : null, }; });