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

Smaller chromeless explorer embeds in articles & a fix for native tables crashing #2892

Merged
merged 2 commits into from
Nov 6, 2023
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
23 changes: 17 additions & 6 deletions db/model/Gdoc/gdocToArchie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { match, P } from "ts-pattern"

function paragraphToString(
paragraph: docs_v1.Schema$Paragraph,
context: { isInList: boolean }
context: { isInList: boolean; isInTable: boolean }
): string {
let text = ""

Expand Down Expand Up @@ -76,7 +76,13 @@ function paragraphToString(
elementText += `${prefix}${fragmentText}`
idx++
}
text += taggedText(elementText)
const nextText = taggedText(elementText)
if (nextText === "{.table}\n") {
context.isInTable = true
} else if (context.isInTable && nextText === "{}\n") {
context.isInTable = false
}
text += nextText
}
return text
}
Expand Down Expand Up @@ -104,7 +110,9 @@ function tableToString(
value: [],
}
const { content = [] } = tableCell
const context = { isInList: false }
// Yes, we're in a table" here, but this boolean is to track whether we should parse Gdocs tables
// in the "top level" of the document, not once we're inside a table already
const context = { isInList: false, isInTable: false }
for (const item of content) {
if (item.paragraph) {
const text = paragraphToString(item.paragraph, context)
Expand All @@ -127,7 +135,7 @@ function tableToString(
for (const row of rows) {
text += `\n${OwidRawGdocBlockToArchieMLString(row)}`
}
text += "\n[]"
text += "\n[]\n"
return text
}

Expand All @@ -136,7 +144,7 @@ export async function gdocToArchie(
): Promise<{ text: string }> {
// prepare the text holder
let text = ""
const context = { isInList: false }
const context = { isInList: false, isInTable: false }

// check if the body key and content key exists, and give up if not
if (!document.body) return { text }
Expand All @@ -147,7 +155,10 @@ export async function gdocToArchie(
if (element.paragraph) {
text += paragraphToString(element.paragraph, context)
} else if (element.table) {
text += tableToString(element.table)
// Skip parsing the Gdoc table if it's not enclosed in a {.table} tag
if (context.isInTable) {
text += tableToString(element.table)
}
}
}
return { text }
Expand Down
8 changes: 5 additions & 3 deletions site/gdocs/ArticleBlock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ const layouts: { [key in Container]: Layouts} = {
["chart"]: "col-start-4 span-cols-8 col-md-start-3 span-md-cols-10 span-sm-cols-12 col-sm-start-2",
["default"]: "col-start-5 span-cols-6 col-md-start-3 span-md-cols-10 span-sm-cols-12 col-sm-start-2",
["divider"]: "col-start-2 span-cols-12",
["explorer"]: "col-start-2 span-cols-12 span-md-cols-12 col-md-start-2",
["explorer"]: "col-start-2 span-cols-12",
["gray-section"]: "span-cols-14 grid grid-cols-12-full-width",
["heading"]: "col-start-5 span-cols-6 col-md-start-3 span-md-cols-10 span-sm-cols-12 col-sm-start-2",
["horizontal-rule"]: "col-start-5 span-cols-6 col-md-start-3 span-md-cols-10 span-sm-cols-12 col-sm-start-2",
Expand Down Expand Up @@ -186,8 +186,10 @@ export default function ArticleBlock({
/>
))
.with({ type: "chart" }, (block) => {
const { isExplorer } = Url.fromURL(block.url)
const layoutSubtype = isExplorer ? "explorer" : "chart"
const { isExplorer, queryStr } = Url.fromURL(block.url)
const areControlsHidden = queryStr.includes("hideControls=true")
const layoutSubtype =
isExplorer && !areControlsHidden ? "explorer" : "chart"
return (
<Chart
className={getLayout(layoutSubtype, containerType)}
Expand Down
Loading