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

Commit 886be9d

Browse files
author
m7r
committed
fix(task): extract repo from github urls #164
1 parent eefbaa7 commit 886be9d

File tree

2 files changed

+50
-3
lines changed

2 files changed

+50
-3
lines changed

spec/prepareLinkSpec.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
describe('prepareLinks', function() {
2+
var grunt = require('grunt');
3+
var prepareLinks = require('../tasks/grunt-ngdocs.js')(grunt).prepareLinks;
4+
5+
it('should handel github https url', function() {
6+
var pkg = {repository: {url: 'https://github.com/owner/name'}};
7+
var opts = {sourceEdit:true, editLink: true};
8+
prepareLinks(pkg, opts);
9+
expect(opts.editLink('test.js')).toEqual('https://github.com/owner/name/edit/master/test.js');
10+
});
11+
12+
it('should handel github https url with .git suffix', function() {
13+
var pkg = {repository: {url: 'https://github.com/owner/name.git'}};
14+
var opts = {sourceEdit:true, editLink: true};
15+
prepareLinks(pkg, opts);
16+
expect(opts.editLink('test.js')).toEqual('https://github.com/owner/name/edit/master/test.js');
17+
});
18+
19+
it('should handel github https url with .git and path suffix', function() {
20+
var pkg = {repository: {url: 'https://github.com/owner/name.git/#123456/'}};
21+
var opts = {sourceEdit:true, editLink: true};
22+
prepareLinks(pkg, opts);
23+
expect(opts.editLink('test.js')).toEqual('https://github.com/owner/name/edit/master/test.js');
24+
});
25+
26+
it('should handel git@github url', function() {
27+
var pkg = {repository: {url: '[email protected]:owner/name'}};
28+
var opts = {sourceEdit:true, editLink: true};
29+
prepareLinks(pkg, opts);
30+
expect(opts.editLink('test.js')).toEqual('https://github.com/owner/name/edit/master/test.js');
31+
});
32+
33+
it('should handel git@github url with .git suffix', function() {
34+
var pkg = {repository: {url: '[email protected]:owner/name.git'}};
35+
var opts = {sourceEdit:true, editLink: true};
36+
prepareLinks(pkg, opts);
37+
expect(opts.editLink('test.js')).toEqual('https://github.com/owner/name/edit/master/test.js');
38+
});
39+
});

tasks/grunt-ngdocs.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,16 @@ var reader = require('../src/reader.js'),
1212
vm = require('vm');
1313

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

2122
module.exports = function(grunt) {
2223
var _ = grunt.util._,
24+
unittest = {},
2325
templates = path.resolve(__dirname, '../src/templates');
2426

2527
grunt.registerMultiTask('ngdocs', 'build documentation', function() {
@@ -161,7 +163,10 @@ module.exports = function(grunt) {
161163
repohosts.some(function(host) {
162164
var match = url.match(host.re);
163165
if (match) {
164-
values.repo = match[1];
166+
values.repo = match[1] || match[2];
167+
if (host.reSuffix) {
168+
values.repo = values.repo.replace(host.reSuffix, '');
169+
}
165170
if (host.sourceLink && options.sourceLink === true) {
166171
options.sourceLink = host.sourceLink;
167172
}
@@ -176,6 +181,8 @@ module.exports = function(grunt) {
176181
options.editLink = makeLinkFn(options.editLink, values);
177182
}
178183

184+
unittest.prepareLinks = prepareLinks;
185+
179186
function prepareSetup(section, options) {
180187
var setup, data, context = {},
181188
file = path.resolve(options.dest, 'js/docs-setup.js');
@@ -286,4 +293,5 @@ module.exports = function(grunt) {
286293

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

289-
};
296+
return unittest;
297+
};

0 commit comments

Comments
 (0)