fix unclickable external links (#2396)

This commit is contained in:
Emma Indal
2020-09-10 12:05:22 +02:00
committed by GitHub
parent fef570b064
commit 296b6376e3
2 changed files with 8 additions and 2 deletions
@@ -26,9 +26,11 @@ export const addLinkClickListener = ({
return dom => {
Array.from(dom.getElementsByTagName('a')).forEach(elem => {
elem.addEventListener('click', (e: MouseEvent) => {
e.preventDefault();
const target = e.target as HTMLAnchorElement;
if (target?.getAttribute('href')) {
const href = target?.getAttribute('href');
if (!href) return;
if (!href.match(/^https?:\/\//i)) {
e.preventDefault();
onClick(e, target.getAttribute('href')!);
}
});
@@ -27,6 +27,10 @@ export const rewriteDocLinks = (): Transformer => {
.forEach((elem: T) => {
const elemAttribute = elem.getAttribute(attributeName);
if (elemAttribute) {
// if link is external, add target to open in a new window or tab
if (elemAttribute.match(/^https?:\/\//i)) {
elem.setAttribute('target', '_blank');
}
const normalizedWindowLocation = window.location.href.endsWith('/')
? window.location.href
: `${window.location.href}/`;