page-nav.ts 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import { computed } from 'vue'
  2. import { useData } from 'vitepress'
  3. import { ensureStartingSlash, isArray, removeExtention as removeExtension } from '../utils'
  4. import { useLang } from './lang'
  5. import { getFlatSideBarLinks, getSidebarConfig } from './sidebar'
  6. export function usePageNav() {
  7. const { page, theme } = useData()
  8. const lang = useLang()
  9. const path = computed(() => {
  10. return removeExtension(ensureStartingSlash(page.value.relativePath))
  11. })
  12. const candidates = computed(() => {
  13. const config = getSidebarConfig(theme.value.sidebars, path.value, lang.value)
  14. return isArray(config) ? getFlatSideBarLinks(config) : []
  15. })
  16. const index = computed(() => {
  17. return candidates.value.findIndex(item => {
  18. return item.link === path.value
  19. })
  20. })
  21. const next = computed(() => {
  22. if (theme.value.nextLinks !== false && index.value > -1 && index.value < candidates.value.length - 1) {
  23. return candidates.value[index.value + 1]
  24. }
  25. return null
  26. })
  27. const prev = computed(() => {
  28. if (theme.value.prevLinks !== false && index.value > 0) {
  29. return candidates.value[index.value - 1]
  30. }
  31. return null
  32. })
  33. const hasLinks = computed(() => !!next.value || !!prev.value)
  34. return {
  35. next,
  36. prev,
  37. hasLinks,
  38. }
  39. }