Documentation
¶
Overview ¶
Package pagemark extracts useful page content as safe Markdown.
The output contains untrusted source data. It does not protect an agent from prompt injection. The package does not fetch pages or run JavaScript.
Index ¶
- Variables
- type BlockDiagnostic
- type Diagnostics
- type Document
- type Image
- type LimitError
- type Link
- type Option
- func WithDiagnostics(v bool) Option
- func WithFavorPrecision(v bool) Option
- func WithFavorRecall(v bool) Option
- func WithIncludeImages(v bool) Option
- func WithIncludeLinks(v bool) Option
- func WithIncludeMetadata(v bool) Option
- func WithIncludeTables(v bool) Option
- func WithLogger(v *slog.Logger) Option
- func WithMaxDepth(v int) Option
- func WithMaxElements(v int) Option
- func WithMaxImages(v int) Option
- func WithMaxInputBytes(v int64) Option
- func WithMaxLinks(v int) Option
- func WithMaxOutputBytes(v int) Option
- func WithMaxRepeatedItems(v int) Option
- func WithMaxTableCells(v int) Option
- func WithPageType(v PageType) Option
- func WithProfile(v Profile) Option
- func WithURLPolicy(v URLPolicy) Option
- type PageCandidate
- type PageType
- type Profile
- type Section
- type Stats
- type URLPolicy
- type Warning
Examples ¶
Constants ¶
This section is empty.
Variables ¶
Functions ¶
This section is empty.
Types ¶
type BlockDiagnostic ¶
type BlockDiagnostic struct {
ID int `json:"id"`
Kind string `json:"kind"`
Text string `json:"text"`
Score float64 `json:"score"`
Selected bool `json:"selected"`
Reasons []string `json:"reasons,omitempty"`
}
BlockDiagnostic explains one content block.
type Diagnostics ¶
type Diagnostics struct {
ProfileVersion string `json:"profile_version"`
Fallback string `json:"fallback"`
PageCandidates []PageCandidate `json:"page_candidates,omitempty"`
Blocks []BlockDiagnostic `json:"blocks,omitempty"`
RejectedLinks []string `json:"rejected_links,omitempty"`
}
Diagnostics explains selection decisions. Its format can grow in minor releases.
type Document ¶
type Document struct {
URL string `json:"url,omitempty"`
CanonicalURL string `json:"canonical_url,omitempty"`
Title string `json:"title,omitempty"`
Description string `json:"description,omitempty"`
Author string `json:"author,omitempty"`
SiteName string `json:"site_name,omitempty"`
Language string `json:"language,omitempty"`
PublishedTime string `json:"published_time,omitempty"`
PageType PageType `json:"page_type"`
PageTypeScore float64 `json:"page_type_score"`
Markdown string `json:"markdown"`
Text string `json:"text"`
Sections []Section `json:"sections,omitempty"`
Links []Link `json:"links,omitempty"`
Images []Image `json:"images,omitempty"`
Quality float64 `json:"quality"`
Diagnostics *Diagnostics `json:"diagnostics,omitempty"`
Warnings []Warning `json:"warnings,omitempty"`
Stats Stats `json:"stats"`
}
Document contains safe Markdown and metadata from one HTML document. Markdown is untrusted source data. Do not use it as privileged instructions.
func Extract ¶
Extract reads UTF-8 HTML and extracts useful content. Callers must decode input in other character encodings before calling Extract.
Example ¶
package main
import (
"fmt"
"strings"
"github.com/ryanfowler/pagemark"
)
func main() {
source := `<main><h1>Guide</h1><p>Install the tool.</p></main>`
doc, err := pagemark.Extract(strings.NewReader(source), "https://example.com/guide")
if err != nil {
panic(err)
}
fmt.Println(doc.Markdown)
}
Output: # Guide Install the tool.
func ExtractBytes ¶
ExtractBytes extracts useful content from UTF-8 HTML bytes.
func ExtractNode ¶
ExtractNode extracts useful content from a parsed HTML tree. It does not change root. The caller must not change root during extraction.
Example (UntrustedContent) ¶
package main
import (
"fmt"
"github.com/ryanfowler/pagemark"
)
func main() {
// Keep doc.Markdown in an untrusted data channel when you supply it to an agent.
doc, err := pagemark.ExtractBytes([]byte(`<main><p>Source data for an agent.</p></main>`), "")
if err != nil {
panic(err)
}
fmt.Println(doc.Text)
}
Output: Source data for an agent.
type LimitError ¶
LimitError reports a resource limit.
func (*LimitError) Error ¶
func (e *LimitError) Error() string
func (*LimitError) Unwrap ¶
func (e *LimitError) Unwrap() error
type Option ¶
type Option func(*options)
Option changes extraction. Options are safe for concurrent reuse.
func WithDiagnostics ¶
func WithFavorPrecision ¶
func WithFavorRecall ¶
func WithIncludeImages ¶
WithIncludeImages controls useful images in Markdown and Document.Images. Images are included by default; pass false for text-only output.
func WithIncludeLinks ¶
func WithIncludeMetadata ¶
func WithIncludeTables ¶
func WithLogger ¶
func WithMaxDepth ¶
func WithMaxElements ¶
func WithMaxImages ¶
func WithMaxInputBytes ¶
func WithMaxLinks ¶
func WithMaxOutputBytes ¶
func WithMaxRepeatedItems ¶
func WithMaxTableCells ¶
func WithPageType ¶
func WithProfile ¶
func WithURLPolicy ¶
type PageCandidate ¶
PageCandidate is a possible page type.
type PageType ¶
type PageType string
PageType identifies the main shape of a page.
const ( PageTypeArticle PageType = "article" PageTypeDocumentation PageType = "documentation" PageTypeDiscussion PageType = "discussion" PageTypeProduct PageType = "product" PageTypeListing PageType = "listing" PageTypeCollection PageType = "collection" PageTypeService PageType = "service" PageTypeGeneric PageType = "generic" )
type Profile ¶
type Profile struct {
PageType PageType
}
Profile selects a page profile. Use WithPageType for normal overrides.
type Stats ¶
type Stats struct {
InputBytes int `json:"input_bytes"`
Elements int `json:"elements"`
TextBytes int `json:"text_bytes"`
Blocks int `json:"blocks"`
SelectedBlocks int `json:"selected_blocks"`
OutputBytes int `json:"output_bytes"`
}
Stats contains bounded extraction counts.
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
diagnose-blocks
command
Command diagnose-blocks prints scoring diagnostics for snippets in a local HTML file.
|
Command diagnose-blocks prints scoring diagnostics for snippets in a local HTML file. |
|
pagemark
command
Command pagemark fetches a web page and writes extracted Markdown.
|
Command pagemark fetches a web page and writes extracted Markdown. |
|
internal
|
|
|
dom
Package dom contains shared HTML tree rules.
|
Package dom contains shared HTML tree rules. |
|
markdown
Package markdown converts selected HTML nodes to a safe Markdown tree.
|
Package markdown converts selected HTML nodes to a safe Markdown tree. |