Skip to content

Configuration ​

Config Struct ​

Config is the unified configuration struct for the HTML library, covering resource management, security, content extraction, output formats, and link filtering.

All 30 fields at a glance (detailed constraints and descriptions in the grouped sections below):

FieldTypeGroupDefault
Config.MaxInputSizeintResource management52428800 (50MB)
Config.MaxCacheEntriesintResource management2000
Config.CacheTTLtime.DurationResource management1h
Config.CacheCleanuptime.DurationResource management5m
Config.WorkerPoolSizeintResource management4
Config.ProcessingTimeouttime.DurationResource management30s
Config.EnableSanitizationboolSecuritytrue
Config.MaxDepthintSecurity500
Config.AllowedBaseDirstringSecurity""
Config.AuditAuditConfigSecurityDefaultAuditConfig()
Config.ExtractArticleboolContent extractiontrue
Config.PreserveImagesboolContent extractiontrue
Config.PreserveLinksboolContent extractiontrue
Config.PreserveVideosboolContent extractiontrue
Config.PreserveAudiosboolContent extractiontrue
Config.InlineImageFormatstringOutput formatnone
Config.InlineLinkFormatstringOutput formatnone
Config.TableFormatstringOutput formatmarkdown
Config.EncodingstringOutput format"" (auto-detect)
Config.ResolveRelativeURLsboolLink extractiontrue
Config.BaseURLstringLink extraction""
Config.IncludeImagesboolLink extractiontrue
Config.IncludeVideosboolLink extractiontrue
Config.IncludeAudiosboolLink extractiontrue
Config.IncludeCSSboolLink extractiontrue
Config.IncludeJSboolLink extractiontrue
Config.IncludeContentLinksboolLink extractiontrue
Config.IncludeExternalLinksboolLink extractiontrue
Config.IncludeIconsboolLink extractiontrue
Config.ScorerScorerExtensionnil (built-in default scorer)

Resource Management ​

FieldTypeDefaultDescription
MaxInputSizeint52428800 (50MB)Maximum input size in bytes; range 1–52428800
MaxCacheEntriesint2000Maximum cache entries; 0 disables the cache (upper bound 100000)
CacheTTLtime.Duration1hCache expiration time; cannot be negative
CacheCleanuptime.Duration5mBackground cleanup interval for expired cache entries; 0 disables background cleanup
WorkerPoolSizeint4Worker pool size for batch processing; range 1–256
ProcessingTimeouttime.Duration30sPer-document processing timeout; 0 means unlimited

TIP

Setting MaxCacheEntries, CacheCleanup, or ProcessingTimeout to 0 is not an error — each has a well-defined meaning (disable cache, disable background cleanup, and no timeout respectively). MaxInputSize, WorkerPoolSize, and MaxDepth must be positive, otherwise a ConfigError is returned.

Therefore the zero value Config{} does not pass Validate() and must not be used directly — always start from DefaultConfig() (or another preset) and override fields as needed.

Security ​

FieldTypeDefaultDescription
EnableSanitizationbooltrueEnable content sanitization; can be disabled only for trusted input
MaxDepthint500Maximum DOM depth, range 1–500
AllowedBaseDirstring""Restricts file operations to this directory; empty (default) means no restriction. Use it when accepting untrusted file-path input
AuditAuditConfigDefaultAuditConfig()Audit configuration

Content Extraction ​

FieldTypeDefaultDescription
ExtractArticlebooltrueEnable intelligent article recognition
PreserveImagesbooltruePreserve image information
PreserveLinksbooltruePreserve link information
PreserveVideosbooltruePreserve video information
PreserveAudiosbooltruePreserve audio information

Output Formats ​

FieldTypeDefaultOptionsDescription
InlineImageFormatstringnonenone, markdown, html, placeholderInline image format
InlineLinkFormatstringnonenone, markdown, htmlInline link format
TableFormatstringmarkdownmarkdown, htmlTable format
Encodingstring""-Specify encoding (auto-detect if empty)
FieldTypeDefaultDescription
ResolveRelativeURLsbooltrueResolve relative URLs (requires BaseURL)
BaseURLstring""Base URL for resolving relative paths
IncludeImagesbooltrueInclude image links
IncludeVideosbooltrueInclude video links
IncludeAudiosbooltrueInclude audio links
IncludeCSSbooltrueInclude CSS links
IncludeJSbooltrueInclude JS links
IncludeContentLinksbooltrueInclude content links
IncludeExternalLinksbooltrueInclude external links
IncludeIconsbooltrueInclude icon links

Extensions ​

FieldTypeDefaultDescription
ScorerScorernilCustom content scorer; uses default scorer when nil

Configuration Presets ​

DefaultConfig ​

DefaultConfig returns a balanced configuration suited to general scenarios — the starting point for most applications.

go
func DefaultConfig() Config
go
cfg := html.DefaultConfig()

TextOnlyConfig ​

Extract plain text only, disabling all media and link preservation (PreserveImages, PreserveLinks, PreserveVideos, PreserveAudios all set to false).

go
func TextOnlyConfig() Config
go
cfg := html.TextOnlyConfig()

MarkdownConfig ​

MarkdownConfig builds on the default configuration and sets both InlineImageFormat and InlineLinkFormat to markdown, suited for output whose inline images and links use Markdown format.

go
func MarkdownConfig() Config
go
cfg := html.MarkdownConfig()

HighSecurityConfig ​

High-security configuration: reduced limits, shorter timeouts, full audit.

go
func HighSecurityConfig() Config
go
cfg := html.HighSecurityConfig()

Overrides compared to DefaultConfig():

FieldDefaultHigh Security
MaxInputSize52428800 (50MB)10485760 (10MB)
MaxCacheEntries2000500
CacheTTL1h30m
CacheCleanup5m1m
WorkerPoolSize42
ProcessingTimeout30s10s
MaxDepth500100
AuditDefaultAuditConfig()HighSecurityAuditConfig()

Validate ​

Validate checks the configuration for validity (value receiver, callable directly as cfg.Validate()); when invalid it returns a *ConfigError wrapping ErrInvalidConfig.

go
func (c Config) Validate() error
go
cfg := html.DefaultConfig()
cfg.MaxInputSize = -1
err := cfg.Validate() // Returns ConfigError

Validation Constraints ​

Config.Validate enforces the following value ranges on numeric fields (violations return a ConfigError, which can be checked via errors.Is(err, html.ErrInvalidConfig)):

FieldConstraintInvalid example
MaxInputSizePositive and ≤ 52428800 (50MB)0, -1, 100000000
MaxCacheEntries≥ 0 and ≤ 100000-1, 200000
CacheTTL≥ 0-1 * time.Second
CacheCleanup≥ 0-1 * time.Minute
WorkerPoolSizePositive and ≤ 2560, 512
MaxDepthPositive and ≤ 5000, 1000
ProcessingTimeout≥ 0-1 * time.Second
Audit.MaxEntries≥ 0 and ≤ 100000 (0 means using the default of 10000)-1, 200000
InlineImageFormatEmpty / none / markdown / html / placeholder"pdf"
InlineLinkFormatEmpty / none / markdown / html"pdf"
TableFormatEmpty / markdown / html"csv"

Format strings are case-insensitive, and an empty value is treated as the default (InlineImageFormat/InlineLinkFormat → none, TableFormat → markdown). New() calls Validate() before creating the Processor, so an invalid configuration never produces a usable Processor.