Skip to content

Commit

Permalink
Add code ArchieML component
Browse files Browse the repository at this point in the history
Reuses the existing CodeSnippet component.
  • Loading branch information
rakyi committed Dec 12, 2024
1 parent 08c9ab2 commit 19c78e3
Show file tree
Hide file tree
Showing 15 changed files with 95 additions and 7 deletions.
1 change: 1 addition & 0 deletions db/model/Gdoc/GdocBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,7 @@ export class GdocBase implements OwidGdocBaseInterface {
"aside",
"blockquote",
"callout",
"code",
"donors",
"expandable-paragraph",
"entry-summary",
Expand Down
5 changes: 5 additions & 0 deletions db/model/Gdoc/enrichedToMarkdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,11 @@ ${items}
exportComponents
)
)
.with({ type: "code" }, (b): string | undefined => {
return (
"```\n" + b.text.map((text) => text.value).join("\n") + "\n```"
)
})
.with({ type: "donors" }, (_): string | undefined =>
markdownComponent("DonorList", {}, exportComponents)
)
Expand Down
8 changes: 8 additions & 0 deletions db/model/Gdoc/enrichedToRaw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import {
RawBlockPeople,
RawBlockPeopleRows,
RawBlockPerson,
RawBlockCode,
} from "@ourworldindata/types"
import { spanToHtmlString } from "./gdocUtils.js"
import { match, P } from "ts-pattern"
Expand Down Expand Up @@ -122,6 +123,13 @@ export function enrichedBlockToRawBlock(
},
})
)
.with(
{ type: "code" },
(b): RawBlockCode => ({
type: b.type,
value: b.text,
})
)
.with(
{ type: "donors" },
(b): RawBlockDonorList => ({
Expand Down
10 changes: 10 additions & 0 deletions db/model/Gdoc/exampleEnrichedBlocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,16 @@ export const enrichedBlockExamples: Record<
caption: boldLinkExampleText,
parseErrors: [],
},
code: {
type: "code",
text: [
{
type: "text",
value: '<iframe src="https://ourworldindata.org/grapher/children-per-woman-un?region=Africa&tab=map" loading="lazy" style="width: 100%; height: 600px; border: 0px none;" allow="web-share; clipboard-write"></iframe>',
},
],
parseErrors: [],
},
donors: {
type: "donors",
value: {},
Expand Down
1 change: 1 addition & 0 deletions db/model/Gdoc/gdocUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ export function extractFilenamesFromBlock(
"callout",
"chart-story",
"chart",
"code",
"donors",
"entry-summary",
"expandable-paragraph",
Expand Down
12 changes: 12 additions & 0 deletions db/model/Gdoc/rawToArchie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import {
RawBlockPeople,
RawBlockPeopleRows,
RawBlockPerson,
RawBlockCode,
} from "@ourworldindata/types"
import { isArray } from "@ourworldindata/utils"
import { match } from "ts-pattern"
Expand Down Expand Up @@ -127,6 +128,16 @@ function* rawBlockChartToArchieMLString(
yield "{}"
}

function* rawBlockCodeToArchieMLString(
block: RawBlockCode
): Generator<string, void, undefined> {
yield "[.+code]"
for (const text of block.value) {
yield* OwidRawGdocBlockToArchieMLStringGenerator(text)
}
yield "[]"
}

function* rawBlockDonorListToArchieMLString(
_block: RawBlockDonorList
): Generator<string, void, undefined> {
Expand Down Expand Up @@ -819,6 +830,7 @@ export function* OwidRawGdocBlockToArchieMLStringGenerator(
.with({ type: "all-charts" }, rawBlockAllChartsToArchieMLString)
.with({ type: "aside" }, rawBlockAsideToArchieMLString)
.with({ type: "chart" }, rawBlockChartToArchieMLString)
.with({ type: "code" }, rawBlockCodeToArchieMLString)
.with({ type: "donors" }, rawBlockDonorListToArchieMLString)
.with({ type: "scroller" }, rawBlockScrollerToArchieMLString)
.with({ type: "callout" }, rawBlockCalloutToArchieMLString)
Expand Down
15 changes: 15 additions & 0 deletions db/model/Gdoc/rawToEnriched.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ import {
EnrichedBlockPerson,
RawBlockPeopleRows,
EnrichedBlockPeopleRows,
RawBlockCode,
EnrichedBlockCode,
} from "@ourworldindata/types"
import {
traverseEnrichedSpan,
Expand All @@ -142,6 +144,7 @@ import {
isArray,
partition,
compact,
toAsciiQuotes,
} from "@ourworldindata/utils"
import { checkIsInternalLink, getLinkType } from "@ourworldindata/components"
import {
Expand Down Expand Up @@ -169,6 +172,7 @@ export function parseRawBlocksToEnrichedBlocks(
.with({ type: "blockquote" }, parseBlockquote)
.with({ type: "callout" }, parseCallout)
.with({ type: "chart" }, parseChart)
.with({ type: "code" }, parseCode)
.with({ type: "donors" }, parseDonorList)
.with({ type: "scroller" }, parseScroller)
.with({ type: "chart-story" }, parseChartStory)
Expand Down Expand Up @@ -492,6 +496,17 @@ const parseChart = (raw: RawBlockChart): EnrichedBlockChart => {
}
}

const parseCode = (raw: RawBlockCode): EnrichedBlockCode => {
return {
type: "code",
text: raw.value.map((text) => ({
type: "text",
value: toAsciiQuotes(text.value),
})),
parseErrors: [],
}
}

const parseDonorList = (raw: RawBlockDonorList): EnrichedBlockDonorList => {
return {
type: "donors",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ import cx from "classnames"
import { SimpleMarkdownText } from "../SimpleMarkdownText"

export const CodeSnippet = ({
className,
code,
theme = "dark",
isTruncated = false,
useMarkdown = false,
}: {
className?: string
code: string
theme?: "dark" | "light"
isTruncated?: boolean
Expand Down Expand Up @@ -40,7 +42,12 @@ export const CodeSnippet = ({
}

return (
<div className={`wp-code-snippet wp-code-snippet--${theme}`}>
<div
className={cx(
`wp-code-snippet wp-code-snippet--${theme}`,
className
)}
>
<pre className="wp-block-code">
<code
className={cx("wp-code-snippet__code", {
Expand Down
12 changes: 12 additions & 0 deletions packages/@ourworldindata/types/src/gdocTypes/ArchieMlComponents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,16 @@ export type EnrichedBlockChart = {
tabs?: ChartTabKeyword[]
} & EnrichedBlockWithParseErrors

export type RawBlockCode = {
type: "code"
value: RawBlockText[]
}

export type EnrichedBlockCode = {
type: "code"
text: RawBlockText[]
} & EnrichedBlockWithParseErrors

export type RawBlockDonorList = {
type: "donors"
value?: Record<string, never> // dummy value to unify block shapes
Expand Down Expand Up @@ -935,6 +945,7 @@ export type OwidRawGdocBlock =
| RawBlockAside
| RawBlockCallout
| RawBlockChart
| RawBlockCode
| RawBlockDonorList
| RawBlockScroller
| RawBlockChartStory
Expand Down Expand Up @@ -985,6 +996,7 @@ export type OwidEnrichedGdocBlock =
| EnrichedBlockAside
| EnrichedBlockCallout
| EnrichedBlockChart
| EnrichedBlockCode
| EnrichedBlockDonorList
| EnrichedBlockScroller
| EnrichedBlockChartStory
Expand Down
2 changes: 2 additions & 0 deletions packages/@ourworldindata/types/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ export {
type RawBlockChart,
type RawBlockChartStory,
type RawBlockChartValue,
type RawBlockCode,
type RawBlockExpandableParagraph,
type RawBlockExplorerTiles,
type RawBlockGraySection,
Expand Down Expand Up @@ -226,6 +227,7 @@ export {
type EnrichedBlockCallout,
type EnrichedBlockChart,
type EnrichedBlockChartStory,
type EnrichedBlockCode,
type EnrichedBlockDonorList,
type EnrichedBlockExpandableParagraph,
type EnrichedBlockExplorerTiles,
Expand Down
1 change: 1 addition & 0 deletions packages/@ourworldindata/utils/src/Util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1708,6 +1708,7 @@ export function traverseEnrichedBlock(
type: P.union(
"chart-story",
"chart",
"code",
"donors",
"horizontal-rule",
"html",
Expand Down
2 changes: 1 addition & 1 deletion packages/@ourworldindata/utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ export { Url, setWindowUrl, getWindowUrl } from "./urls/Url.js"

export { type UrlMigration, performUrlMigrations } from "./urls/UrlMigration.js"

export { camelCaseProperties, titleCase } from "./string.js"
export { camelCaseProperties, titleCase, toAsciiQuotes } from "./string.js"

export { serializeJSONForHTML, deserializeJSONFromHTML } from "./serializers.js"

Expand Down
4 changes: 4 additions & 0 deletions packages/@ourworldindata/utils/src/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,7 @@ export const titleCase = (str: string): string => {
})
.join(" ")
}

export function toAsciiQuotes(str: string): string {
return str.replace(/[“”]/g, '"').replace(/[‘’]/g, "'")
}
8 changes: 7 additions & 1 deletion site/gdocs/components/ArticleBlock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
TocHeadingWithTitleSupertitle,
Url,
} from "@ourworldindata/utils"
import { convertHeadingTextToId } from "@ourworldindata/components"
import { CodeSnippet, convertHeadingTextToId } from "@ourworldindata/components"
import SDGGrid from "./SDGGrid.js"
import { BlockErrorBoundary, BlockErrorFallback } from "./BlockErrorBoundary.js"
import { match } from "ts-pattern"
Expand Down Expand Up @@ -245,6 +245,12 @@ export default function ArticleBlock({
/>
)
})
.with({ type: "code" }, (block) => (
<CodeSnippet
className={getLayout("code-snippet", containerType)}
code={block.text.map((text) => text.value).join("\n")}
/>
))
.with({ type: "donors" }, (_block) => (
<Donors className={getLayout("donors", containerType)} />
))
Expand Down
12 changes: 8 additions & 4 deletions site/gdocs/pages/AboutPage.scss
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,18 @@
margin-bottom: 24px;
}

.article-block__image {
margin: 0 0 24px;
}

.article-block__chart {
margin-bottom: 24px;
}

.article-block__code-snippet {
margin-bottom: 16px;
}

.article-block__image {
margin: 0 0 24px;
}

.article-block__prominent-link {
margin-bottom: 24px;
}
Expand Down

0 comments on commit 19c78e3

Please sign in to comment.