Skip to content

Package Functions ​

Package functions are the one-off entry points of github.com/cybergodev/html: they reuse Processor instances internally via sync.Pool, so you never manage a lifecycle. Pooled instances have the cache disabled and reset their statistics and audit log on return; for caching, statistics, or audit, create a dedicated Processor with html.New.

Internal Mechanism ​

Two conclusions

  • A pooled instance has no cache. Package functions reuse Processor instances through a sync.Pool. A pooled instance starts from DefaultConfig() with caching turned off (MaxCacheEntries, CacheTTL, and CacheCleanup zeroed), and it resets statistics and audit state on return. Package-function calls therefore never reuse results across calls.
  • Passing a custom Config switches to a one-shot instance. A pooled instance has a fixed config, which cannot be reused safely with a different one. Each call with a custom Config therefore creates a new Processor and Closes it before returning; its cache fields are zeroed as well.

When you need caching, accumulated statistics, or an audit log, create a long-lived Processor with html.New.

Config Parameter Resolution ​

Apart from the GroupLinksByType helper, the cfg ...Config parameter of every package function is optional and variadic, parsed by the internal resolveConfig:

ArgumentBehaviorPooled?
NoneUses DefaultConfig()Yes (pooled=true)
OneUses that Config on a one-shot ProcessorNo (pooled=false)
Two or moreReturns ErrMultipleConfigs—

Switch to a Processor when you need caching or accumulated statistics

Neither the pooled path nor the one-shot instance has a cache, and neither retains statistics or audit entries across calls. When you need to reuse a custom config across loops, HTTP handlers, or batch jobs and want the cache to pay off, create a Processor directly.

Constructors ​

New ​

Creates a dedicated Processor instance. The cache, accumulated statistics, and audit log live on the instance and stay queryable.

go
func New(cfg ...Config) (*Processor, error)

cfg ...Config follows the resolution rules above (omitted uses DefaultConfig(); two or more return ErrMultipleConfigs), then Config.Validate() runs. Unlike the pooled and one-shot instances behind package functions, the instance from New holds a long-lived cache and cumulative statistics. See Processor for the method list and lifecycle.

Content Extraction ​

Extract ​

Extract content from HTML bytes and return the full Result.

go
func Extract(htmlBytes []byte, cfg ...Config) (*Result, error)

Parameters:

ParameterTypeDescription
htmlBytes[]byteHTML content
cfg...ConfigOptional config, at most one

Example:

go
result, err := html.Extract(data)
if err != nil {
	log.Fatal(err)
}
fmt.Println(result.Title, result.Text)

Full example with field access and error handling:

go
package main

import (
	"fmt"
	"log"

	"github.com/cybergodev/html"
)

func main() {
	data := []byte(`<html><head><title>Example Page</title></head>
<body><h1>Welcome</h1><p>Body content <a href="https://example.com">link</a>.</p></body></html>`)

	// No Config passed: uses the pooled path
	result, err := html.Extract(data)
	if err != nil {
		log.Fatalf("extraction failed: %v", err)
	}

	fmt.Println("Title:", result.Title)
	fmt.Println("Words:", result.WordCount)
	fmt.Println("Links:", len(result.Links))
	// Output:
	// Title: Example Page
	// Words: 5
	// Links: 1
}

Error returns: Extract returns the same errors as Processor.Extract, plus two that are specific to package functions:

ErrorCondition
ErrMultipleConfigsTwo or more Config arguments passed
ErrInvalidConfig (wrapped in *ConfigError)The passed Config fails validation (e.g. MaxInputSize<=0)

ExtractFromFile ​

Extract content from an HTML file.

go
func ExtractFromFile(filePath string, cfg ...Config) (*Result, error)

Error returns: in addition to Extract's errors, file access may return a *FileError: FileErr wraps ErrFileNotFound (file does not exist) or ErrInvalidFilePath (empty path; a path-traversal denial for paths containing .. components also wraps the latter, so errors.Is matches). When AllowedBaseDir is configured and the path escapes it, FileErr is path outside allowed directory, which matches no sentinel error (see Security). A file larger than MaxInputSize is rejected before it is read, returning an *InputError wrapping ErrInputTooLarge.

Text Extraction ​

ExtractText ​

Extract plain text only.

go
func ExtractText(htmlBytes []byte, cfg ...Config) (string, error)

ExtractTextFromFile ​

ExtractTextFromFile is the file variant of ExtractText: it reads an HTML file from disk and returns its plain text. File-access errors are the same as ExtractFromFile.

go
func ExtractTextFromFile(filePath string, cfg ...Config) (string, error)
go
text, err := html.ExtractTextFromFile("page.html")
if err != nil {
	log.Fatal(err)
}

Context Variants ​

Every extraction, output, link, and batch function has a context.Context variant for cancellation and timeout control; New and GroupLinksByType have none.

