summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--packages/gitbook/src/modifiers/summary/__tests__/deleteByPath.js15
-rw-r--r--packages/gitbook/src/modifiers/summary/__tests__/movePath.js17
-rw-r--r--packages/gitbook/src/modifiers/summary/movePath.js7
-rw-r--r--packages/gitbook/src/utils/path.js1
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
};