nextAndPrevLinks.ts 1.3 KB

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