diff --git a/api/link-preview.js b/api/link-preview.js new file mode 100644 index 0000000..3f8f338 --- /dev/null +++ b/api/link-preview.js @@ -0,0 +1,128 @@ +import { jsonResponse, serverError } from '../server/json.js'; + +const MAX_HTML_LENGTH = 180000; +const REQUEST_TIMEOUT_MS = 6000; +const PREVIEW_CACHE_TTL_MS = 1000 * 60 * 60 * 12; +const previewCache = new Map(); + +function extractMetaTag(html, attribute, value) { + const pattern = new RegExp(`]*${attribute}=["']${value}["'][^>]*content=["']([^"']+)["'][^>]*>`, 'i'); + const match = html.match(pattern); + return match ? decodeHtml(match[1].trim()) : ''; +} + +function extractTitle(html) { + const match = html.match(/