feat: add custom slug validation and handling for posts

- Enhance content configuration with slug validation to ensure only valid characters are used
- Update post routing to support custom slugs with fallback to default slug
- Implement slug duplication check to prevent conflicts across different languages
- Modify various page components to use custom or default slugs in URL generation
This commit is contained in:
radishzzz 2025-01-25 03:14:52 +00:00
parent ee35006f7c
commit a26031d490
10 changed files with 67 additions and 11 deletions

View file

@ -7,6 +7,31 @@ export type Post = CollectionEntry<'posts'>
export type PostData = Post['data']
export type PostsGroupByYear = Map<number, Post[]>
// Check if the slug is duplicated under the same language.
export async function checkSlugDuplication(posts: Post[]): Promise<string[]> {
const slugMap = new Map<string, Set<string>>() // Map<lang, Set<slug>>
const duplicates: string[] = []
posts.forEach((post) => {
const lang = post.data.lang || ''
const slug = post.data.slug || post.slug
if (!slugMap.has(lang)) {
slugMap.set(lang, new Set())
}
const slugSet = slugMap.get(lang)!
if (slugSet.has(slug)) {
duplicates.push(`Duplicate slug "${slug}" found in language "${lang || 'default'}"`)
}
else {
slugSet.add(slug)
}
})
return duplicates
}
// Get all posts except drafts (include pinned)
export async function getPosts(lang?: string) {
const defaultLocale = themeConfig.global.locale