nav.ts 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import { computed } from 'vue'
  2. import { useRoute, useSiteData, inBrowser } from 'vitepress'
  3. import type { DefaultTheme } from '../config'
  4. export function useLocaleLinks() {
  5. const route = useRoute()
  6. const site = useSiteData()
  7. return computed(() => {
  8. const theme = site.value.themeConfig as DefaultTheme.Config
  9. const locales = theme.locales
  10. if (!locales) {
  11. return null
  12. }
  13. const localeKeys = Object.keys(locales)
  14. if (localeKeys.length <= 1) {
  15. return null
  16. }
  17. // handle site base
  18. const siteBase = inBrowser ? site.value.base : '/'
  19. const siteBaseWithoutSuffix = siteBase.endsWith('/')
  20. ? siteBase.slice(0, -1)
  21. : siteBase
  22. // remove site base in browser env
  23. const routerPath = route.path.slice(siteBaseWithoutSuffix.length)
  24. const currentLangBase = localeKeys.find((key) => {
  25. return key === '/' ? false : routerPath.startsWith(key)
  26. })
  27. const currentContentPath = currentLangBase
  28. ? routerPath.substring(currentLangBase.length - 1)
  29. : routerPath
  30. const candidates = localeKeys.map((v) => {
  31. const localePath = v.endsWith('/') ? v.slice(0, -1) : v
  32. return {
  33. text: locales[v].label,
  34. link: `${localePath}${currentContentPath}`
  35. }
  36. })
  37. const currentLangKey = currentLangBase ? currentLangBase : '/'
  38. const selectText = locales[currentLangKey].selectText
  39. ? locales[currentLangKey].selectText
  40. : 'Languages'
  41. return {
  42. text: selectText,
  43. items: candidates
  44. } as DefaultTheme.NavItemWithChildren
  45. })
  46. }