Implementation:1broseidon Ketch extract Extractor
| Knowledge Sources | |
|---|---|
| Domains | Extraction, HTML_to_Markdown |
| Last Updated | 2026-10-27 00:00 GMT |
Overview
Converts raw HTML into clean markdown via a layered fallback: structural extraction (chrome pruned by what the content is), then readability scoring, then raw HTML→markdown.
Description
The Extractor takes a page URL and raw HTML and returns a `*Result{Title, Markdown}`. The pipeline is layered: first semanticExtract (the ADR-0005 engine) reads the page's own structure — landmark, uniform sections, or the smallest element holding its prose — and prunes chrome by identity (what an element is), not by a score. If that yields nothing, the readability parser scores candidates and renders its article to HTML→markdown. If that also fails, the raw HTML is converted directly. A `rawTableFallback` recovers data tables that the readability article dropped.
The `mode` field (`ModeClean` default, `ModeComplete`) controls how aggressively chrome is pruned; it is folded into the page-cache key so the two modes never share entries.
Usage
Constructed once per Scraper (`extract.NewWithMode`). Called by `Scraper.Scrape` and `ScrapeConditional` after the (possibly browser-rendered) HTML is in hand.
Code Reference
Source Location
- Repository: ketch
- File: extract/extract.go (entry), extract/semantic.go (engine)
Signature
type Result struct {
Title string
Markdown string
}
type Mode string
const (
ModeClean Mode = "clean"
ModeComplete Mode = "complete"
)
type Extractor struct {
mode Mode
}
func New() *Extractor
func NewWithMode(mode Mode) *Extractor
func (e *Extractor) Mode() Mode
// Extract takes a URL and raw HTML, extracts the main content, and converts
// it to markdown. Pipeline: semanticExtract → readability → raw HTML→markdown.
func (e *Extractor) Extract(pageURL, html string) (*Result, error)
Import
import "github.com/1broseidon/ketch/extract"
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| pageURL | string | Yes | Page URL (for relative-link resolution) |
| html | string | Yes | Raw or browser-rendered HTML |
Outputs
| Name | Type | Description |
|---|---|---|
| return | *Result | Title + Markdown |
| return error | error | Only on unparseable pageURL |
Usage Examples
Markdown Extraction
ext := extract.NewWithMode(extract.ModeClean)
result, err := ext.Extract("https://example.com/post", html)
if err != nil { return err }
fmt.Println(result.Title)
fmt.Println(result.Markdown)
Complete Mode (More Chrome Kept)
// ModeComplete keeps navigation/boilerplate the clean mode drops; folded
// into the cache key so the modes never share entries.
ext := extract.NewWithMode(extract.ModeComplete)