Documentation
¶
Overview ¶
Package web provides pure HTML search result parsing, URL normalization, domain filtering, and fetched content formatting (HTML-to-Markdown, HTML-to-Text, and JSON/XML pretty-printing) helpers.
Index ¶
- Constants
- Variables
- func BuildSearchURL(endpoint *url.URL, query string) string
- func CleanResultURL(raw string) string
- func FormatContent(rawURL, contentType string, body []byte, format Format) string
- func HTMLToMarkdown(rawHTML string, baseURL string) string
- func HTMLToText(rawHTML string) string
- func MatchesAnyDomain(host string, domains []string) bool
- func ResultHost(rawURL string) string
- type Format
- type Hit
Constants ¶
const MaxSnippetBytes = 300
MaxSnippetBytes is the maximum byte length of one result snippet.
Variables ¶
var ErrBotChallenge = errors.New("search engine bot protection triggered; try again later or rephrase the query")
ErrBotChallenge is returned when a search endpoint serves a CAPTCHA or anomaly interstitial instead of search results.
Functions ¶
func BuildSearchURL ¶
BuildSearchURL appends the encoded query to the search endpoint, preserving any query parameters the endpoint itself carries.
func CleanResultURL ¶
CleanResultURL resolves a search engine result link to its destination. Result pages wrap the real URL in a redirect (/l/?uddg=<encoded>) so absolute, relative, and protocol-relative redirect forms all resolve to the target.
func FormatContent ¶
FormatContent converts a fetched response body into the requested Format based on its Content-Type (or sniffed MIME type when Content-Type is missing or application/octet-stream).
func HTMLToMarkdown ¶
HTMLToMarkdown converts a full HTML document into clean GitHub-Flavored Markdown without cropping layout containers such as nav, header, footer, or aside. Relative links and images are resolved against baseURL when valid.
func HTMLToText ¶
HTMLToText converts a full HTML document into readable plain text without Markdown markup tags, preserving paragraph breaks, headings, and list bullets.
func MatchesAnyDomain ¶
MatchesAnyDomain reports whether host equals or is a subdomain of any of the given domain patterns, or any pattern is the "*" wildcard. Leading "*." and "." decorations are tolerated.
func ResultHost ¶
ResultHost returns the lowercase hostname of a result URL, or "" when the URL is unparsable or host-less.
Types ¶
type Format ¶
type Format string
Format identifies the output representation used when formatting fetched web content.
const ( // FormatMarkdown converts HTML to GitHub-Flavored Markdown and pretty-prints // JSON/XML responses. It is the default format. FormatMarkdown Format = "markdown" // FormatText converts HTML to readable plain text and pretty-prints // JSON/XML responses. FormatText Format = "text" // FormatHTML returns the raw response body unmodified. FormatHTML Format = "html" )
func ParseFormat ¶
ParseFormat normalizes and validates a requested fetch output format. An empty string defaults to FormatMarkdown.
type Hit ¶
type Hit struct {
Title string `json:"title"`
URL string `json:"url"`
Snippet string `json:"snippet,omitempty"`
}
Hit is one ranked web search result.
func FilterHits ¶
FilterHits applies the optional domain allow/block filters and the final result cap. Blocked domains win over allowed domains. When limit > 0, it caps the returned hits at limit and reports whether additional matching hits were dropped beyond the cap.