summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorDan Allen <dan@opendevise.com>2018-02-08 00:50:09 -0700
committerDan Allen <dan@opendevise.com>2018-02-08 16:23:01 -0700
commitecbb74a88bf069238a8b62d4ca488d7efe28f10d (patch)
tree5f811be57cd52c807e94aa183a7be38e3737ace9 /src
parentcd3d1da65937904b0849599c7f939e85d7bc2de5 (diff)
downloadalpine-antora-theme-ecbb74a88bf069238a8b62d4ca488d7efe28f10d.tar.bz2
alpine-antora-theme-ecbb74a88bf069238a8b62d4ca488d7efe28f10d.tar.xz
correctly relativize URL when target ends with a fragment identifier
Diffstat (limited to 'src')
-rw-r--r--src/helpers/relativize.js19
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) === '/'
+}