diff --git a/test/spec/util.spec.js b/test/spec/util.spec.js index 4b2c842..67d6365 100644 --- a/test/spec/util.spec.js +++ b/test/spec/util.spec.js @@ -150,20 +150,32 @@ test('utils.buildUrlObj', t => { test('utils.buildLocalPath', t => { let localPath; - localPath = utils.buildLocalPath('/path/to/file', '', '/path/to/inputDir', '/path/to/outputDir'); + localPath = utils.buildLocalPath('/path/to/file?foo=bar#hash', '', '/path/to/inputDir', '/path/to/outputDir'); t.is(localPath, '/path/to/outputDir/path/to/file'); localPath = utils.buildLocalPath('http://example.com/path/to/file?foo=bar#hash', '', '/path/to/inputDir', '/path/to/outputDir'); t.is(localPath, '/path/to/outputDir/example.com/path/to/file'); - localPath = utils.buildLocalPath('file:///path/to/inputDir/subdir/file', '', '/path/to/inputDir', '/path/to/outputDir'); + localPath = utils.buildLocalPath('file:///path/to/inputDir/subdir/file?foo=bar#hash', '', '/path/to/inputDir', '/path/to/outputDir'); t.is(localPath, '/path/to/outputDir/subdir/file'); localPath = utils.buildLocalPath('http://example2.com/path/to/file?foo=bar#hash', 'http://example.com/path/to/file', '/path/to/inputDir', '/path/to/outputDir'); t.is(localPath, '/path/to/outputDir/example2.com/path/to/file'); - localPath = utils.buildLocalPath('file:///path/to/inputDir/subdir2/file', 'file:///path/to/inputDir/subdir/file', '/path/to/inputDir', '/path/to/outputDir'); + localPath = utils.buildLocalPath('file:///path/to/inputDir/subdir2/file?foo=bar#hash', 'file:///path/to/inputDir/subdir/file', '/path/to/inputDir', '/path/to/outputDir'); t.is(localPath, '/path/to/outputDir/subdir2/file'); localPath = utils.buildLocalPath('../../path2/to/file?foo=bar#hash', 'http://example.com/path/to/file', '/path/to/inputDir', '/path/to/outputDir'); t.is(localPath, '/path/to/outputDir/example.com/path2/to/file'); - localPath = utils.buildLocalPath('../subdir2/file', 'file:///path/to/inputDir/subdir/file', '/path/to/inputDir', '/path/to/outputDir'); + localPath = utils.buildLocalPath('../subdir2/file?foo=bar#hash', 'file:///path/to/inputDir/subdir/file', '/path/to/inputDir', '/path/to/outputDir'); t.is(localPath, '/path/to/outputDir/subdir2/file'); - localPath = utils.buildLocalPath('../subdir2/file', '/path/to/inputDir/subdir/file', '/path/to/inputDir', '/path/to/outputDir'); + localPath = utils.buildLocalPath('../subdir2/file?foo=bar#hash', '/path/to/inputDir/subdir/file', '/path/to/inputDir', '/path/to/outputDir'); t.is(localPath, '/path/to/outputDir/subdir2/file'); }); + +test('utils.removeQueryString', t => { + let result; + result = utils.removeQueryString('/path/to/file?foo=bar'); + t.is(result, '/path/to/file'); + result = utils.removeQueryString('/path/to/file?foo=bar&abc=def'); + t.is(result, '/path/to/file'); + result = utils.removeQueryString('/path/to/file#hash'); + t.is(result, '/path/to/file'); + result = utils.removeQueryString('/path/to/file?foo=bar&abc=def#hash'); + t.is(result, '/path/to/file'); +}); diff --git a/utils.js b/utils.js index 58fa4fc..9fd95da 100644 --- a/utils.js +++ b/utils.js @@ -107,6 +107,11 @@ function buildAbsolutePath(relativePath, basePath, inputDir, outputDir) { return path.join(outputDir, fullPath.startsWith(inputDir) ? path.relative(inputDir, fullPath) : fullPath); } +function removeQueryString(absolutePath) { + const obj = buildUrlObj(`file://${absolutePath}`); + return obj.pathname; +} + function buildLocalPath(uri, parentUri, inputDir, outputDir) { print(`buildLocalPath: uri=${uri}, parentUri=${parentUri}, inputDir=${inputDir}, outputDir=${outputDir}`); let localPath; @@ -114,7 +119,7 @@ function buildLocalPath(uri, parentUri, inputDir, outputDir) { if (path.isAbsolute(uri)) { localPath = path.join(outputDir, uri); print(`\tFrom absolute path to localPath: ${localPath}`); - return localPath; + return removeQueryString(localPath); } obj = buildUrlObj(uri); if (obj) { @@ -131,7 +136,7 @@ function buildLocalPath(uri, parentUri, inputDir, outputDir) { } localPath = buildAbsolutePath(uri, parentUri, inputDir, outputDir); print(`\tFrom relative path to localPath: ${localPath}`); - return localPath; + return removeQueryString(localPath); } module.exports = { @@ -144,5 +149,6 @@ module.exports = { getDateTimeString, mkdirP, buildUrlObj, + removeQueryString, buildLocalPath };