Skip to content

Commit

Permalink
Merge pull request #1216 from webkom/internal-links-no-new-tab
Browse files Browse the repository at this point in the history
Only open links in new tab if they're targeting another host
  • Loading branch information
norbye authored Aug 25, 2024
2 parents 12863b9 + be355ad commit 354c8e9
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@webkom/lego-editor",
"version": "2.3.2",
"version": "2.4.0",
"description": "A React editor written in TS with slate.js for lego-webapp",
"type": "module",
"main": "./dist/lego-editor.umd.cjs",
Expand Down
5 changes: 3 additions & 2 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { withHistory } from 'slate-history';
import isHotKey from 'is-hotkey';
import Toolbar from './components/Toolbar';
import ImageBlock from './components/ImageBlock';
import isInternalLink from './utils/isInternalLink';
import {
basePlugin,
insertTab,
Expand Down Expand Up @@ -179,8 +180,8 @@ const renderElement = (props: RenderElementProps): JSX.Element => {
<a
{...attributes}
href={element.url}
rel="noopener noreferrer"
target="_blank"
rel={isInternalLink(element.url) ? undefined : 'noopener noreferrer'}
target={isInternalLink(element.url) ? undefined : '_blank'}
>
{children}
</a>
Expand Down
3 changes: 2 additions & 1 deletion src/serializer.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Node as SlateNode, Text, Element } from 'slate';
import escape from 'escape-html';
import isInternalLink from './utils/isInternalLink';
import { jsx } from 'slate-hyperscript';
import { Mark } from './custom-types';

Expand Down Expand Up @@ -114,7 +115,7 @@ export const serialize = (node: SlateNode): string => {
case 'image_caption':
return `<figcaption>${children}</figcaption>`;
case 'link':
return `<a target="_blank" href="${node.url}">${children}</a>`;
return `<a ${isInternalLink(node.url) ? '' : 'target="_blank" '}href="${node.url}">${children}</a>`;
case 'quote':
return `<blockquote>${children}</blockquote>`;
default:
Expand Down
19 changes: 19 additions & 0 deletions src/utils/isInternalLink.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* Check if a link url is an internal link or an external one by comparing the host with the current host.
* [protocol]//[hostname]:[port]/[pathname] - [host] = [hostname]:[port]
*
* @param linkUrlString The URL of the link
* @returns boolean
*/
const isInternalLink = (linkUrlString: string): boolean => {
try {
const linkUrl = new URL(linkUrlString);
const currentUrl = new URL(window.location.href);
return linkUrl.host === currentUrl.host;
} catch (e) {
// Handle invalid URLs as if they are not internal
return false;
}
};

export default isInternalLink;

0 comments on commit 354c8e9

Please sign in to comment.