document

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Jun 25, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package document turns a file's bytes into a Document: raw content, fence-aware source lines, and a parsed frontmatter/data map. It is the format-agnostic substrate every rule reads from.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SplitTableCells added in v0.2.0

func SplitTableCells(text string) []string

SplitTableCells splits one table row into trimmed cell values. It strips a single leading and trailing pipe if present and splits on unescaped pipes, preserving `\|` escapes inside cell text.

Types

type Document

type Document struct {
	Path        string
	Format      Format
	Raw         []byte
	Lines       []Line
	Frontmatter map[string]any // markdown: parsed frontmatter; data: whole file
	Body        []byte         // markdown content after frontmatter (nil for data)
	BodyOffset  int            // byte offset in Raw where Body begins (0 for data)
}

Document is the parsed view of a single file.

func Parse

func Parse(format Format, path string, raw []byte) (*Document, error)

Parse dispatches to the parser registered for format.

func ParseData

func ParseData(path string, raw []byte) (*Document, error)

ParseData loads a YAML/TOML/JSON data file into Document.Frontmatter (the whole file becomes the parsed map). Data files have no body or AST.

func ParseMarkdown

func ParseMarkdown(path string, raw []byte) (*Document, error)

ParseMarkdown builds a markdown Document: it extracts YAML/TOML/JSON frontmatter into a map and records where the body begins.

type Format

type Format string

Format identifies how a file is parsed and which rules apply to it.

const (
	Markdown Format = "markdown"
	Data     Format = "data"
)

type Line

type Line struct {
	Num     int    // 1-based line number
	Text    string // line content without the trailing newline
	Start   int    // byte offset of the line start in Document.Raw
	End     int    // byte offset just past Text (before the newline)
	InFence bool   // true when the line is INSIDE a fenced code block
}

Line is one source line with byte offsets and fenced-code-block state.

func SplitLines

func SplitLines(raw []byte) []Line

SplitLines splits raw into fence-aware Lines. A fence delimiter line toggles the in-fence state but is itself reported with InFence=false; only the lines strictly between an opening and closing delimiter are InFence=true.

type ParseFunc

type ParseFunc func(path string, raw []byte) (*Document, error)

ParseFunc builds a Document from a file's bytes.

type Table added in v0.2.0

type Table struct {
	Cols      int      // column count, taken from the header row
	HeaderIdx int      // Lines index of the header row
	SepIdx    int      // Lines index of the delimiter (separator) row
	RowIdxs   []int    // Lines indices of the data rows
	Align     []string // per-column alignment: "", "left", "center", "right"
}

Table describes one GFM pipe table located in a Document. All index fields (HeaderIdx, SepIdx, RowIdxs) are positions into Document.Lines, not line numbers, so callers can read the underlying Line for offsets and text.

func Tables added in v0.2.0

func Tables(d *Document) []Table

Tables finds every GFM pipe table in d. A table is a delimiter row whose preceding non-fenced line contains a pipe (the header), followed by zero or more data rows up to the next blank or non-pipe line. Fenced lines are skipped.

Jump to

Keyboard shortcuts

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