Supercharge your AI agents with data from the web and beyond. Building the library for superintelligence. 🔥
TypeScript aiai-agentsai-crawlerai-scrapingai-searchcrawlerdata-extractionhtml-to-markdownllmmarkdownscraperscrapingweb-crawlerweb-dataweb-data-extractionweb-scraperweb-scrapingweb-searchwebscraping
Fix: Concatenate metadata arrays into strings with exceptions (#1574)
devin-ai-integration[bot]a5a915d63924
diff --git a/apps/api/sharedLibs/html-transformer/src/lib.rs b/apps/api/sharedLibs/html-transformer/src/lib.rsindex 8d944f22b..eb3be8b9e 100644--- a/apps/api/sharedLibs/html-transformer/src/lib.rs+++ b/apps/api/sharedLibs/html-transformer/src/lib.rs@@ -128,17 +128,30 @@ pub unsafe extern "C" fn extract_metadata(html: *const libc::c_char) -> *mut libif let Some(content) = attrs.get("content") {if let Some(v) = out.get(name) {match v {- Value::String(_) => {- if name != "title" { // preserve title tag in metadata- out.insert(name.to_string(), Value::Array(vec! [v.clone(), Value::String(content.to_string())]));+ Value::String(existing) => {+ if name == "description" {+ out.insert(name.to_string(), Value::String(format!("{}, {}", existing, content)));+ } else if name != "title" { // preserve title tag in metadata+ out.insert(name.to_string(), Value::Array(vec! [Value::String(existing.clone()), Value::String(content.to_string())]));}},- Value::Array(_) => {- match out.get_mut(name) {- Some(Value::Array(x)) => {- x.push(Value::String(content.to_string()));- },- _ => unreachable!(),+ Value::Array(existing_array) => {+ if name == "description" {+ let mut values: Vec<String> = existing_array.iter()+ .filter_map(|v| match v {+ Value::String(s) => Some(s.clone()),+ _ => None,+ })+ .collect();+ values.push(content.to_string());+ out.insert(name.to_string(), Value::String(values.join(", ")));+ } else {+ match out.get_mut(name) {+ Some(Value::Array(x)) => {+ x.push(Value::String(content.to_string()));+ },+ _ => unreachable!(),+ }}},_ => unreachable!(),diff --git a/apps/api/src/__tests__/snips/metadata-concat.test.ts b/apps/api/src/__tests__/snips/metadata-concat.test.tsnew file mode 100644index 000000000..b02319a85--- /dev/null+++ b/apps/api/src/__tests__/snips/metadata-concat.test.ts@@ -0,0 +1,44 @@+import { extractMetadata } from "../../scraper/scrapeURL/lib/extractMetadata";+import { jest, describe, it, expect } from "@jest/globals";++describe("Metadata concatenation", () => {+ it("should concatenate description field into a string while preserving arrays for other metadata fields", async () => {+ const html = `+ <html>+ <head>+ <meta name="description" content="First description">+ <meta name="description" content="Second description">+ <meta property="og:locale:alternate" content="en_US">+ <meta property="og:locale:alternate" content="fr_FR">+ <meta name="keywords" content="first keyword">+ <meta name="keywords" content="second keyword">+ </head>+ <body></body>+ </html>+ `;++ const meta: any = {+ url: "https://example.com",+ id: "test-id",+ logger: {+ warn: jest.fn(),+ error: jest.fn()+ }+ };++ const metadata = await extractMetadata(meta, html);++ expect(metadata.description).toBeDefined();+ expect(Array.isArray(metadata.description)).toBe(false);+ expect(typeof metadata.description).toBe("string");+ expect(metadata.description).toBe("First description, Second description");++ expect(metadata.ogLocaleAlternate).toBeDefined();+ expect(Array.isArray(metadata.ogLocaleAlternate)).toBe(true);+ expect(metadata.ogLocaleAlternate).toEqual(["en_US", "fr_FR"]);++ expect(metadata.keywords).toBeDefined();+ expect(Array.isArray(metadata.keywords)).toBe(true);+ expect(metadata.keywords).toEqual(["first keyword", "second keyword"]);+ });+});diff --git a/apps/api/src/scraper/scrapeURL/lib/extractMetadata.ts b/apps/api/src/scraper/scrapeURL/lib/extractMetadata.tsindex e0d68534d..61d5ab048 100644--- a/apps/api/src/scraper/scrapeURL/lib/extractMetadata.ts+++ b/apps/api/src/scraper/scrapeURL/lib/extractMetadata.ts@@ -137,12 +137,22 @@ export async function extractMetadata(const content = soup(elem).attr("content");if (name && content) {- if (customMetadata[name] === undefined) {- customMetadata[name] = content;- } else if (Array.isArray(customMetadata[name])) {- (customMetadata[name] as string[]).push(content);+ if (name === "description") {+ if (customMetadata[name] === undefined) {+ customMetadata[name] = content;+ } else {+ customMetadata[name] = Array.isArray(customMetadata[name])+ ? [...customMetadata[name] as string[], content].join(", ")+ : `${customMetadata[name]}, ${content}`;+ }} else {- customMetadata[name] = [customMetadata[name] as string, content];+ if (customMetadata[name] === undefined) {+ customMetadata[name] = content;+ } else if (Array.isArray(customMetadata[name])) {+ (customMetadata[name] as string[]).push(content);+ } else {+ customMetadata[name] = [customMetadata[name] as string, content];+ }}}} catch (error) {