feat(theme): ignore inner-page items in next/prev link

Next/prev links should jump to a separate page.  This would be spoiled
by inner-page links with hashes, so jump over them.
pull/3663/head
Christian Georgi 2 years ago
parent df8753bd92
commit 3feb12df86

@ -8,7 +8,13 @@ export function usePrevNext() {
return computed(() => {
const sidebar = getSidebar(theme.value.sidebar, page.value.relativePath)
const candidates = getFlatSideBarLinks(sidebar)
const links = getFlatSideBarLinks(sidebar)
// ignore inner-page links with hashes
const candidates = uniqBy(
links,
(link) => link.link.substring(0, link.link.lastIndexOf('#')) || link.link
)
const index = candidates.findIndex((link) => {
return isActive(page.value.relativePath, link.link)
@ -61,3 +67,11 @@ export function usePrevNext() {
}
})
}
function uniqBy<T>(array: T[], keyFn: (item: T) => any): T[] {
const seen = new Set()
return array.filter((item) => {
const k = keyFn(item)
return seen.has(k) ? false : seen.add(k)
})
}

Loading…
Cancel
Save