[dyad] Added link analysis feature - wrote 4 file(s)

This commit is contained in:
[dyad]
2026-01-20 16:21:07 +01:00
parent e5d6580987
commit a04c53bae4
4 changed files with 193 additions and 2 deletions

View File

@@ -21,6 +21,13 @@ export interface ImageAltData {
size: number | null;
}
export interface LinkData {
href: string;
text: string;
type: "internal" | "external" | "anchor" | "other";
rel: string;
}
export async function extractMetaData(url: string, keyword?: string) {
if (!url) {
return { error: "URL is required." };
@@ -175,7 +182,6 @@ export async function extractMetaData(url: string, keyword?: string) {
const imageSizePromises = imageSrcs.map(async (img) => {
try {
// Use a HEAD request for efficiency
const res = await fetch(img.src, { method: "HEAD" });
if (res.ok) {
const contentLength = res.headers.get("content-length");
@@ -192,6 +198,33 @@ export async function extractMetaData(url: string, keyword?: string) {
const imageAltData: ImageAltData[] = await Promise.all(imageSizePromises);
const links: LinkData[] = [];
const pageUrl = new URL(formattedUrl);
$("a").each((i, el) => {
const href = $(el).attr("href");
if (!href) return;
const text = $(el).text().trim();
const rel = $(el).attr("rel") || "";
let type: LinkData["type"] = "external";
let absoluteUrl = href;
try {
const linkUrl = new URL(href, formattedUrl);
absoluteUrl = linkUrl.href;
if (linkUrl.hostname === pageUrl.hostname) {
type = "internal";
}
} catch (e) {
if (href.startsWith("#")) type = "anchor";
else if (href.startsWith("mailto:") || href.startsWith("tel:"))
type = "other";
}
links.push({ href: absoluteUrl, text, type, rel });
});
return {
data: {
title,
@@ -207,6 +240,7 @@ export async function extractMetaData(url: string, keyword?: string) {
keyword: trimmedKeyword || null,
keywordCount,
images: imageAltData.length > 0 ? imageAltData : null,
links: links.length > 0 ? links : null,
},
};
} catch (error) {