PageEdit.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import { computed } from 'vue';
  2. import OutboundLink from './icons/OutboundLink.vue';
  3. import { endingSlashRE, isExternal } from '../utils';
  4. import { useRoute, useSiteData } from 'vitepress';
  5. function createEditLink(repo, docsRepo, docsDir, docsBranch, path) {
  6. const bitbucket = /bitbucket.org/;
  7. if (bitbucket.test(repo)) {
  8. const base = isExternal(docsRepo) ? docsRepo : repo;
  9. return (base.replace(endingSlashRE, '') +
  10. `/src` +
  11. `/${docsBranch}/` +
  12. (docsDir ? docsDir.replace(endingSlashRE, '') + '/' : '') +
  13. path +
  14. `?mode=edit&spa=0&at=${docsBranch}&fileviewer=file-view-default`);
  15. }
  16. const base = isExternal(docsRepo)
  17. ? docsRepo
  18. : `https://github.com/${docsRepo}`;
  19. return (base.replace(endingSlashRE, '') +
  20. `/edit` +
  21. `/${docsBranch}/` +
  22. (docsDir ? docsDir.replace(endingSlashRE, '') + '/' : '') +
  23. path);
  24. }
  25. export default {
  26. components: {
  27. OutboundLink
  28. },
  29. setup() {
  30. const route = useRoute();
  31. const siteData = useSiteData();
  32. const editLink = computed(() => {
  33. const pageData = route.data;
  34. const showEditLink = pageData.frontmatter.editLink == null
  35. ? siteData.value.themeConfig.editLinks
  36. : pageData.frontmatter.editLink;
  37. const { repo, docsDir = '', docsBranch = 'master', docsRepo = repo } = siteData.value.themeConfig;
  38. const { relativePath } = pageData;
  39. if (showEditLink && relativePath && repo) {
  40. return createEditLink(repo, docsRepo || repo, docsDir, docsBranch, relativePath);
  41. }
  42. return null;
  43. });
  44. const editLinkText = computed(() => siteData.value.themeConfig.editLinkText || 'Edit this page');
  45. return {
  46. editLink,
  47. editLinkText
  48. };
  49. }
  50. };