-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: allow for quiz exports to handle images
- Loading branch information
1 parent
b4f24d8
commit 44f8c5d
Showing
4 changed files
with
170 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import type { docs_v1 } from "@googleapis/docs"; | ||
|
||
/** | ||
* Helper to find the index of a placeholder in the document. | ||
*/ | ||
export async function findPlaceholderIndex( | ||
googleDocs: docs_v1.Docs, | ||
documentId: string, | ||
placeholder: string, | ||
): Promise<number | null> { | ||
const doc = await googleDocs.documents.get({ documentId }); | ||
const body = doc.data.body?.content; | ||
|
||
if (!body) return null; | ||
|
||
for (const element of body) { | ||
if (element.table) { | ||
for (const row of element.table.tableRows || []) { | ||
for (const cell of row.tableCells || []) { | ||
for (const cellElement of cell.content || []) { | ||
if (cellElement.paragraph?.elements) { | ||
for (const textElement of cellElement.paragraph.elements) { | ||
if (textElement.textRun?.content?.includes(placeholder)) { | ||
return textElement.startIndex ?? null; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
if (element.paragraph?.elements) { | ||
for (const textElement of element.paragraph.elements) { | ||
if (textElement.textRun?.content?.includes(placeholder)) { | ||
return textElement.startIndex ?? null; | ||
} | ||
} | ||
} | ||
} | ||
return null; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
packages/exports/src/gSuite/docs/processImageReplacements.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import type { docs_v1 } from "@googleapis/docs"; | ||
|
||
import { findPlaceholderIndex } from "./findPlaceholderIndex"; | ||
|
||
export async function processImageReplacements< | ||
Data extends Record<string, string | string[] | null | undefined>, | ||
>({ | ||
googleDocs, | ||
documentId, | ||
data, | ||
}: { | ||
googleDocs: docs_v1.Docs; | ||
documentId: string; | ||
data: Data; | ||
}) { | ||
// The method here is too locate the placeholder in the documents, delete it, and insert the image. | ||
for (const [key, value] of Object.entries(data)) { | ||
if (typeof value === "string" && value.includes("![image](")) { | ||
const imageUrl = value.match(/!\[.*?\]\((.*?)\)/)?.[1]; | ||
if (!imageUrl) continue; | ||
|
||
const placeholder = `{{${key}}}`; | ||
const index = await findPlaceholderIndex( | ||
googleDocs, | ||
documentId, | ||
placeholder, | ||
); | ||
|
||
if (index !== null) { | ||
await googleDocs.documents.batchUpdate({ | ||
documentId, | ||
requestBody: { | ||
requests: [ | ||
{ | ||
deleteContentRange: { | ||
range: { | ||
startIndex: index, | ||
endIndex: index + placeholder.length, | ||
}, | ||
}, | ||
}, | ||
], | ||
}, | ||
}); | ||
|
||
await googleDocs.documents.batchUpdate({ | ||
documentId, | ||
requestBody: { | ||
requests: [ | ||
{ | ||
insertInlineImage: { | ||
uri: imageUrl, | ||
location: { | ||
segmentId: null, | ||
index, | ||
}, | ||
}, | ||
}, | ||
], | ||
}, | ||
}); | ||
} | ||
} | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
packages/exports/src/gSuite/docs/processTextReplacements.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import type { docs_v1 } from "@googleapis/docs"; | ||
|
||
import type { ValueToString } from "../../utils"; | ||
|
||
export async function processTextReplacements< | ||
Data extends Record<string, string | string[] | null | undefined>, | ||
>({ | ||
googleDocs, | ||
documentId, | ||
data, | ||
missingData, | ||
warnIfMissing, | ||
valueToString, | ||
}: { | ||
googleDocs: docs_v1.Docs; | ||
documentId: string; | ||
data: Data; | ||
missingData: string[]; | ||
warnIfMissing: (keyof Data)[]; | ||
valueToString: ValueToString<Data>; | ||
}) { | ||
for (const [key, value] of Object.entries(data)) { | ||
const valueStr = valueToString(key, value); | ||
|
||
// check if the value is empty and mark as missing if needed | ||
if (!valueStr.trim() && warnIfMissing.includes(key)) { | ||
missingData.push(key); | ||
} | ||
|
||
// text replacement logic | ||
if (typeof value !== "string" || !value.includes("![image](")) { | ||
await googleDocs.documents.batchUpdate({ | ||
documentId, | ||
requestBody: { | ||
requests: [ | ||
{ | ||
replaceAllText: { | ||
replaceText: valueStr, | ||
containsText: { | ||
text: `{{${key}}}`, | ||
matchCase: false, | ||
}, | ||
}, | ||
}, | ||
], | ||
}, | ||
}); | ||
} | ||
} | ||
} |