nextAndPrevLinks.js 1.3 KB

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