package responsemodifers import ( "bytes" "embed" "fmt" "golang.org/x/net/html" "golang.org/x/net/html/atom" "html/template" "io" "ladder/proxychain" "log" //"github.com/go-shiori/dom" "github.com/markusmobius/go-trafilatura" ) //go:embed generate_readable_outline.html var templateFS embed.FS // GenerateReadableOutline creates an reader-friendly distilled representation of the article. // This is a reliable way of bypassing soft-paywalled articles, where the content is hidden, but still present in the DOM. func GenerateReadableOutline() proxychain.ResponseModification { // get template only once, and resuse for subsequent calls f := "generate_readable_outline.html" tmpl, err := template.ParseFS(templateFS, f) if err != nil { panic(fmt.Errorf("tx.GenerateReadableOutline Error: %s not found", f)) } return func(chain *proxychain.ProxyChain) error { // =========================================================== // 1. extract dom contents using reading mode algo // =========================================================== opts := trafilatura.Options{ IncludeImages: true, IncludeLinks: true, FavorRecall: true, Deduplicate: true, FallbackCandidates: nil, // TODO: https://github.com/markusmobius/go-trafilatura/blob/main/examples/chained/main.go // implement fallbacks from "github.com/markusmobius/go-domdistiller" and "github.com/go-shiori/go-readability" OriginalURL: chain.Request.URL, } extract, err := trafilatura.Extract(chain.Response.Body, opts) if err != nil { return err } // ============================================================================ // 2. render generate_readable_outline.html template using metadata from step 1 // ============================================================================ // render DOM to string without H1 title removeFirstH1(extract.ContentNode) var b bytes.Buffer html.Render(&b, extract.ContentNode) distilledHTML := b.String() // populate template parameters data := map[string]interface{}{ "Success": true, "Footer": extract.Metadata.License, "Image": extract.Metadata.Image, "Description": extract.Metadata.Description, "Hostname": extract.Metadata.Hostname, "Url": chain.Request.URL, "Title": extract.Metadata.Title, // todo: modify CreateReadableDocument so we don't have