Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix($theme-default): fix wrong editLink (close: #1115, #1425) #1419

Merged
merged 3 commits into from
Mar 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions packages/@vuepress/core/lib/node/Page.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ module.exports = class Page {
*/

constructor ({
path,
path: _path,
meta,
title,
content,
Expand All @@ -58,12 +58,16 @@ module.exports = class Page {

if (relative) {
this.regularPath = encodeURI(fileToPath(relative))
} else if (path) {
this.regularPath = encodeURI(path)
} else if (_path) {
this.regularPath = encodeURI(_path)
} else if (permalink) {
this.regularPath = encodeURI(permalink)
}

if (filePath) {
this.relativePath = path.relative(context.sourceDir, filePath).replace(/\\/g, '/')
}

this.key = 'v-' + hash(`${this._filePath}${this.regularPath}`)
// Using regularPath first, would be override by permalink later.
this.path = this.regularPath
Expand All @@ -85,11 +89,7 @@ module.exports = class Page {
enhancers = [],
preRender = {}
}) {
// relative path
let relPath

if (this._filePath) {
relPath = path.relative(this._context.sourceDir, this._filePath)
logger.developer(`static_route`, chalk.cyan(this.path))
this._content = await fs.readFile(this._filePath, 'utf-8')
} else if (this._content) {
Expand Down Expand Up @@ -124,7 +124,7 @@ module.exports = class Page {
if (excerpt) {
const { html } = markdown.render(excerpt, {
frontmatter: this.frontmatter,
relPath
relPath: this.relativePath
})
this.excerpt = html
}
Expand Down
22 changes: 8 additions & 14 deletions packages/@vuepress/theme-default/components/Page.vue
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
</template>

<script>
import { resolvePage, normalize, outboundRE, endingSlashRE } from '../util'
import { resolvePage, outboundRE, endingSlashRE } from '../util'

export default {
props: ['sidebarItems'],
Expand Down Expand Up @@ -116,14 +116,8 @@ export default {
docsRepo = repo
} = this.$site.themeConfig

let path = normalize(this.$page.path)
if (endingSlashRE.test(path)) {
path += 'README.md'
} else {
path += '.md'
}
if (docsRepo && editLinks) {
return this.createEditLink(repo, docsRepo, docsDir, docsBranch, path)
if (docsRepo && editLinks && this.$page.relativePath) {
return this.createEditLink(repo, docsRepo, docsDir, docsBranch, this.$page.relativePath)
}
},

Expand All @@ -146,8 +140,8 @@ export default {
return (
base.replace(endingSlashRE, '')
+ `/src`
+ `/${docsBranch}`
+ (docsDir ? '/' + docsDir.replace(endingSlashRE, '') : '')
+ `/${docsBranch}/`
+ (docsDir ? docsDir.replace(endingSlashRE, '') + '/' : '')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that changes here is unnecessary?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In past implementations, path is prefixed with a /, however in the pr, path is not prefixed with /. So adding a ending slash is a must.

+ path
+ `?mode=edit&spa=0&at=${docsBranch}&fileviewer=file-view-default`
)
Expand All @@ -156,11 +150,11 @@ export default {
const base = outboundRE.test(docsRepo)
? docsRepo
: `https://github.com/${docsRepo}`

return (
base.replace(endingSlashRE, '')
+ `/edit/${docsBranch}`
+ (docsDir ? '/' + docsDir.replace(endingSlashRE, '') : '')
+ `/edit`
+ `/${docsBranch}/`
+ (docsDir ? docsDir.replace(endingSlashRE, '') + '/' : '')
+ path
)
}
Expand Down