markdown

package
v0.62.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 14, 2026 License: Apache-2.0 Imports: 21 Imported by: 0

Documentation

Overview

Package markdown extracts and transforms Markdown documents.

Reader can emit one whole document or heading-scoped documents with source metadata. TextSplitter is the etl.TextSplitter for structure-aware, token-bounded chunking: headings are repeated as retrieval context, tables split only between rows, lists only between items, and fenced code blocks only between lines while retaining their fences.

Index

Constants

View Source
const (
	MetadataHeading      = "markdown.heading"
	MetadataHeadingLevel = "markdown.heading.level"
	MetadataHeadingPath  = "markdown.heading.path"
)
View Source
const DefaultMaxExpandedBytes = 32 << 20

Variables

View Source
var ErrReferenceExpansionTooLarge = errors.New("markdown: reference expansion exceeds byte limit")

ErrReferenceExpansionTooLarge means no partially expanded source is exposed.

View Source
var ErrSemanticUnitTooLarge = errors.New("markdown splitter: semantic unit exceeds token limit")

ErrSemanticUnitTooLarge reports a Markdown unit that cannot be split without losing its semantic boundary.

Functions

This section is empty.

Types

type Reader

type Reader struct {
	// contains filtered or unexported fields
}

Read admits one attempt on the sequential source. Reopen the input to retry after a failure; concurrent or later attempts return etl.ErrSourceConsumed.

func NewReader

func NewReader(source io.Reader, config ReaderConfig) (*Reader, error)

func (*Reader) Read

func (r *Reader) Read(ctx context.Context) ([]*document.Document, error)

Read consumes the source once. Blank input yields no documents.

type ReaderConfig

type ReaderConfig struct {
	HeadingSplitLevel int
	SourceName        string
	Metadata          coremetadata.Map
	SourceBudget      etl.SourceBudget
	// MaxExpandedBytes bounds source bytes before parsing and after reference expansion.
	// Zero uses DefaultMaxExpandedBytes; whole-document reads do not expand links.
	MaxExpandedBytes int
}

ReaderConfig controls Markdown extraction. HeadingSplitLevel emits one document per section split on headings at or above that level (1 = H1, 2 = H1+H2). Zero disables splitting; non-zero values must be in [1, 6]. Metadata is cloned by NewReader and cannot contain the reader's heading keys. A zero SourceBudget uses etl.DefaultMaxSourceBytes.

type TextSplitter added in v0.60.0

type TextSplitter struct {
	// contains filtered or unexported fields
}

TextSplitter is the etl.TextSplitter that produces token-bounded Markdown chunks without severing tables, list items, or code lines. Active heading ancestry is repeated in each chunk so independently retrieved chunks retain their section context.

func NewTextSplitter added in v0.60.0

func NewTextSplitter(config TextSplitterConfig) (*TextSplitter, error)

func (*TextSplitter) SplitText added in v0.60.0

func (t *TextSplitter) SplitText(ctx context.Context, source string) ([]string, error)

SplitText splits one Markdown source string. Every returned chunk is non-empty and within MaxTokensPerChunk. The total is bounded by MaxChunks.

type TextSplitterConfig added in v0.60.0

type TextSplitterConfig struct {
	Tokenizer tokenizer.Tokenizer

	MaxTokensPerChunk int
	MaxChunks         int
	// MaxSearchWork bounds each paragraph prefix search in bytes rendered or
	// encoded plus tokens decoded; zero uses etl.DefaultMaxSearchWork.
	MaxSearchWork int
	// MaxExpandedBytes bounds source bytes before parsing and after reference expansion.
	// Zero uses DefaultMaxExpandedBytes.
	MaxExpandedBytes int
}

TextSplitterConfig configures structure-aware Markdown chunking. Zero limits use documented defaults; negative limits are rejected.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL