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.

Resource Management

FieldTypeDefaultDescription
MaxInputSizeint52428800 (50MB)Maximum input size in bytes
MaxCacheEntriesint2000Maximum cache entries
CacheTTLtime.Duration1hCache expiration time
CacheCleanuptime.Duration5mCache cleanup interval
WorkerPoolSizeint4Worker pool size
ProcessingTimeouttime.Duration30sProcessing timeout

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.

Security

FieldTypeDefaultDescription
EnableSanitizationbooltrueEnable content sanitization; can be disabled only for trusted input
MaxDepthint500Maximum DOM depth
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

Balanced configuration for general use.

go
cfg := html.DefaultConfig()

TextOnlyConfig

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

go
cfg := html.TextOnlyConfig()

MarkdownConfig

Optimized for Markdown output, with inline images and links using Markdown format.

go
cfg := html.MarkdownConfig()

HighSecurityConfig

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

go
cfg := html.HighSecurityConfig()

Overrides compared to DefaultConfig():

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

Validate

Validate the configuration.

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

Validation Constraints

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
MaxCacheEntries0 and ≤ 100000-1, 200000
CacheTTL0-1 * time.Second
CacheCleanup0-1 * time.Minute
WorkerPoolSizePositive and ≤ 2560, 512
MaxDepthPositive and ≤ 5000, 1000
ProcessingTimeout0-1 * time.Second
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/InlineLinkFormatnone, TableFormatmarkdown). New() calls Validate() before creating the Processor, so an invalid configuration never produces a usable Processor.