summaryrefslogtreecommitdiffstats
path: root/lib/utils/location.js
diff options
context:
space:
mode:
authorSamy Pesse <samypesse@gmail.com>2016-04-30 20:15:08 +0200
committerSamy Pesse <samypesse@gmail.com>2016-04-30 20:15:08 +0200
commit36b49c66c6b75515bc84dd678fd52121a313e8d2 (patch)
treebc7e0f703d4557869943ec7f9495cac7a5027d4f /lib/utils/location.js
parent87db7cf1d412fa6fbd18e9a7e4f4755f2c0c5547 (diff)
parent80b8e340dadc54377ff40500f86b1de631395806 (diff)
downloadgitbook-36b49c66c6b75515bc84dd678fd52121a313e8d2.zip
gitbook-36b49c66c6b75515bc84dd678fd52121a313e8d2.tar.gz
gitbook-36b49c66c6b75515bc84dd678fd52121a313e8d2.tar.bz2
Merge branch 'fixes'
Diffstat (limited to 'lib/utils/location.js')
-rw-r--r--lib/utils/location.js49
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
};