mirror of
https://github.com/reonokiy/blog.nokiy.net.git
synced 2025-06-16 03:32:51 +02:00
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:
parent
ee35006f7c
commit
a26031d490
10 changed files with 67 additions and 11 deletions
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue