-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
42 lines (36 loc) · 1.02 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
'use strict'
/**
* Find username and repo from given GitHub URL
*
* @export
* @param {string} url
* @returns {Object}
*/
function findUsernameAndRepo (ghurl) {
const RootRe = /((https?:\/\/)?github.com\/|[email protected]:)/g
const GitExtRe = /.git$/g
// Example
// [email protected]:rohmanhm/gh-to-pages.git => rohmanhm/gh-to-pages
// https://github.com/rohmanhm/gh-to-pages => rohmanhm/gh-to-pages
const ghShortURL = GitExtRe.test(ghurl)
? ghurl.replace(RootRe, '').replace(GitExtRe, '')
: ghurl.replace(RootRe, '')
const [username, repo] = ghShortURL.split('/')
return { username, repo }
}
/**
* Convert GitHub URL to GitHub Pages
*
* @param {any} ghurl
* @returns
*/
module.exports = function (ghurl) {
const ghPages = 'github.io'
const { username, repo } = findUsernameAndRepo(ghurl)
const rootGhPages = `https://${username}.${ghPages}`
if (rootGhPages === repo) {
return rootGhPages
}
return `${rootGhPages}/${repo}`
}
module.exports.findUsernameAndRepo = findUsernameAndRepo