-
-
Notifications
You must be signed in to change notification settings - Fork 229
/
Copy pathpageOverrides.tsx
93 lines (82 loc) · 3.27 KB
/
pageOverrides.tsx
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
import { PageOverrides } from "../site/LongFormPage.js"
import { BAKED_BASE_URL } from "../settings/serverSettings.js"
import { urlToSlug, FullPost } from "@ourworldindata/utils"
import { FormattingOptions } from "@ourworldindata/types"
import { getTopSubnavigationParentItem } from "../site/gdocs/utils.js"
import { logErrorAndMaybeCaptureInSentry } from "../serverUtils/errorLog.js"
import {
getFullPostBySlugFromSnapshot,
isPostSlugCitable,
} from "../db/model/Post.js"
import { KnexReadonlyTransaction } from "../db/db.js"
export const getPostBySlugLogToSlackNoThrow = async (
knex: KnexReadonlyTransaction,
slug: string
) => {
try {
return await getFullPostBySlugFromSnapshot(knex, slug)
} catch (err) {
void logErrorAndMaybeCaptureInSentry(err)
return undefined
}
}
export const getLandingOnlyIfParent = async (
knex: KnexReadonlyTransaction,
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(knex, 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.
await logErrorAndMaybeCaptureInSentry(
new Error(
// This error often shows up intermittently as a false-positive (DB unavailable when calling getPostBySlug()?)
// If it happens systematically, please check
// the href of the "subnavs[${formattingOptions.subnavId}]"
// landing page (the first item in the array): it might be
// out-of-date and redirected.
`Citation overrides not applied for ${post.slug}. This might be an intermittent issue.`
)
)
}
return landing
}
export const getPageOverrides = async (
knex: KnexReadonlyTransaction,
post: FullPost,
formattingOptions: FormattingOptions
): Promise<PageOverrides | undefined> => {
const landing = await getLandingOnlyIfParent(knex, post, formattingOptions)
if (!landing) return
const isParentLandingCitable = isPostSlugCitable(landing.slug)
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
)
}