fix unclickable external links (#2396)
This commit is contained in:
@@ -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}/`;
|
||||
|
||||
Reference in New Issue
Block a user