All files / owid-grapher/baker formatWordpressPost.tsx

17.02% Statements 81/476
100% Branches 1/1
20% Functions 1/5
17.02% Lines 81/476

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 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 6101x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x           2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                   1x 1x                                                   1x 1x                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
import * as cheerio from "cheerio"
import urlSlug from "url-slug"
import * as lodash from "lodash"
import * as React from "react"
import * as ReactDOMServer from "react-dom/server"
import { HTTPS_ONLY } from "../settings/serverSettings"
import { getTables } from "../db/wpdb"
import Tablepress from "../site/Tablepress"
import { GrapherExports } from "../baker/GrapherBakingUtils"
import * as path from "path"
import { renderBlocks } from "../site/blocks"
import { RelatedCharts } from "../site/blocks/RelatedCharts"
import {
    DataValueProps,
    FormattedPost,
    FormattingOptions,
    FullPost,
    JsonError,
    TocHeading,
} from "../clientUtils/owidTypes"
import { Footnote } from "../site/Footnote"
import { LoadingIndicator } from "../grapher/loadingIndicator/LoadingIndicator"
import { PROMINENT_LINK_CLASSNAME } from "../site/blocks/ProminentLink"
import { countryProfileSpecs } from "../site/countryProfileProjects"
import { DataToken } from "../site/DataToken"
import {
    dataValueRegex,
    DEEP_LINK_CLASS,
    extractDataValuesConfiguration,
    formatDataValue,
    formatLinks,
    parseKeyValueArgs,
} from "./formatting"
import { mathjax } from "mathjax-full/js/mathjax"
import { TeX } from "mathjax-full/js/input/tex"
import { SVG } from "mathjax-full/js/output/svg"
import { liteAdaptor } from "mathjax-full/js/adaptors/liteAdaptor"
import { RegisterHTMLHandler } from "mathjax-full/js/handlers/html"
import { AllPackages } from "mathjax-full/js/input/tex/AllPackages"
import { replaceIframesWithExplorerRedirectsInWordPressPost } from "./replaceExplorerRedirects"
import { EXPLORERS_ROUTE_FOLDER } from "../explorer/ExplorerConstants"
import {
    getDataValue,
    getOwidChartDimensionConfigForVariable,
    getOwidVariableDisplayConfig,
} from "../db/model/Variable"
import { AnnotatingDataValue } from "../site/AnnotatingDataValue"
import { renderAutomaticProminentLinks } from "./siteRenderers"
import {
    getBodyHtml,
    GRAPHER_PREVIEW_CLASS,
    SUMMARY_CLASSNAME,
} from "../site/formatting"
 
const initMathJax = () => {
    const adaptor = liteAdaptor()
    RegisterHTMLHandler(adaptor)
 
    const tex = new TeX({ packages: AllPackages })
    const svg = new SVG({ fontCache: "none" })
    const doc = mathjax.document("", {
        InputJax: tex,
        OutputJax: svg,
    })
 
    return function format(latex: string): string {
        const node = doc.convert(latex, {
            display: true,
        })
        return adaptor.outerHTML(node) // as HTML
    }
}
 
const formatMathJax = initMathJax()
 
// A modifed FontAwesome icon
const INTERACTIVE_ICON_SVG = `<svg aria-hidden="true" focusable="false" data-prefix="fas" data-icon="hand-pointer" class="svg-inline--fa fa-hand-pointer fa-w-14" role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 448 617">
    <path fill="currentColor" d="M448,344.59v96a40.36,40.36,0,0,1-1.06,9.16l-32,136A40,40,0,0,1,376,616.59H168a40,40,0,0,1-32.35-16.47l-128-176a40,40,0,0,1,64.7-47.06L104,420.58v-276a40,40,0,0,1,80,0v200h8v-40a40,40,0,1,1,80,0v40h8v-24a40,40,0,1,1,80,0v24h8a40,40,0,1,1,80,0Zm-256,80h-8v96h8Zm88,0h-8v96h8Zm88,0h-8v96h8Z" transform="translate(0 -0.41)"/>
    <path fill="currentColor" opacity="0.6" d="M239.76,234.78A27.5,27.5,0,0,1,217,192a87.76,87.76,0,1,0-145.9,0A27.5,27.5,0,1,1,25.37,222.6,142.17,142.17,0,0,1,1.24,143.17C1.24,64.45,65.28.41,144,.41s142.76,64,142.76,142.76a142.17,142.17,0,0,1-24.13,79.43A27.47,27.47,0,0,1,239.76,234.78Z" transform="translate(0 -0.41)"/>
</svg>`
 
