repo.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. import { computed } from 'vue';
  2. import { useData } from 'vitepress';
  3. import { EXTERNAL_URL_RE } from '../../shared';
  4. export const platforms = ['GitHub', 'GitLab', 'Bitbucket'].map((platform) => {
  5. return [platform, new RegExp(platform, 'i')];
  6. });
  7. export function useRepo() {
  8. const { site } = useData();
  9. return computed(() => {
  10. const theme = site.value.themeConfig;
  11. const name = theme.docsRepo || theme.repo;
  12. if (!name) {
  13. return null;
  14. }
  15. const link = getRepoUrl(name);
  16. const text = getRepoText(link, theme.repoLabel);
  17. return { text, link };
  18. });
  19. }
  20. function getRepoUrl(repo) {
  21. // if the full url is not provided, default to GitHub repo
  22. return EXTERNAL_URL_RE.test(repo) ? repo : `https://github.com/${repo}`;
  23. }
  24. function getRepoText(url, text) {
  25. if (text) {
  26. return text;
  27. }
  28. // if no label is provided, deduce it from the repo url
  29. const hosts = url.match(/^https?:\/\/[^/]+/);
  30. if (!hosts) {
  31. return 'Source';
  32. }
  33. const platform = platforms.find(([_p, re]) => re.test(hosts[0]));
  34. if (platform && platform[0]) {
  35. return platform[0];
  36. }
  37. return 'Source';
  38. }