mirror of
https://github.com/reonokiy/blog.nokiy.net.git
synced 2025-06-16 03:32:51 +02:00
feat: enhance internationalization support with dynamic routing
This commit is contained in:
parent
d6c98880d3
commit
8c19d26cfd
12 changed files with 199 additions and 87 deletions
|
@ -1,17 +1,25 @@
|
|||
import type { CollectionEntry } from 'astro:content'
|
||||
import themeConfig from '@/config'
|
||||
import { langPath } from '@/utils/ui'
|
||||
import { getCollection } from 'astro:content'
|
||||
|
||||
export type Post = CollectionEntry<'posts'>
|
||||
export type PostData = Post['data']
|
||||
export type PostsGroupByYear = Map<number, Post[]>
|
||||
|
||||
// Get all posts and sort by publish date
|
||||
export async function getPosts() {
|
||||
// Get all posts except drafts (include pinned)
|
||||
export async function getPosts(lang?: string) {
|
||||
const defaultLocale = themeConfig.global.locale
|
||||
const currentLang = lang || defaultLocale
|
||||
|
||||
const posts = await getCollection(
|
||||
'posts',
|
||||
({ data }: Post) => {
|
||||
const shouldInclude = (import.meta.env.DEV || !data.draft) && !data.pin
|
||||
return shouldInclude
|
||||
const shouldInclude = import.meta.env.DEV || !data.draft
|
||||
if (!langPath.includes(currentLang)) {
|
||||
return shouldInclude && data.lang === ''
|
||||
}
|
||||
return shouldInclude && (data.lang === currentLang || data.lang === '')
|
||||
},
|
||||
)
|
||||
|
||||
|
@ -20,27 +28,23 @@ export async function getPosts() {
|
|||
)
|
||||
}
|
||||
|
||||
// Get all posts except drafts (not pinned)
|
||||
export async function getRegularPosts(lang?: string) {
|
||||
const posts = await getPosts(lang)
|
||||
return posts.filter(post => !post.data.pin)
|
||||
}
|
||||
|
||||
// Get pinned posts
|
||||
export async function getPinnedPosts() {
|
||||
const posts = await getCollection(
|
||||
'posts',
|
||||
({ data }: Post) => {
|
||||
const shouldInclude = (import.meta.env.DEV || !data.draft) && data.pin
|
||||
return shouldInclude
|
||||
},
|
||||
)
|
||||
|
||||
return posts.sort((a: Post, b: Post) =>
|
||||
b.data.published.valueOf() - a.data.published.valueOf(),
|
||||
)
|
||||
export async function getPinnedPosts(lang?: string) {
|
||||
const posts = await getPosts(lang)
|
||||
return posts.filter(post => post.data.pin)
|
||||
}
|
||||
|
||||
// Group posts by year and sort
|
||||
export async function getPostsByYear(): Promise<PostsGroupByYear> {
|
||||
const posts = await getPosts()
|
||||
// Get posts group by year (not pinned)
|
||||
export async function getPostsByYear(lang?: string): Promise<PostsGroupByYear> {
|
||||
const posts = await getRegularPosts(lang)
|
||||
const yearMap = new Map<number, Post[]>()
|
||||
|
||||
// Group by year
|
||||
posts.forEach((post: Post) => {
|
||||
const year = post.data.published.getFullYear()
|
||||
if (!yearMap.has(year)) {
|
||||
|
@ -49,7 +53,6 @@ export async function getPostsByYear(): Promise<PostsGroupByYear> {
|
|||
yearMap.get(year)?.push(post)
|
||||
})
|
||||
|
||||
// Sort posts within each year
|
||||
yearMap.forEach((yearPosts) => {
|
||||
yearPosts.sort((a: Post, b: Post) => {
|
||||
const aDate = a.data.published
|
||||
|
@ -66,9 +69,23 @@ export async function getPostsByYear(): Promise<PostsGroupByYear> {
|
|||
return new Map([...yearMap.entries()].sort((a, b) => b[0] - a[0]))
|
||||
}
|
||||
|
||||
// Group posts by tags
|
||||
export async function sortPostsByTags() {
|
||||
const posts = await getPosts()
|
||||
// Get all tags
|
||||
export async function getAllTags(lang?: string) {
|
||||
const posts = await getPosts(lang)
|
||||
const tags = new Set<string>()
|
||||
|
||||
posts.forEach((post: Post) => {
|
||||
post.data.tags?.forEach((tag: string) =>
|
||||
tags.add(tag),
|
||||
)
|
||||
})
|
||||
|
||||
return Array.from(tags)
|
||||
}
|
||||
|
||||
// Get posts group by each tag
|
||||
export async function getPostsGroupByTags(lang?: string) {
|
||||
const posts = await getRegularPosts(lang)
|
||||
const tagMap = new Map<string, Post[]>()
|
||||
|
||||
posts.forEach((post: Post) => {
|
||||
|
@ -85,28 +102,10 @@ export async function sortPostsByTags() {
|
|||
return tagMap
|
||||
}
|
||||
|
||||
// Get posts by specific tag
|
||||
export async function getPostsByTag(tag: string) {
|
||||
const posts = await getPosts()
|
||||
|
||||
// Get all posts by one tag
|
||||
export async function getPostsByTag(tag: string, lang?: string) {
|
||||
const posts = await getRegularPosts(lang)
|
||||
return posts.filter((post: Post) =>
|
||||
post.data.tags?.includes(tag),
|
||||
)
|
||||
}
|
||||
|
||||
// Get all tags list
|
||||
export async function getAllTags() {
|
||||
const posts = await getCollection(
|
||||
'posts',
|
||||
({ data }: Post) => import.meta.env.DEV || !data.draft,
|
||||
)
|
||||
const tags = new Set<string>()
|
||||
|
||||
posts.forEach((post: Post) => {
|
||||
post.data.tags?.forEach((tag: string) =>
|
||||
tags.add(tag),
|
||||
)
|
||||
})
|
||||
|
||||
return Array.from(tags)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue