From c91e48b08da1ff5fb2c00e7e9fc5e12ea2cf8347 Mon Sep 17 00:00:00 2001 From: Glavin Wiechert Date: Wed, 29 Aug 2018 18:38:16 -0300 Subject: [PATCH] Handle blob and tree being in owner and project name --- index.js | 5 +++-- test.js | 8 ++++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index dd39f06..c837847 100644 --- a/index.js +++ b/index.js @@ -52,12 +52,13 @@ function parse(str) { } var blob = str.indexOf('blob'); - if (blob !== -1) { + if (hasBlob && blob !== -1) { obj.blob = str.slice(blob + 5); } + var hasTree = seg[2] === 'tree'; var tree = str.indexOf('tree'); - if (tree !== -1) { + if (hasTree && tree !== -1) { var idx = tree + 5; var branch = str.slice(idx); var slash = branch.indexOf('/'); diff --git a/test.js b/test.js index 1015721..37405c6 100644 --- a/test.js +++ b/test.js @@ -78,6 +78,14 @@ describe('parse-github-url', function() { assert.equal(gh('https://github.com/assemble/verb/tree/dev').filepath, null); assert.equal(gh('https://raw.githubusercontent.com/assemble/verb/dev/README.md').filepath, 'README.md'); assert.equal(gh('https://raw.githubusercontent.com/assemble/verb/dev/bar/README.md').filepath, 'bar/README.md'); + assert.equal(gh('https://github.com/owner/blob/blob/foo/README.md').filepath, 'README.md'); + assert.equal(gh('https://github.com/blob/project/blob/foo/README.md').filepath, 'README.md'); + assert.equal(gh('https://github.com/owner/tree/tree/foo/README.md').filepath, null); + assert.equal(gh('https://github.com/tree/project/tree/foo/README.md').filepath, null); + assert.equal(gh('https://raw.githubusercontent.com/owner/tree/dev/README.md').filepath, 'README.md'); + assert.equal(gh('https://raw.githubusercontent.com/tree/project/dev/README.md').filepath, 'README.md'); + assert.equal(gh('https://raw.githubusercontent.com/owner/blob/dev/README.md').filepath, 'README.md'); + assert.equal(gh('https://raw.githubusercontent.com/blob/project/dev/README.md').filepath, 'README.md'); }); it('should use master branch when another branch is not defined:', function() {