import path from "path" ; /** Get URL for any asset from a repo */ export function repoAssetURL ( url , filepath , version ? ) { let sanitizedURL = repoURL ( url ) ; const { hostname , pathname } = new URL ( sanitizedURL ) ; switch ( hostname ) { case "bitbucket.org" : { return `https://bitbucket.org${ path . join ( "/" , pathname , version || "HEAD" , filepath ) }` ; } case "github.com" : { const [ repo , dir ] = pathname . split ( "/tree/" ) ; return `https://raw.githubusercontent.com${ path . join ( "/" , repo , version || "HEAD" , dir ? dir . replace ( /[^/]+/ , "" ) : "" , filepath ) }` ; } case "gitlab.com" : { return `https://gitlab.com${ path . join ( "/" , pathname , "-" , "raw" , version || "HEAD" , filepath ) }` ; } default : { return undefined ; } } } /** Turn repo URL into normal URL */ export function repoURL ( url ) { return url . trim ( ) . replace ( /^git\+/i , "" ) . replace ( /\.git$/i , "" ) . replace ( /git@/ , "" ) . replace ( /(bitbucket|github|gitlab)\.([a-z]+):/ , "$1.$2/" ) . replace ( /bitbucket:/ , "$1.org/" ) . replace ( /(github|gitlab):/ , "$1.com/" ) . replace ( /^(http\s*:\/\/|https\s*:\/\/|\/\/)?/ , "https://" ) ; }