Skip to content

Package Functions

Package-level functions are ideal for one-time calls. They use sync.Pool internally to reuse Processor instances, so no manual lifecycle management is needed. Note: the pooled Processor disables caching and audit retention; for caching/statistics/audit, create a dedicated Processor with html.New.

Content Extraction

Extract

Extract content from HTML bytes, returning a complete 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)

ExtractFromFile

Extract content from an HTML file.

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

Text Extraction

ExtractText

Extract plain text content only.

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

ExtractTextFromFile

Extract plain text from a file.

go
func ExtractTextFromFile(filePath string, cfg ...Config) (string, error)

Context Variants

All functions support context.Context variants for cancellation and timeout control:

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 detailed 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 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

For detailed 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) *BatchResultFile + context

For detailed usage and examples, see Batch Processing.