Leon4gr45/builder
0
1import { Readability } from '@mozilla/readability';2import TurndownService from 'turndown';3 4let turndown: TurndownService | null = null;5function getTurndown(): TurndownService {6 if (!turndown) turndown = new TurndownService({ headingStyle: 'atx', codeBlockStyle: 'fenced' });7 return turndown;8}9 10// Minimum readable-text length before we trust Readability's extraction over11// the raw document body. Short fixtures/pages get low-quality Readability12// output (heading demotion, URL normalization), so fall back to the raw body.13const MIN_READABLE_LENGTH = 200;14 15export function htmlToMarkdown(html: string, baseUrl: string): string {16 try {17 const doc = new DOMParser().parseFromString(html, 'text/html');18 // Resolve relative links against the source URL so markdown links work.19 const base = doc.createElement('base');20 base.href = baseUrl;21 doc.head?.appendChild(base);22 const article = new Readability(doc).parse();23 const readableText = article?.textContent?.trim() ?? '';24 const contentHtml =25 article?.content && readableText.length >= MIN_READABLE_LENGTH26 ? article.content27 : doc.body?.innerHTML || html;28 const md = getTurndown().turndown(contentHtml).trim();29 return md || (doc.body?.textContent || '').trim();30 } catch {31 return html;32 }33}34 