add chunk content debugging to identify duplication source

This commit is contained in:
Russell Ballestrini 2025-07-11 22:10:46 -04:00
parent 60743c5bfb
commit e2c211b9c6

View file

@ -645,7 +645,8 @@ export async function translateHTML(htmlContent, targetLanguage, sourceUrl = nul
// Debug: log chunk content to see what we're actually translating
chunks.forEach((chunk, index) => {
console.log(`Chunk ${index + 1} preview:`, chunk.substring(0, 200) + (chunk.length > 200 ? '...' : ''));
console.log(`Chunk ${index + 1} length: ${chunk.length} chars`);
console.log(`Chunk ${index + 1} preview:`, chunk.substring(0, 400) + (chunk.length > 400 ? '...' : ''));
});
// Process chunks in batches to avoid overwhelming the API
@ -659,6 +660,7 @@ export async function translateHTML(htmlContent, targetLanguage, sourceUrl = nul
const batchPromises = batch.map((chunk, batchIndex) => {
const chunkIndex = i + batchIndex + 1;
console.log(`Translating chunk ${chunkIndex}/${chunks.length}`);
console.log(`Chunk ${chunkIndex} content preview:`, chunk.substring(0, 300));
return translateFragment(chunk, targetLanguage);
});
@ -679,9 +681,12 @@ export async function translateHTML(htmlContent, targetLanguage, sourceUrl = nul
const originalDoc = originalParser.parseFromString(processedContent, 'text/html');
// Extract only the body content from each translated chunk to avoid stacking full HTML documents
const cleanedChunks = translatedChunks.map(chunk => {
const cleanedChunks = translatedChunks.map((chunk, index) => {
console.log(`Raw translated chunk ${index + 1} preview:`, chunk.substring(0, 500) + '...');
const chunkDoc = new DOMParser().parseFromString(chunk, 'text/html');
return chunkDoc.body ? chunkDoc.body.innerHTML : chunk;
const bodyContent = chunkDoc.body ? chunkDoc.body.innerHTML : chunk;
console.log(`Extracted body content ${index + 1} preview:`, bodyContent.substring(0, 300) + '...');
return bodyContent;
});
// Replace the body content with reassembled translated chunks