-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: fix next and prev links not working
- Loading branch information
Showing
6 changed files
with
69 additions
and
170 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,69 @@ | ||
import { computed } from 'vue' | ||
import { useRoute, useSiteData } from 'vitepress' | ||
import { useSiteDataByRoute, usePageData } from 'vitepress' | ||
import { DefaultTheme } from '../config' | ||
|
||
export function useNextAndPrevLinks() { | ||
const route = useRoute() | ||
// TODO: could this be useSiteData<DefaultTheme.Config> or is the siteData | ||
// resolved and has a different structure? | ||
const siteData = useSiteData() | ||
|
||
const resolveLink = (targetLink: string) => { | ||
let target: DefaultTheme.SideBarLink | undefined | ||
Object.keys(siteData.value.themeConfig.sidebar).some((k) => { | ||
return siteData.value.themeConfig.sidebar[k].some( | ||
(v: { children: any }) => { | ||
if (Array.isArray(v.children)) { | ||
target = v.children.find((value: any) => { | ||
return value.link === targetLink | ||
}) | ||
} | ||
return !!target | ||
} | ||
) | ||
const site = useSiteDataByRoute() | ||
const page = usePageData() | ||
|
||
const candidates = computed(() => { | ||
return getFlatSidebarLinks(site.value.themeConfig?.sidebar) | ||
}) | ||
|
||
const currentPath = computed(() => { | ||
return '/' + page.value.relativePath.replace(/(index)?\.(md|html)$/, '') | ||
}) | ||
|
||
const currentIndex = computed(() => { | ||
return candidates.value.findIndex((item) => { | ||
return item.link === currentPath.value | ||
}) | ||
return target | ||
} | ||
}) | ||
|
||
const next = computed(() => { | ||
const pageData = route.data | ||
if (pageData.frontmatter.next === false) { | ||
return undefined | ||
if ( | ||
site.value.themeConfig?.nextLinks !== false && | ||
currentIndex.value > -1 && | ||
currentIndex.value < candidates.value.length - 1 | ||
) { | ||
return candidates.value[currentIndex.value + 1] | ||
} | ||
if (typeof pageData.frontmatter.next === 'string') { | ||
return resolveLink(pageData.frontmatter.next) | ||
} | ||
return pageData.next | ||
}) | ||
|
||
const prev = computed(() => { | ||
const pageData = route.data | ||
if (pageData.frontmatter.prev === false) { | ||
return undefined | ||
} | ||
if (typeof pageData.frontmatter.prev === 'string') { | ||
return resolveLink(pageData.frontmatter.prev) | ||
if (site.value.themeConfig?.prevLinks !== false && currentIndex.value > 0) { | ||
return candidates.value[currentIndex.value - 1] | ||
} | ||
return pageData.prev | ||
}) | ||
|
||
const hasLinks = computed(() => { | ||
return !!next.value || !!prev.value | ||
}) | ||
const hasLinks = computed(() => !!next.value || !!prev.value) | ||
|
||
return { | ||
next, | ||
prev, | ||
hasLinks | ||
} | ||
} | ||
|
||
function getFlatSidebarLinks( | ||
sidebar?: DefaultTheme.SideBarConfig | ||
): DefaultTheme.SideBarLink[] { | ||
if (!sidebar || sidebar === 'auto') { | ||
return [] | ||
} | ||
|
||
return sidebar.reduce<DefaultTheme.SideBarLink[]>((links, item) => { | ||
if (item.link) { | ||
links.push({ text: item.text, link: item.link }) | ||
} | ||
|
||
if ((item as DefaultTheme.SideBarGroup).children) { | ||
links = [ | ||
...links, | ||
...getFlatSidebarLinks((item as DefaultTheme.SideBarGroup).children) | ||
] | ||
} | ||
|
||
return links | ||
}, []) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters