Documentation
¶
Overview ¶
Package docx extracts body text from DOCX (Office Open XML) sources.
A .docx is a ZIP archive of XML; the document body lives at word/document.xml. This extractor unzips in memory, stream-decodes that part, and concatenates text runs (<w:t>), emitting a blank line between paragraphs (<w:p>) so the downstream boundary-aware chunker sees paragraph boundaries. The result is a single text/plain artifact in the same shape as the core text extractor.
Unlike the PDF and HTML extractors, this package needs no third-party dependency (stdlib archive/zip + encoding/xml). It is kept a subpackage anyway so all formats are uniformly opt-in (see docs/adr/0006-optional-adapters-as-subpackages.md). Wire it into a registry with:
reg.Register("application/vnd.openxmlformats-officedocument.wordprocessingml.document", docx.New())
Scope is body text only: headers, footers, footnotes, endnotes, comments, and embedded objects are not extracted. That can be added later by parsing the additional archive parts; it is omitted now to keep the first version small.
Index ¶
Constants ¶
const DefaultMaxDecompressedBytes int64 = 64 << 20 // 64 MiB
DefaultMaxDecompressedBytes caps how much of word/document.xml the decoder will read. The Registry bounds the *compressed* input, but a zip bomb can expand far beyond that, so the uncompressed stream needs its own ceiling. 64 MiB is far larger than any realistic body-text document.xml while still bounding memory.
const MediaType content.MediaType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
MediaType is the media type this extractor handles.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Extractor ¶
type Extractor struct {
// MaxDecompressedBytes caps the uncompressed size of word/document.xml the
// decoder reads, defending against a zip bomb whose compressed form fits under
// the Registry's input cap but expands to exhaust memory. Zero or negative
// means DefaultMaxDecompressedBytes.
MaxDecompressedBytes int64
}
Extractor extracts body text from DOCX sources. It carries only immutable configuration and is safe for concurrent use.
func (Extractor) Extract ¶
func (e Extractor) Extract(ctx context.Context, r io.Reader, parentID string) ([]content.Artifact, error)
Extract reads r as a .docx archive and returns a single text/plain artifact derived from parentID, with paragraphs separated by blank lines. It honors ctx cancellation while reading. It returns:
- extract.ErrNoContent if the document parses cleanly but has no body text;
- an error matching extract.ErrMalformedSource if the archive is not a valid zip, lacks word/document.xml, contains unparseable XML, or whose word/document.xml decompresses past MaxDecompressedBytes (a zip bomb).