[dyad] Added FAQ structured data extraction - wrote 4 file(s)

This commit is contained in:
[dyad]
2026-01-20 13:37:56 +01:00
parent 31835cda52
commit 677d7fdb53
4 changed files with 91 additions and 1 deletions

View File

@@ -2,6 +2,11 @@
import * as cheerio from "cheerio";
interface FaqItem {
question: string;
answer: string;
}
export async function extractMetaData(url: string) {
if (!url) {
return { error: "URL is required." };
@@ -37,7 +42,45 @@ export async function extractMetaData(url: string) {
"No description found";
const image = $('meta[property="og:image"]').attr("content") || null;
return { data: { title, description, image } };
const faqData: FaqItem[] = [];
$('script[type="application/ld+json"]').each((i, el) => {
const jsonContent = $(el).html();
if (!jsonContent) return;
try {
const data = JSON.parse(jsonContent);
const graph = data["@graph"] || [data];
for (const item of graph) {
if (item["@type"] === "FAQPage" && Array.isArray(item.mainEntity)) {
item.mainEntity.forEach((qa: any) => {
if (
qa["@type"] === "Question" &&
qa.name &&
qa.acceptedAnswer &&
qa.acceptedAnswer.text
) {
faqData.push({
question: qa.name,
answer: qa.acceptedAnswer.text,
});
}
});
}
}
} catch (e) {
// Ignore parsing errors
}
});
return {
data: {
title,
description,
image,
faq: faqData.length > 0 ? faqData : null,
},
};
} catch (error) {
console.error(error);
if (error instanceof Error && error.message.includes("Invalid URL")) {