import { htmlToMarkdown } from "../internal/html-utils.ts"; export type MarkdownFallbackInput = { html: string; markdown: string; title: string; description?: string | null; url?: string; }; /** * Preserve an authoritative title and description when a source serves an * HTML shell without a readable body. This prevents an otherwise successful * crawl from becoming an empty record downstream. */ export function ensureCrawlMarkdown(input: MarkdownFallbackInput): string { if (input.markdown.trim().length >= 50) return input.markdown; const converted = htmlToMarkdown(input.html); if (converted.trim().length >= 50) return converted; const title = input.title.trim() || sourceTitle(input.url); return [`# ${title}`, input.description?.trim() ?? ""] .filter(Boolean) .join("\n\n"); } function sourceTitle(url?: string): string { try { return new URL(url ?? "").hostname || "Crawled source"; } catch { return "Crawled source"; } }