diff --git a/plugins/techdocs/src/reader/components/Reader.tsx b/plugins/techdocs/src/reader/components/Reader.tsx index 550b8df923..01299b8fe6 100644 --- a/plugins/techdocs/src/reader/components/Reader.tsx +++ b/plugins/techdocs/src/reader/components/Reader.tsx @@ -116,6 +116,7 @@ export const Reader = ({ entityId, onReady }: Props) => { return dom; }, addLinkClickListener({ + baseUrl: window.location.origin, onClick: (_: MouseEvent, url: string) => { const parsedUrl = new URL(url); navigate(`${parsedUrl.pathname}${parsedUrl.hash}`); diff --git a/plugins/techdocs/src/reader/transformers/addLinkClickListener.test.ts b/plugins/techdocs/src/reader/transformers/addLinkClickListener.test.ts index 8e942b8be1..775780fe4d 100644 --- a/plugins/techdocs/src/reader/transformers/addLinkClickListener.test.ts +++ b/plugins/techdocs/src/reader/transformers/addLinkClickListener.test.ts @@ -14,23 +14,61 @@ * limitations under the License. */ -import { createTestShadowDom, FIXTURES } from '../../test-utils'; +import { createTestShadowDom } from '../../test-utils'; import { addLinkClickListener } from '.'; describe('addLinkClickListener', () => { it('calls onClick when a link has been clicked', () => { const fn = jest.fn(); - const shadowDom = createTestShadowDom(FIXTURES.FIXTURE_STANDARD_PAGE, { - preTransformers: [], - postTransformers: [ - addLinkClickListener({ - onClick: fn, - }), - ], - }); + const shadowDom = createTestShadowDom( + ` + + + + Link + + + `, + { + preTransformers: [], + postTransformers: [ + addLinkClickListener({ + baseUrl: 'http://localhost:3000', + onClick: fn, + }), + ], + }, + ); shadowDom.querySelector('a')?.click(); expect(fn).toHaveBeenCalledTimes(1); }); + + it('does not call onClick when a link links to another baseUrl', () => { + const fn = jest.fn(); + const shadowDom = createTestShadowDom( + ` + + + + Link + + + `, + { + preTransformers: [], + postTransformers: [ + addLinkClickListener({ + baseUrl: 'http://localhost:3000', + onClick: fn, + }), + ], + }, + ); + + shadowDom.querySelector('a')?.click(); + + expect(fn).toHaveBeenCalledTimes(0); + }); }); diff --git a/plugins/techdocs/src/reader/transformers/addLinkClickListener.ts b/plugins/techdocs/src/reader/transformers/addLinkClickListener.ts index 3566b403a2..b6a6509550 100644 --- a/plugins/techdocs/src/reader/transformers/addLinkClickListener.ts +++ b/plugins/techdocs/src/reader/transformers/addLinkClickListener.ts @@ -17,10 +17,12 @@ import type { Transformer } from './index'; type AddLinkClickListenerOptions = { + baseUrl: string; onClick: (e: MouseEvent, newUrl: string) => void; }; export const addLinkClickListener = ({ + baseUrl, onClick, }: AddLinkClickListenerOptions): Transformer => { return dom => { @@ -28,8 +30,9 @@ export const addLinkClickListener = ({ elem.addEventListener('click', (e: MouseEvent) => { const target = e.target as HTMLAnchorElement; const href = target?.getAttribute('href'); + if (!href) return; - if (!href.match(/^https?:\/\//i)) { + if (href.startsWith(baseUrl)) { e.preventDefault(); onClick(e, target.getAttribute('href')!); }