summaryrefslogtreecommitdiffstats
path: root/lib/utils
diff options
context:
space:
mode:
authorSamy Pessé <samypesse@gmail.com>2016-06-29 10:31:06 +0200
committerSamy Pessé <samypesse@gmail.com>2016-06-29 10:31:06 +0200
commitc9c2dde2b63505177265c66b1a6c4dd358415416 (patch)
tree444b6a539c93f677c6f8ee5eac67dca8de7cdab8 /lib/utils
parentd9a1d387c7a61aa18cdb9b2916bc761e0f902804 (diff)
downloadgitbook-c9c2dde2b63505177265c66b1a6c4dd358415416.zip
gitbook-c9c2dde2b63505177265c66b1a6c4dd358415416.tar.gz
gitbook-c9c2dde2b63505177265c66b1a6c4dd358415416.tar.bz2
Add method "isFile" and "isReadme" in SummaryArticle
Diffstat (limited to 'lib/utils')
-rw-r--r--lib/utils/location.js86
1 files changed, 51 insertions, 35 deletions
diff --git a/lib/utils/location.js b/lib/utils/location.js
index 17edc00..00d8004 100644
--- a/lib/utils/location.js
+++ b/lib/utils/location.js
@@ -40,13 +40,28 @@ function normalize(s) {
}
/**
- Convert a relative path to absolute
+ * Flatten a path, it removes the leading "/"
+ *
+ * @param {String} href
+ * @return {String}
+ */
+function flatten(href) {
+ href = normalize(href);
+ if (href[0] == '/') {
+ href = normalize(href.slice(1));
+ }
+
+ return href;
+}
- @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}
-*/
+/**
+ * Convert a relative path to absolute
+ *
+ * @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) || isDataURI(_href)) {
return _href;
@@ -74,50 +89,51 @@ function toAbsolute(_href, dir, outdir) {
}
/**
- 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}
-*/
+ * 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) {
var isDirectory = file.slice(-1) === '/';
return normalize(path.relative(dir, file)) + (isDirectory? '/': '');
}
/**
- 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}
-*/
+ * 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}
-*/
+ * 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,
- isDataURI: isDataURI,
- isExternal: isExternal,
- isRelative: isRelative,
- isAnchor: isAnchor,
- normalize: normalize,
- toAbsolute: toAbsolute,
- relative: relative,
- relativeForFile: relativeForFile
+ isDataURI: isDataURI,
+ isExternal: isExternal,
+ isRelative: isRelative,
+ isAnchor: isAnchor,
+ normalize: normalize,
+ toAbsolute: toAbsolute,
+ relative: relative,
+ relativeForFile: relativeForFile,
+ flatten: flatten
};