Skip to content

Commit

Permalink
Add people ArchieML component
Browse files Browse the repository at this point in the history
  • Loading branch information
rakyi committed Nov 29, 2024
1 parent 014d419 commit 874da67
Show file tree
Hide file tree
Showing 19 changed files with 308 additions and 2 deletions.
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",
"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
2 changes: 1 addition & 1 deletion packages/@ourworldindata/types/src/gdocTypes/Gdoc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ export interface OwidGdocAuthorInterface extends OwidGdocBaseInterface {
export interface OwidGdocAboutContent {
type: OwidGdocType.AboutPage
title: string
excerpt: string
excerpt?: string
"featured-image"?: string
authors: string[]
body: OwidEnrichedGdocBlock[]
Expand Down
4 changes: 4 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
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
3 changes: 2 additions & 1 deletion site/gdocs/OwidGdoc.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
RelatedChart,
OwidGdocType,
OwidGdoc as OwidGdocInterface,
OwidGdocAboutInterface,
OwidGdocMinimalPostInterface,
OwidGdocHomepageMetadata,
DbEnrichedLatestWork,
Expand Down Expand Up @@ -87,7 +88,7 @@ export function OwidGdoc({
(props) => <GdocPost {...props} />
)
.with({ content: { type: OwidGdocType.AboutPage } }, (props) => (
<AboutPage {...props} />
<AboutPage {...(props as OwidGdocAboutInterface)} />
))
.with({ content: { type: OwidGdocType.DataInsight } }, (props) => (
<DataInsightPage {...props} />
Expand Down
Loading

0 comments on commit 874da67

Please sign in to comment.