|
| 1 | +import { useLang, usePages } from '@rspress/core/runtime'; |
| 2 | +import { |
| 3 | + getCustomMDXComponent, |
| 4 | + renderInlineMarkdown, |
| 5 | +} from '@rspress/core/theme'; |
| 6 | +import React from 'react'; |
| 7 | +import { BlogAvatar } from './BlogAvatar'; |
| 8 | + |
| 9 | +export interface BlogItem { |
| 10 | + title?: string; |
| 11 | + description?: string; |
| 12 | + date?: Date; |
| 13 | + link?: string; |
| 14 | + authors?: string[]; |
| 15 | +} |
| 16 | + |
| 17 | +export interface BlogProps { |
| 18 | + posts: BlogItem[]; |
| 19 | +} |
| 20 | + |
| 21 | +export const useBlogPages = (): BlogItem[] => { |
| 22 | + const { pages } = usePages(); |
| 23 | + const lang = useLang(); |
| 24 | + |
| 25 | + const blogPages = pages |
| 26 | + .filter(page => page.lang === lang) |
| 27 | + .filter( |
| 28 | + page => |
| 29 | + page.routePath.includes('/blog/') && !page.routePath.endsWith('/blog/'), |
| 30 | + ) |
| 31 | + .sort((a, b) => { |
| 32 | + const dateA = a.frontmatter?.date |
| 33 | + ? new Date(a.frontmatter?.date as string) |
| 34 | + : new Date(0); |
| 35 | + const dateB = b.frontmatter?.date |
| 36 | + ? new Date(b.frontmatter?.date as string) |
| 37 | + : new Date(0); |
| 38 | + return dateB.getTime() - dateA.getTime(); |
| 39 | + }); |
| 40 | + |
| 41 | + return blogPages.map( |
| 42 | + ({ |
| 43 | + frontmatter: { description, date, authors, badge_text }, |
| 44 | + routePath, |
| 45 | + title, |
| 46 | + }) => { |
| 47 | + const itemDate = date ? new Date(date as string) : undefined; |
| 48 | + // Extract filename from routePath, e.g. '/en/blog/lynx-3-5' -> 'lynx-3-5' |
| 49 | + const filename = routePath.split('/').pop(); |
| 50 | + return { |
| 51 | + date: itemDate, |
| 52 | + description, |
| 53 | + link: routePath, |
| 54 | + title: title, |
| 55 | + authors: authors as string[] | undefined, |
| 56 | + badgeText: badge_text as string | undefined, |
| 57 | + filename, |
| 58 | + }; |
| 59 | + }, |
| 60 | + ); |
| 61 | +}; |
| 62 | + |
| 63 | +export function BlogList() { |
| 64 | + const { h2: H2, p: P, a: A, hr: Hr } = getCustomMDXComponent(); |
| 65 | + |
| 66 | + const blogPages = useBlogPages(); |
| 67 | + const lang = useLang(); |
| 68 | + |
| 69 | + return ( |
| 70 | + <> |
| 71 | + {blogPages.map(({ date, description, link, title, authors }, index) => ( |
| 72 | + <React.Fragment key={link || index}> |
| 73 | + {title && ( |
| 74 | + <H2 id={link}> |
| 75 | + <A href={link}>{title}</A> |
| 76 | + </H2> |
| 77 | + )} |
| 78 | + {date && ( |
| 79 | + <P> |
| 80 | + <em> |
| 81 | + {new Intl.DateTimeFormat(lang, { |
| 82 | + year: 'numeric', |
| 83 | + month: 'long', |
| 84 | + day: 'numeric', |
| 85 | + }).format(date)} |
| 86 | + </em> |
| 87 | + </P> |
| 88 | + )} |
| 89 | + {authors && ( |
| 90 | + <> |
| 91 | + {authors.map(author => ( |
| 92 | + <BlogAvatar author={author} key={author} /> |
| 93 | + ))} |
| 94 | + </> |
| 95 | + )} |
| 96 | + {description && <P {...renderInlineMarkdown(description)} />} |
| 97 | + {index < blogPages.length - 1 && <Hr />} |
| 98 | + </React.Fragment> |
| 99 | + ))} |
| 100 | + </> |
| 101 | + ); |
| 102 | +} |
0 commit comments