diff options
| author | Dhravya <[email protected]> | 2024-04-13 09:55:29 -0700 |
|---|---|---|
| committer | Dhravya <[email protected]> | 2024-04-13 09:55:29 -0700 |
| commit | 57e699a6ee35b161cf69aa82bec6c50f114b1055 (patch) | |
| tree | a40d3c983c4d7661797e1c18591019cc09c3f083 /apps/web/src/server | |
| parent | merge (diff) | |
| parent | fix edge case for getting metadata (diff) | |
| download | supermemory-57e699a6ee35b161cf69aa82bec6c50f114b1055.tar.xz supermemory-57e699a6ee35b161cf69aa82bec6c50f114b1055.zip | |
conflicts
Diffstat (limited to 'apps/web/src/server')
| -rw-r--r-- | apps/web/src/server/helpers.ts | 38 |
1 files changed, 20 insertions, 18 deletions
diff --git a/apps/web/src/server/helpers.ts b/apps/web/src/server/helpers.ts index 109fa0c3..8538f743 100644 --- a/apps/web/src/server/helpers.ts +++ b/apps/web/src/server/helpers.ts @@ -1,29 +1,31 @@ +import * as cheerio from "cheerio"; + export async function getMetaData(url: string) { const response = await fetch(url); const html = await response.text(); + const $ = cheerio.load(html); + // Extract the base URL const baseUrl = new URL(url).origin; // Extract title - const titleMatch = html.match(/<title>(.*?)<\/title>/); - const title = titleMatch ? titleMatch[1] : "Title not found"; - - // Extract meta description - const descriptionMatch = html.match( - /<meta name="description" content="(.*?)"\s*\/?>/, - ); - const description = descriptionMatch - ? descriptionMatch[1] - : "Description not found"; - - // Extract favicon - const faviconMatch = html.match( - /<link rel="(?:icon|shortcut icon)" href="(.*?)"\s*\/?>/, - ); - const favicon = faviconMatch - ? faviconMatch[1] - : "https://supermemory.dhr.wtf/web.svg"; + const title = $("title").text().trim(); + + const description = $("meta[name=description]").attr("content") ?? ""; + + const _favicon = + $("link[rel=icon]").attr("href") ?? "https://supermemory.dhr.wtf/web.svg"; + + let favicon = + _favicon.trim().length > 0 + ? _favicon.trim() + : "https://supermemory.dhr.wtf/web.svg"; + if (favicon.startsWith("/")) { + favicon = baseUrl + favicon; + } else if (favicon.startsWith("./")) { + favicon = baseUrl + favicon.slice(1); + } // Prepare the metadata object const metadata = { |