diff --git a/plugins/techdocs/src/reader/transformers/rewriteDocLinks.test.ts b/plugins/techdocs/src/reader/transformers/rewriteDocLinks.test.ts index ca85c64fbe..01467f665d 100644 --- a/plugins/techdocs/src/reader/transformers/rewriteDocLinks.test.ts +++ b/plugins/techdocs/src/reader/transformers/rewriteDocLinks.test.ts @@ -56,6 +56,21 @@ describe('rewriteDocLinks', () => { 'http://localhost/example-docs/example-page', ]); }); + + it('should rewrite non-parseable URLs as text', () => { + const expectedText = `www.my-internet.[top-level-domain]/pathname/[URLkey]`; + const shadowDom = createTestShadowDom( + `${expectedText}`, + { + preTransformers: [rewriteDocLinks()], + postTransformers: [], + }, + ); + + // There should be no tags, but the link text should remain. + expect(getSample(shadowDom, 'a', 'href')).toEqual([]); + expect(shadowDom.innerHTML).toContain(expectedText); + }); }); describe('normalizeUrl', () => { diff --git a/plugins/techdocs/src/reader/transformers/rewriteDocLinks.ts b/plugins/techdocs/src/reader/transformers/rewriteDocLinks.ts index 5c4a730963..8c4bc29201 100644 --- a/plugins/techdocs/src/reader/transformers/rewriteDocLinks.ts +++ b/plugins/techdocs/src/reader/transformers/rewriteDocLinks.ts @@ -31,12 +31,19 @@ export const rewriteDocLinks = (): Transformer => { if (elemAttribute.match(/^https?:\/\//i)) { elem.setAttribute('target', '_blank'); } - const normalizedWindowLocation = normalizeUrl(window.location.href); - elem.setAttribute( - attributeName, - new URL(elemAttribute, normalizedWindowLocation).toString(), - ); + try { + const normalizedWindowLocation = normalizeUrl( + window.location.href, + ); + elem.setAttribute( + attributeName, + new URL(elemAttribute, normalizedWindowLocation).toString(), + ); + } catch (_e) { + // Non-parseable links should be re-written as plain text. + elem.replaceWith(elem.textContent || elemAttribute); + } } }); };