Skip to content

Commit

Permalink
Merge branch 'master' into Upgrade-to-Astro-4.0
Browse files Browse the repository at this point in the history
  • Loading branch information
narthur authored Mar 8, 2024
2 parents 5e67fc9 + 72ef0e7 commit 24dd61b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/lib/getRedirects.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
import getPosts from "./getPosts";
import getTags from "./getTags";

export default async function getRedirects(): Promise<Record<string, string>> {
const posts = await getPosts();
const tags = await getTags();

return posts.reduce<Record<string, string>>((acc, post) => {
const postRedirects = posts.reduce<Record<string, string>>((acc, post) => {
if (!post.redirects.length) return acc;
post.redirects.forEach((r) => {
acc[`/${r}`] = `/${post.slug}`;
});
return acc;
}, {});

const tagRedirects = tags.reduce<Record<string, string>>((acc, tag) => {
if (tag.redirect) {
acc[`/tags/${tag.redirect}`] = `/tags/${tag.name}`;
}
return acc;
}, {});

return { ...postRedirects, ...tagRedirects };
}
2 changes: 2 additions & 0 deletions src/lib/getTags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ type Tag = {
name: string;
posts: Post[];
count: number;
redirect: string | null;
};

const getTags = memoize(makeTags);
Expand All @@ -21,6 +22,7 @@ async function makeTags(): Promise<Tag[]> {
name: t,
posts: matched,
count: matched.length,
redirect: t.includes(" ") ? t.replace(/ /g, "+") : null,
};
});

Expand Down

0 comments on commit 24dd61b

Please sign in to comment.