Moved things and added title extraction
This commit is contained in:
@@ -6,6 +6,7 @@ import { insertUrl } from "../db/urls"
|
||||
import { advancedUrlSchema, urlFormSchema } from "../schema/url"
|
||||
import { deleteUrl as deleteUrlDb } from "../db/urls"
|
||||
import { revalidatePath } from "next/cache"
|
||||
import { getWebsiteTitle } from "../websiteTitle"
|
||||
|
||||
type Response = {
|
||||
error: boolean
|
||||
@@ -34,6 +35,7 @@ export async function addUrl(unsafeData: unknown): Promise<Response> {
|
||||
await insertUrl({
|
||||
...data,
|
||||
slug: data.slug.length === 0 ? undefined : data.slug,
|
||||
title: await getWebsiteTitle(data.url)
|
||||
})
|
||||
|
||||
revalidatePath("/dashboard")
|
||||
@@ -66,7 +68,7 @@ export async function createAdvanceUrl(unsafeData: unknown): Promise<Response> {
|
||||
await insertUrl({
|
||||
...data,
|
||||
slug: data.slug?.length === 0 ? undefined : data.slug,
|
||||
title: data.title?.length === 0 ? undefined : data.title,
|
||||
title: data.title?.length === 0 ? await getWebsiteTitle(data.url) : data.title,
|
||||
maxVisits: data.maxVisits > 0 ? data.maxVisits : undefined,
|
||||
})
|
||||
|
||||
|
||||
31
src/lib/websiteTitle.ts
Normal file
31
src/lib/websiteTitle.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
export async function getWebsiteTitle(url: string): Promise<string | null> {
|
||||
const res = await fetch(url, { redirect: 'follow' })
|
||||
|
||||
if (!res.ok) {
|
||||
return null
|
||||
}
|
||||
|
||||
const contentType = res.headers.get('content-type')
|
||||
if (!contentType || !contentType.includes('text/html')) {
|
||||
return null
|
||||
}
|
||||
|
||||
const html = await res.text()
|
||||
|
||||
const titleMatch = html.match(/<title[^>]*>([^<]*)<\/title>/i)
|
||||
|
||||
if (titleMatch && titleMatch[1]) {
|
||||
const title = titleMatch[1]
|
||||
.replace(/</g, '<')
|
||||
.replace(/>/g, '>')
|
||||
.replace(/&/g, '&')
|
||||
.replace(/"/g, '"')
|
||||
.replace(/'/g, "'")
|
||||
.replace(/ /g, ' ')
|
||||
.trim()
|
||||
|
||||
return title || null
|
||||
}
|
||||
|
||||
return null
|
||||
}
|
||||
Reference in New Issue
Block a user