-
Notifications
You must be signed in to change notification settings - Fork 0
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
feat: allow for quiz exports to handle images #357
Closed
Closed
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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, | ||
}, | ||
}, | ||
}, | ||
], | ||
}, | ||
}); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.