From 35e09160402bc5484359687562311f9eb02626af Mon Sep 17 00:00:00 2001 From: Oliver Sand Date: Mon, 10 May 2021 17:45:19 +0200 Subject: [PATCH] Handle URLs with a `#hash` correctly when rewriting link URLs Signed-off-by: Oliver Sand --- .changeset/techdocs-lemon-cooks-drum.md | 5 +++++ .../reader/transformers/rewriteDocLinks.test.ts | 15 +++++++++++++++ .../src/reader/transformers/rewriteDocLinks.ts | 15 ++++++++++++--- 3 files changed, 32 insertions(+), 3 deletions(-) create mode 100644 .changeset/techdocs-lemon-cooks-drum.md diff --git a/.changeset/techdocs-lemon-cooks-drum.md b/.changeset/techdocs-lemon-cooks-drum.md new file mode 100644 index 0000000000..d8f2954c2a --- /dev/null +++ b/.changeset/techdocs-lemon-cooks-drum.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-techdocs': patch +--- + +Handle URLs with a `#hash` correctly when rewriting link URLs. diff --git a/plugins/techdocs/src/reader/transformers/rewriteDocLinks.test.ts b/plugins/techdocs/src/reader/transformers/rewriteDocLinks.test.ts index 06da8a447c..ca85c64fbe 100644 --- a/plugins/techdocs/src/reader/transformers/rewriteDocLinks.test.ts +++ b/plugins/techdocs/src/reader/transformers/rewriteDocLinks.test.ts @@ -16,6 +16,7 @@ import { createTestShadowDom, getSample } from '../../test-utils'; import { rewriteDocLinks } from '../transformers'; +import { normalizeUrl } from './rewriteDocLinks'; describe('rewriteDocLinks', () => { it('should not do anything', () => { @@ -56,3 +57,17 @@ describe('rewriteDocLinks', () => { ]); }); }); + +describe('normalizeUrl', () => { + it.each([ + ['http://example.org', 'http://example.org/'], + ['http://example.org/', 'http://example.org/'], + ['http://example.org/folder', 'http://example.org/folder/'], + ['http://example.org/folder/', 'http://example.org/folder/'], + ['http://example.org/folder#intro', 'http://example.org/folder/#intro'], + ['http://example.org/folder/#intro', 'http://example.org/folder/#intro'], + ['http://example.org/folder#', 'http://example.org/folder/#'], + ])('should handle %s', (url, expected) => { + expect(normalizeUrl(url)).toEqual(expected); + }); +}); diff --git a/plugins/techdocs/src/reader/transformers/rewriteDocLinks.ts b/plugins/techdocs/src/reader/transformers/rewriteDocLinks.ts index 7b0c23c3e3..54e5537347 100644 --- a/plugins/techdocs/src/reader/transformers/rewriteDocLinks.ts +++ b/plugins/techdocs/src/reader/transformers/rewriteDocLinks.ts @@ -31,9 +31,7 @@ export const rewriteDocLinks = (): Transformer => { if (elemAttribute.match(/^https?:\/\//i)) { elem.setAttribute('target', '_blank'); } - const normalizedWindowLocation = window.location.href.endsWith('/') - ? window.location.href - : `${window.location.href}/`; + const normalizedWindowLocation = normalizeUrl(window.location.href); elem.setAttribute( attributeName, @@ -48,3 +46,14 @@ export const rewriteDocLinks = (): Transformer => { return dom; }; }; + +/** Make sure that the input url always ends with a '/' */ +export function normalizeUrl(input: string): string { + const url = new URL(input); + + if (!url.pathname.endsWith('/')) { + url.pathname += '/'; + } + + return url.toString(); +}