11import { computed } from 'vue'
22import { useRoute } from 'vitepress'
33import { useLayout } from 'vitepress/theme'
4+ import { useI18n } from 'vue-i18n'
5+ import { localePath } from '../utils/i18n'
46import type { DefaultTheme } from 'vitepress'
57
68export interface BreadcrumbItem {
@@ -11,6 +13,15 @@ export interface BreadcrumbItem {
1113export function useBreadcrumb ( ) {
1214 const route = useRoute ( )
1315 const { sidebar } = useLayout ( )
16+ const { t } = useI18n ( )
17+
18+ // 获取主页链接
19+ const getHomeItem = ( ) : BreadcrumbItem => {
20+ return {
21+ text : t ( 'breadcrumb.home' ) ,
22+ link : localePath ( '/' ) ,
23+ }
24+ }
1425
1526 // 递归查找匹配的面包屑路径
1627 const findBreadcrumbPath = (
@@ -59,11 +70,22 @@ export function useBreadcrumb() {
5970
6071 // 计算面包屑导航
6172 const breadcrumbItems = computed ( ( ) => {
73+ // 首先获取主页项
74+ const homeItem = getHomeItem ( )
75+
76+ // 如果当前就是主页,只显示主页
77+ const currentPath = route . path
78+ const normalizedCurrentPath = normalizePath ( currentPath )
79+ const normalizedHomePath = normalizePath ( homeItem . link || '/' )
80+
81+ if ( normalizedCurrentPath === normalizedHomePath || currentPath === homeItem . link ) {
82+ return [ homeItem ]
83+ }
84+
6285 if ( ! sidebar . value || ! route . path ) {
63- return [ ]
86+ return [ homeItem ]
6487 }
6588
66- const currentPath = route . path
6789 let sidebarItems : DefaultTheme . SidebarItem [ ] = [ ]
6890
6991 // 处理不同的 sidebar 格式
@@ -87,11 +109,17 @@ export function useBreadcrumb() {
87109 }
88110
89111 if ( sidebarItems . length === 0 ) {
90- return [ ]
112+ return [ homeItem ]
91113 }
92114
93115 const breadcrumbPath = findBreadcrumbPath ( sidebarItems , currentPath )
94- return breadcrumbPath || [ ]
116+
117+ // 始终在面包屑路径前添加主页链接
118+ if ( breadcrumbPath && breadcrumbPath . length > 0 ) {
119+ return [ homeItem , ...breadcrumbPath ]
120+ }
121+
122+ return [ homeItem ]
95123 } )
96124
97125 return {
0 commit comments