page-nav.ts 1.3 KB

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