diff options
author | Soreine <nicolas@gitbook.com> | 2017-03-09 16:09:45 +0100 |
---|---|---|
committer | Soreine <nicolas@gitbook.com> | 2017-03-09 16:09:45 +0100 |
commit | 621f0ef0016bda3f65fe4bf01a339ec693cee3ef (patch) | |
tree | af6d46c9b2e6f4bf751da8ea7253011e1a83c244 | |
parent | 956e5e484a64adb76a73789b848fe219a365a404 (diff) | |
download | gitbook-621f0ef0016bda3f65fe4bf01a339ec693cee3ef.zip gitbook-621f0ef0016bda3f65fe4bf01a339ec693cee3ef.tar.gz gitbook-621f0ef0016bda3f65fe4bf01a339ec693cee3ef.tar.bz2 |
gitbook: Extract `isInside` to PathUtils
-rw-r--r-- | packages/gitbook/src/modifiers/summary/deleteByPath.js | 59 | ||||
-rw-r--r-- | packages/gitbook/src/utils/path.js | 79 |
2 files changed, 77 insertions, 61 deletions
diff --git a/packages/gitbook/src/modifiers/summary/deleteByPath.js b/packages/gitbook/src/modifiers/summary/deleteByPath.js index 3ab6c92..a22f6b3 100644 --- a/packages/gitbook/src/modifiers/summary/deleteByPath.js +++ b/packages/gitbook/src/modifiers/summary/deleteByPath.js @@ -1,13 +1,14 @@ -const Path = require('path'); +const PathUtils = require('../../utils/path'); /** - Delete all articles that point to a file under the given path, unless - some of their children do not share that same path. - - @param {Summary} summary - @param {String} path Can be a file path or directory path - @return {Summary} -*/ + * Delete all articles pointing to a file under the given path. If some + * of their children do not share that same path, the articles are + * simply unlinked. + * + * @param {Summary} summary + * @param {String} path Can be a file path or directory path + * @return {Summary} + */ function deleteByPath(summary, path) { const parts = summary.getParts() .map((part) => { @@ -19,11 +20,11 @@ function deleteByPath(summary, path) { } /** - Same as `deleteByPath` but for a list of articles. - - @param {List<Article>} articles - @param {String} path - @return {List<Article} + * Same as `deleteByPath` but for a list of articles. + * + * @param {List<Article>} articles + * @param {String} path + * @return {List<Article} */ function deleteArticlesByPath(articles, path) { return articles @@ -32,35 +33,15 @@ function deleteArticlesByPath(articles, path) { articles: deleteArticlesByPath(article.articles, path) })) // Then delete top level articles if they don't have any descendant left. - .filterNot( - article => article.getArticles().isEmpty() && isInside(article, path) + .filterNot(article => + article.getArticles().isEmpty() + && PathUtils.isInside(article.getPath() || '', path) ) // Unlink those left - .map(article => isInside(article, path) ? article.merge({ ref: '' }) : article); -} - -/** - @param {Article} article - @param {String} potentialParent - @return {Boolean} True if path match the parent path, or if path is inside parent path - */ -function isInside(article, potentialParent) { - // For inside-directory checking, we want to allow trailing slashes, so normalize. - const path = stripTrailingSep(article.getPath() || ''); - potentialParent = stripTrailingSep(potentialParent); - - return path.lastIndexOf(potentialParent, 0) === 0 && - ( - path[potentialParent.length] === Path.sep || - path[potentialParent.length] === undefined + .map(article => PathUtils.isInside(article.getPath() || '', path) + ? article.merge({ ref: '' }) + : article ); } -function stripTrailingSep(path) { - if (path[path.length - 1] === Path.sep) { - return path.slice(0, -1); - } - return path; -} - module.exports = deleteByPath; diff --git a/packages/gitbook/src/utils/path.js b/packages/gitbook/src/utils/path.js index 03328e8..4c4b70b 100644 --- a/packages/gitbook/src/utils/path.js +++ b/packages/gitbook/src/utils/path.js @@ -1,38 +1,44 @@ -const path = require('path'); +const Path = require('path'); const error = require('./error'); -// Normalize a filename +/** + * Normalize a filename + */ function normalizePath(filename) { - return path.normalize(filename); + return Path.normalize(filename); } -// Return true if file path is inside a folder +/** + * Return true if file path is inside a folder + */ function isInRoot(root, filename) { - root = path.normalize(root); - filename = path.normalize(filename); + root = Path.normalize(root); + filename = Path.normalize(filename); if (root === '.') { return true; } - if (root[root.length - 1] != path.sep) { - root = root + path.sep; + if (root[root.length - 1] != Path.sep) { + root = root + Path.sep; } return (filename.substr(0, root.length) === root); } -// Resolve paths in a specific folder -// Throw error if file is outside this folder +/** + * Resolve paths in a specific folder + * Throw error if file is outside this folder + */ function resolveInRoot(root, ...args) { const input = args .reduce((current, p) => { // Handle path relative to book root ("/README.md") if (p[0] == '/' || p[0] == '\\') return p.slice(1); - return current ? path.join(current, p) : path.normalize(p); + return current ? Path.join(current, p) : Path.normalize(p); }, ''); - const result = path.resolve(root, input); + const result = Path.resolve(root, input); if (!isInRoot(root, result)) { throw new error.FileOutOfScopeError({ @@ -44,26 +50,55 @@ function resolveInRoot(root, ...args) { return result; } -// Chnage extension of a file +/** + * Change extension of a file + */ function setExtension(filename, ext) { - return path.join( - path.dirname(filename), - path.basename(filename, path.extname(filename)) + ext + return Path.join( + Path.dirname(filename), + Path.basename(filename, Path.extname(filename)) + ext ); } -/* - Return true if a filename is relative. - - @param {String} - @return {Boolean} -*/ +/** + * Return true if a filename is relative. + * + * @param {String} + * @return {Boolean} + */ function isPureRelative(filename) { return (filename.indexOf('./') === 0 || filename.indexOf('../') === 0); } +/** + * Implementation based on https://github.com/domenic/path-is-inside/ + * + * @param {String} path + * @param {String} potentialParent + * @return {Boolean} True if path match the parent path, or if path is inside parent path + */ +function isInside(path = '', potentialParent) { + // For inside-directory checking, we want to allow trailing slashes, so normalize. + path = stripTrailingSep(path); + potentialParent = stripTrailingSep(potentialParent); + + return path.lastIndexOf(potentialParent, 0) === 0 && + ( + path[potentialParent.length] === Path.sep || + path[potentialParent.length] === undefined + ); +} + +function stripTrailingSep(path) { + if (path[path.length - 1] === Path.sep) { + return path.slice(0, -1); + } + return path; +} + module.exports = { isInRoot, + isInside, resolveInRoot, normalize: normalizePath, setExtension, |