Skip to content

Commit f811f56

Browse files
committed
feat(app): 动态管理页面标题
- 新增 DocumentTitleManager 组件监听路由变化 - 根据当前路径和文件夹配置设置文档标题 - 支持首页、关于、项目、文章、文档、文件、新闻及动态页面标题翻译 - 取消了原先的静态页面标题设置逻辑 - 在 AppContent 中集成 DocumentTitleManager 以实现标题动态更新
1 parent 78f4038 commit f811f56

1 file changed

Lines changed: 59 additions & 10 deletions

File tree

src/App.jsx

Lines changed: 59 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import React, { useEffect, useState } from 'react'
2-
import { BrowserRouter, Routes, Route } from 'react-router-dom'
2+
import { BrowserRouter, Routes, Route, useLocation } from 'react-router-dom'
33
import { ConfigProvider, useConfig } from './config/ConfigContext'
44
import { ThemeProvider } from './components/theme/ThemeContext'
5-
import { I18nProvider } from './i18n/I18nContext'
5+
import { I18nProvider, useI18n } from './i18n/I18nContext'
66
import { Layout } from './components/layout/Layout'
77
import { Home } from './pages/Home'
88
import { About } from './pages/About'
@@ -15,19 +15,65 @@ import { DynamicDocumentPage } from './pages/DynamicDocumentPage'
1515
import { generateFolderConfigs } from './utils/folderScanner'
1616
import { getRouterBasename } from './utils/pathUtils'
1717

18+
/**
19+
* 页面标题管理组件
20+
* 监听路由变化,动态更新页面标题为:主标题 - 当前页面
21+
*/
22+
function DocumentTitleManager({ folderConfigs }) {
23+
const location = useLocation()
24+
const { config } = useConfig()
25+
const { t } = useI18n()
26+
const siteTitle = config?.site?.title || '个人主页'
27+
28+
useEffect(() => {
29+
// 获取当前路径(去除开头的 /)
30+
const pathname = location.pathname.replace(/^\/*/, '')
31+
32+
// 根据路径确定页面标题
33+
let pageTitle = ''
34+
35+
if (!pathname || pathname === '') {
36+
// 首页
37+
pageTitle = t('pages.home')
38+
} else if (pathname === 'about') {
39+
pageTitle = t('pages.about')
40+
} else if (pathname === 'projects') {
41+
pageTitle = t('pages.projects')
42+
} else if (pathname === 'posts') {
43+
pageTitle = t('pages.posts')
44+
} else if (pathname === 'pages') {
45+
pageTitle = t('pages.docs')
46+
} else if (pathname === 'files') {
47+
pageTitle = t('pages.files')
48+
} else if (pathname === 'news') {
49+
pageTitle = t('pages.news')
50+
} else {
51+
// 检查是否是动态生成的文档集合页面
52+
const folderConfig = folderConfigs.find(cfg => cfg.name === pathname)
53+
if (folderConfig) {
54+
pageTitle = folderConfig.title || pathname
55+
} else {
56+
// 未知页面,使用路径作为标题
57+
pageTitle = pathname
58+
}
59+
}
60+
61+
// 更新文档标题
62+
if (pageTitle) {
63+
document.title = `${siteTitle} - ${pageTitle}`
64+
} else {
65+
document.title = siteTitle
66+
}
67+
}, [location.pathname, siteTitle, folderConfigs, t])
68+
69+
return null // 这是一个纯逻辑组件,不渲染任何内容
70+
}
71+
1872
function AppContent() {
1973
const { config } = useConfig() // 获取完整的配置对象
20-
const siteConfig = config?.site // 站点配置
2174
const [folderConfigs, setFolderConfigs] = useState([])
2275
const [loading, setLoading] = useState(true)
2376

24-
// 动态设置页面标题
25-
useEffect(() => {
26-
if (siteConfig?.title) {
27-
document.title = siteConfig.title
28-
}
29-
}, [siteConfig?.title])
30-
3177
// 加载文件夹配置
3278
useEffect(() => {
3379
async function loadConfigs() {
@@ -62,6 +108,9 @@ function AppContent() {
62108
<I18nProvider>
63109
<ThemeProvider>
64110
<BrowserRouter basename={basename}>
111+
{/* 页面标题管理 */}
112+
<DocumentTitleManager folderConfigs={folderConfigs} />
113+
65114
<Routes>
66115
<Route path="/" element={<Layout folderConfigs={folderConfigs} />}>
67116
<Route index element={<Home />} />

0 commit comments

Comments
 (0)