From: Dan Allen Date: Wed, 18 Mar 2020 21:41:45 +0000 (-0600) Subject: modify implementation of relativize helper X-Git-Url: https://old-git.evergreen-ils.org/?a=commitdiff_plain;h=cdc5f6ca6254b0f0571837afc61ff5fa56a0e953;p=eg-antora.git modify implementation of relativize helper - consolidate logic - return to if not root-relative - return to prefixed with site path if from is undefined - export unnamed function --- diff --git a/src/helpers/relativize.js b/src/helpers/relativize.js index 76adfa4..c96a701 100644 --- a/src/helpers/relativize.js +++ b/src/helpers/relativize.js @@ -2,33 +2,23 @@ const { posix: path } = require('path') -// TODO memoize me -function relativize (to, ctx) { - let from - // legacy invocation - if (arguments.length > 2) { - [from, to, ctx] = arguments - } else { - from = ctx.data.root.page.url - } +module.exports = (to, from, ctx) => { if (!to) return '#' - const hashIdx = to.indexOf('#') - if (!hashIdx) return to + // NOTE only legacy invocation provides both to and from + if (!ctx) from = (ctx = from).data.root.page.url + if (to.charAt() !== '/') return to if (!from) return (ctx.data.root.site.path || '') + to let hash = '' + const hashIdx = to.indexOf('#') if (~hashIdx) { - hash = to.substr(hashIdx) to = to.substr(0, hashIdx) + hash = to.substr(hashIdx) } - if (from === to) { - return hash || (isDir(to) ? './' : path.basename(to)) - } else { - return path.relative(path.dirname(from + '.'), to) + (isDir(to) ? '/' + hash : hash) - } + return to === from + ? hash || (isDir(to) ? './' : path.basename(to)) + : (path.relative(path.dirname(from + '.'), to) || '.') + (isDir(to) ? '/' + hash : hash) } function isDir (str) { return str.charAt(str.length - 1) === '/' } - -module.exports = relativize