about summary refs log tree commit diff
path: root/src/lib/strings.ts
diff options
context:
space:
mode:
authorPaul Frazee <pfrazee@gmail.com>2022-11-23 16:29:17 -0600
committerPaul Frazee <pfrazee@gmail.com>2022-11-23 16:29:17 -0600
commit89638dbd188023851e67d818dcd4c23204e3c70c (patch)
tree8599dccb3b2367a9d217fd7ed2e518c907c6148c /src/lib/strings.ts
parenteb106a9758ef41515b2dc55bfc5f0f9c1bad6bb8 (diff)
downloadvoidsky-89638dbd188023851e67d818dcd4c23204e3c70c.tar.zst
Implement a link metadata fetching util function
Diffstat (limited to 'src/lib/strings.ts')
-rw-r--r--src/lib/strings.ts51
1 files changed, 51 insertions, 0 deletions
diff --git a/src/lib/strings.ts b/src/lib/strings.ts
index 480dfdcc3..74f4ea6ad 100644
--- a/src/lib/strings.ts
+++ b/src/lib/strings.ts
@@ -220,3 +220,54 @@ export function convertBskyAppUrlIfNeeded(url: string): string {
   }
   return url
 }
+
+const htmlTitleRegex = /<title>([^<]+)<\/title>/i
+export function extractHtmlMeta(html: string): Record<string, string> {
+  const res: Record<string, string> = {}
+
+  {
+    const match = htmlTitleRegex.exec(html)
+    if (match) {
+      res.title = match[1].trim()
+    }
+  }
+
+  {
+    let metaMatch
+    let propMatch
+    const metaRe = /<meta[\s]([^>]+)>/gis
+    while ((metaMatch = metaRe.exec(html))) {
+      let propName
+      let propValue
+      const propRe = /(name|property|content)="([^"]+)"/gis
+      while ((propMatch = propRe.exec(metaMatch[1]))) {
+        if (propMatch[1] === 'content') {
+          propValue = propMatch[2]
+        } else {
+          propName = propMatch[2]
+        }
+      }
+      if (!propName || !propValue) {
+        continue
+      }
+      switch (propName?.trim()) {
+        case 'title':
+        case 'og:title':
+        case 'twitter:title':
+          res.title = propValue?.trim()
+          break
+        case 'description':
+        case 'og:description':
+        case 'twitter:description':
+          res.description = propValue?.trim()
+          break
+        case 'og:image':
+        case 'twitter:image':
+          res.image = propValue?.trim()
+          break
+      }
+    }
+  }
+
+  return res
+}