|
| 1 | +/** |
| 2 | + * Read page content - supports raw HTML, text, and markdown formats |
| 3 | + */ |
| 4 | + |
| 5 | +import { SentienceBrowser } from './browser'; |
| 6 | +import TurndownService from 'turndown'; |
| 7 | + |
| 8 | +export interface ReadOptions { |
| 9 | + format?: 'raw' | 'text' | 'markdown'; |
| 10 | +} |
| 11 | + |
| 12 | +export interface ReadResult { |
| 13 | + status: 'success' | 'error'; |
| 14 | + url: string; |
| 15 | + format: 'raw' | 'text' | 'markdown'; |
| 16 | + content: string; |
| 17 | + length: number; |
| 18 | + error?: string; |
| 19 | +} |
| 20 | + |
| 21 | +/** |
| 22 | + * Read page content as raw HTML, text, or markdown |
| 23 | + * |
| 24 | + * @param browser - SentienceBrowser instance |
| 25 | + * @param options - Read options |
| 26 | + * @returns ReadResult with page content |
| 27 | + * |
| 28 | + * @example |
| 29 | + * // Get raw HTML (default) |
| 30 | + * const result = await read(browser); |
| 31 | + * const htmlContent = result.content; |
| 32 | + * |
| 33 | + * @example |
| 34 | + * // Get high-quality markdown (uses Turndown internally) |
| 35 | + * const result = await read(browser, { format: 'markdown' }); |
| 36 | + * const markdown = result.content; |
| 37 | + * |
| 38 | + * @example |
| 39 | + * // Get plain text |
| 40 | + * const result = await read(browser, { format: 'text' }); |
| 41 | + * const text = result.content; |
| 42 | + */ |
| 43 | +export async function read( |
| 44 | + browser: SentienceBrowser, |
| 45 | + options: ReadOptions = {} |
| 46 | +): Promise<ReadResult> { |
| 47 | + const page = browser.getPage(); |
| 48 | + const format = options.format || 'raw'; // Default to 'raw' for Turndown compatibility |
| 49 | + |
| 50 | + // For markdown format, get raw HTML first, then convert with Turndown |
| 51 | + if (format === 'markdown') { |
| 52 | + // Get raw HTML from extension |
| 53 | + const rawResult = (await page.evaluate( |
| 54 | + (opts) => { |
| 55 | + return (window as any).sentience.read(opts); |
| 56 | + }, |
| 57 | + { format: 'raw' } |
| 58 | + )) as ReadResult; |
| 59 | + |
| 60 | + if (rawResult.status !== 'success') { |
| 61 | + return rawResult; |
| 62 | + } |
| 63 | + |
| 64 | + // Convert to markdown using Turndown |
| 65 | + try { |
| 66 | + const turndownService = new TurndownService({ |
| 67 | + headingStyle: 'atx', // Use # for headings |
| 68 | + bulletListMarker: '-', // Use - for lists |
| 69 | + codeBlockStyle: 'fenced', // Use ``` for code blocks |
| 70 | + }); |
| 71 | + |
| 72 | + // Add custom rules for better conversion |
| 73 | + turndownService.addRule('strikethrough', { |
| 74 | + filter: ['del', 's', 'strike'] as any, |
| 75 | + replacement: (content: string) => `~~${content}~~`, |
| 76 | + }); |
| 77 | + |
| 78 | + // Strip unwanted tags |
| 79 | + turndownService.remove(['script', 'style', 'nav', 'footer', 'header', 'noscript']); |
| 80 | + |
| 81 | + const htmlContent = rawResult.content; |
| 82 | + const markdownContent = turndownService.turndown(htmlContent); |
| 83 | + |
| 84 | + // Return result with markdown content |
| 85 | + return { |
| 86 | + status: 'success', |
| 87 | + url: rawResult.url, |
| 88 | + format: 'markdown', |
| 89 | + content: markdownContent, |
| 90 | + length: markdownContent.length, |
| 91 | + }; |
| 92 | + } catch (e) { |
| 93 | + // If conversion fails, return error |
| 94 | + return { |
| 95 | + status: 'error', |
| 96 | + url: rawResult.url, |
| 97 | + format: 'markdown', |
| 98 | + content: '', |
| 99 | + length: 0, |
| 100 | + error: `Markdown conversion failed: ${e}`, |
| 101 | + }; |
| 102 | + } |
| 103 | + } else { |
| 104 | + // For "raw" or "text", call extension directly |
| 105 | + const result = (await page.evaluate( |
| 106 | + (opts) => { |
| 107 | + return (window as any).sentience.read(opts); |
| 108 | + }, |
| 109 | + { format } |
| 110 | + )) as ReadResult; |
| 111 | + |
| 112 | + return result; |
| 113 | + } |
| 114 | +} |
0 commit comments