Skip to content
This repository has been archived by the owner on Jan 23, 2022. It is now read-only.

Commit

Permalink
fix(task): extract repo from github urls #164
Browse files Browse the repository at this point in the history
  • Loading branch information
m7r committed Aug 12, 2015
1 parent eefbaa7 commit 886be9d
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 3 deletions.
39 changes: 39 additions & 0 deletions spec/prepareLinkSpec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
describe('prepareLinks', function() {
var grunt = require('grunt');
var prepareLinks = require('../tasks/grunt-ngdocs.js')(grunt).prepareLinks;

it('should handel github https url', function() {
var pkg = {repository: {url: 'https://github.com/owner/name'}};
var opts = {sourceEdit:true, editLink: true};
prepareLinks(pkg, opts);
expect(opts.editLink('test.js')).toEqual('https://github.com/owner/name/edit/master/test.js');
});

it('should handel github https url with .git suffix', function() {
var pkg = {repository: {url: 'https://github.com/owner/name.git'}};
var opts = {sourceEdit:true, editLink: true};
prepareLinks(pkg, opts);
expect(opts.editLink('test.js')).toEqual('https://github.com/owner/name/edit/master/test.js');
});

it('should handel github https url with .git and path suffix', function() {
var pkg = {repository: {url: 'https://github.com/owner/name.git/#123456/'}};
var opts = {sourceEdit:true, editLink: true};
prepareLinks(pkg, opts);
expect(opts.editLink('test.js')).toEqual('https://github.com/owner/name/edit/master/test.js');
});

it('should handel git@github url', function() {
var pkg = {repository: {url: 'git@github.com:owner/name'}};
var opts = {sourceEdit:true, editLink: true};
prepareLinks(pkg, opts);
expect(opts.editLink('test.js')).toEqual('https://github.com/owner/name/edit/master/test.js');
});

it('should handel git@github url with .git suffix', function() {
var pkg = {repository: {url: 'git@github.com:owner/name.git'}};
var opts = {sourceEdit:true, editLink: true};
prepareLinks(pkg, opts);
expect(opts.editLink('test.js')).toEqual('https://github.com/owner/name/edit/master/test.js');
});
});
14 changes: 11 additions & 3 deletions tasks/grunt-ngdocs.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@ var reader = require('../src/reader.js'),
vm = require('vm');

var repohosts = [
{ re: /https?:\/\/github.com\/([^\/]+\/[^\/]+)|git@github.com:(.*)\.git/,
{ re: /https?:\/\/github.com\/([^\/]+\/[^\/]+)|git@github.com:(.*)/,
reSuffix: /\.git.*$/,
sourceLink: 'https://github.com/{{repo}}/blob/{{sha}}/{{file}}#L{{codeline}}',
editLink: 'https://github.com/{{repo}}/edit/master/{{file}}'
}
];

module.exports = function(grunt) {
var _ = grunt.util._,
unittest = {},
templates = path.resolve(__dirname, '../src/templates');

grunt.registerMultiTask('ngdocs', 'build documentation', function() {
Expand Down Expand Up @@ -161,7 +163,10 @@ module.exports = function(grunt) {
repohosts.some(function(host) {
var match = url.match(host.re);
if (match) {
values.repo = match[1];
values.repo = match[1] || match[2];
if (host.reSuffix) {
values.repo = values.repo.replace(host.reSuffix, '');
}
if (host.sourceLink && options.sourceLink === true) {
options.sourceLink = host.sourceLink;
}
Expand All @@ -176,6 +181,8 @@ module.exports = function(grunt) {
options.editLink = makeLinkFn(options.editLink, values);
}

unittest.prepareLinks = prepareLinks;

function prepareSetup(section, options) {
var setup, data, context = {},
file = path.resolve(options.dest, 'js/docs-setup.js');
Expand Down Expand Up @@ -286,4 +293,5 @@ module.exports = function(grunt) {

function now() { return new Date().getTime(); }

};
return unittest;
};

0 comments on commit 886be9d

Please sign in to comment.