diff options
Diffstat (limited to 'lib/utils/location.js')
-rw-r--r-- | lib/utils/location.js | 49 |
1 files changed, 43 insertions, 6 deletions
diff --git a/lib/utils/location.js b/lib/utils/location.js index ba43644..84a71ad 100644 --- a/lib/utils/location.js +++ b/lib/utils/location.js @@ -30,9 +30,14 @@ function normalize(s) { return path.normalize(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 +/** + Convert relative to absolute path + + @param {String} href + @param {String} dir: directory parent of the file currently in rendering process + @param {String} outdir: directory parent from the html output + @return {String} +*/ function toAbsolute(_href, dir, outdir) { if (isExternal(_href)) return _href; outdir = outdir == undefined? dir : outdir; @@ -54,17 +59,49 @@ function toAbsolute(_href, dir, outdir) { return _href; } -// Convert an absolute path to a relative path for a specific folder (dir) -// ('test/', 'hello.md') -> '../hello.md' +/** + Convert an absolute path to a relative path for a specific folder (dir) + ('test/', 'hello.md') -> '../hello.md' + + @param {String} dir: current directory + @param {String} file: absolute path of file + @return {String} +*/ function relative(dir, file) { return normalize(path.relative(dir, file)); } +/** + Convert an absolute path to a relative path for a specific folder (dir) + ('test/test.md', 'hello.md') -> '../hello.md' + + @param {String} baseFile: current file + @param {String} file: absolute path of file + @return {String} +*/ +function relativeForFile(baseFile, file) { + return relative(path.dirname(baseFile), file); +} + +/** + Compare two paths, return true if they are identical + ('README.md', './README.md') -> true + + @param {String} p1: first path + @param {String} p2: second path + @return {Boolean} +*/ +function areIdenticalPaths(p1, p2) { + return normalize(p1) === normalize(p2); +} + module.exports = { + areIdenticalPaths: areIdenticalPaths, isExternal: isExternal, isRelative: isRelative, isAnchor: isAnchor, normalize: normalize, toAbsolute: toAbsolute, - relative: relative + relative: relative, + relativeForFile: relativeForFile }; |