diff --git a/src/app/actions.ts b/src/app/actions.ts index 152e2a5..538108d 100644 --- a/src/app/actions.ts +++ b/src/app/actions.ts @@ -15,7 +15,7 @@ export interface HeadlineNode { children: HeadlineNode[]; } -export async function extractMetaData(url: string) { +export async function extractMetaData(url: string, keyword?: string) { if (!url) { return { error: "URL is required." }; } @@ -112,6 +112,15 @@ export async function extractMetaData(url: string) { path.push(node); }); + let keywordCount: number | null = null; + const trimmedKeyword = keyword?.trim(); + if (trimmedKeyword) { + const bodyText = $("body").text(); + const regex = new RegExp(trimmedKeyword, "gi"); + const matches = bodyText.match(regex); + keywordCount = matches ? matches.length : 0; + } + return { data: { title, @@ -119,6 +128,8 @@ export async function extractMetaData(url: string) { image, faq: faqData.length > 0 ? faqData : null, headlines: headlines.length > 0 ? headlines : null, + keyword: trimmedKeyword || null, + keywordCount, }, }; } catch (error) { diff --git a/src/components/meta-form.tsx b/src/components/meta-form.tsx index 4d4e768..4ea6ebb 100644 --- a/src/components/meta-form.tsx +++ b/src/components/meta-form.tsx @@ -5,7 +5,15 @@ import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Textarea } from "@/components/ui/textarea"; import { Card, CardContent } from "@/components/ui/card"; -import { Globe, Edit, Check, Loader2, X, ImageOff } from "lucide-react"; +import { + Globe, + Edit, + Check, + Loader2, + X, + ImageOff, + Search, +} from "lucide-react"; import { extractMetaData, type HeadlineNode } from "@/app/actions"; import { LengthIndicator } from "./length-indicator"; import { CopyButton } from "./copy-button"; @@ -21,10 +29,13 @@ interface MetaData { image?: string | null; faq?: { question: string; answer: string }[] | null; headlines?: HeadlineNode[] | null; + keyword?: string | null; + keywordCount?: number | null; } export function MetaForm() { const [url, setUrl] = useState(""); + const [keyword, setKeyword] = useState(""); const [loading, setLoading] = useState(false); const [error, setError] = useState(null); const [metaData, setMetaData] = useState(null); @@ -53,7 +64,7 @@ export function MetaForm() { setIsEditingDescription(false); setImageError(false); - const result = await extractMetaData(url); + const result = await extractMetaData(url, keyword); if (result.error) { setError(result.error); @@ -66,6 +77,7 @@ export function MetaForm() { const handleClear = () => { setUrl(""); + setKeyword(""); setLoading(false); setError(null); setMetaData(null); @@ -89,6 +101,17 @@ export function MetaForm() { onChange={(e) => setUrl(e.target.value)} /> +
+ + setKeyword(e.target.value)} + /> +