Skip to content

Commit

Permalink
Merge pull request #4 from CloudCannon/handle-slashes-in-src
Browse files Browse the repository at this point in the history
Detect source path even when flanked by slashes
  • Loading branch information
rphillips-cc authored Sep 12, 2024
2 parents 85191a9 + 5473366 commit 336addc
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/util/paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ function normalisePath(path) {
.replace(/^\.\//g, '');
}

function removeLeadingAndTrailingSlashes(path) {
return path.replace(/^\/+|\/+$/g, '');
}

function stripTopPath(path, topPath) {
const normalisedTop = normalisePath(topPath);
return path.startsWith(normalisedTop) ? path.substring(normalisedTop.length) : path;
Expand All @@ -15,7 +19,7 @@ function isTopPath(basePath, index, basePaths) {
}

function getSourcePath(inputPath, source) {
return stripTopPath(normalisePath(inputPath), source).replace(/^\/+/, '');
return stripTopPath(normalisePath(inputPath), removeLeadingAndTrailingSlashes(source)).replace(/^\/+/, '');
}

module.exports = {
Expand Down
9 changes: 9 additions & 0 deletions tests/util/items.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,15 @@ test('processes item in custom source', async (t) => {
t.deepEqual(await processItem(customPage, 'pages', 'src'), processedPage);
});

test('processes item with custom source formatted differently', async (t) => {
const customPage = {
...page,
inputPath: './src/page.html'
};

t.deepEqual(await processItem(customPage, 'pages', '/src'), processedPage);
});

test('processes invalid item', async (t) => {
t.is(await processItem({}, 'pages', '.'), undefined);
});
Expand Down

0 comments on commit 336addc

Please sign in to comment.