Fix automation syntax errors and add infographics support

Critical fixes:
- Enhanced escaping function to handle special characters ($, `, ", etc.)
- Added infographics field support to extraction scripts
- Prevents build failures from malformed TypeScript generation

This fixes the "Expected } but found enablement" error that occurred
when processing the 15 new articles.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
kbanc85 2025-11-06 09:13:21 -05:00
parent b1ef96c48b
commit a81a78fbf5
2 changed files with 33 additions and 4 deletions

View File

@ -17,15 +17,24 @@ interface NewClaimInput {
originalUrl: string;
quote: string;
keyStatistics: Array<{ stat: string; context: string }>;
infographics: Array<{
filename: string;
alt: string;
caption?: string;
}>;
supportingContext: string;
}
function escapeForTypeScript(str: string): string {
return str
.replace(/\\/g, '\\\\')
.replace(/'/g, "\\'")
.replace(/\n/g, '\\n')
.replace(/\r/g, '\\r');
.replace(/\\/g, '\\\\') // Escape backslashes first
.replace(/"/g, '\\"') // Escape double quotes
.replace(/'/g, "\\'") // Escape single quotes
.replace(/`/g, '\\`') // Escape backticks
.replace(/\$/g, '\\$') // Escape dollar signs (for template literals)
.replace(/\n/g, '\\n') // Escape newlines
.replace(/\r/g, '\\r') // Escape carriage returns
.replace(/\t/g, '\\t'); // Escape tabs
}
function generateClaimEntry(claim: NewClaimInput): string {
@ -37,6 +46,15 @@ function generateClaimEntry(claim: NewClaimInput): string {
` { stat: "${escapeForTypeScript(stat.stat)}", context: "${escapeForTypeScript(stat.context)}" }`
).join(',\n');
const infographicsArray = claim.infographics.length > 0
? claim.infographics.map(img => {
const caption = img.caption
? `, caption: "${escapeForTypeScript(img.caption)}"`
: '';
return ` { filename: "${escapeForTypeScript(img.filename)}", alt: "${escapeForTypeScript(img.alt)}"${caption} }`;
}).join(',\n')
: '';
return ` {
slug: "${claim.slug}",
title: "${escapeForTypeScript(claim.title)}",
@ -58,6 +76,7 @@ ${claimTitlesArray}
keyStatistics: [
${statsArray}
],
infographics: [${infographicsArray ? '\n' + infographicsArray + '\n ' : ''}],
supportingContext: "${escapeForTypeScript(claim.supportingContext)}",
}`;
}

View File

@ -17,6 +17,11 @@ interface ExtractedClaimData {
originalUrl: string;
quote: string;
keyStatistics: Array<{ stat: string; context: string }>;
infographics: Array<{
filename: string;
alt: string;
caption?: string;
}>;
supportingContext: string;
}
@ -144,6 +149,11 @@ Return ONLY valid JSON, no other text:`;
// Add originalUrl
extractedData.originalUrl = articleUrl;
// Set infographics to empty array (image extraction not yet implemented)
if (!extractedData.infographics) {
extractedData.infographics = [];
}
// Validate required fields
if (!extractedData.claims || extractedData.claims.length !== 5) {
reject(new Error(`Expected 5 claims, got ${extractedData.claims?.length || 0}`));