All files / owid-grapher/baker pageOverrides.tsx

56.25% Statements 45/80
88.89% Branches 8/9
25% Functions 1/4
56.25% Lines 45/80

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 821x 1x 1x 1x 1x 1x 1x 1x 1x                   1x 1x 4x 4x 4x 4x 4x 4x 3x 3x 4x 3x 3x 4x 2x 2x 2x 2x 2x 4x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 1x 1x                                   1x 1x                      
import { PageOverrides } from "../site/LongFormPage"
import { BAKED_BASE_URL } from "../settings/serverSettings"
import { stringifyUnkownError, urlToSlug } from "../clientUtils/Util"
import { FormattingOptions, FullPost } from "../clientUtils/owidTypes"
import { getPostBySlug, isPostCitable } from "../db/wpdb"
import { getTopSubnavigationParentItem } from "../site/SiteSubnavigation"
import { logErrorAndMaybeSendToSlack } from "../serverUtils/slackLog"
 
export const getPostBySlugLogToSlackNoThrow = async (slug: string) => {
    let post
    try {
        post = await getPostBySlug(slug)
    } catch (err) {
        logErrorAndMaybeSendToSlack(stringifyUnkownError(err))
    } finally {
        return post
    }
}
 
export const getLandingOnlyIfParent = async (
    post: FullPost,
    formattingOptions: FormattingOptions
) => {
    if (!formattingOptions.subnavId) return
 
    const landingItemHref = getTopSubnavigationParentItem(
        formattingOptions.subnavId
    )?.href
    if (!landingItemHref) return
 
    const landingSlug = urlToSlug(landingItemHref)
    if (landingSlug === post.slug) return
 
    // Using no-throw version to prevent throwing and stopping baking mid-way.
    // It is more desirable to temporarily deploy with citation overrides
    // absent, while fixing the issue.
    const landing = await getPostBySlugLogToSlackNoThrow(landingSlug)
    if (!landing) {
        // todo: the concept of "citation overrides" does not belong to that
        // generic function. Logging this message should be the responsibility
        // of the caller function.
        logErrorAndMaybeSendToSlack(
            new Error(
                `Citation overrides not applied for ${post.slug}. Please check the href of the "subnavs[${formattingOptions.subnavId}]" landing page (the first item in the array): it is likely out-of-date and is being redirected.`
            )
        )
    }
 
    return landing
}
 
export const getPageOverrides = async (
    post: FullPost,
    formattingOptions: FormattingOptions
): Promise<PageOverrides | undefined> => {
    const landing = await getLandingOnlyIfParent(post, formattingOptions)
    if (!landing) return

    const isParentLandingCitable = await isPostCitable(landing)
    if (!isParentLandingCitable) return

    return {
        citationTitle: landing.title,
        citationSlug: landing.slug,
        citationCanonicalUrl: `${BAKED_BASE_URL}/${landing.slug}`,
        citationAuthors: landing.authors,
        citationPublicationDate: landing.date,
    }
}
 
export const isPageOverridesCitable = (pageOverrides?: PageOverrides) => {
    if (!pageOverrides) return false

    return (
        !!pageOverrides.citationTitle &&
        !!pageOverrides.citationSlug &&
        !!pageOverrides.citationCanonicalUrl &&
        !!pageOverrides.citationAuthors &&
        !!pageOverrides.citationPublicationDate
    )
}