Skip to content

Commit

Permalink
🐛 fix broken parsing when using inline table without {.table} tags
Browse files Browse the repository at this point in the history
  • Loading branch information
ikesau committed Nov 3, 2023
1 parent 878d4b9 commit 8688aaf
Showing 1 changed file with 17 additions and 6 deletions.
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

0 comments on commit 8688aaf

Please sign in to comment.