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; originalUrl: string;
quote: string; quote: string;
keyStatistics: Array<{ stat: string; context: string }>; keyStatistics: Array<{ stat: string; context: string }>;
infographics: Array<{
filename: string;
alt: string;
caption?: string;
}>;
supportingContext: string; supportingContext: string;
} }
function escapeForTypeScript(str: string): string { function escapeForTypeScript(str: string): string {
return str return str
.replace(/\\/g, '\\\\') .replace(/\\/g, '\\\\') // Escape backslashes first
.replace(/'/g, "\\'") .replace(/"/g, '\\"') // Escape double quotes
.replace(/\n/g, '\\n') .replace(/'/g, "\\'") // Escape single quotes
.replace(/\r/g, '\\r'); .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 { function generateClaimEntry(claim: NewClaimInput): string {
@ -37,6 +46,15 @@ function generateClaimEntry(claim: NewClaimInput): string {
` { stat: "${escapeForTypeScript(stat.stat)}", context: "${escapeForTypeScript(stat.context)}" }` ` { stat: "${escapeForTypeScript(stat.stat)}", context: "${escapeForTypeScript(stat.context)}" }`
).join(',\n'); ).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 ` { return ` {
slug: "${claim.slug}", slug: "${claim.slug}",
title: "${escapeForTypeScript(claim.title)}", title: "${escapeForTypeScript(claim.title)}",
@ -58,6 +76,7 @@ ${claimTitlesArray}
keyStatistics: [ keyStatistics: [
${statsArray} ${statsArray}
], ],
infographics: [${infographicsArray ? '\n' + infographicsArray + '\n ' : ''}],
supportingContext: "${escapeForTypeScript(claim.supportingContext)}", supportingContext: "${escapeForTypeScript(claim.supportingContext)}",
}`; }`;
} }

View File

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