Browse Source

description links fix

Viktoriia 1 month ago
parent
commit
276bf23826
1 changed files with 28 additions and 6 deletions
  1. 28 6
      src/screens/InAppScreens/TravelsScreen/EventScreen/index.tsx

+ 28 - 6
src/screens/InAppScreens/TravelsScreen/EventScreen/index.tsx

@@ -1280,14 +1280,36 @@ const WebDisplay = React.memo(function WebDisplay({ html }: { html: string }) {
   const token = storage.get('token', StoreType.STRING) as string;
   const processedHtml = React.useMemo(() => {
     let updatedHtml = html;
+    const hrefRegex = /href="((?!http)[^"]+)"/g;
+    const imgSrcRegex = /src="((?:\.{0,2}\/)*img\/[^"]*)"/g;
 
-    updatedHtml = updatedHtml.replace(/src="(?:\/(?:\.\.\/)*|\/)img\//g, `src="${API_HOST}/img/`);
+    const normalizePath = (path: string): string => {
+      const segments = path.split('/').filter(Boolean);
+      const resolved: string[] = [];
 
-    updatedHtml = updatedHtml.replace(/href="(?!http)([^"]*)"/g, (match, path) => {
-      const separator = path.includes('?') ? '&' : '?';
-      const fullUrl = `${API_HOST}${path}${separator}token=${encodeURIComponent(token)}`;
-      return `href="${fullUrl}"`;
-    });
+      for (const segment of segments) {
+        if (segment === '..') resolved.pop();
+        else if (segment !== '.') resolved.push(segment);
+      }
+
+      return '/' + resolved.join('/');
+    };
+
+    updatedHtml = updatedHtml
+      .replace(hrefRegex, (match, rawPath) => {
+        const normalizedPath = normalizePath(rawPath);
+        const fullUrl = `${API_HOST}${normalizedPath}`;
+        if (normalizedPath.includes('shop')) {
+          const separator = fullUrl.includes('?') ? '&' : '?';
+          return `href="${fullUrl}${separator}token=${encodeURIComponent(token)}"`;
+        }
+        return `href="${fullUrl}"`;
+      })
+
+      .replace(imgSrcRegex, (_match, rawSrc) => {
+        const normalizedImg = normalizePath(rawSrc);
+        return `src="${API_HOST}${normalizedImg}"`;
+      });
 
     return updatedHtml;
   }, [html, token]);