import { Nil } from "./types" ; import { trimPrefix , isDefined } from "./utility/primitive" ; param path export function splitPath ( path ) { return withoutTrailing ( withoutLeading ( path ) ) . split ( "/" ) ; } const cachedExternalRegex = /^(?:(?:http|https):\/\/(?!(?:www\.)?archit.us)[\w./=?#-_]+)|(?:mailto:.+)$/ ; let externalRegex = null ; /**
 * Lazily gets the regular expression used to determine if a link is external, using
 * `window.location.host` if available and caching the created regex
 */ export function getExternalRegex ( ) { if ( isDefined ( externalRegex ) ) return externalRegex ; if ( typeof window !== "undefined" ) { const regexStr = `^(?:(?:http|https):\\/\\/(?!(?:www\\.)?${ window . location . host })[\\w./=?#-_]+)|(?:mailto:.+)$` ; const regex = new RegExp ( regexStr ) ; externalRegex = regex ; return externalRegex ; } return cachedExternalRegex ; } param href export function isExternal ( path ) { return getExternalRegex ( ) . test ( path ) ; } param path export function trimFilePath ( path , extensions = [ ] ) { let withoutExtensions = path ; if ( typeof extensions === "string" ) { withoutExtensions = withoutExtensions . replace ( `.${ extensions }` , "" ) ; } else if ( isDefined ( withoutExtensions ) ) { extensions . forEach ( ( ext ) => { withoutExtensions = withoutExtensions . replace ( `.${ ext }` , "" ) ; } ) ; } return `/${ withoutExtensions . replace ( "index" , "" ) . replace ( /\/$/ , "" ) }` ; } param path param prefix export function withPathPrefix ( path , prefix ) { if ( isDefined ( prefix ) && prefix . trim ( ) . length > 0 ) { if ( prefix !== "/" ) { return `${ normalizePath ( prefix ) }/${ withoutLeading ( path ) }` ; } return `/${ withoutLeading ( path ) }` ; } return path ; } /**
 * Normalizes an absolute site path
 */ export function normalizePath ( path ) { return `/${ withoutTrailing ( withoutLeading ( path ) ) }` ; } param pathPrefix export function withoutTrailing ( path ) { return path . slice ( - 1 ) === "/" ? path . slice ( 0 , - 1 ) : path ; } param pathPrefix export function withoutLeading ( path ) { return path . slice ( 0 , 1 ) === "/" ? path . slice ( 1 ) : path ; } /**
 * Performs basic window location matching on an (absolute) path, taking
 * into account partial matching and an optional path prefix
 */ export function locationMatches ( { path , pathPrefix = null , location , partial = false , } ) { let actualLocation = location ; if ( isDefined ( pathPrefix ) && pathPrefix !== "/" ) { const withLeading = `/${ withoutLeading ( pathPrefix ) }` ; if ( ! actualLocation . startsWith ( withLeading ) ) return false ; actualLocation = trimPrefix ( actualLocation , withLeading ) ; } const normalizedLocation = normalizePath ( actualLocation ) ; const normalizedPath = normalizePath ( path ) ; return partial ? normalizedLocation . startsWith ( normalizedPath ) : normalizedLocation === normalizedPath ; }