forked from owid/owid-grapher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreplaceExplorerRedirects.ts
33 lines (28 loc) · 1.09 KB
/
replaceExplorerRedirects.ts
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
28
29
30
31
32
33
import {
explorerUrlMigrationsById,
migrateExplorerUrl,
} from "../explorer/urlMigrations/ExplorerUrlMigrations.js"
import { getExplorerRedirectForPath } from "../explorerAdminServer/ExplorerRedirects.js"
import { Url } from "../clientUtils/urls/Url.js"
export const replaceIframesWithExplorerRedirectsInWordPressPost = (
cheerio: CheerioStatic
) =>
cheerio("iframe")
.toArray()
.forEach((el) => {
const srcUrl = Url.fromURL(el.attribs["src"].trim())
const resolvedUrl = resolveExplorerRedirect(srcUrl)
if (srcUrl === resolvedUrl) return
el.attribs["src"] = resolvedUrl.fullUrl
})
export const resolveExplorerRedirect = (url: Url): Url => {
if (!url.pathname) return url
let tmpUrl
const explorerRedirect = getExplorerRedirectForPath(url.pathname)
if (explorerRedirect) {
const { migrationId, baseQueryStr } = explorerRedirect
const { migrateUrl } = explorerUrlMigrationsById[migrationId]
tmpUrl = migrateUrl(url, baseQueryStr)
}
return migrateExplorerUrl(tmpUrl ?? url)
}