diff options
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 }; |