const replaceUrlsWithCdnUrls = (cdnPrefix: string, rawJson: string) => {
    const standardWidgetFieldRegex = /:('|")(\/userfiles.+?)('|")/gi; // "normal" cms fields
    const escapedHtmlRegex = /('|\\")(\/userfiles.+?)('|\\")/gi; // rich content/escaped html
    const unquotedUrlRegex = /url\((\/userfiles.+?)\)/gi; // css urls w/o quotes
    return rawJson
        .replace(standardWidgetFieldRegex, (_, q1, url, q2) => `:${q1}${cdnPrefix}${url}${q2}`.toLowerCase())
        .replace(escapedHtmlRegex, (_, q1, url, q2) => `${q1}${cdnPrefix}${url}${q2}`.toLowerCase())
        .replace(unquotedUrlRegex, (_, url) => `url(${cdnPrefix}${url})`.toLowerCase());
};

export default replaceUrlsWithCdnUrls;
