Skip to content

Commit

Permalink
fix: STOP-955 handling open URL based on domain name (#2697)
Browse files Browse the repository at this point in the history
merging STOP-955 into main
  • Loading branch information
SB-venkatyadavilli authored Sep 3, 2024
1 parent 9b29936 commit 154bb66
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
2 changes: 1 addition & 1 deletion packages/elements-dev-portal/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@stoplight/elements-dev-portal",
"version": "2.4.2",
"version": "2.4.3",
"description": "UI components for composing beautiful developer documentation.",
"keywords": [],
"sideEffects": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,13 +130,27 @@ const externalRegex = new RegExp('^(?:[a-z]+:)?//', 'i');
const LinkComponent: CustomComponentMapping['a'] = ({ children, href, title }) => {
const ctx = React.useContext(NodeLinkContext);

if (href && externalRegex.test(href)) {
// Open external URL in a new tab
return (
<a href={href} target="_blank" rel="noreferrer" title={title ? title : undefined}>
{children}
</a>
);
try {
if (href && externalRegex.test(href)) {
const baseURL = window.location.host;
const hrefURL = new URL(href).host;

if (baseURL === hrefURL) {
// Open URL in same tab if domain match
return (
<a href={href} title={title ? title : undefined}>
{children}
</a>
);
}
return (
<a href={href} target="_blank" rel="noreferrer" title={title ? title : undefined}>
{children}
</a>
);
}
} catch (error) {
console.error(error);
}

if (href && ctx) {
Expand Down

0 comments on commit 154bb66

Please sign in to comment.