-
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.
fix: more flexible and efficient solution for images in docs
- Loading branch information
Showing
8 changed files
with
167 additions
and
173 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
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
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
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,70 @@ | ||
import type { docs_v1 } from "@googleapis/docs"; | ||
|
||
export async function findMarkdownImages( | ||
googleDocs: docs_v1.Docs, | ||
documentId: string, | ||
): Promise<{ url: string; altText: string; startIndex: number }[]> { | ||
const doc = await googleDocs.documents.get({ documentId }); | ||
const body = doc.data.body?.content; | ||
|
||
if (!body) return []; | ||
|
||
const markdownImageRegex = /!\[(.*?)\]\((.*?)\)/g; | ||
const matches: { url: string; altText: string; startIndex: number }[] = []; | ||
|
||
function handleMatch( | ||
match: RegExpExecArray, | ||
textElement: docs_v1.Schema$ParagraphElement, | ||
): void { | ||
const [, altText, url] = match; | ||
const textElementStartIndex = textElement.startIndex; | ||
const matchIndex = match.index; | ||
if ( | ||
typeof textElementStartIndex !== "number" || | ||
typeof matchIndex !== "number" | ||
) { | ||
return; | ||
} | ||
const startIndex = textElementStartIndex + matchIndex; | ||
if (url && typeof altText === "string") { | ||
matches.push({ url, altText, startIndex }); | ||
} | ||
} | ||
|
||
function processTextElements(elements: docs_v1.Schema$ParagraphElement[]) { | ||
for (const textElement of elements) { | ||
const textContent = textElement.textRun?.content ?? ""; | ||
let match: RegExpExecArray | null; | ||
while ((match = markdownImageRegex.exec(textContent)) !== null) { | ||
handleMatch(match, textElement); | ||
} | ||
} | ||
} | ||
|
||
function processTableCells(cells: docs_v1.Schema$TableCell[]) { | ||
for (const cell of cells) { | ||
for (const cellElement of cell.content ?? []) { | ||
if (cellElement.paragraph?.elements) { | ||
processTextElements(cellElement.paragraph.elements); | ||
} | ||
} | ||
} | ||
} | ||
|
||
function processBodyElements(elements: docs_v1.Schema$StructuralElement[]) { | ||
for (const element of elements) { | ||
if (element.table) { | ||
for (const row of element.table.tableRows ?? []) { | ||
processTableCells(row.tableCells ?? []); | ||
} | ||
} | ||
if (element.paragraph?.elements) { | ||
processTextElements(element.paragraph.elements); | ||
} | ||
} | ||
} | ||
|
||
processBodyElements(body); | ||
|
||
return matches; | ||
} |
This file was deleted.
Oops, something went wrong.
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,62 @@ | ||
import type { docs_v1 } from "@googleapis/docs"; | ||
import { aiLogger } from "@oakai/logger"; | ||
|
||
const log = aiLogger("exports"); | ||
|
||
export function imageReplacements( | ||
markdownImages: { url: string; altText: string; startIndex: number }[], | ||
): { requests: docs_v1.Schema$Request[] } { | ||
if (markdownImages.length === 0) { | ||
log.info("No Markdown images to process."); | ||
return { requests: [] }; | ||
} | ||
|
||
let cumulativeShift = 0; // Tracks the total index shift from previous operations | ||
|
||
const requests: docs_v1.Schema$Request[] = []; | ||
|
||
markdownImages.forEach((image) => { | ||
// Construct the full Markdown reference | ||
const markdownImageReference = `![${image.altText}](${image.url})`; | ||
|
||
const markdownLength = markdownImageReference.length; | ||
|
||
// Adjust the start and end index dynamically based on the cumulative shift | ||
const adjustedStartIndex = image.startIndex + cumulativeShift; | ||
const adjustedEndIndex = adjustedStartIndex + markdownLength; | ||
|
||
// Request to delete the exact range of the Markdown reference | ||
requests.push({ | ||
deleteContentRange: { | ||
range: { | ||
startIndex: adjustedStartIndex, | ||
endIndex: adjustedEndIndex, | ||
}, | ||
}, | ||
}); | ||
|
||
// Request to insert the inline image at the adjusted startIndex | ||
requests.push({ | ||
insertInlineImage: { | ||
uri: image.url, | ||
location: { | ||
index: adjustedStartIndex, // Insert at the same startIndex where the Markdown was removed | ||
}, | ||
objectSize: { | ||
height: { | ||
magnitude: 150, | ||
unit: "PT", | ||
}, | ||
width: { | ||
magnitude: 150, | ||
unit: "PT", | ||
}, | ||
}, | ||
}, | ||
}); | ||
const netShift = 1 - markdownLength; // Inline image adds 1, Markdown removes its length | ||
cumulativeShift += netShift; | ||
}); | ||
|
||
return { requests }; | ||
} |
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
120 changes: 0 additions & 120 deletions
120
packages/exports/src/gSuite/docs/processImagesReplacements.ts
This file was deleted.
Oops, something went wrong.