diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/helpers/relativize.js | 19 |
1 files changed, 14 insertions, 5 deletions
diff --git a/src/helpers/relativize.js b/src/helpers/relativize.js index abab118..84615c9 100644 --- a/src/helpers/relativize.js +++ b/src/helpers/relativize.js @@ -4,11 +4,20 @@ const { posix: path } = require('path') // TODO memoize module.exports = (from, to) => { - if (to === '#') { - return to - } else if (to.charAt(to.length - 1) === '/') { - return from === to ? './' : path.relative(path.dirname(from + '.'), to) + '/' + if (to.charAt() === '#') return to + let hash = '' + const hashIdx = to.indexOf('#') + if (~hashIdx) { + hash = to.substr(hashIdx) + to = to.substr(0, hashIdx) + } + if (from === to) { + return hash || (isDir(to) ? './' : path.basename(to)) } else { - return path.relative(path.dirname(from + '.'), to) + return path.relative(path.dirname(from + '.'), to) + (isDir(to) ? '/' : '') + hash } } + +function isDir (str) { + return str.charAt(str.length - 1) === '/' +} |