From 154bb66e90fe32f64a55fbbbdad07b0402991d9e Mon Sep 17 00:00:00 2001
From: SB-venkatyadavilli
<165135356+SB-venkatyadavilli@users.noreply.github.com>
Date: Tue, 3 Sep 2024 20:39:25 +0530
Subject: [PATCH] fix: STOP-955 handling open URL based on domain name (#2697)
merging STOP-955 into main
---
packages/elements-dev-portal/package.json | 2 +-
.../components/NodeContent/NodeContent.tsx | 28 ++++++++++++++-----
2 files changed, 22 insertions(+), 8 deletions(-)
diff --git a/packages/elements-dev-portal/package.json b/packages/elements-dev-portal/package.json
index f6d9ac3fb..7921d0c70 100644
--- a/packages/elements-dev-portal/package.json
+++ b/packages/elements-dev-portal/package.json
@@ -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": [
diff --git a/packages/elements-dev-portal/src/components/NodeContent/NodeContent.tsx b/packages/elements-dev-portal/src/components/NodeContent/NodeContent.tsx
index bb6f24326..293a6447f 100644
--- a/packages/elements-dev-portal/src/components/NodeContent/NodeContent.tsx
+++ b/packages/elements-dev-portal/src/components/NodeContent/NodeContent.tsx
@@ -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 (
-
- {children}
-
- );
+ 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 (
+
+ {children}
+
+ );
+ }
+ return (
+
+ {children}
+
+ );
+ }
+ } catch (error) {
+ console.error(error);
}
if (href && ctx) {