edit-link.ts 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839
  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 { repo, docsDir = '', docsBranch = 'dev', docsRepo = repo, editLinks } = theme.value
  18. const showEditLink = frontmatter.value.editLink != null ? frontmatter.value.editLink : editLinks
  19. const { relativePath } = page.value
  20. if (!showEditLink || !relativePath || !repo) {
  21. return null
  22. }
  23. return createGitHubUrl(docsRepo, docsDir, docsBranch, relativePath, '', '')
  24. }
  25. return createCrowdinUrl(lang.value)
  26. })
  27. const text = computed(() => {
  28. return canEditSource.value ? editLink.value['edit-on-github'] : editLink.value['edit-on-crowdin']
  29. })
  30. return {
  31. url,
  32. text,
  33. }
  34. }