From 1d88a6e0b5be40d2c636d623b61e69e06e5a9767 Mon Sep 17 00:00:00 2001 From: Yufan Sheng Date: Wed, 4 Sep 2024 16:59:21 +0800 Subject: [PATCH] feat: support the post alias for short links. --- src/content/config.ts | 1 + .../posts/2024/2024-09-04-third-bloom-in-sex.mdx | 4 ++++ src/helpers/schema.ts | 10 +++++++++- src/middleware.ts | 16 +++++++++++++++- 4 files changed, 29 insertions(+), 2 deletions(-) diff --git a/src/content/config.ts b/src/content/config.ts index 23dbc06..2f4a9ed 100644 --- a/src/content/config.ts +++ b/src/content/config.ts @@ -64,6 +64,7 @@ const postsCollection = defineCollection({ date: z.date(), updated: z.date().optional(), comments: z.boolean().optional().default(true), + alias: z.array(z.string()).optional().default([]), tags: z.array(z.string()).optional().default([]), category: z.string(), summary: z.string().optional().default(''), diff --git a/src/content/posts/2024/2024-09-04-third-bloom-in-sex.mdx b/src/content/posts/2024/2024-09-04-third-bloom-in-sex.mdx index 7839d54..e2c48d5 100644 --- a/src/content/posts/2024/2024-09-04-third-bloom-in-sex.mdx +++ b/src/content/posts/2024/2024-09-04-third-bloom-in-sex.mdx @@ -1,6 +1,10 @@ --- title: 梅开三度 - 莲动妙处 slug: third-bloom-in-sex +alias: + - pa-pa-pa + - pia-pia-pia + - papapa date: 2024-09-04 15:42:12 updated: 2024-09-04 15:50:12 tags: diff --git a/src/helpers/schema.ts b/src/helpers/schema.ts index 5101a18..7408977 100644 --- a/src/helpers/schema.ts +++ b/src/helpers/schema.ts @@ -94,13 +94,21 @@ if (!options.isProd() && missingCovers.length > 0) { console.warn(missingCovers); } -// Validate the posts and pages' slug. They should be unique globally. +// Validate the posts and pages' slug and alias. They should be unique globally. const postsSlugs = new Set(); for (const post of posts) { if (postsSlugs.has(post.slug)) { throw new Error(`Duplicate post slug: ${post.slug}`); } postsSlugs.add(post.slug); + + for (const alias of post.alias) { + if (postsSlugs.has(alias)) { + throw new Error(`Duplicate alias ${alias} in post ${post.slug}`); + } + + postsSlugs.add(alias); + } } for (const page of pages) { if (postsSlugs.has(page.slug)) { diff --git a/src/middleware.ts b/src/middleware.ts index 68c2db5..1c60cca 100644 --- a/src/middleware.ts +++ b/src/middleware.ts @@ -2,7 +2,21 @@ import { posts } from '@/helpers/schema'; import { urlJoin } from '@/helpers/tools'; import { defineMiddleware } from 'astro:middleware'; -const mappings = new Map(posts.map((post) => [urlJoin('/', post.slug), post.permalink])); +const mappings = new Map(); + +const rewrites = posts.map((post) => ({ + sources: [ + urlJoin('/', post.slug), + ...post.alias.flatMap((alias) => [urlJoin('/', alias), urlJoin('/posts/', alias)]), + ], + target: post.permalink, +})); + +for (const rewrite of rewrites) { + for (const source of rewrite.sources) { + mappings.set(source, rewrite.target); + } +} export const onRequest = defineMiddleware(({ request: { method }, url: { pathname }, redirect }, next) => { // This is used for redirect my old blog posts to a new mapping.