From ca91265808e60872cdcb3f5088b48f838d313ca6 Mon Sep 17 00:00:00 2001 From: Tobias Bocanegra Date: Fri, 1 Nov 2024 10:50:10 +0100 Subject: [PATCH] fix: also treat self-ref links to index.plain.html correctly --- src/steps/utils.js | 12 ++++++++++-- test/steps/utils.test.js | 3 +++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/steps/utils.js b/src/steps/utils.js index eae612e1..ef7cdde0 100644 --- a/src/steps/utils.js +++ b/src/steps/utils.js @@ -212,10 +212,18 @@ export function rewriteUrl(state, url) { || host === state.previewHost || host === state.liveHost) { if (hash && state.info?.path) { - if (pathname === state.info.path - || (pathname.endsWith('.plain.html') && pathname.substring(0, pathname.length - 11) === state.info.path)) { + if (pathname === state.info.path) { return hash; } + if (pathname.endsWith('.plain.html')) { + let resource = pathname.substring(0, pathname.length - 11); + if (resource.endsWith('/index')) { + resource = resource.substring(0, resource.length - 5); + } + if (resource === state.info.path) { + return hash; + } + } } return `${pathname}${search}${hash}`; } diff --git a/test/steps/utils.test.js b/test/steps/utils.test.js index a4c1b67c..fad43804 100644 --- a/test/steps/utils.test.js +++ b/test/steps/utils.test.js @@ -187,6 +187,9 @@ describe('Rewrite URLs test', () => { assert.strictEqual(rewriteUrl({ info: { path: '/blog/article' }, }, 'https://main--pages--adobe.hlx3.page/blog/article.plain.html#heading'), '#heading'); + assert.strictEqual(rewriteUrl({ + info: { path: '/blog/' }, + }, 'https://main--pages--adobe.hlx3.page/blog/index.plain.html#heading'), '#heading'); }); it('does not replace prod url', () => {