diff options
author | Samy Pesse <samypesse@gmail.com> | 2016-02-12 23:57:43 +0100 |
---|---|---|
committer | Samy Pesse <samypesse@gmail.com> | 2016-02-12 23:57:43 +0100 |
commit | 4c6717e23488656686f276aa2b40ce1d1c7641f8 (patch) | |
tree | c728f106224cedc836b83b06609011990eac4f5a /lib/utils/location.js | |
parent | 39b6562d1445e9a6c43a377d2a978eefa6458755 (diff) | |
download | gitbook-4c6717e23488656686f276aa2b40ce1d1c7641f8.zip gitbook-4c6717e23488656686f276aa2b40ce1d1c7641f8.tar.gz gitbook-4c6717e23488656686f276aa2b40ce1d1c7641f8.tar.bz2 |
Add conversion of svg to png for assets inliner
Diffstat (limited to 'lib/utils/location.js')
-rw-r--r-- | lib/utils/location.js | 31 |
1 files changed, 30 insertions, 1 deletions
diff --git a/lib/utils/location.js b/lib/utils/location.js index efe1425..3f3cb37 100644 --- a/lib/utils/location.js +++ b/lib/utils/location.js @@ -1,4 +1,5 @@ var url = require('url'); +var path = require('path'); // Is the url an external url function isExternal(href) { @@ -24,8 +25,36 @@ function isAnchor(href) { } } +// Normalize a path to be a link +function normalize(s) { + return s.replace(/\\/g, '/'); +} + +// Convert relative to absolute path +// dir: directory parent of the file currently in rendering process +// outdir: directory parent from the html output +function toAbsolute(_href, dir, outdir) { + outdir = outdir == undefined? dir : outdir; + + if (isExternal(_href)) return _href; + + // Path "_href" inside the base folder + var hrefInRoot = path.normalize(path.join(dir, _href)); + if (_href[0] == '/') hrefInRoot = path.normalize(_href.slice(1)); + + // Make it relative to output + _href = path.relative(outdir, hrefInRoot); + + // Normalize windows paths + _href = normalize(_href); + + return _href; +} + module.exports = { isExternal: isExternal, isRelative: isRelative, - isAnchor: isAnchor + isAnchor: isAnchor, + normalize: normalize, + toAbsolute: toAbsolute }; |