import url from "url" ; import path from "path" ; function isExternal ( href ) { try { return Boolean ( url . parse ( href ) . protocol ) && ! isDataURI ( href ) ; } catch ( err ) { return false ; } } function isDataURI ( href ) { try { return Boolean ( url . parse ( href ) . protocol ) && url . parse ( href ) . protocol === "data:" ; } catch ( err ) { return false ; } } function isRelative ( href ) { return ! isExternal ( href ) ; } function isAnchor ( href ) { try { const parsed = url . parse ( href ) ; return ! ! ( ! parsed . protocol && ! parsed . path && parsed . hash ) ; } catch ( err ) { return false ; } } function normalize ( s ) { return path . normalize ( s ) . replace ( /\\/g , "/" ) ; } param { string } href return { string } function flatten ( href ) { href = normalize ( href ) ; if ( href [ 0 ] == "/" ) { href = normalize ( href . slice ( 1 ) ) ; } return href ; } param { string } _href param { string } dir param { string } outdir return { string } function toAbsolute ( _href , dir , outdir ) { if ( isExternal ( _href ) || isDataURI ( _href ) ) { return _href ; } outdir = outdir == undefined ? dir : outdir ; _href = normalize ( _href ) ; dir = normalize ( dir ) ; outdir = normalize ( outdir ) ; let hrefInRoot = normalize ( path . join ( dir , _href ) ) ; if ( _href [ 0 ] == "/" ) { hrefInRoot = normalize ( _href . slice ( 1 ) ) ; } _href = path . relative ( outdir , hrefInRoot ) ; _href = normalize ( _href ) ; return _href ; } param { string } dir param { string } file return { string } function relative ( dir , file ) { const isDirectory = file . slice ( - 1 ) === "/" ; return normalize ( path . relative ( dir , file ) ) + ( isDirectory ? "/" : "" ) ; } param { string } baseFile param { string } file return { string } function relativeForFile ( baseFile , file ) { return relative ( path . dirname ( baseFile ) , file ) ; } param { string } p1 param { string } p2 return { boolean } function areIdenticalPaths ( p1 , p2 ) { return normalize ( p1 ) === normalize ( p2 ) ; } export default { areIdenticalPaths : areIdenticalPaths , isDataURI : isDataURI , isExternal : isExternal , isRelative : isRelative , isAnchor : isAnchor , normalize : normalize , toAbsolute : toAbsolute , relative : relative , relativeForFile : relativeForFile , flatten : flatten , } ;