{
  "version": 3,
  "sources": ["../src/catch-links.ts"],
  "sourcesContent": ["/**\n * Callback on any link click that is local to the server.\n *\n * @param {HTMLElement} root The root element to listen on.\n * @param {(href:string)=>void} cb The function to call on link click.\n */\nexport function CatchLinks (\n    root:HTMLElement,\n    cb:(href:string) => void,\n    opts:{\n        handleAnchor?:boolean|((href:string)=>boolean),\n        handleLink?:(href:string)=>boolean,\n    } = {},\n):()=>void {\n    root.addEventListener('click', onClick)\n\n    function onClick (ev:MouseEvent) {\n        // if command click, do nothing\n        if (ev.altKey || ev.ctrlKey || ev.metaKey || ev.shiftKey ||\n            ev.defaultPrevented) {\n            return true\n        }\n\n        let anchor:null|HTMLElement = null\n        for (\n            let n = (ev.target as HTMLElement|null);\n            n && n.parentNode;\n            n = n.parentElement\n        ) {\n            if (n!.nodeName === 'A') {\n                anchor = n\n                break\n            }\n        }\n\n        // if not a link click, do nothing\n        if (!anchor) return true\n\n        const url = new URL(anchor.getAttribute('href')!, location.origin)\n        const urlPath = url.pathname + url.search\n\n        // if not local, do nothing\n        if (url.host !== location.host) return true\n\n        // if we were given a function to check, call it\n        if (opts.handleLink) {\n            if (!opts.handleLink(urlPath)) return\n        }\n\n        const handleAnchor = opts.handleAnchor === undefined ? true : opts.handleAnchor\n\n        // else, handle the click\n        if (url.href.includes('#')) {\n            if (typeof handleAnchor === 'function') {\n                // do we want to handle this?\n                const handle = handleAnchor(url.href)\n                if (handle) {\n                    ev.preventDefault()\n                    cb(resolve(location.pathname, urlPath || '') +\n                        (url.hash || ''))\n                    return false\n                }\n            } else {\n                if (handleAnchor) {\n                    ev.preventDefault()\n                    cb(resolve(location.pathname, urlPath || '') +\n                        (url.hash || ''))\n                    return false\n                }\n            }\n        } else {\n            ev.preventDefault()\n            cb(resolve(location.pathname, urlPath || '') + (url.hash || ''))\n            return false\n        }\n    }\n\n    return function unlisten () {\n        root.removeEventListener('click', onClick)\n    }\n}\n\nCatchLinks.resolve = resolve\n\nexport default CatchLinks\n\n/**\n * Resolve a local link to a full local path.\n *\n * @param {string} from\n * @param {string} to\n */\nexport function resolve (from:string, to:string):string {\n    const isRelative = (to.charAt(0) !== '/')\n    if (!isRelative) return to\n\n    const fromArr = from.split('/')\n        .map(path => path.replaceAll('/', ''))\n        .filter(Boolean)\n    const toArr = to.split('/').map(path => path.replaceAll('/', ''))\n\n    const str = fromArr.concat(toArr).join('/')\n    return str.charAt(0) === '/' ? str : '/' + str\n}\n"],
  "mappings": "4dAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,gBAAAE,EAAA,YAAAC,EAAA,YAAAC,IAAA,eAAAC,EAAAL,GAMO,SAASM,EACZC,EACAC,EACAC,EAGI,CAAC,EACE,CACPF,EAAK,iBAAiB,QAASG,CAAO,EAEtC,SAASA,EAASC,EAAe,CAE7B,GAAIA,EAAG,QAAUA,EAAG,SAAWA,EAAG,SAAWA,EAAG,UAC5CA,EAAG,iBACH,MAAO,GAGX,IAAIC,EAA0B,KAC9B,QACQC,EAAKF,EAAG,OACZE,GAAKA,EAAE,WACPA,EAAIA,EAAE,cAEN,GAAIA,EAAG,WAAa,IAAK,CACrBD,EAASC,EACT,KACJ,CAIJ,GAAI,CAACD,EAAQ,MAAO,GAEpB,IAAME,EAAM,IAAI,IAAIF,EAAO,aAAa,MAAM,EAAI,SAAS,MAAM,EAC3DG,EAAUD,EAAI,SAAWA,EAAI,OAGnC,GAAIA,EAAI,OAAS,SAAS,KAAM,MAAO,GAGvC,GAAIL,EAAK,YACD,CAACA,EAAK,WAAWM,CAAO,EAAG,OAGnC,IAAMC,EAAeP,EAAK,eAAiB,OAAY,GAAOA,EAAK,aAGnE,GAAIK,EAAI,KAAK,SAAS,GAAG,GACrB,GAAI,OAAOE,GAAiB,YAGxB,GADeA,EAAaF,EAAI,IAAI,EAEhC,OAAAH,EAAG,eAAe,EAClBH,EAAGS,EAAQ,SAAS,SAAUF,GAAW,EAAE,GACtCD,EAAI,MAAQ,GAAG,EACb,WAGPE,EACA,OAAAL,EAAG,eAAe,EAClBH,EAAGS,EAAQ,SAAS,SAAUF,GAAW,EAAE,GACtCD,EAAI,MAAQ,GAAG,EACb,OAIf,QAAAH,EAAG,eAAe,EAClBH,EAAGS,EAAQ,SAAS,SAAUF,GAAW,EAAE,GAAKD,EAAI,MAAQ,GAAG,EACxD,EAEf,CA3DS,OAAAI,EAAAR,EAAA,WA6DFQ,EAAA,UAAqB,CACxBX,EAAK,oBAAoB,QAASG,CAAO,CAC7C,EAFO,WAGX,CA1EgBQ,EAAAZ,EAAA,cA4EhBA,EAAW,QAAUW,EAErB,IAAOE,EAAQb,EAQR,SAASW,EAASG,EAAaC,EAAkB,CAEpD,GAAI,EADgBA,EAAG,OAAO,CAAC,IAAM,KACpB,OAAOA,EAExB,IAAMC,EAAUF,EAAK,MAAM,GAAG,EACzB,IAAIG,GAAQA,EAAK,WAAW,IAAK,EAAE,CAAC,EACpC,OAAO,OAAO,EACbC,EAAQH,EAAG,MAAM,GAAG,EAAE,IAAIE,GAAQA,EAAK,WAAW,IAAK,EAAE,CAAC,EAE1DE,EAAMH,EAAQ,OAAOE,CAAK,EAAE,KAAK,GAAG,EAC1C,OAAOC,EAAI,OAAO,CAAC,IAAM,IAAMA,EAAM,IAAMA,CAC/C,CAXgBP,EAAAD,EAAA",
  "names": ["catch_links_exports", "__export", "CatchLinks", "catch_links_default", "resolve", "__toCommonJS", "CatchLinks", "root", "cb", "opts", "onClick", "ev", "anchor", "n", "url", "urlPath", "handleAnchor", "resolve", "__name", "catch_links_default", "from", "to", "fromArr", "path", "toArr", "str"]
}
