Skip to content

Commit c1bbd05

Browse files
ycmjasonyyx990803
authored andcommitted
fix: infer source link label from repo url (#168)
1 parent 99bc0aa commit c1bbd05

File tree

2 files changed

+31
-11
lines changed

2 files changed

+31
-11
lines changed

docs/default-theme-config/README.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ next: false
216216
---
217217
```
218218

219-
## GitHub Repo and Edit Links
219+
## Git Repo and Edit Links
220220

221221
Providing `themeConfig.repo` auto generates a GitHub link in the navbar and "Edit this page" links at the bottom of each page.
222222

@@ -226,6 +226,9 @@ module.exports = {
226226
themeConfig: {
227227
// Assumes GitHub. Can also be a full GitLab url.
228228
repo: 'vuejs/vuepress',
229+
// Customising the header label
230+
// Defaults to "GitHub"/"GitLab"/"Bitbucket" depending on `themeConfig.repo`
231+
repoLabel: 'Contribute!',
229232
// if your docs are not at the root of the repo
230233
docsDir: 'docs',
231234
// optional, defaults to master

lib/default-theme/NavLinks.vue

+27-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<template>
2-
<nav class="nav-links" v-if="userLinks.length || githubLink">
2+
<nav class="nav-links" v-if="userLinks.length || repoLink">
33
<!-- user links -->
44
<div
55
class="nav-item"
@@ -8,13 +8,13 @@
88
<DropdownLink v-if="item.type === 'links'" :item="item"/>
99
<NavLink v-else :item="item"/>
1010
</div>
11-
<!-- github link -->
12-
<a v-if="githubLink"
13-
:href="githubLink"
14-
class="github-link"
11+
<!-- repo link -->
12+
<a v-if="repoLink"
13+
:href="repoLink"
14+
class="repo-link"
1515
target="_blank"
1616
rel="noopener noreferrer">
17-
GitHub
17+
{{ repoLabel }}
1818
<OutboundLink/>
1919
</a>
2020
</nav>
@@ -69,14 +69,31 @@ export default {
6969
})
7070
}))
7171
},
72-
githubLink () {
72+
repoLink () {
7373
const { repo } = this.$site.themeConfig
7474
if (repo) {
7575
return /^https?:/.test(repo)
7676
? repo
7777
: `https://github.com/${repo}`
7878
}
79-
}
79+
},
80+
repoLabel () {
81+
if (!this.repoLink) return
82+
if (this.$site.themeConfig.repoLabel) {
83+
return this.$site.themeConfig.repoLabel
84+
}
85+
86+
const repoHost = this.repoLink.match(/^https?:\/\/[^/]+/)[0]
87+
const platforms = ['GitHub', 'GitLab', 'Bitbucket']
88+
for (let i = 0; i < platforms.length; i++) {
89+
const platform = platforms[i]
90+
if (new RegExp(platform, 'i').test(repoHost)) {
91+
return platform
92+
}
93+
}
94+
95+
return 'Source'
96+
},
8097
},
8198
methods: {
8299
isActive
@@ -100,12 +117,12 @@ export default {
100117
display inline-block
101118
margin-left 1.5rem
102119
line-height 2rem
103-
.github-link
120+
.repo-link
104121
margin-left 1.5rem
105122
106123
@media (max-width: $MQMobile)
107124
.nav-links
108-
.nav-item, .github-link
125+
.nav-item, .repo-link
109126
margin-left 0
110127
111128
@media (min-width: $MQMobile)

0 commit comments

Comments
 (0)