fix(theme): fix prev/next nav when include sep, close #846 (#849)

This commit is contained in:
pengzhanbo 2026-02-12 01:01:13 +08:00 committed by GitHub
parent 5a73b59297
commit d2b4654ae3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 11 additions and 5 deletions

View File

@ -38,7 +38,8 @@ const textTag = computed(() => {
const itemRole = computed(() => (isLink.value ? undefined : 'button')) const itemRole = computed(() => (isLink.value ? undefined : 'button'))
const isSeparator = computed(() => item.link?.startsWith('---')) const SEPARATOR_RE = /^-{3,}$/
const isSeparator = computed(() => item.link && SEPARATOR_RE.test(item.link))
const classes = computed(() => [ const classes = computed(() => [
[`level-${depth}`], [`level-${depth}`],

View File

@ -14,6 +14,8 @@ interface UsePrevNextResult {
next: ComputedRef<NavItemWithLink | null> next: ComputedRef<NavItemWithLink | null>
} }
const SEPARATOR_RE = /^-{3,}$/
export function usePrevNext(): UsePrevNextResult { export function usePrevNext(): UsePrevNextResult {
const route = useRoute() const route = useRoute()
const { frontmatter, theme } = useData() const { frontmatter, theme } = useData()
@ -100,10 +102,13 @@ function flatSidebar(sidebar: ThemeSidebarItem[], res: NavItemWithLink[] = []):
* Resolve `prev` or `next` config from sidebar items * Resolve `prev` or `next` config from sidebar items
*/ */
function resolveFromSidebarItems(sidebarItems: NavItemWithLink[], currentPath: string, offset: number): null | NavItemWithLink { function resolveFromSidebarItems(sidebarItems: NavItemWithLink[], currentPath: string, offset: number): null | NavItemWithLink {
const index = sidebarItems.findIndex(item => resolveRouteFullPath(item.link) === currentPath) let index = sidebarItems.findIndex(item => resolveRouteFullPath(item.link) === currentPath)
if (index !== -1) { if (index === -1)
const targetItem = sidebarItems[index + offset] return null
if (targetItem?.link) { // eslint-disable-next-line no-cond-assign
while ((index += offset) >= 0) {
const targetItem = sidebarItems[index]
if (targetItem?.link && !SEPARATOR_RE.test(targetItem.link)) {
return targetItem return targetItem
} }
} }