Skip to content

Commit

Permalink
fix(nav): autmatically resolve to first child if has no index
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Feb 23, 2024
1 parent 038198a commit 28d9d39
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 11 deletions.
14 changes: 8 additions & 6 deletions app/components/AppHeader.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ const { metaSymbol } = useShortcuts()
const docsNav = useDocsNav()
const headerLinks = computed(() => {
return docsNav.links.map((link) => {
return {
...link,
children: link.children?.filter((child) => !child.children || child.children.some((c) => c.to === child.to)),
}
})
return docsNav.links
.filter((link) => link.hasIndex)
.map((link) => {
return {
...link,
children: link.children?.filter((child) => !child.children || child.children.some((c) => c.to === child.to)),
}
})
})
</script>

Expand Down
21 changes: 16 additions & 5 deletions app/utils/nav.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,35 @@ export function useDocsNav() {

const links = computed(() => {
return mapContentNavigation(navigation.value).map((item) => {
console.log(item)

// Flatren single child
if (item.children?.length === 1) {
return {
item = {
...item,
...item.children[0],
children: undefined,
active: isActive(item.to as string),
}
}

// Check if group index is not exists and default to first child
const originalTo = item.to as string
if (item.children?.length && !item.children.some((c) => c.to === originalTo)) {
item.to = item.children[0].to
}

return {
...item,
label: titleCase(item.to),
active: isActive(item.to as string),
originalTo,
hasIndex: item.to === originalTo,
label: titleCase(originalTo),
active: isActive(originalTo),
}
})
})

const activeLinks = computed(() => {
return links.value.find((l) => route.path.startsWith(l.to as string))?.children || []
return links.value.find((l) => route.path.startsWith(l.originalTo))?.children || []
})

return reactive({
Expand Down
3 changes: 3 additions & 0 deletions docs/.foo/bar.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Foo bar

foobar...
3 changes: 3 additions & 0 deletions docs/.foo/baz.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Foo baz

foobaz...

0 comments on commit 28d9d39

Please sign in to comment.