llama1's picture
Upload 781 files
5da4770 verified
/**
* Constructs a preview URL for HTML files in the sandbox environment.
* Properly handles URL encoding of file paths by encoding each path segment individually.
*
* @param sandboxUrl - The base URL of the sandbox
* @param filePath - The path to the HTML file (can include /workspace/ prefix)
* @returns The properly encoded preview URL, or undefined if inputs are invalid
*/
export function constructHtmlPreviewUrl(
sandboxUrl: string | undefined,
filePath: string | undefined,
): string | undefined {
if (!sandboxUrl || !filePath) {
return undefined;
}
// Remove /workspace/ prefix if present
const processedPath = filePath.replace(/^\/workspace\//, '');
// Split the path into segments and encode each segment individually
const pathSegments = processedPath
.split('/')
.map((segment) => encodeURIComponent(segment));
// Join the segments back together with forward slashes
const encodedPath = pathSegments.join('/');
return `${sandboxUrl}/${encodedPath}`;
}