FunctionSignature
ExtractWithContext(ctx context.Context, htmlBytes []byte, cfg ...Config) (*Result, error)
ExtractFromFileWithContext(ctx context.Context, filePath string, cfg ...Config) (*Result, error)
ExtractTextWithContext(ctx context.Context, htmlBytes []byte, cfg ...Config) (string, error)
ExtractTextFromFileWithContext(ctx context.Context, filePath string, cfg ...Config) (string, error)
go
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

result, err := html.ExtractWithContext(ctx, data)

Output Formats ​

FunctionSignatureDescription
ExtractToMarkdown(htmlBytes []byte, cfg ...Config) (string, error)HTML → Markdown
ExtractToMarkdownFromFile(filePath string, cfg ...Config) (string, error)File → Markdown
ExtractToMarkdownWithContext(ctx context.Context, htmlBytes []byte, cfg ...Config) (string, error)With context
ExtractToMarkdownFromFileWithContext(ctx context.Context, filePath string, cfg ...Config) (string, error)File + context
ExtractToJSON(htmlBytes []byte, cfg ...Config) ([]byte, error)HTML → JSON
ExtractToJSONFromFile(filePath string, cfg ...Config) ([]byte, error)File → JSON
ExtractToJSONWithContext(ctx context.Context, htmlBytes []byte, cfg ...Config) ([]byte, error)With context
ExtractToJSONFromFileWithContext(ctx context.Context, filePath string, cfg ...Config) ([]byte, error)File + context

For usage and examples see Output Formats.

FunctionSignatureDescription
ExtractAllLinks(htmlBytes []byte, cfg ...Config) ([]LinkResource, error)Extract all links
ExtractAllLinksFromFile(filePath string, cfg ...Config) ([]LinkResource, error)Extract links from a file
ExtractAllLinksWithContext(ctx context.Context, htmlBytes []byte, cfg ...Config) ([]LinkResource, error)With context
ExtractAllLinksFromFileWithContext(ctx context.Context, filePath string, cfg ...Config) ([]LinkResource, error)File + context

Companion grouping helper:

go
func GroupLinksByType(links []LinkResource) map[string][]LinkResource

Groups link resources into a map[type][]LinkResource keyed by LinkResource.Type (link/image/css, etc.).

For usage and examples see Link Extraction.

Batch Processing ​

FunctionSignatureDescription
ExtractBatch(htmlContents [][]byte, cfg ...Config) *BatchResultBatch extraction
ExtractBatchWithContext(ctx context.Context, htmlContents [][]byte, cfg ...Config) *BatchResultWith context
ExtractBatchFiles(filePaths []string, cfg ...Config) *BatchResultBatch file extraction
ExtractBatchFilesWithContext(ctx context.Context, filePaths []string, cfg ...Config) *BatchResultFiles + context

For usage and examples see Batch Processing.

Reader Mode ​

Reader mode returns a whole-page sectioned view (*ReadResult) instead of picking a single article: links are always preserved, and navigation and footer blocks are labeled by kind. There are 12 package functions; signatures, the ReadResult/PageSection/SectionKind structures, and the key semantics are in Reader Mode.

FunctionDescription
ReadPageWhole-page sections
ReadPageWithContextWith context
ReadPageFromFileRead from a file
ReadPageFromFileWithContextFile + context
ReadPageToMarkdownWhole-page Markdown (with links)
ReadPageToMarkdownWithContextWith context
ReadPageToMarkdownTextWhole-page Markdown (no links)
ReadPageToMarkdownTextWithContextWith context
ReadPageFromFileToMarkdownFile → Markdown (with links)
ReadPageFromFileToMarkdownWithContextFile + context
ReadPageFromFileToMarkdownTextFile → Markdown (no links)
ReadPageFromFileToMarkdownTextWithContextFile + context

ReadPage* does not participate in the extraction cache; ReadPage*ToMarkdown* uses a temporary Processor internally and never pollutes the main cache. See Reader Mode.

Package Functions vs Processor ​

Both call into Processor underneath; they differ in resource reuse and retained state:

DimensionPackage functionsProcessor
CacheNone (pooled config MaxCacheEntries=0)Yes (deep copy returned on hit)
StatisticsNot retained across calls (ResetStatistics on pooled return; the custom-Config path discards the instance)Accumulated; GetStatistics anytime
Audit logNot retained across calls (ClearAuditLog on pooled return; the custom-Config path discards the instance)Accumulated; queryable via GetAuditLog
Custom ConfigOne-shot Processor created and destroyed per call (cache fields zeroed)Same instance reused
LifecycleAutomatic (pool or one-shot instance)Manual defer Close()
Suited forOne-off calls, scripts, low-frequency requestsHigh-frequency calls, long-running services, caching

When to switch to a Processor

A single or occasional call is fine with package functions. When you extract repeatedly inside loops, HTTP handlers, or batch jobs, create one long-lived Processor and reuse it; only then does the cache pay off.