I only need "Extract article body content", you do not need to extract other things. please fix this code :
function extractNewsDetails(htmlContent, targetTitle) {
// Initialize details object
const details = {
title: targetTitle,
description: '',
image: '',
author: '',
publishedDate: '',
modifiedDate: '',
articleSection: [],
content: ''
};
try {
// Extract meta description
const descriptionMatch = htmlContent.match(/<meta name="description" content="([^"]*)"/i);
if (descriptionMatch && descriptionMatch[1]) {
details.description = descriptionMatch[1];
}
// Extract og:image
const ogImageMatch = htmlContent.match(/<meta property="og:image" content="([^"]*)"/i);
if (ogImageMatch && ogImageMatch[1]) {
details.image = ogImageMatch[1];
}
// Extract author
const authorMatch = htmlContent.match(/<meta property="article:author" content="([^"]*)"/i);
if (authorMatch && authorMatch[1]) {
details.author = authorMatch[1];
}
// Extract published date
const publishedDateMatch = htmlContent.match(/<meta property="article:published_time" content="([^"]*)"/i);
if (publishedDateMatch && publishedDateMatch[1]) {
details.publishedDate = publishedDateMatch[1];
}
// Extract modified date
const modifiedDateMatch = htmlContent.match(/<meta property="article:modified_time" content="([^"]*)"/i);
if (modifiedDateMatch && modifiedDateMatch[1]) {
details.modifiedDate = modifiedDateMatch[1];
}
// Extract article section from JSON-LD
const jsonLdMatch = htmlContent.match(/<script type="application\/ld\+json">([\s\S]*?)<\/script>/i);
if (jsonLdMatch && jsonLdMatch[1]) {
try {
const jsonLd = JSON.parse(jsonLdMatch[1]);
if (jsonLd.articleSection) {
details.articleSection = jsonLd.articleSection;
}
} catch (e) {
console.error('Error parsing JSON-LD:', e);
}
}
// Extract article body content
const articleBodyMatch = htmlContent.match(/<div class="article-body">([\s\S]*?)<\/div>\s*<\/article>/i);
if (articleBodyMatch && articleBodyMatch[1]) {
// Clean up content by removing scripts, styles, ads, and newsletter sections
let content = articleBodyMatch[1]
.replace(/<script[\s\S]*?<\/script>/gi, '') // Remove script tags
.replace(/<style[\s\S]*?<\/style>/gi, '') // Remove style tags
.replace(/<div class="ad-zone-container"[\s\S]*?<\/div>/gi, '') // Remove ad zones
.replace(/<div class="w-newsletter-in_article"[\s\S]*?<\/div>/gi, '') // Remove newsletter sections
.replace(/<[^>]+>/g, '') // Remove all HTML tags
.trim()
.replace(/\s+/g, ' '); // Normalize whitespace
details.content = content;
}
// Verify title exists
const titleRegex = new RegExp(`<h[1-6][^>]*>${targetTitle}</h[1-6]>`, 'i');
if (!titleRegex.test(htmlContent)) {
return { error: "Title not found in the document" };
}
} catch (e) {
return { error: "Error processing HTML content: " + e.message };
}
// Return details as JSON string
return JSON.stringify(details, null, 2);
}
// Main execution for n8n
const htmlContent = $input.first().json.data; // Use the provided source data
const targetTitle = "Virgin Atlantic Trims Flights On 3 Routes To The United States This Winter";
return [{ json: extractNewsDetails(htmlContent, targetTitle) }];