[dyad] Added FAQ structured data extraction - wrote 4 file(s)
This commit is contained in:
@@ -2,6 +2,11 @@
|
|||||||
|
|
||||||
import * as cheerio from "cheerio";
|
import * as cheerio from "cheerio";
|
||||||
|
|
||||||
|
interface FaqItem {
|
||||||
|
question: string;
|
||||||
|
answer: string;
|
||||||
|
}
|
||||||
|
|
||||||
export async function extractMetaData(url: string) {
|
export async function extractMetaData(url: string) {
|
||||||
if (!url) {
|
if (!url) {
|
||||||
return { error: "URL is required." };
|
return { error: "URL is required." };
|
||||||
@@ -37,7 +42,45 @@ export async function extractMetaData(url: string) {
|
|||||||
"No description found";
|
"No description found";
|
||||||
const image = $('meta[property="og:image"]').attr("content") || null;
|
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) {
|
} catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
if (error instanceof Error && error.message.includes("Invalid URL")) {
|
if (error instanceof Error && error.message.includes("Invalid URL")) {
|
||||||
|
|||||||
33
src/components/faq-display.tsx
Normal file
33
src/components/faq-display.tsx
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
"use client";
|
||||||
|
|
||||||
|
import {
|
||||||
|
Accordion,
|
||||||
|
AccordionContent,
|
||||||
|
AccordionItem,
|
||||||
|
AccordionTrigger,
|
||||||
|
} from "@/components/ui/accordion";
|
||||||
|
|
||||||
|
interface FaqDisplayProps {
|
||||||
|
faqs: {
|
||||||
|
question: string;
|
||||||
|
answer: string;
|
||||||
|
}[];
|
||||||
|
}
|
||||||
|
|
||||||
|
export function FaqDisplay({ faqs }: FaqDisplayProps) {
|
||||||
|
return (
|
||||||
|
<div>
|
||||||
|
<h3 className="font-semibold text-card-foreground mb-2">
|
||||||
|
FAQ Structured Data
|
||||||
|
</h3>
|
||||||
|
<Accordion type="single" collapsible className="w-full">
|
||||||
|
{faqs.map((faq, index) => (
|
||||||
|
<AccordionItem value={`item-${index}`} key={index}>
|
||||||
|
<AccordionTrigger>{faq.question}</AccordionTrigger>
|
||||||
|
<AccordionContent>{faq.answer}</AccordionContent>
|
||||||
|
</AccordionItem>
|
||||||
|
))}
|
||||||
|
</Accordion>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -11,11 +11,13 @@ import { LengthIndicator } from "./length-indicator";
|
|||||||
import { CopyButton } from "./copy-button";
|
import { CopyButton } from "./copy-button";
|
||||||
import { SerpPreview } from "./serp-preview";
|
import { SerpPreview } from "./serp-preview";
|
||||||
import { ResultsSkeleton } from "./results-skeleton";
|
import { ResultsSkeleton } from "./results-skeleton";
|
||||||
|
import { FaqDisplay } from "./faq-display";
|
||||||
|
|
||||||
interface MetaData {
|
interface MetaData {
|
||||||
title: string;
|
title: string;
|
||||||
description: string;
|
description: string;
|
||||||
image?: string | null;
|
image?: string | null;
|
||||||
|
faq?: { question: string; answer: string }[] | null;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function MetaForm() {
|
export function MetaForm() {
|
||||||
@@ -159,6 +161,10 @@ export function MetaForm() {
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
|
{metaData.faq && metaData.faq.length > 0 && (
|
||||||
|
<FaqDisplay faqs={metaData.faq} />
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
<div className="space-y-6">
|
<div className="space-y-6">
|
||||||
<div>
|
<div>
|
||||||
|
|||||||
@@ -31,6 +31,14 @@ export function ResultsSkeleton() {
|
|||||||
<Skeleton className="h-5 w-32 mb-2" />
|
<Skeleton className="h-5 w-32 mb-2" />
|
||||||
<Skeleton className="aspect-video w-full rounded-md" />
|
<Skeleton className="aspect-video w-full rounded-md" />
|
||||||
</div>
|
</div>
|
||||||
|
<div>
|
||||||
|
<Skeleton className="h-5 w-40 mb-3" />
|
||||||
|
<div className="space-y-2">
|
||||||
|
<Skeleton className="h-10 w-full" />
|
||||||
|
<Skeleton className="h-10 w-full" />
|
||||||
|
<Skeleton className="h-10 w-full" />
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div className="space-y-6">
|
<div className="space-y-6">
|
||||||
<div>
|
<div>
|
||||||
|
|||||||
Reference in New Issue
Block a user