Skip to content

Commit

Permalink
New helper "repoWebUrl"
Browse files Browse the repository at this point in the history
  • Loading branch information
nknapp committed May 21, 2017
1 parent c395c89 commit 134ac29
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
14 changes: 14 additions & 0 deletions docs/helpers.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Default Handlebars-helpers for Thought
* [.hasCoveralls()](#helpers.hasCoveralls) ⇒ <code>boolean</code>
* [.hasGreenkeeper(options)](#helpers.hasGreenkeeper)
* [.github(filePath)](#helpers.github) ⇒ <code>string</code>
* [.repoWebUrl(gitUrl)](#helpers.repoWebUrl)
* [.githubRepo()](#helpers.githubRepo) ⇒ <code>string</code>
* [.arr(...args)](#helpers.arr)

Expand Down Expand Up @@ -274,6 +275,19 @@ set correctly in package.json
| --- | --- | --- |
| filePath | <code>string</code> | the path to the file |
<a name="helpers.repoWebUrl"></a>
### helpers.repoWebUrl(gitUrl)
Returns the http-url for viewing a git-repository in the browser given a repo-url from the package.json
Currently, only github urls are supported
**Kind**: static method of [<code>helpers</code>](#helpers)
**Access**: public
| Param | Type | Description |
| --- | --- | --- |
| gitUrl | <code>string</code> | the git url from the repository.url-property of package.json |
<a name="helpers.githubRepo"></a>
### helpers.githubRepo() ⇒ <code>string</code>
Expand Down
22 changes: 21 additions & 1 deletion handlebars/helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ module.exports = {
withPackageOf,
github,
githubRepo,
repoWebUrl,
npm,
htmlId,
hasCoveralls,
Expand Down Expand Up @@ -380,7 +381,7 @@ function transformTree (object, fn) {
* @memberOf helpers
*/
function github (filePath) {
// Build url to correct version and file in github
// Build url to correct version and file in githubs
const packageJson = findPackage(path.resolve(filePath), true)
const url = packageJson && packageJson.repository && packageJson.repository.url
if (url && url.match(/github.com/)) {
Expand All @@ -391,6 +392,25 @@ function github (filePath) {
}
}

/**
* Returns the http-url for viewing a git-repository in the browser given a repo-url from the package.json
* Currently, only github urls are supported
* @param {string} gitUrl the git url from the repository.url-property of package.json
* @access public
* @memberOf helpers
*/
function repoWebUrl (gitUrl) {
if (!gitUrl) {
return undefined
}
const match = gitUrl.match(/.*?(:\/\/|@)github\.com[/:](.*?)(#.*?)?$/)
if (match) {
return 'https://github.com/' + match[2].replace(/\.git$/, '')
} else {
return null
}
}

/**
* Returns the current repository group and name (e.g. `nknapp/thought` for this project)
*
Expand Down
7 changes: 7 additions & 0 deletions test/helper-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -543,4 +543,11 @@ describe('thought-helpers:', function () {
return expectHbs("{{#each (arr 'a' 'b' 'c')}}v:{{.}} {{/each}}", {}).to.eventually.equal('v:a v:b v:c')
})
})

describe('The "repoWebUrl" helper', function () {
it('should return a github-web-url for a github-ssh-url', function () {
return expect(helpers.repoWebUrl('git+ssh://git@github.com/nknapp/thought-plugin-jsdoc.git'))
.to.equal('https://github.com/nknapp/thought-plugin-jsdoc')
})
})
})

0 comments on commit 134ac29

Please sign in to comment.