import path from "path" ; import url from "url" ; import LocationUtils from "../../utils/location" ; import editHTMLElement from "./editHTMLElement" ; param { string } currentFile param { Function param { HTMLDom } $ function resolveLinks ( currentFile , resolveFile , $ ) { const currentDirectory = path . dirname ( currentFile ) ; return editHTMLElement ( $ , "a" , ( $a ) => { let href = $a . attr ( "href" ) ; if ( ! href ) { return ; } if ( LocationUtils . isExternal ( href ) ) { $a . attr ( "target" , "_blank" ) ; return ; } const parsed = url . parse ( href ) ; href = parsed . pathname || "" ; if ( href ) { href = LocationUtils . toAbsolute ( href , currentDirectory , "." ) ; href = resolveFile ( href ) ; href = LocationUtils . relative ( currentDirectory , href ) ; } href = href + ( parsed . hash || "" ) ; $a . attr ( "href" , href ) ; } ) ; } export default resolveLinks ;