const extractLatex = (html: string): [string, string[]] => {
    const latexBlocks: string[] = []
    html = html.replace(/\[latex\]([\s\S]*?)\[\/latex\]/gm, (_, latex) => {
        latexBlocks.push(
            latex.replace("\\[", "").replace("\\]", "").replace(/\$\$/g, "")
        )
        return "[latex]"
    })
    return [html, latexBlocks]
}
 
const formatLatex = async (
    html: string,
    latexBlocks?: string[]
): Promise<string> => {
    if (!latexBlocks) [html, latexBlocks] = extractLatex(html)

    // return early so we don't do unnecessary work for sites without latex
    if (!latexBlocks.length) return html

    const compiled: string[] = []
    for (const latex of latexBlocks) {
        try {
            compiled.push(formatMathJax(latex))
        } catch (err) {
            compiled.push(
                `${latex} (Could not format equation due to MathJax error)`
            )
        }
    }

    let i = -1
    return html.replace(/\[latex\]/g, () => {
        i += 1
        return compiled[i]
    })
}
 
export const formatWordpressPost = async (
    post: FullPost,
    html: string,
    formattingOptions: FormattingOptions,
    grapherExports?: GrapherExports
): Promise<FormattedPost> => {
    // Strip comments
    html = html.replace(/<!--[^>]+-->/g, "")

    // Need to skirt around wordpress formatting to get proper latex rendering
    let latexBlocks
    ;[html, latexBlocks] = extractLatex(html)

    const references: any[] = []
    html = html.replace(/\[cite\]([\s\S]*?)\[\/cite\]/gm, () => {
        references.push({}) // Todo
        return ``
    })

    html = await formatLatex(html, latexBlocks)

    // Footnotes
    const footnotes: string[] = []
    html = html.replace(/{ref}([\s\S]*?){\/ref}/gm, (_, footnote) => {
        if (formattingOptions.footnotes) {
            footnotes.push(footnote)
            const i = footnotes.length
            const href = `#note-${i}`

            return ReactDOMServer.renderToStaticMarkup(
                <a id={`ref-${i}`} className="ref" href={href}>
                    <Footnote index={i} />
                </a>
            )
        } else {
            return ""
        }
    })

    const dataValuesConfigurationsMap = await extractDataValuesConfiguration(
        html
    )
    const dataValues = new Map<string, DataValueProps>()
    for (const [
        dataValueConfigurationString,
        dataValueConfiguration,
    ] of dataValuesConfigurationsMap) {
        const { queryArgs, template } = dataValueConfiguration
        const { variableId, chartId } = queryArgs
        const { value, year, unit, entityName } =
            (await getDataValue(queryArgs)) || {}

        if (!value || !year || !entityName)
            throw new JsonError(
                `Missing data for query ${dataValueConfigurationString}. Please check the tag for any missing or wrong argument.`
            )
        if (!template)
            throw new JsonError(
                `Missing template for query ${dataValueConfigurationString}`
            )

        let formattedValue
        if (variableId && chartId) {
            const legacyVariableDisplayConfig =
                await getOwidVariableDisplayConfig(variableId)
            const legacyChartDimension =
                await getOwidChartDimensionConfigForVariable(
                    variableId,
                    chartId
                )
            formattedValue = formatDataValue(
                value,
                variableId,
                legacyVariableDisplayConfig,
                legacyChartDimension
            )
        }

        dataValues.set(dataValueConfigurationString, {
            value,
            formattedValue,
            template,
            year,
            unit,
            entityName,
        })
    }

    html = html.replace(dataValueRegex, (_, dataValueConfigurationString) => {
        const dataValueProps: DataValueProps | undefined = dataValues.get(
            dataValueConfigurationString
        )
        if (!dataValueProps)
            throw new JsonError(
                `Missing data value for query ${dataValueConfigurationString}`
            )
        return ReactDOMServer.renderToString(
            <AnnotatingDataValue dataValueProps={dataValueProps} />
        )
    })

    // Needs to be happen after DataValue replacements, as the DataToken regex
    // would otherwise capture DataValue tags
    const dataTokenRegex = /{{\s*([a-zA-Z]+)\s*(.+?)\s*}}/g

    html = html.replace(
        dataTokenRegex,
        (_, token, dataTokenConfigurationString) => {
            return ReactDOMServer.renderToString(
                <DataToken
                    token={token}
                    {...parseKeyValueArgs(dataTokenConfigurationString)}
                />
            )
        }
    )

    // Insert [table id=foo] tablepress tables
    const tables = await getTables()
    html = html.replace(/\[table\s+id=(\d+)\s*\/\]/g, (match, tableId) => {
        const table = tables.get(tableId)
        if (table)
            return ReactDOMServer.renderToStaticMarkup(
                <Tablepress data={table.data} />
            )
        return "UNKNOWN TABLE"
    })

    // No need for wordpress urls
    html = html.replace(new RegExp("/app/uploads", "g"), "/uploads")

    // Give "Add country" text (and variations) the appearance of "+ Add Country" chart control
    html = html.replace(
        /(\+ )?[a|A]dd [c|C]ountry/g,
        `<span class="add-country">
            <span class="icon">
                <svg width="16" height="16"><path d="M3,8 h10 m-5,-5 v10"></path></svg>
            </span>
            Add country
        </span>`
    )

    const cheerioEl = cheerio.load(html)

    // Related charts
    // Mimicking SSR output of additional information block from PHP
    if (
        !countryProfileSpecs.some(
            (spec) => post.slug === spec.landingPageSlug
        ) &&
        post.relatedCharts &&
        post.relatedCharts.length !== 0
    ) {
        const allCharts = `
        <block type="additional-information">
            <content>
                <h3>All our charts on ${post.title}</h3>
                ${ReactDOMServer.renderToStaticMarkup(
                    <div>
                        <RelatedCharts charts={post.relatedCharts} />
                    </div>
                )}
            </content>
        </block>
        `
        const $summary = cheerioEl(`.${SUMMARY_CLASSNAME}`)
        if ($summary.length !== 0) {
            $summary.after(allCharts)
        } else {
            cheerioEl("body > h2:first-of-type, body > h3:first-of-type")
                .first()
                .before(allCharts)
        }
    }

    // SSR rendering of Gutenberg blocks, before hydration on client
    renderBlocks(cheerioEl)
    await renderAutomaticProminentLinks(cheerioEl, post)

    // Extract inline styling
    let style
    const $style = cheerioEl("style")
    if ($style.length) {
        style = $style
            .toArray()
            .map((el) => cheerioEl(el).html() || "")
            .reduce((prev, curr) => prev + curr)
        $style.remove()
    }

    // Extract blog info content
    let info
    const $info = cheerioEl(".blog-info")
    if ($info.length) {
        info = $info.html() ?? undefined
        $info.remove()
    }

    // Extract last updated
    let lastUpdated
    const $lastUpdated = cheerioEl(".wp-block-last-updated")
    if ($lastUpdated.length) {
        lastUpdated = $lastUpdated.find("p").first().html() ?? undefined
        $lastUpdated.remove()
    }

    // Extract page supertitle
    let supertitle
    const $supertitle = cheerioEl(".wp-block-owid-supertitle")
    if ($supertitle.length) {
        supertitle = $supertitle.text()
        $supertitle.remove()
    }

    // Extract page byline
    let byline
    const $byline = cheerioEl(".wp-block-owid-byline")
    if ($byline.length) {
        byline = $byline.html() ?? undefined
        $byline.remove()
    }

    // Replace URLs pointing to Explorer redirect URLs with the destination URLs
    replaceIframesWithExplorerRedirectsInWordPressPost(cheerioEl)

    // Replace grapher iframes with static previews
    if (grapherExports) {
        const grapherIframes = cheerioEl("iframe")
            .toArray()
            .filter((el) => (el.attribs["src"] || "").match(/\/grapher\//))
        for (const el of grapherIframes) {
            const $el = cheerioEl(el)
            const src = el.attribs["src"].trim()
            const chart = grapherExports.get(src)
            if (chart) {
                const output = `
                <figure data-grapher-src="${src}" class="${GRAPHER_PREVIEW_CLASS}">
                    <a href="${src}" target="_blank">
                        <div><img src="${chart.svgUrl}" width="${chart.width}" height="${chart.height}" loading="lazy" data-no-lightbox /></div>
                        <div class="interactionNotice">
                            <span class="icon">${INTERACTIVE_ICON_SVG}</span>
                            <span class="label">Click to open interactive version</span>
                        </div>
                    </a>
                </figure>`
                if (el.parent.tagName === "p") {
                    // We are about to replace <iframe> with <figure>. However, there cannot be <figure> within <p>,
                    // so we are lifting the <figure> out.
                    // Where does this markup  come from? Historically, wpautop wrapped <iframe> in <p>. Some non-Gutengerg
                    // posts will still show that, until they are converted. As a reminder, wpautop is not being used
                    // on the overall post content anymore, neither on the Wordpress side nor on the grapher side (through
                    // the wpautop npm package), but its effects are still "present" after the result of wpautop were committed
                    // to the DB during a one-time refactoring session.
                    // <p><iframe></iframe></p>  -->  <p></p><figure></figure>
                    const $p = $el.parent()
                    $p.after(output)
                    $el.remove()
                } else if (el.parent.tagName === "figure") {
                    // Support for <iframe> wrapped in <figure>
                    // <figure> automatically added by Gutenberg on copy / paste <iframe>
                    // Lifting up <iframe> out of <figure>, before it becomes a <figure> itself.
                    // <figure><iframe></iframe></figure>  -->  <figure></figure>
                    const $figure = $el.parent()
                    $figure.after(output)
                    $figure.remove()
                } else {
                    // No lifting up otherwise, just replacing <iframe> with <figure>
                    // <iframe></iframe>  -->  <figure></figure>
                    $el.after(output)
                    $el.remove()
                }
            }
        }
    }

    // Replace explorer iframes with iframeless embed
    const explorerIframes = cheerioEl("iframe")
        .toArray()
        .filter((el) =>
            (el.attribs["src"] || "").includes(`/${EXPLORERS_ROUTE_FOLDER}/`)
        )
    for (const el of explorerIframes) {
        const $el = cheerioEl(el)
        const src = el.attribs["src"].trim()
        // set a default style if none exists on the existing iframe
        const style = el.attribs["style"] || "width: 100%; height: 600px;"
        const cssClass = el.attribs["class"]
        const $figure = cheerioEl(
            ReactDOMServer.renderToStaticMarkup(
                <figure data-explorer-src={src} className={cssClass}>
                    <LoadingIndicator />
                </figure>
            )
        )
        $figure.attr("style", style)
        $el.after($figure)
        $el.remove()
    }

    // Any remaining iframes
    for (const iframe of cheerioEl("iframe").toArray()) {
        // Ensure https embeds
        if (HTTPS_ONLY && iframe.attribs["src"]) {
            iframe.attribs["src"] = iframe.attribs["src"].replace(
                "http://",
                "https://"
            )
        }
        // Lazy load unless "loading" attribute already specified
        if (!iframe.attribs["loading"]) {
            iframe.attribs["loading"] = "lazy"
        }
    }

    // Remove any empty elements
    for (const p of cheerioEl("p").toArray()) {
        const $p = cheerioEl(p)
        if ($p.contents().length === 0) $p.remove()
    }

    // Wrap tables so we can do overflow-x: scroll if needed
    for (const table of cheerioEl("table").toArray()) {
        const $table = cheerioEl(table)
        const $div = cheerioEl("<div class='tableContainer'></div>")
        $table.after($div)
        $div.append($table)
    }

    // Make sticky-right layout the default for columns
    cheerioEl(".wp-block-columns").each((_, columns) => {
        const $columns = cheerioEl(columns)
        if (columns.attribs.class === "wp-block-columns") {
            $columns.addClass("is-style-sticky-right")
        }
    })

    // Image processing
    // Assumptions:
    // - original images are not uploaded with a suffix "-[number]x[number]"
    //   (without the quotes).
    // - variants are being generated by wordpress when the original is uploaded
    // - images are never legitimate direct descendants of <a> tags.
    //   <a><img /></a> is considered deprecated (was used to create direct links to
    //   the full resolution variant) and wrapping <a> will be removed to prevent
    //   conflicts with lightboxes. Chosen over preventDefault() in front-end code
    //   to avoid clicks before javascript executes.
    for (const el of cheerioEl("img").toArray()) {
        // Recreate source image path by removing automatically added image
        // dimensions (e.g. remove 800x600).
        const src = el.attribs["src"]
        const parsedPath = path.parse(src)
        let originalFilename = ""
        if (parsedPath.ext !== ".svg") {
            originalFilename = parsedPath.name.replace(/-\d+x\d+$/, "")
            const originalSrc = path.format({
                dir: parsedPath.dir,
                name: originalFilename,
                ext: parsedPath.ext,
            })
            el.attribs["data-high-res-src"] = originalSrc
        } else {
            originalFilename = parsedPath.name
        }
 
        // Remove wrapping <a> tag, conflicting with lightbox (cf. assumptions above)
        if (el.parent.tagName === "a") {
            const $a = cheerioEl(el.parent)
            $a.replaceWith(cheerioEl(el))
        }
 
        // Add alt attribute
        if (!el.attribs["alt"]) {
            el.attribs["alt"] = lodash.capitalize(
                originalFilename.replace(/[-_]/g, " ")
            )
        }
 
        // Lazy load all images, unless they already have a "loading" attribute.
        if (!el.attribs["loading"]) {
            el.attribs["loading"] = "lazy"
        }
    }
 
    // Table of contents and deep links
    const tocHeadings: TocHeading[] = []
    const existingSlugs: string[] = []
    let parentHeading: TocHeading | null = null
 
    cheerioEl("h1, h2, h3, h4").each((_, el) => {
        const $heading = cheerioEl(el)
        const headingText = $heading.text()
 
        let slug = urlSlug(headingText)
 
        // Avoid If the slug already exists, try prepend the parent
        if (existingSlugs.indexOf(slug) !== -1 && parentHeading) {
            slug = `${parentHeading.slug}-${slug}`
        }
 
        existingSlugs.push(slug)
 
        // Table of contents
        if (formattingOptions.toc) {
            if ($heading.is("#footnotes") && footnotes.length > 0) {
                tocHeadings.push({
                    text: headingText,
                    slug: "footnotes",
                    isSubheading: false,
                })
            } else if (!$heading.is("h1") && !$heading.is("h4")) {
                if ($heading.is("h2")) {
                    const tocHeading = {
                        text: headingText,
                        slug: slug,
                        isSubheading: false,
                    }
                    tocHeadings.push(tocHeading)
                    parentHeading = tocHeading
                } else if (
                    $heading.closest(`.${PROMINENT_LINK_CLASSNAME}`).length ===
                        0 &&
                    $heading.closest(".wp-block-owid-additional-information")
                        .length === 0
                ) {
                    tocHeadings.push({
                        text: headingText,
                        html: $heading.html() || undefined,
                        slug: slug,
                        isSubheading: true,
                    })
                }
            }
        }
 
        $heading.attr("id", slug)
        // Add deep link for headings not contained in <a> tags already
        // (e.g. within a prominent link block)
        if (
            !$heading.closest(`.${PROMINENT_LINK_CLASSNAME}`).length && // already wrapped in <a>
            !$heading.closest(".wp-block-owid-additional-information").length && // prioritize clean SSR of AdditionalInformation
            !$heading.closest(".wp-block-help").length
        ) {
            $heading.prepend(
                `<a class="${DEEP_LINK_CLASS}" href="#${slug}"></a>`
            )
        }
    })
 
    return {
        ...post,
        supertitle,
        lastUpdated,
        byline,
        info,
        style,
        footnotes: footnotes,
        references: references,
        tocHeadings: tocHeadings,
        pageDesc: post.excerpt || cheerioEl("p").first().text(),
        html: getBodyHtml(cheerioEl),
    }
}
 
export const formatPost = async (
    post: FullPost,
    formattingOptions: FormattingOptions,
    grapherExports?: GrapherExports
): Promise<FormattedPost> => {
    const html = formatLinks(post.content)
 
    // No formatting applied, plain source HTML returned
    if (formattingOptions.raw)
        return {
            ...post,
            html,
            footnotes: [],
            references: [],
            tocHeadings: [],
            pageDesc: post.excerpt || "",
        }
 
    // Override formattingOptions if specified in the post (as an HTML comment)
    const options: FormattingOptions = {
        toc: post.type === "page",
        footnotes: true,
        ...formattingOptions,
    }
 
    return formatWordpressPost(post, html, options, grapherExports)
}