Skip to content

Commit 3b14375

Browse files
shigmaulivz
authored andcommitted
fix($theme-default): fix wrong editLink (close: #1115, #1125) (#1419)
1 parent cb02f26 commit 3b14375

File tree

2 files changed

+16
-22
lines changed

2 files changed

+16
-22
lines changed

packages/@vuepress/core/lib/node/Page.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ module.exports = class Page {
3737
*/
3838

3939
constructor ({
40-
path,
40+
path: _path,
4141
meta,
4242
title,
4343
content,
@@ -58,12 +58,16 @@ module.exports = class Page {
5858

5959
if (relative) {
6060
this.regularPath = encodeURI(fileToPath(relative))
61-
} else if (path) {
62-
this.regularPath = encodeURI(path)
61+
} else if (_path) {
62+
this.regularPath = encodeURI(_path)
6363
} else if (permalink) {
6464
this.regularPath = encodeURI(permalink)
6565
}
6666

67+
if (filePath) {
68+
this.relativePath = path.relative(context.sourceDir, filePath).replace(/\\/g, '/')
69+
}
70+
6771
this.key = 'v-' + hash(`${this._filePath}${this.regularPath}`)
6872
// Using regularPath first, would be override by permalink later.
6973
this.path = this.regularPath
@@ -85,11 +89,7 @@ module.exports = class Page {
8589
enhancers = [],
8690
preRender = {}
8791
}) {
88-
// relative path
89-
let relPath
90-
9192
if (this._filePath) {
92-
relPath = path.relative(this._context.sourceDir, this._filePath)
9393
logger.developer(`static_route`, chalk.cyan(this.path))
9494
this._content = await fs.readFile(this._filePath, 'utf-8')
9595
} else if (this._content) {
@@ -124,7 +124,7 @@ module.exports = class Page {
124124
if (excerpt) {
125125
const { html } = markdown.render(excerpt, {
126126
frontmatter: this.frontmatter,
127-
relPath
127+
relPath: this.relativePath
128128
})
129129
this.excerpt = html
130130
}

packages/@vuepress/theme-default/components/Page.vue

+8-14
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
</template>
6363

6464
<script>
65-
import { resolvePage, normalize, outboundRE, endingSlashRE } from '../util'
65+
import { resolvePage, outboundRE, endingSlashRE } from '../util'
6666
6767
export default {
6868
props: ['sidebarItems'],
@@ -116,14 +116,8 @@ export default {
116116
docsRepo = repo
117117
} = this.$site.themeConfig
118118
119-
let path = normalize(this.$page.path)
120-
if (endingSlashRE.test(path)) {
121-
path += 'README.md'
122-
} else {
123-
path += '.md'
124-
}
125-
if (docsRepo && editLinks) {
126-
return this.createEditLink(repo, docsRepo, docsDir, docsBranch, path)
119+
if (docsRepo && editLinks && this.$page.relativePath) {
120+
return this.createEditLink(repo, docsRepo, docsDir, docsBranch, this.$page.relativePath)
127121
}
128122
},
129123
@@ -146,8 +140,8 @@ export default {
146140
return (
147141
base.replace(endingSlashRE, '')
148142
+ `/src`
149-
+ `/${docsBranch}`
150-
+ (docsDir ? '/' + docsDir.replace(endingSlashRE, '') : '')
143+
+ `/${docsBranch}/`
144+
+ (docsDir ? docsDir.replace(endingSlashRE, '') + '/' : '')
151145
+ path
152146
+ `?mode=edit&spa=0&at=${docsBranch}&fileviewer=file-view-default`
153147
)
@@ -156,11 +150,11 @@ export default {
156150
const base = outboundRE.test(docsRepo)
157151
? docsRepo
158152
: `https://github.com/${docsRepo}`
159-
160153
return (
161154
base.replace(endingSlashRE, '')
162-
+ `/edit/${docsBranch}`
163-
+ (docsDir ? '/' + docsDir.replace(endingSlashRE, '') : '')
155+
+ `/edit`
156+
+ `/${docsBranch}/`
157+
+ (docsDir ? docsDir.replace(endingSlashRE, '') + '/' : '')
164158
+ path
165159
)
166160
}

0 commit comments

Comments
 (0)