Moved things and added title extraction

This commit is contained in:
2025-06-27 14:30:39 +02:00
parent 50cc087bed
commit e4c382a4dc
18 changed files with 43 additions and 13 deletions

31
src/lib/websiteTitle.ts Normal file
View 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(/&lt;/g, '<')
.replace(/&gt;/g, '>')
.replace(/&amp;/g, '&')
.replace(/&quot;/g, '"')
.replace(/&#39;/g, "'")
.replace(/&nbsp;/g, ' ')
.trim()
return title || null
}
return null
}