diff options
| author | yxshv <[email protected]> | 2024-04-13 20:49:56 +0530 |
|---|---|---|
| committer | yxshv <[email protected]> | 2024-04-13 20:49:56 +0530 |
| commit | 68ee684a1a33fb654796eab210d8974a42349120 (patch) | |
| tree | 54ef1504c4e75e74dc8d636cc0b69d2f34f7136a /apps/web/src/server | |
| parent | space add (diff) | |
| download | supermemory-68ee684a1a33fb654796eab210d8974a42349120.tar.xz supermemory-68ee684a1a33fb654796eab210d8974a42349120.zip | |
fix favicons
Diffstat (limited to 'apps/web/src/server')
| -rw-r--r-- | apps/web/src/server/helpers.ts | 28 |
1 files changed, 12 insertions, 16 deletions
diff --git a/apps/web/src/server/helpers.ts b/apps/web/src/server/helpers.ts index 109fa0c3..a20474b2 100644 --- a/apps/web/src/server/helpers.ts +++ b/apps/web/src/server/helpers.ts @@ -1,29 +1,25 @@ +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"; + const title = $('title').text().trim() - // Extract meta description - const descriptionMatch = html.match( - /<meta name="description" content="(.*?)"\s*\/?>/, - ); - const description = descriptionMatch - ? descriptionMatch[1] - : "Description not found"; + const description = $('meta[name=description]').attr('content') ?? '' - // 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 _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 + } // Prepare the metadata object const metadata = { |