@@ -15,6 +15,14 @@ import siteMetadata from '@/data/siteMetadata'
1515import { notFound } from 'next/navigation'
1616import type { Locale } from '@/lib/i18n'
1717
18+ interface BlogWithTitleZh extends Blog {
19+ titleZh ?: string
20+ }
21+
22+ interface AuthorWithNameEn extends Authors {
23+ nameEn ?: string
24+ }
25+
1826const defaultLayout = 'PostLayout'
1927const layouts = {
2028 PostSimple,
@@ -39,16 +47,17 @@ export async function generateMetadata(props: {
3947 }
4048
4149 // 根据语言选择标题
50+ const postWithTitleZh = post as BlogWithTitleZh
4251 const displayTitle =
43- locale === 'zh' && ( post as Blog & { titleZh ?: string } ) . titleZh
44- ? ( post as Blog & { titleZh ?: string } ) . titleZh
45- : post . title
52+ locale === 'zh' && postWithTitleZh . titleZh ? postWithTitleZh . titleZh : post . title
4653
4754 // 根据语言选择作者名
4855 const authors = authorDetails . map ( ( author ) => {
49- return locale === 'zh'
50- ? author . name
51- : ( author as Authors & { nameEn ?: string } ) . nameEn || author . name
56+ if ( locale === 'zh' ) {
57+ return author . name
58+ }
59+ const authorWithNameEn = author as AuthorWithNameEn
60+ return authorWithNameEn . nameEn || author . name
5261 } )
5362
5463 const publishedAt = new Date ( post . date ) . toISOString ( )
@@ -132,19 +141,18 @@ export default async function Page(props: { params: Promise<{ slug: string[]; lo
132141 const Layout = layouts [ post . layout || defaultLayout ]
133142
134143 // 根据语言选择标题
144+ const postWithTitleZh = post as BlogWithTitleZh
135145 const displayTitle =
136- locale === 'zh' && ( post as Blog & { titleZh ?: string } ) . titleZh
137- ? ( post as Blog & { titleZh ?: string } ) . titleZh
138- : post . title
146+ locale === 'zh' && postWithTitleZh . titleZh ? postWithTitleZh . titleZh : post . title
139147
140148 // 根据语言选择作者名
141- const displayAuthorDetails = authorDetails . map ( ( author ) => ( {
142- ... author ,
143- displayName :
144- locale === 'zh'
145- ? author . name
146- : ( author as Authors & { nameEn ?: string } ) . nameEn || author . name ,
147- } ) )
149+ const displayAuthorDetails = authorDetails . map ( ( author ) => {
150+ const authorWithNameEn = author as AuthorWithNameEn
151+ return {
152+ ... author ,
153+ displayName : locale === 'zh' ? author . name : authorWithNameEn . nameEn || author . name ,
154+ }
155+ } )
148156
149157 return (
150158 < >
@@ -154,7 +162,7 @@ export default async function Page(props: { params: Promise<{ slug: string[]; lo
154162 />
155163 < Layout
156164 content = { { ...mainContent , title : displayTitle } as CoreContent < Blog > }
157- authorDetails = { displayAuthorDetails as ( CoreContent < Authors > & { displayName ?: string } ) [ ] }
165+ authorDetails = { displayAuthorDetails }
158166 next = { next }
159167 prev = { prev }
160168 locale = { locale }
0 commit comments