Skip to content

Commit 26b0115

Browse files
committed
✨ feat: Show "Edit this page" links at the bottom of each page
Signed-off-by: sqrtthree <[email protected]>
1 parent 577f662 commit 26b0115

File tree

3 files changed

+70
-1
lines changed

3 files changed

+70
-1
lines changed

Page.vue

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
<Content custom />
44
<div class="content__footer-container">
55
<div class="content__footer">
6+
<div v-if="editLink" class="edit-link">
7+
<a :href="editLink" target="_blank" rel="noopener noreferrer">{{ editLinkText }}</a>
8+
<svg viewBox="0 0 33 32" version="1.1" xmlns="http://www.w3.org/2000/svg" height="16" width="16"><g id="Page-1" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd"><g id="github" fill="#000"><path d="M16.3,0 C7.3,0 -3.55271368e-15,7.3 -3.55271368e-15,16.3 C-3.55271368e-15,23.5 4.7,29.6 11.1,31.8 C11.9,31.9 12.2,31.4 12.2,31 L12.2,28.2 C7.7,29.2 6.7,26 6.7,26 C6,24.2 5,23.7 5,23.7 C3.5,22.7 5.1,22.7 5.1,22.7 C6.7,22.8 7.6,24.4 7.6,24.4 C9.1,26.9 11.4,26.2 12.3,25.8 C12.4,24.7 12.9,24 13.3,23.6 C9.7,23.2 5.9,21.8 5.9,15.5 C5.9,13.7 6.5,12.3 7.6,11.1 C7.4,10.7 6.9,9 7.8,6.8 C7.8,6.8 9.2,6.4 12.3,8.5 C13.6,8.1 15,8 16.4,8 C17.8,8 19.2,8.2 20.5,8.5 C23.6,6.4 25,6.8 25,6.8 C25.9,9 25.3,10.7 25.2,11.1 C26.2,12.2 26.9,13.7 26.9,15.5 C26.9,21.8 23.1,23.1 19.5,23.5 C20.1,24 20.6,25 20.6,26.5 L20.6,31 C20.6,31.4 20.9,31.9 21.7,31.8 C28.2,29.6 32.8,23.5 32.8,16.3 C32.6,7.3 25.3,0 16.3,0 L16.3,0 Z" id="Shape"></path></g></g></svg>
9+
</div>
610
<time v-if="lastUpdated" class="last-updated">
711
<span class="prefix">{{ lastUpdatedText }}: </span>
812
<span class="time">{{ lastUpdated }}</span>
@@ -13,6 +17,8 @@
1317
</template>
1418

1519
<script>
20+
import { isExternalLink } from './utils'
21+
1622
const isHeading = el => {
1723
const tagname = el.tagName.toLowerCase()
1824
@@ -50,6 +56,43 @@ export default {
5056
5157
return 'Last Updated'
5258
},
59+
editLink() {
60+
if (this.$page.frontmatter.editLink === false) {
61+
return
62+
}
63+
64+
const {
65+
repo,
66+
editLinks,
67+
docsDir = '',
68+
docsBranch = 'master',
69+
docsRepo = repo,
70+
} = this.$site.themeConfig
71+
72+
let path = this.$page.path
73+
74+
if (path.substr(-1) === '/') {
75+
path += 'README.md'
76+
} else {
77+
path += '.md'
78+
}
79+
80+
if (docsRepo && editLinks) {
81+
const base = isExternalLink(docsRepo)
82+
? docsRepo
83+
: `https://github.com/${docsRepo}`
84+
85+
return (
86+
base.replace(/\/$/, '') +
87+
`/edit/${docsBranch}` +
88+
(docsDir ? '/' + docsDir.replace(/\/$/, '') : '') +
89+
path
90+
)
91+
}
92+
},
93+
editLinkText() {
94+
return this.$site.themeConfig.editLinkText || `Edit this page`
95+
},
5396
},
5497
watch: {
5598
$route(to, from) {
@@ -136,6 +179,14 @@ export default {
136179
font-size: 14px
137180
color: #999
138181
182+
.edit-link
183+
a
184+
margin-right: .5em
185+
font-weight: 600
186+
color: #000
187+
svg
188+
vertical-align: middle
189+
139190
.page--block-layout
140191
.content__footer-container
141192
margin: 0 -3rem

docs/.vuepress/config.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,24 @@ module.exports = {
1818
},
1919
},
2020
themeConfig: {
21+
// Assumes GitHub. Can also be a full GitLab url.
22+
repo: 'sqrthree/vuepress-theme-api',
23+
// Customising the header label
24+
// Defaults to "GitHub"/"GitLab"/"Bitbucket" depending on `themeConfig.repo`
25+
repoLabel: 'Contribute!',
26+
27+
// Optional options for generating "Edit this page" link
28+
29+
// if your docs are in a different repo from your main project:
30+
docsRepo: 'sqrthree/vuepress-theme-api',
31+
// if your docs are not at the root of the repo:
32+
docsDir: 'docs',
33+
// if your docs are in a specific branch (defaults to 'master'):
34+
docsBranch: 'master',
35+
// defaults to false, set to true to enable
36+
editLinks: true,
37+
// custom text for edit link. Defaults to "Edit this page"
38+
// editLinkText: 'Help us improve this page',
2139
lastUpdated: 'Last Updated', // string | boolean
2240
sidebarGroupOrder: [
2341
'getting-started',

utils.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export function title(str) {
5050
}
5151

5252
export function isExternalLink(path) {
53-
return /^(https?:)/.test(path)
53+
return /^(https?:|mailto:|tel:)/.test(path)
5454
}
5555

5656
export function matchLocalePathFromPath(path, locales) {

0 commit comments

Comments
 (0)