|
| 1 | +import { useCurrentModule } from "./useCurrentModule" |
| 2 | +import { useRoute, withBase } from 'vitepress' |
| 3 | +import { computed } from 'vue' |
| 4 | + |
| 5 | +export interface Breadcrumb { |
| 6 | + text: string |
| 7 | + link?: string |
| 8 | + icon?: string |
| 9 | +} |
| 10 | + |
| 11 | +export function useBreadcrumbs() { |
| 12 | + const { currentDocModule } = useCurrentModule() |
| 13 | + const route = useRoute() |
| 14 | + |
| 15 | + const breadcrumbs = computed<Breadcrumb[]>(() => { |
| 16 | + const items: Breadcrumb[] = [] |
| 17 | + |
| 18 | + // 1. Add module info with link |
| 19 | + items.push({ |
| 20 | + text: currentDocModule.value.text, |
| 21 | + link: currentDocModule.value.subpath, |
| 22 | + }) |
| 23 | + |
| 24 | + // Find current page in sidebar |
| 25 | + const currentSidebar = currentDocModule.value.sidebar.find(section => { |
| 26 | + return section.items.some(item => |
| 27 | + item.link && withBase(item.link) + '/' === route.path || |
| 28 | + (item.items?.some(subitem => withBase(subitem.link) + '/' === route.path)) |
| 29 | + ) |
| 30 | + }) |
| 31 | + |
| 32 | + // 2. Add section label if exists |
| 33 | + if (currentSidebar?.label) |
| 34 | + items.push({ text: currentSidebar.label }) |
| 35 | + |
| 36 | + // 3. Add parent item if exists (for nested items) |
| 37 | + const parentItem = currentSidebar?.items.find(item => |
| 38 | + item.items?.some(subitem => withBase(subitem.link) + '/' === route.path) |
| 39 | + ) |
| 40 | + if (parentItem) |
| 41 | + items.push({ text: parentItem.text }) |
| 42 | + |
| 43 | + // 4. Add current item |
| 44 | + const currentItem = currentSidebar?.items.find(item => item.link === route.path) || |
| 45 | + currentSidebar?.items |
| 46 | + .find(item => item.items?.some(subitem => subitem.link === route.path)) |
| 47 | + ?.items?.find(subitem => subitem.link === route.path) |
| 48 | + |
| 49 | + if (currentItem) |
| 50 | + items.push({ text: currentItem.text }) |
| 51 | + |
| 52 | + return items |
| 53 | + }) |
| 54 | + |
| 55 | + return { |
| 56 | + breadcrumbs |
| 57 | + } |
| 58 | +} |
0 commit comments