[dyad] Added image alt tag analysis - wrote 3 file(s)
This commit is contained in:
@@ -15,6 +15,11 @@ export interface HeadlineNode {
|
||||
children: HeadlineNode[];
|
||||
}
|
||||
|
||||
export interface ImageAltData {
|
||||
src: string;
|
||||
alt: string;
|
||||
}
|
||||
|
||||
export async function extractMetaData(url: string, keyword?: string) {
|
||||
if (!url) {
|
||||
return { error: "URL is required." };
|
||||
@@ -122,6 +127,24 @@ export async function extractMetaData(url: string, keyword?: string) {
|
||||
keywordCount = matches ? matches.length : 0;
|
||||
}
|
||||
|
||||
const imageAltData: ImageAltData[] = [];
|
||||
$("img").each((i, el) => {
|
||||
const src = $(el).attr("src");
|
||||
const alt = $(el).attr("alt") || "";
|
||||
|
||||
if (src) {
|
||||
try {
|
||||
const absoluteSrc = new URL(src, formattedUrl).href;
|
||||
imageAltData.push({
|
||||
src: absoluteSrc,
|
||||
alt: alt.trim(),
|
||||
});
|
||||
} catch (e) {
|
||||
// Ignore invalid URLs
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
return {
|
||||
data: {
|
||||
title,
|
||||
@@ -131,6 +154,7 @@ export async function extractMetaData(url: string, keyword?: string) {
|
||||
headlines: headlines.length > 0 ? headlines : null,
|
||||
keyword: trimmedKeyword || null,
|
||||
keywordCount,
|
||||
images: imageAltData.length > 0 ? imageAltData : null,
|
||||
},
|
||||
};
|
||||
} catch (error) {
|
||||
|
||||
Reference in New Issue
Block a user