All files / owid-grapher/baker replaceExplorerRedirects.ts

39.13% Statements 9/23
100% Branches 0/0
0% Functions 0/1
39.13% Lines 9/23

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 281x 1x 1x 1x 1x 1x 1x 1x 1x                                      
import {
    explorerUrlMigrationsById,
    migrateExplorerUrl,
} from "../explorer/urlMigrations/ExplorerUrlMigrations"
import { getExplorerRedirectForPath } from "../explorerAdminServer/ExplorerRedirects"
import { Url } from "../clientUtils/urls/Url"
 
export const replaceIframesWithExplorerRedirectsInWordPressPost = (
    cheerio: CheerioStatic
) =>
    cheerio("iframe")
        .toArray()
        .forEach((el) => {
            let url = Url.fromURL(el.attribs["src"].trim())
            if (!url.pathname) return

            const explorerRedirect = getExplorerRedirectForPath(url.pathname)
            if (explorerRedirect) {
                const { migrationId, baseQueryStr } = explorerRedirect
                const { migrateUrl } = explorerUrlMigrationsById[migrationId]
                url = migrateUrl(url, baseQueryStr)
            }

            url = migrateExplorerUrl(url)
 
            el.attribs["src"] = url.fullUrl
        })