feat: support the post alias for short links.

This commit is contained in:
Yufan Sheng 2024-09-04 16:59:21 +08:00
parent 89c7270b57
commit b40c86a34e
Signed by: syhily
GPG Key ID: 9D18A22A7DCD5A9B
4 changed files with 29 additions and 2 deletions

View File

@ -64,6 +64,7 @@ const postsCollection = defineCollection({
date: z.date(), date: z.date(),
updated: z.date().optional(), updated: z.date().optional(),
comments: z.boolean().optional().default(true), comments: z.boolean().optional().default(true),
alias: z.array(z.string()).optional().default([]),
tags: z.array(z.string()).optional().default([]), tags: z.array(z.string()).optional().default([]),
category: z.string(), category: z.string(),
summary: z.string().optional().default(''), summary: z.string().optional().default(''),

View File

@ -1,6 +1,10 @@
--- ---
title: 梅开三度 - 莲动妙处 title: 梅开三度 - 莲动妙处
slug: third-bloom-in-sex slug: third-bloom-in-sex
alias:
- pa-pa-pa
- pia-pia-pia
- papapa
date: 2024-09-04 15:42:12 date: 2024-09-04 15:42:12
updated: 2024-09-04 15:50:12 updated: 2024-09-04 15:50:12
tags: tags:

View File

@ -94,13 +94,21 @@ if (!options.isProd() && missingCovers.length > 0) {
console.warn(missingCovers); 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<string>(); const postsSlugs = new Set<string>();
for (const post of posts) { for (const post of posts) {
if (postsSlugs.has(post.slug)) { if (postsSlugs.has(post.slug)) {
throw new Error(`Duplicate post slug: ${post.slug}`); throw new Error(`Duplicate post slug: ${post.slug}`);
} }
postsSlugs.add(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) { for (const page of pages) {
if (postsSlugs.has(page.slug)) { if (postsSlugs.has(page.slug)) {

View File

@ -2,7 +2,21 @@ import { posts } from '@/helpers/schema';
import { urlJoin } from '@/helpers/tools'; import { urlJoin } from '@/helpers/tools';
import { defineMiddleware } from 'astro:middleware'; import { defineMiddleware } from 'astro:middleware';
const mappings = new Map<string, string>(posts.map((post) => [urlJoin('/', post.slug), post.permalink])); const mappings = new Map<string, string>();
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) => { export const onRequest = defineMiddleware(({ request: { method }, url: { pathname }, redirect }, next) => {
// This is used for redirect my old blog posts to a new mapping. // This is used for redirect my old blog posts to a new mapping.