Skip to content
This repository was archived by the owner on Dec 15, 2022. It is now read-only.

Commit 8e5fdaf

Browse files
author
Wliu
authored
Merge pull request #101 from atom/wl-better-wiki-support
Add better support for wikis
2 parents 392c355 + 58beebf commit 8e5fdaf

File tree

2 files changed

+75
-7
lines changed

2 files changed

+75
-7
lines changed

lib/github-file.js

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ export default class GitHubFile {
7979
}
8080

8181
getLineRangeSuffix (lineRange) {
82-
if (lineRange && atom.config.get('open-on-github.includeLineNumbersInUrls')) {
82+
if (lineRange && !this.isGitHubWikiURL(this.githubRepoURL()) && atom.config.get('open-on-github.includeLineNumbersInUrls')) {
8383
lineRange = Range.fromObject(lineRange)
8484
const startRow = lineRange.start.row + 1
8585
const endRow = lineRange.end.row + 1
@@ -128,7 +128,7 @@ export default class GitHubFile {
128128
const repoRelativePath = this.repoRelativePath()
129129

130130
if (this.isGitHubWikiURL(gitHubRepoURL)) {
131-
return `${gitHubRepoURL.slice(0, -5)}/wiki/${this.extractFileName(repoRelativePath)}`
131+
return `${gitHubRepoURL}/${this.extractFileName(repoRelativePath)}`
132132
} else if (this.isGistURL(gitHubRepoURL)) {
133133
return `${gitHubRepoURL}#file-${this.encodeSegments(repoRelativePath.replace(/\./g, '-'))}`
134134
} else {
@@ -138,10 +138,12 @@ export default class GitHubFile {
138138

139139
// Internal
140140
blobURLForMaster () {
141-
if (this.isGistURL(this.githubRepoURL())) {
141+
const gitHubRepoURL = this.githubRepoURL()
142+
143+
if (this.isGitHubWikiURL(gitHubRepoURL) || this.isGistURL(gitHubRepoURL)) {
142144
return this.blobURL() // Gists do not have branches
143145
} else {
144-
return `${this.githubRepoURL()}/blob/master/${this.encodeSegments(this.repoRelativePath())}`
146+
return `${gitHubRepoURL}/blob/master/${this.encodeSegments(this.repoRelativePath())}`
145147
}
146148
}
147149

@@ -151,7 +153,9 @@ export default class GitHubFile {
151153
const encodedSHA = this.encodeSegments(this.sha())
152154
const repoRelativePath = this.repoRelativePath()
153155

154-
if (this.isGistURL(gitHubRepoURL)) {
156+
if (this.isGitHubWikiURL(gitHubRepoURL)) {
157+
return `${gitHubRepoURL}/${this.extractFileName(repoRelativePath)}/${encodedSHA}`
158+
} else if (this.isGistURL(gitHubRepoURL)) {
155159
return `${gitHubRepoURL}/${encodedSHA}#file-${this.encodeSegments(repoRelativePath.replace(/\./g, '-'))}`
156160
} else {
157161
return `${gitHubRepoURL}/blob/${encodedSHA}/${this.encodeSegments(repoRelativePath)}`
@@ -167,7 +171,9 @@ export default class GitHubFile {
167171
historyURL () {
168172
const gitHubRepoURL = this.githubRepoURL()
169173

170-
if (this.isGistURL(gitHubRepoURL)) {
174+
if (this.isGitHubWikiURL(gitHubRepoURL)) {
175+
return `${gitHubRepoURL}/${this.extractFileName(this.repoRelativePath())}/_history`
176+
} else if (this.isGistURL(gitHubRepoURL)) {
171177
return `${gitHubRepoURL}/revisions`
172178
} else {
173179
return `${gitHubRepoURL}/commits/${this.remoteBranchName()}/${this.encodeSegments(this.repoRelativePath())}`
@@ -225,6 +231,8 @@ export default class GitHubFile {
225231

226232
// Remove trailing .git and trailing slashes
227233
url = url.replace(/\.git$/, '').replace(/\/+$/, '')
234+
// Change .wiki to /wiki
235+
url = url.replace(/\.wiki$/, '/wiki')
228236

229237
if (!this.isBitbucketURL(url)) {
230238
return url
@@ -240,7 +248,7 @@ export default class GitHubFile {
240248
}
241249

242250
isGitHubWikiURL (url) {
243-
return /\.wiki$/.test(url)
251+
return /\/wiki$/.test(url)
244252
}
245253

246254
isBitbucketURL (url) {

spec/github-file-spec.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,21 @@ describe('GitHubFile', function () {
235235
})
236236
})
237237

238+
describe('when the file is part of a GitHub wiki', () => {
239+
let fixtureName = 'github-remote-wiki'
240+
241+
beforeEach(async () => {
242+
setupWorkingDir(fixtureName)
243+
await setupGithubFile()
244+
})
245+
246+
it('opens the GitHub.com wiki URL for the file and behaves exactly like open', () => {
247+
spyOn(githubFile, 'openURLInBrowser')
248+
githubFile.openOnMaster()
249+
expect(githubFile.openURLInBrowser).toHaveBeenCalledWith('https://github.com/some-user/some-repo/wiki/some-file')
250+
})
251+
})
252+
238253
describe('when the file is part of a GitHub gist', () => {
239254
let fixtureName = 'github-remote-gist'
240255

@@ -340,6 +355,21 @@ describe('GitHubFile', function () {
340355
})
341356
})
342357

358+
describe('when the file is part of a GitHub wiki', () => {
359+
let fixtureName = 'github-remote-wiki'
360+
361+
beforeEach(async () => {
362+
setupWorkingDir(fixtureName)
363+
await setupGithubFile()
364+
})
365+
366+
it('opens the GitHub.com wiki history URL for the file', () => {
367+
spyOn(githubFile, 'openURLInBrowser')
368+
githubFile.history()
369+
expect(githubFile.openURLInBrowser).toHaveBeenCalledWith('https://github.com/some-user/some-repo/wiki/some-file/_history')
370+
})
371+
})
372+
343373
describe('when the file is part of a GitHub gist', () => {
344374
let fixtureName = 'github-remote-gist'
345375

@@ -381,6 +411,21 @@ describe('GitHubFile', function () {
381411
})
382412
})
383413

414+
describe('when the file is part of a GitHub wiki', () => {
415+
let fixtureName = 'github-remote-wiki'
416+
417+
beforeEach(async () => {
418+
setupWorkingDir(fixtureName)
419+
atom.config.set('open-on-github.includeLineNumbersInUrls', true)
420+
await setupGithubFile()
421+
})
422+
423+
it('copies the GitHub.com wiki URL to the clipboard and ignores any selection ranges', () => {
424+
githubFile.copyURL([[0, 0], [1, 1]])
425+
expect(atom.clipboard.read()).toBe('https://github.com/some-user/some-repo/wiki/some-file/80b7897ceb6bd7531708509b50afeab36a4b73fd')
426+
})
427+
})
428+
384429
describe('when the file is part of a GitHub gist', () => {
385430
let fixtureName = 'github-remote-gist'
386431

@@ -422,6 +467,21 @@ describe('GitHubFile', function () {
422467
})
423468
})
424469

470+
describe('when the file is part of a GitHub wiki', () => {
471+
let fixtureName = 'github-remote-wiki'
472+
473+
beforeEach(async () => {
474+
setupWorkingDir(fixtureName)
475+
await setupGithubFile()
476+
})
477+
478+
it('opens the GitHub.com wiki history URL for the file', () => {
479+
spyOn(githubFile, 'openURLInBrowser')
480+
githubFile.openRepository()
481+
expect(githubFile.openURLInBrowser).toHaveBeenCalledWith('https://github.com/some-user/some-repo/wiki')
482+
})
483+
})
484+
425485
describe('when the file is part of a GitHub gist', () => {
426486
let fixtureName = 'github-remote-gist'
427487

0 commit comments

Comments
 (0)