-
-
Notifications
You must be signed in to change notification settings - Fork 229
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
Entry Emulator - Summary Block #2638
Changes from 6 commits
9462823
87070e8
dc767a9
fcc7d04
cc9b02c
8cf7dad
527dc2a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,9 +26,22 @@ import { | |
EnrichedBlockProminentLink, | ||
BlockImageSize, | ||
detailOnDemandRegex, | ||
EnrichedBlockEntrySummary, | ||
EnrichedBlockEntrySummaryItem, | ||
spansToUnformattedPlainText, | ||
checkNodeIsSpanLink, | ||
Url, | ||
EnrichedBlockCallout, | ||
} from "@ourworldindata/utils" | ||
import { match, P } from "ts-pattern" | ||
import { compact, flatten, isPlainObject, partition } from "lodash" | ||
import { | ||
compact, | ||
flatten, | ||
get, | ||
isArray, | ||
isPlainObject, | ||
partition, | ||
} from "lodash" | ||
import cheerio from "cheerio" | ||
import { spansToSimpleString } from "./gdocUtils.js" | ||
|
||
|
@@ -223,6 +236,10 @@ type ErrorNames = | |
| "unhandled html tag found" | ||
| "prominent link missing title" | ||
| "prominent link missing url" | ||
| "summary item isn't text" | ||
| "summary item doesn't have link" | ||
| "summary item has DataValue" | ||
| "Unknown content type inside summary block" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: remove capitalization for consistency |
||
|
||
interface BlockParseError { | ||
name: ErrorNames | ||
|
@@ -337,11 +354,12 @@ function isArchieMlComponent( | |
export function convertAllWpComponentsToArchieMLBlocks( | ||
blocksOrComponents: ArchieBlockOrWpComponent[] | ||
): OwidEnrichedGdocBlock[] { | ||
return blocksOrComponents.flatMap((blockOrComponent) => { | ||
if (isArchieMlComponent(blockOrComponent)) return [blockOrComponent] | ||
return blocksOrComponents.flatMap((blockOrComponentOrToc) => { | ||
if (isArchieMlComponent(blockOrComponentOrToc)) | ||
return [blockOrComponentOrToc] | ||
else { | ||
return convertAllWpComponentsToArchieMLBlocks( | ||
blockOrComponent.childrenResults | ||
blockOrComponentOrToc.childrenResults | ||
) | ||
} | ||
}) | ||
|
@@ -596,6 +614,92 @@ function finishWpComponent( | |
} | ||
} else return { ...content, errors } | ||
}) | ||
.with("owid/summary", () => { | ||
// Summaries can either be lists of anchor links, or paragraphs of text | ||
// If it's a paragraph of text, we want to turn it into a callout block | ||
// If it's a list of anchor links, we want to turn it into a toc block | ||
Comment on lines
+618
to
+620
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When a summary block is both text and list (e.g. https://ourworldindata.org/vaccination), both conditions get skipped below and the summary gets stripped out of the output. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, we don't really have a way to render a similar component in Gdocs. Instead we'll track it and deal with it manually. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah right, I'd missed the last error |
||
const contentIsAllText = | ||
content.content.find( | ||
(block) => "type" in block && block.type !== "text" | ||
) === undefined | ||
|
||
if (contentIsAllText) { | ||
const callout: EnrichedBlockCallout = { | ||
type: "callout", | ||
title: "Summary", | ||
text: content.content as EnrichedBlockText[], | ||
parseErrors: [], | ||
} | ||
return { errors: [], content: [callout] } | ||
} | ||
|
||
const contentIsList = | ||
content.content.length === 1 && | ||
"type" in content.content[0] && | ||
content.content[0].type === "list" | ||
if (contentIsList) { | ||
const listItems = get(content, ["content", 0, "items"]) | ||
const items: EnrichedBlockEntrySummaryItem[] = [] | ||
const errors = content.errors | ||
if (isArray(listItems)) { | ||
listItems.forEach((item) => { | ||
if (item.type === "text") { | ||
const value = item.value[0] | ||
if (checkNodeIsSpanLink(value)) { | ||
const { hash } = Url.fromURL(value.url) | ||
const text = spansToUnformattedPlainText( | ||
value.children | ||
) | ||
if (text.includes("DataValue")) { | ||
errors.push({ | ||
name: "summary item has DataValue", | ||
details: text, | ||
}) | ||
} | ||
items.push({ | ||
// Remove "#" from the beginning of the slug | ||
slug: hash.slice(1), | ||
text: text, | ||
}) | ||
} else { | ||
errors.push({ | ||
name: "summary item doesn't have link", | ||
details: value | ||
? `spanType is ${value.spanType}` | ||
: "No item", | ||
}) | ||
} | ||
} else { | ||
errors.push({ | ||
name: "summary item isn't text", | ||
details: `item is type: ${item.type}`, | ||
}) | ||
} | ||
}) | ||
} | ||
const toc: EnrichedBlockEntrySummary = { | ||
type: "entry-summary", | ||
items, | ||
parseErrors: [], | ||
} | ||
return { errors: [], content: [toc] } | ||
} | ||
|
||
const error: BlockParseError = { | ||
name: "Unknown content type inside summary block", | ||
details: | ||
"Unknown summary content: " + | ||
content.content | ||
.map((block) => | ||
"type" in block ? block.type : block.tagName | ||
) | ||
.join(", "), | ||
} | ||
return { | ||
errors: [error], | ||
content: [], | ||
} | ||
}) | ||
.otherwise(() => { | ||
return { | ||
errors: [ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do you mind explaining how you generated this list of slugs?