|
@@ -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]);
|