Skip to content

Commit

Permalink
🎉 transform blockquotes into prominent links when appropriate
Browse files Browse the repository at this point in the history
  • Loading branch information
ikesau committed Nov 3, 2023
1 parent 0e11821 commit a020005
Showing 1 changed file with 47 additions and 2 deletions.
49 changes: 47 additions & 2 deletions db/model/Gdoc/htmlToEnriched.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import {
EnrichedBlockGraySection,
EnrichedBlockStickyRightContainer,
EnrichedBlockBlockquote,
traverseEnrichedSpan,
} from "@ourworldindata/utils"
import { match, P } from "ts-pattern"
import {
Expand Down Expand Up @@ -789,6 +790,39 @@ function finishWpComponent(
})
}

function extractProminentLinkFromBlockQuote(
spans: Span[]
): EnrichedBlockProminentLink | undefined {
const spansContainRelatedChart = spansToSimpleString(spans)
.toLowerCase()
.includes("related chart")

if (!spansContainRelatedChart) return undefined

let isRelatedChart = false
let url = ""

spans.forEach((span) =>
traverseEnrichedSpan(span, (span) => {
if (
span.spanType === "span-link" &&
span.url.includes("/grapher/")
) {
url = span.url
isRelatedChart = true
}
})
)

if (isRelatedChart)
return {
type: "prominent-link",
url,
parseErrors: [],
}
return
}

function isEnrichedTextBlock(
item: ArchieBlockOrWpComponent
): item is EnrichedBlockText {
Expand Down Expand Up @@ -822,9 +856,20 @@ function cheerioToArchieML(
.with({ tagName: "address" }, unwrapElementWithContext)
.with(
{ tagName: "blockquote" },
(): BlockParseResult<EnrichedBlockBlockquote> => {
(): BlockParseResult<
EnrichedBlockBlockquote | EnrichedBlockProminentLink
> => {
const spansResult = getSpansFromChildren(element, context)

// Sometimes blockquotes were used for prominent links before we had a bespoke
// component for them. Using some simple heuristics we try to convert these if possible
const prominentLink = extractProminentLinkFromBlockQuote(
spansResult.content
)
if (prominentLink)
return {
errors: [],
content: [prominentLink],
}
return {
errors: spansResult.errors,
content: [
Expand Down

0 comments on commit a020005

Please sign in to comment.