feat: support the post alias for short links.
This commit is contained in:
parent
ecce0cd2c7
commit
1d88a6e0b5
@ -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(''),
|
||||
|
@ -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:
|
||||
|
@ -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<string>();
|
||||
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)) {
|
||||
|
@ -2,7 +2,21 @@ import { posts } from '@/helpers/schema';
|
||||
import { urlJoin } from '@/helpers/tools';
|
||||
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) => {
|
||||
// This is used for redirect my old blog posts to a new mapping.
|
||||
|
Loading…
Reference in New Issue
Block a user