aboutsummaryrefslogtreecommitdiff
path: root/apps/web/lib/get-metadata.ts
diff options
context:
space:
mode:
Diffstat (limited to 'apps/web/lib/get-metadata.ts')
-rw-r--r--apps/web/lib/get-metadata.ts40
1 files changed, 40 insertions, 0 deletions
diff --git a/apps/web/lib/get-metadata.ts b/apps/web/lib/get-metadata.ts
new file mode 100644
index 00000000..4609e49b
--- /dev/null
+++ b/apps/web/lib/get-metadata.ts
@@ -0,0 +1,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;
+}