aboutsummaryrefslogtreecommitdiff
path: root/apps/web/lib/get-metadata.ts
blob: 4609e49b5dcdf5c49b8ce286d7e3bc8bcd6752d0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
"use server";
import * as cheerio from "cheerio";

// TODO: THIS SHOULD PROBABLY ALSO FETCH THE OG-IMAGE
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 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 = {
    title,
    description,
    image: favicon,
    baseUrl,
  };
  return metadata;
}