From 7619203cb3282b89082387e87a868fa56abbfcd1 Mon Sep 17 00:00:00 2001 From: Ryan Collins Date: Thu, 12 Sep 2024 11:27:21 +1200 Subject: [PATCH 1/2] Detect source path even when flanked by slashes --- src/util/paths.js | 6 +++++- tests/util/items.test.js | 9 +++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/util/paths.js b/src/util/paths.js index 09fe545..b222ec9 100644 --- a/src/util/paths.js +++ b/src/util/paths.js @@ -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; @@ -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 = { diff --git a/tests/util/items.test.js b/tests/util/items.test.js index dfd62d4..59e2e59 100644 --- a/tests/util/items.test.js +++ b/tests/util/items.test.js @@ -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); }); From 54733665fd27597c4aea91f81af14ac041cb33b3 Mon Sep 17 00:00:00 2001 From: Ryan Collins Date: Thu, 12 Sep 2024 11:58:55 +1200 Subject: [PATCH 2/2] Handle multiple slashes --- src/util/paths.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/util/paths.js b/src/util/paths.js index b222ec9..11ca39e 100644 --- a/src/util/paths.js +++ b/src/util/paths.js @@ -6,7 +6,7 @@ function normalisePath(path) { } function removeLeadingAndTrailingSlashes(path) { - return path.replace(/^\/|\/$/g, ''); + return path.replace(/^\/+|\/+$/g, ''); } function stripTopPath(path, topPath) {