diff options
author | Soreine <nicolas@gitbook.com> | 2017-03-10 16:10:25 +0100 |
---|---|---|
committer | Soreine <nicolas@gitbook.com> | 2017-03-10 16:10:25 +0100 |
commit | 53c9a3bc015458e48d4929dfeaeec3964f48dfa9 (patch) | |
tree | afc75a3d1f4d740cc4590c29bea29dad3efd2fdc | |
parent | 8d6e31bf6089d207302fecd5fa71b8d9889413cd (diff) | |
download | gitbook-origin/summary-files-operations.zip gitbook-origin/summary-files-operations.tar.gz gitbook-origin/summary-files-operations.tar.bz2 |
More flexible with dirs pathorigin/summary-files-operations
4 files changed, 31 insertions, 9 deletions
diff --git a/packages/gitbook/src/modifiers/summary/__tests__/deleteByPath.js b/packages/gitbook/src/modifiers/summary/__tests__/deleteByPath.js index 2557210..d1e229f 100644 --- a/packages/gitbook/src/modifiers/summary/__tests__/deleteByPath.js +++ b/packages/gitbook/src/modifiers/summary/__tests__/deleteByPath.js @@ -98,9 +98,8 @@ describe('deleteByPath', () => { ]); }); - it('should handle dirs', () => { - const newSummary = deleteByPath(summary, '2/'); - expectParts(newSummary, [ + describe('should handle dirs', () => { + const expectedParts = [ { articles: [ { @@ -118,7 +117,15 @@ describe('deleteByPath', () => { } ] } - ]); + ]; + + [ + deleteByPath(summary, '2/'), + deleteByPath(summary, '2') + ].map((newSummary, i) => + it('# ' + i, + () => expectParts(newSummary, expectedParts)) + ); }); it('should not delete descendants that don\'t match', () => { diff --git a/packages/gitbook/src/modifiers/summary/__tests__/movePath.js b/packages/gitbook/src/modifiers/summary/__tests__/movePath.js index ac2911c..b233642 100644 --- a/packages/gitbook/src/modifiers/summary/__tests__/movePath.js +++ b/packages/gitbook/src/modifiers/summary/__tests__/movePath.js @@ -112,9 +112,8 @@ describe('movePath', () => { ]); }); - it('should handle dirs', () => { - const newSummary = movePath(summary, '2/', '3/'); - expectParts(newSummary, [ + describe('should handle dirs', () => { + const expectedParts = [ { articles: [ { @@ -146,7 +145,17 @@ describe('movePath', () => { } ] } - ]); + ]; + + [ + movePath(summary, '2/', '3/'), + movePath(summary, '2/', '3'), + movePath(summary, '2', '3'), + movePath(summary, '2', '3/') + ].map((newSummary, i) => + it('# ' + i, + () => expectParts(newSummary, expectedParts)) + ); }); }); diff --git a/packages/gitbook/src/modifiers/summary/movePath.js b/packages/gitbook/src/modifiers/summary/movePath.js index e864212..8dc6f49 100644 --- a/packages/gitbook/src/modifiers/summary/movePath.js +++ b/packages/gitbook/src/modifiers/summary/movePath.js @@ -4,11 +4,16 @@ const PathUtils = require('../../utils/path'); * Update refs of all articles matching the given path, to the new path. * Refs are updated as if you were moving a file or a directory. * @param {Summary} summary - * @param {String} path Can be a file path, or directory path (end with '/') + * @param {String} path Can be a file path, or directory path * @param {String} newPath * @return {Summary} */ function movePath(summary, path, newPath) { + // Normalize dirs path by + // stripping trailing separators for dirs. + path = PathUtils.stripTrailingSep(path); + newPath = PathUtils.stripTrailingSep(newPath); + const parts = summary.getParts() .map((part) => { const articles = moveArticlesPath(part.getArticles(), path, newPath); diff --git a/packages/gitbook/src/utils/path.js b/packages/gitbook/src/utils/path.js index 4c4b70b..d48dc3a 100644 --- a/packages/gitbook/src/utils/path.js +++ b/packages/gitbook/src/utils/path.js @@ -102,5 +102,6 @@ module.exports = { resolveInRoot, normalize: normalizePath, setExtension, + stripTrailingSep, isPureRelative }; |