Fixes WSOD when loading docs with un-parseable URLs that were auto-transformed to links by mkdocs

Signed-off-by: Eric Peterson <ericpeterson@spotify.com>
This commit is contained in:
Eric Peterson
2021-05-24 17:51:53 +02:00
parent 0ed1f45660
commit fd381447a5
2 changed files with 27 additions and 5 deletions
@@ -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(
`<a href="http://${expectedText}">${expectedText}</a>`,
{
preTransformers: [rewriteDocLinks()],
postTransformers: [],
},
);
// There should be no <a> tags, but the link text should remain.
expect(getSample(shadowDom, 'a', 'href')).toEqual([]);
expect(shadowDom.innerHTML).toContain(expectedText);
});
});
describe('normalizeUrl', () => {
@@ -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);
}
}
});
};