Add getPartPartsFromUrl function

This commit is contained in:
Sam Carlton 2022-05-05 13:47:23 -05:00
parent 93afef5f62
commit b188d3cd72

View file

@ -38,14 +38,21 @@ export function getSiteUrl () {
throw new Error('Could not find site URL') throw new Error('Could not find site URL')
} }
export function getPathPartsFromAstroRequest ( AstroRequest ) { export function getPartPartsFromUrl ( urlString ) {
// Parse the request url if ( typeof urlString !== 'string' ) throw new Error('urlString must be a string')
const requestUrl = new URL( AstroRequest.url )
const pathParts = requestUrl.pathname const url = new URL( urlString )
const pathParts = url.pathname
.replace(/^\/+/, '') // Trim slashes from the beginning .replace(/^\/+/, '') // Trim slashes from the beginning
.replace(/\/+$/, '') // Trim slashes from the end .replace(/\/+$/, '') // Trim slashes from the end
.split('/') .split('/')
return pathParts return pathParts
} }
export function getPathPartsFromAstroRequest ( AstroRequest ) {
// Parse the request url
return getPartPartsFromUrl ( AstroRequest.url )
}