Spaces:
Sleeping
Sleeping
| import { JSDOM } from 'jsdom' | |
| import { Readability } from '@mozilla/readability' | |
| export async function extractContent(page, format = 'text') { | |
| const [html, title, url] = await Promise.all([ | |
| page.content(), | |
| page.title(), | |
| Promise.resolve(page.url()), | |
| ]) | |
| const dom = new JSDOM(html, { url }) | |
| const reader = new Readability(dom.window.document) | |
| const article = reader.parse() | |
| if (!article) { | |
| // Readability gave up — grab raw body text as fallback | |
| const text = await page.evaluate(() => document.body?.innerText ?? '') | |
| if (format === 'json') { | |
| return { url, title, text, excerpt: text.slice(0, 300), source: 'fallback' } | |
| } | |
| return text | |
| } | |
| if (format === 'json') { | |
| return { | |
| url, | |
| title: article.title, | |
| byline: article.byline, | |
| siteName: article.siteName, | |
| excerpt: article.excerpt, | |
| length: article.length, | |
| content: article.textContent.trim(), | |
| html: article.content, | |
| publishedTime: article.publishedTime ?? null, | |
| source: 'readability', | |
| } | |
| } | |
| return article.textContent.trim() | |
| } | |