Enhance content parsing and metadata extraction in HTMLContentRenderer and ToggleTableRow
- Improved HTMLContentRenderer to better handle JSON content and extract HTML safely. - Updated ToggleTableRow to robustly extract meta descriptions, including parsing potential JSON strings. - Refactored Content page to conditionally display meta descriptions based on JSON parsing results, enhancing user experience.
This commit is contained in:
@@ -169,12 +169,35 @@ const ToggleMetadata: React.FC<ToggleMetadataProps> = ({ row, contentMetadata })
|
||||
contentMetadata?.metadata?.categories ||
|
||||
[];
|
||||
|
||||
const metaDescription =
|
||||
row.meta_description ||
|
||||
row.content_meta_description ||
|
||||
contentMetadata?.meta_description ||
|
||||
contentMetadata?.metadata?.meta_description ||
|
||||
null;
|
||||
// Extract meta_description, avoiding JSON strings
|
||||
let metaDescription: string | null = null;
|
||||
|
||||
// Try direct fields first
|
||||
if (row.meta_description && typeof row.meta_description === 'string') {
|
||||
metaDescription = row.meta_description;
|
||||
} else if (row.content_meta_description && typeof row.content_meta_description === 'string') {
|
||||
metaDescription = row.content_meta_description;
|
||||
} else if (contentMetadata?.meta_description && typeof contentMetadata.meta_description === 'string') {
|
||||
metaDescription = contentMetadata.meta_description;
|
||||
} else if (contentMetadata?.metadata?.meta_description && typeof contentMetadata.metadata.meta_description === 'string') {
|
||||
metaDescription = contentMetadata.metadata.meta_description;
|
||||
}
|
||||
|
||||
// If metaDescription looks like JSON, try to parse it
|
||||
if (metaDescription && metaDescription.trim().startsWith('{')) {
|
||||
try {
|
||||
const parsed = JSON.parse(metaDescription);
|
||||
// If parsed object has meta_description, use that
|
||||
if (parsed.meta_description && typeof parsed.meta_description === 'string') {
|
||||
metaDescription = parsed.meta_description;
|
||||
} else {
|
||||
// If it's a full JSON response, extract meta_description from it
|
||||
metaDescription = parsed.meta_description || null;
|
||||
}
|
||||
} catch {
|
||||
// Not valid JSON, keep as is
|
||||
}
|
||||
}
|
||||
|
||||
const hasMetadata =
|
||||
primaryKeyword ||
|
||||
@@ -263,18 +286,6 @@ const ToggleMetadata: React.FC<ToggleMetadataProps> = ({ row, contentMetadata })
|
||||
</div>
|
||||
);
|
||||
};
|
||||
<div className="html-content-wrapper">
|
||||
<HTMLContentRenderer
|
||||
content={content}
|
||||
className="text-sm text-gray-700 dark:text-gray-300 leading-relaxed"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
);
|
||||
};
|
||||
|
||||
/**
|
||||
* Toggle Button Component - To be used in table cells
|
||||
|
||||
Reference in New Issue
Block a user