Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new about page template and people component #4217

Merged
merged 2 commits into from
Nov 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions db/model/Gdoc/GdocBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,8 @@ export class GdocBase implements OwidGdocBaseInterface {
"list",
"missing-data",
"numbered-list",
"people",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

people has person and person has text, so they should be handled in a with that extracts links from the text 🙂

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the comment at the top of this .with() call applies:

// no urls directly on any of these blocks
// their children may contain urls, but they'll be addressed by traverseEnrichedBlock

The links seem to work fine. AFAICT, we'd need a special handling for them here only if they were added as a property on a block, e.g. if {.person} had a url: property.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aha whoops! Hoisted by my own petard. Wrote this one before I saw the update to traverseEnrichedBlocks and then didn't think to delete it.

"person",
"pull-quote",
"sdg-grid",
"sdg-toc",
Expand Down
20 changes: 20 additions & 0 deletions db/model/Gdoc/enrichedToMarkdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { getLinkType } from "@ourworldindata/components"
import {
OwidEnrichedGdocBlock,
Span,
compact,
excludeNullish,
isArray,
} from "@ourworldindata/utils"
Expand Down Expand Up @@ -156,6 +157,25 @@ ${items}
.with({ type: "list" }, (b): string | undefined =>
b.items.map((item) => `* ${spansToMarkdown(item.value)}`).join("\n")
)
.with({ type: "people" }, (b): string | undefined =>
b.items
.map((item) => enrichedBlockToMarkdown(item, exportComponents))
.join("\n")
)
.with({ type: "person" }, (b): string | undefined => {
const items = [
b.image &&
markdownComponent(
"Image",
{ filename: b.image, alt: b.name },
exportComponents
),
`### ${b.name}`,
b.title,
enrichedBlocksToMarkdown(b.text, exportComponents),
]
return compact(items).join("\n")
})
.with(
{ type: "pull-quote" },
(b): string | undefined => `> ${spansToMarkdown(b.text)}`
Expand Down
21 changes: 21 additions & 0 deletions db/model/Gdoc/enrichedToRaw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ import {
RawBlockHomepageIntro,
RawBlockLatestDataInsights,
RawBlockSocials,
RawBlockPeople,
RawBlockPerson,
} from "@ourworldindata/types"
import { spanToHtmlString } from "./gdocUtils.js"
import { match, P } from "ts-pattern"
Expand Down Expand Up @@ -182,6 +184,25 @@ export function enrichedBlockToRawBlock(
value: b.items.map((item) => spansToHtmlText(item.value)),
})
)
.with(
{ type: "people" },
(b): RawBlockPeople => ({
type: b.type,
value: b.items.map(enrichedBlockToRawBlock) as RawBlockPerson[],
})
)
.with(
{ type: "person" },
(b): RawBlockPerson => ({
type: "person",
value: {
image: b.image,
name: b.name,
title: b.title,
text: b.text.map(enrichedBlockToRawBlock) as RawBlockText[],
},
})
)
.with(
{ type: "pull-quote" },
(b): RawBlockPullQuote => ({
Expand Down
15 changes: 15 additions & 0 deletions db/model/Gdoc/exampleEnrichedBlocks.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
BlockImageSize,
EnrichedBlockChart,
EnrichedBlockPerson,
EnrichedBlockText,
HorizontalAlign,
OwidEnrichedGdocBlock,
Expand Down Expand Up @@ -48,6 +49,14 @@ const enrichedChart: EnrichedBlockChart = {
parseErrors: [],
}

const enrichedBlockPerson: EnrichedBlockPerson = {
type: "person",
image: "example.png",
name: "Max Roser",
text: [enrichedBlockText],
parseErrors: [],
}

export const enrichedBlockExamples: Record<
OwidEnrichedGdocBlock["type"],
OwidEnrichedGdocBlock
Expand Down Expand Up @@ -176,6 +185,12 @@ export const enrichedBlockExamples: Record<
items: [enrichedBlockText],
parseErrors: [],
},
people: {
type: "people",
items: [enrichedBlockPerson, enrichedBlockPerson],
parseErrors: [],
},
person: enrichedBlockPerson,
"pull-quote": {
type: "pull-quote",
text: [spanSimpleText],
Expand Down
4 changes: 4 additions & 0 deletions db/model/Gdoc/gdocUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,9 @@ export function extractFilenamesFromBlock(
if (item.filename) filenames.add(item.filename)
if (item.smallFilename) filenames.add(item.smallFilename)
})
.with({ type: "person" }, (item) => {
if (item.image) filenames.add(item.image)
})
.with({ type: "prominent-link" }, (item) => {
if (item.thumbnail) filenames.add(item.thumbnail)
})
Expand Down Expand Up @@ -233,6 +236,7 @@ export function extractFilenamesFromBlock(
"list",
"missing-data",
"numbered-list",
"people",
"pill-row",
"pull-quote",
"recirc",
Expand Down
31 changes: 31 additions & 0 deletions db/model/Gdoc/rawToArchie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ import {
RawBlockHomepageSearch,
RawBlockLatestDataInsights,
RawBlockSocials,
RawBlockPeople,
RawBlockPerson,
} from "@ourworldindata/types"
import { isArray } from "@ourworldindata/utils"
import { match } from "ts-pattern"
Expand Down Expand Up @@ -213,6 +215,33 @@ function* rawBlockNumberedListToArchieMLString(
yield* listToArchieMLString(block.value, "numbered-list")
}

function* rawBlockPeopleToArchieMLString(
block: RawBlockPeople
): Generator<string, void, undefined> {
yield "[.+people]"
if (typeof block.value !== "string") {
for (const b of block.value) {
yield* OwidRawGdocBlockToArchieMLStringGenerator(b)
}
}
yield "[]"
}

function* rawBlockPersonToArchieMLString(
block: RawBlockPerson
): Generator<string, void, undefined> {
yield "{.person}"
yield* propertyToArchieMLString("image", block.value)
yield* propertyToArchieMLString("name", block.value)
yield* propertyToArchieMLString("title", block.value)
yield "[.+text]"
for (const b of block.value.text) {
yield* OwidRawGdocBlockToArchieMLStringGenerator(b)
}
yield "[]"
yield "{}"
}

function* rawBlockPullQuoteToArchieMLString(
block: RawBlockPullQuote
): Generator<string, void, undefined> {
Expand Down Expand Up @@ -773,6 +802,8 @@ export function* OwidRawGdocBlockToArchieMLStringGenerator(
.with({ type: "video" }, rawBlockVideoToArchieMLString)
.with({ type: "list" }, rawBlockListToArchieMLString)
.with({ type: "numbered-list" }, rawBlockNumberedListToArchieMLString)
.with({ type: "people" }, rawBlockPeopleToArchieMLString)
.with({ type: "person" }, rawBlockPersonToArchieMLString)
.with({ type: "pull-quote" }, rawBlockPullQuoteToArchieMLString)
.with(
{ type: "horizontal-rule" },
Expand Down
50 changes: 50 additions & 0 deletions db/model/Gdoc/rawToEnriched.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ import {
SocialLinkType,
RawBlockLatestWork,
EnrichedBlockLatestWork,
RawBlockPeople,
EnrichedBlockPeople,
RawBlockPerson,
EnrichedBlockPerson,
} from "@ourworldindata/types"
import {
traverseEnrichedSpan,
Expand Down Expand Up @@ -166,6 +170,8 @@ export function parseRawBlocksToEnrichedBlocks(
.with({ type: "video" }, parseVideo)
.with({ type: "list" }, parseList)
.with({ type: "numbered-list" }, parseNumberedList)
.with({ type: "people" }, parsePeople)
.with({ type: "person" }, parsePerson)
.with({ type: "pull-quote" }, parsePullQuote)
.with(
{ type: "horizontal-rule" },
Expand Down Expand Up @@ -790,6 +796,50 @@ const parseList = (raw: RawBlockList): EnrichedBlockList => {
// return { errors, texts }
// }

const parsePerson = (raw: RawBlockPerson): EnrichedBlockPerson => {
const createError = (error: ParseError): EnrichedBlockPerson => ({
type: "person",
name: "",
text: [],
parseErrors: [error],
})

if (!raw.value?.name) {
return createError({ message: "Person must have a name" })
}

return {
type: "person",
image: raw.value.image,
name: raw.value.name,
title: raw.value.title,
text: raw.value.text.map(parseText),
parseErrors: [],
}
}

const parsePeople = (raw: RawBlockPeople): EnrichedBlockPeople => {
const createError = (
error: ParseError,
items: EnrichedBlockPerson[] = []
): EnrichedBlockPeople => ({
type: "people",
items,
parseErrors: [error],
})

if (typeof raw.value === "string")
return createError({
message: "Value is a string, not a list of {.person} blocks",
})

return {
type: "people",
items: raw.value.map(parsePerson),
parseErrors: [],
}
}

const parsePullQuote = (raw: RawBlockPullQuote): EnrichedBlockPullQuote => {
const createError = (
error: ParseError,
Expand Down
32 changes: 32 additions & 0 deletions packages/@ourworldindata/types/src/gdocTypes/ArchieMlComponents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,34 @@ export type EnrichedBlockNumberedList = {
items: EnrichedBlockText[]
} & EnrichedBlockWithParseErrors

export type RawBlockPeople = {
type: "people"
value: RawBlockPerson[] | ArchieMLUnexpectedNonObjectValue
}

export type RawBlockPerson = {
type: "person"
value: {
image?: string
name: string
title?: string
text: RawBlockText[]
}
}

export type EnrichedBlockPeople = {
type: "people"
items: EnrichedBlockPerson[]
} & EnrichedBlockWithParseErrors

export type EnrichedBlockPerson = {
type: "person"
image?: string
name: string
title?: string
text: EnrichedBlockText[]
} & EnrichedBlockWithParseErrors

export type RawBlockPullQuote = {
type: "pull-quote"
value: OwidRawGdocBlock[] | ArchieMLUnexpectedNonObjectValue
Expand Down Expand Up @@ -887,6 +915,8 @@ export type OwidRawGdocBlock =
| RawBlockImage
| RawBlockVideo
| RawBlockList
| RawBlockPeople
| RawBlockPerson
| RawBlockPullQuote
| RawBlockRecirc
| RawBlockResearchAndWriting
Expand Down Expand Up @@ -933,6 +963,8 @@ export type OwidEnrichedGdocBlock =
| EnrichedBlockImage
| EnrichedBlockVideo
| EnrichedBlockList
| EnrichedBlockPeople
| EnrichedBlockPerson
| EnrichedBlockPullQuote
| EnrichedBlockRecirc
| EnrichedBlockResearchAndWriting
Expand Down
17 changes: 17 additions & 0 deletions packages/@ourworldindata/types/src/gdocTypes/Gdoc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,17 +202,32 @@ export interface OwidGdocAuthorInterface extends OwidGdocBaseInterface {
latestWorkLinks?: DbEnrichedLatestWork[]
}

export interface OwidGdocAboutContent {
type: OwidGdocType.AboutPage
title: string
excerpt?: string
"featured-image"?: string
authors: string[]
body: OwidEnrichedGdocBlock[]
}

export interface OwidGdocAboutInterface extends OwidGdocBaseInterface {
content: OwidGdocAboutContent
}

export type OwidGdocContent =
| OwidGdocPostContent
| OwidGdocDataInsightContent
| OwidGdocHomepageContent
| OwidGdocAuthorContent
| OwidGdocAboutContent

export type OwidGdoc =
| OwidGdocPostInterface
| OwidGdocDataInsightInterface
| OwidGdocHomepageInterface
| OwidGdocAuthorInterface
| OwidGdocAboutInterface

export enum OwidGdocErrorMessageType {
Error = "error",
Expand All @@ -226,6 +241,8 @@ export type OwidGdocProperty =
| keyof OwidGdocDataInsightContent
| keyof OwidGdocAuthorInterface
| keyof OwidGdocAuthorContent
| keyof OwidGdocAboutInterface
| keyof OwidGdocAboutContent

export type OwidGdocErrorMessageProperty =
| OwidGdocProperty
Expand Down
6 changes: 6 additions & 0 deletions packages/@ourworldindata/types/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,8 @@ export {
type RawBlockList,
type RawBlockMissingData,
type RawBlockNumberedList,
type RawBlockPeople,
type RawBlockPerson,
type RawBlockPosition,
type RawBlockProminentLink,
type RawBlockPullQuote,
Expand Down Expand Up @@ -237,6 +239,8 @@ export {
type EnrichedBlockList,
type EnrichedBlockMissingData,
type EnrichedBlockNumberedList,
type EnrichedBlockPeople,
type EnrichedBlockPerson,
type EnrichedBlockProminentLink,
type EnrichedBlockPullQuote,
type EnrichedBlockRecirc,
Expand Down Expand Up @@ -284,6 +288,8 @@ export {
type OwidGdocErrorMessage,
OwidGdocErrorMessageType,
type OwidGdocLinkJSON,
type OwidGdocAboutContent,
type OwidGdocAboutInterface,
type OwidGdocAuthorContent,
type OwidGdocAuthorInterface,
type OwidGdocBaseInterface,
Expand Down
12 changes: 12 additions & 0 deletions packages/@ourworldindata/utils/src/Util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1664,6 +1664,18 @@ export function traverseEnrichedBlock(
)
}
)
.with({ type: "people" }, (people) => {
callback(people)
for (const item of people.items) {
traverseEnrichedBlock(item, callback, spanCallback)
}
})
.with({ type: "person" }, (person) => {
callback(person)
for (const node of person.text) {
traverseEnrichedBlock(node, callback, spanCallback)
}
})
.with(
{
type: P.union(
Expand Down
Loading