Skip to content

Commit a637414

Browse files
ginchauspeCopilot
andauthored
Potential fix for pull request finding
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
1 parent 79218de commit a637414

1 file changed

Lines changed: 16 additions & 1 deletion

File tree

wp-react-lib/src/util/index.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,21 @@ const WP_ROOT = process.env.VITE_REACT_APP_WP || '/wp';
44
// Escapes special regex metacharacters in a literal string.
55
const escapeRegex = (s) => s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
66

7+
// Normalizes WP_ROOT to have exactly one leading "/" and no trailing "/".
8+
// Examples:
9+
// "wp" -> "/wp"
10+
// "/wp/" -> "/wp"
11+
// "//wp//"-> "/wp"
12+
// "/" -> "" (no subdirectory)
13+
const normalizeWpRoot = (root) => {
14+
if (!root) return '';
15+
let r = root.trim();
16+
// Remove all leading and trailing slashes
17+
r = r.replace(/^\/+/, '').replace(/\/+$/, '');
18+
if (!r) return '';
19+
return '/' + r;
20+
};
21+
722
// Builds a regex that matches the scheme + configured WP hostname(s) + optional WP
823
// subdirectory path (e.g. /wp), so the entire origin prefix can be replaced with the
924
// locale slug in one step.
@@ -13,7 +28,7 @@ const buildUrlRegex = () => {
1328
?.split(",").map(h => h.trim()).filter(Boolean) || [];
1429
if (!hosts.length) return null;
1530
const hostsPattern = hosts.map(escapeRegex).join('|');
16-
const wpRootPattern = escapeRegex(WP_ROOT);
31+
const wpRootPattern = escapeRegex(normalizeWpRoot(WP_ROOT));
1732
return new RegExp(`^(http|https)://(${hostsPattern})(?:${wpRootPattern}(?=/|$))?`, 'ig');
1833
};
1934

0 commit comments

Comments
 (0)