edit-link.ts 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import { computed } from 'vue'
  2. import { useData } from 'vitepress'
  3. import { useLang } from '../composables/lang'
  4. import { useLocale } from '../composables/locale'
  5. import { defaultLang } from '../constant'
  6. import { createCrowdinUrl, createGitHubUrl } from '../utils'
  7. import editLinkLocale from '../../i18n/component/edit-link.json'
  8. export function useEditLink() {
  9. const { page, theme, frontmatter } = useData()
  10. const lang = useLang()
  11. const editLink = useLocale(editLinkLocale)
  12. const canEditSource = computed(() => {
  13. return lang.value === defaultLang
  14. })
  15. const url = computed(() => {
  16. if (canEditSource.value) {
  17. const {
  18. repo,
  19. docsDir = '',
  20. docsBranch = 'dev',
  21. docsRepo = repo,
  22. editLinks,
  23. } = theme.value
  24. const showEditLink =
  25. frontmatter.value.editLink != null
  26. ? frontmatter.value.editLink
  27. : editLinks
  28. const { relativePath } = page.value
  29. if (!showEditLink || !relativePath || !repo) {
  30. return null
  31. }
  32. return createGitHubUrl(
  33. docsRepo,
  34. docsDir,
  35. docsBranch,
  36. relativePath,
  37. '',
  38. ''
  39. )
  40. }
  41. return createCrowdinUrl(lang.value)
  42. })
  43. const text = computed(() => {
  44. return canEditSource.value
  45. ? editLink.value['edit-on-github']
  46. : editLink.value['edit-on-crowdin']
  47. })
  48. return {
  49. url,
  50. text,
  51. }
  52. }