feat(ingest): readability wrapper

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
root
2026-06-01 03:34:51 +10:00
parent 8d2afcd040
commit c6e72e93d5
2 changed files with 41 additions and 0 deletions

16
lib/ingest/readability.js Normal file
View File

@@ -0,0 +1,16 @@
import { JSDOM } from 'jsdom';
import { Readability } from '@mozilla/readability';
export function extract(html, url) {
const dom = new JSDOM(html, { url });
const reader = new Readability(dom.window.document);
const a = reader.parse();
if (!a) return { title: null, textContent: '', excerpt: null, byline: null, siteName: null };
return {
title: a.title || null,
textContent: (a.textContent || '').trim(),
excerpt: a.excerpt || null,
byline: a.byline || null,
siteName: a.siteName || null
};
}

View File

@@ -0,0 +1,25 @@
import { describe, it, expect } from 'vitest';
import { extract } from '../../lib/ingest/readability.js';
const HTML = `
<html><head><title>Blackflame Notes</title>
<meta property="og:site_name" content="Hynesy"/>
</head><body><article>
<h1>Blackflame Notes</h1>
<p>An essay on the Cradle aesthetic and the blackflame motif. This is a longer paragraph that gives readability enough text to consider this the main content of the page.</p>
<p>A second paragraph also part of the article.</p>
</article></body></html>`;
describe('readability.extract', () => {
it('pulls title and text', () => {
const out = extract(HTML, 'https://example.com/x');
expect(out.title).toMatch(/Blackflame/);
expect(out.textContent).toMatch(/Cradle/);
expect(out.siteName).toBe('Hynesy');
});
it('returns empty struct when nothing parseable', () => {
const out = extract('<html></html>', 'https://example.com');
expect(out.textContent).toBe('');
});
});