Skip to content

Config ​

Config Struct ​

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

FieldTypeGroupDefault
Config.MaxInputSizeintResource management52428800 (50 MB)
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.ExtractMetadataboolContent 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 (50 MB)Maximum input in bytes; positive and ≤ 52428800 (50 MB)
MaxCacheEntriesint2000Cache entry limit; 0 disables the cache (upper bound 100000)
CacheTTLtime.Duration1hCache expiration time; cannot be negative
CacheCleanuptime.Duration5mInterval for sweeping expired entries; 0 disables background cleanup
WorkerPoolSizeint4Worker pool size for batch processing; range 1–256
ProcessingTimeouttime.Duration30sPer-document processing timeout; 0 means unlimited

Zero-value semantics and where to start

MaxCacheEntries, CacheCleanup, and ProcessingTimeout accept 0 with a defined meaning: disable the cache, disable background cleanup, and no timeout. 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. Start from DefaultConfig() or another preset, then override fields. html.New() with no arguments uses DefaultConfig() internally and calls Validate() before returning, so it is always safe.

Security ​

FieldTypeDefaultDescription
EnableSanitizationbooltrueEnable content cleaning; disable only for trusted input
MaxDepthint500Maximum DOM depth, range 1–500
AllowedBaseDirstring""Restrict file operations to this directory; empty means no restriction
AuditAuditConfigDefaultAuditConfig()Audit configuration

Set AllowedBaseDir whenever file paths come from untrusted input.

Content Extraction ​

FieldTypeDefaultDescription
ExtractArticlebooltrueEnable article detection
ExtractMetadatabooltrueCollect JSON-LD, OpenGraph, <meta>, and canonical data, filling the metadata fields of Result/ReadResult, and prefer metadata titles for the title (JSON-LD headline > og:title, before <title>)
PreserveImagesbooltruePreserve image information
PreserveLinksbooltruePreserve link information
PreserveVideosbooltruePreserve video information
PreserveAudiosbooltruePreserve audio information

With ExtractMetadata=true, five fields are filled: Author, PublishedTime, Description, SiteName, and CanonicalURL. Their source priority is in Type Definitions. When it is off, those five fields stay empty and Title falls back to <title> → body h1 → first h1 in the document → first h2.

This field also participates in the cache key: ExtractMetadata is one of the 6 boolean switches in the cache key (the other five are ExtractArticle, PreserveImages, PreserveLinks, PreserveVideos, and PreserveAudios), so changing it yields a different cache entry.

PreserveVideos/PreserveAudios have one more input-size boundary: URLs that appear only in the raw HTML text (JSON inside scripts, comments, and similar spots) rather than in media elements are scanned additionally only when the document is at most 1 MB; larger documents get media elements extracted from the parsed DOM only.

Output Formats ​

FieldTypeDefaultOptionsDescription
InlineImageFormatstringnonenone, markdown, html, placeholderInline image format
InlineLinkFormatstringnonenone, markdown, htmlInline link format
TableFormatstringmarkdownmarkdown, htmlTable format
Encodingstring""-Encoding to use (e.g. utf-8, windows-1252, gbk); auto-detect when empty

In reader mode (ReadPage*), InlineLinkFormat="none" is treated as markdown — links are part of the reading-view contract, and none only takes effect for the Extract family (see Reader Mode).

FieldTypeDefaultDescription
ResolveRelativeURLsbooltrueResolve relative URLs; when BaseURL is empty, the base is inferred from the document
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; the built-in scorer is used when nil

Configuration Presets ​

DefaultConfig ​

DefaultConfig returns a balanced configuration: the starting point for most applications, and the foundation the other presets and custom configurations are built on.

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

TextOnlyConfig ​

TextOnlyConfig builds on the default configuration, setting PreserveImages, PreserveLinks, PreserveVideos, and PreserveAudios all to false, so no media or link information is retained. The Include* filters of ExtractAllLinks are unaffected by this preset.

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

MarkdownConfig ​

MarkdownConfig builds on the default configuration and sets InlineImageFormat and InlineLinkFormat to markdown, for rendering inline images and links in the body as Markdown. ExtractToMarkdown always emits Markdown and does not need this preset.

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

HighSecurityConfig ​

HighSecurityConfig tightens limits, shortens timeouts, and enables full audit.

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

Differences from DefaultConfig():

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

Validate ​

Checks the configuration for validity (value receiver, callable 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 these bounds on numeric fields and format strings; violations return a ConfigError (check with errors.Is(err, html.ErrInvalidConfig)):

FieldConstraintInvalid example
MaxInputSizePositive and ≤ 52428800 (50 MB)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 the default of 10000)-1, 200000
Audit.MaxRawValueLength≥ 0 (0 means the default of 200)-1
InlineImageFormatEmpty / none / markdown / html / placeholder"pdf"
InlineLinkFormatEmpty / none / markdown / html"pdf"
TableFormatEmpty / markdown / html"csv"

Format strings are case-insensitive, and an empty value means the default (InlineImageFormat/InlineLinkFormat → none, TableFormat → markdown). New() calls Validate() before returning, so an invalid config never yields a usable Processor.