parser

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Jun 16, 2025 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package parser provides markdown parsing functionality and Abstract Syntax Tree definitions.

Index

Constants

View Source
const (
	// StrongEmphasisLevel defines the level for strong emphasis (**)
	StrongEmphasisLevel = 2
)

Constants

Variables

This section is empty.

Functions

func DebugString

func DebugString(doc *Document) string

DebugString returns a debug representation of a document

func NodeTypeString

func NodeTypeString(t NodeType) string

NodeTypeString returns a string representation of the node type

Types

type BasicParser

type BasicParser struct{}

BasicParser represents a simple placeholder parser (for testing)

func NewBasicParser

func NewBasicParser() *BasicParser

NewBasicParser creates a new basic parser

func (*BasicParser) Parse

func (p *BasicParser) Parse(content []byte) (*Document, error)

Parse implements a basic placeholder parser

func (*BasicParser) Validate

func (p *BasicParser) Validate() error

Validate validates the parser configuration

type CodeBlock

type CodeBlock struct {
	Language string
	Content  string
	Fenced   bool
	Fence    string
}

CodeBlock represents a code block node

func (*CodeBlock) String

func (n *CodeBlock) String() string

func (*CodeBlock) Type

func (n *CodeBlock) Type() NodeType

Type returns the node type for CodeBlock nodes.

type Document

type Document struct {
	Children []Node
}

Document represents the root document node

func (*Document) GetAllNodes

func (n *Document) GetAllNodes() []Node

GetAllNodes returns all nodes in the document as a flat slice.

func (*Document) String

func (n *Document) String() string

func (*Document) Type

func (n *Document) Type() NodeType

Type returns the node type for Document nodes.

type GoldmarkParser

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

GoldmarkParser implements the Parser interface using goldmark

func NewGoldmarkParser

func NewGoldmarkParser() *GoldmarkParser

NewGoldmarkParser creates a new goldmark-based parser

func (*GoldmarkParser) Parse

func (p *GoldmarkParser) Parse(content []byte) (*Document, error)

Parse parses the given markdown content and returns an AST

func (*GoldmarkParser) Validate

func (p *GoldmarkParser) Validate() error

Validate checks if the parser is properly configured

type Heading

type Heading struct {
	Level int
	Text  string
	Style string // "atx" or "setext"
}

Heading represents a heading node

func (*Heading) String

func (n *Heading) String() string

func (*Heading) Type

func (n *Heading) Type() NodeType

Type returns the node type for Heading nodes.

type List

type List struct {
	Ordered bool
	Items   []*ListItem
	Marker  string
}

List represents a list node

func (*List) String

func (n *List) String() string

func (*List) Type

func (n *List) Type() NodeType

Type returns the node type for List nodes.

type ListItem

type ListItem struct {
	Text     string
	Marker   string
	Children []Node // Support for nested lists and other elements
}

ListItem represents a list item node

func (*ListItem) String

func (n *ListItem) String() string

func (*ListItem) Type

func (n *ListItem) Type() NodeType

Type returns the node type for ListItem nodes.

type Node

type Node interface {
	Type() NodeType
	String() string
}

Node represents a basic node in the markdown AST

func FindFirstNode

func FindFirstNode(doc *Document, nodeType NodeType) Node

FindFirstNode finds the first node of a specific type

func FindNodes

func FindNodes(doc *Document, nodeType NodeType) []Node

FindNodes finds all nodes of a specific type in the tree

type NodeType

type NodeType int

NodeType represents the type of a node in the AST

const (
	// NodeDocument represents a document node containing the entire markdown structure
	NodeDocument NodeType = iota
	// NodeHeading represents a heading node (# Title)
	NodeHeading
	// NodeParagraph represents a paragraph of text
	NodeParagraph
	// NodeList represents an ordered or unordered list
	NodeList
	// NodeListItem represents a single item within a list
	NodeListItem
	// NodeCodeBlock represents a code block (fenced or indented)
	NodeCodeBlock
	// NodeText represents plain text content
	NodeText
)

type Paragraph

type Paragraph struct {
	Text string
}

Paragraph represents a paragraph node

func (*Paragraph) String

func (n *Paragraph) String() string

func (*Paragraph) Type

func (n *Paragraph) Type() NodeType

Type returns the node type for Paragraph nodes.

type Parser

type Parser interface {
	Parse(content []byte) (*Document, error)
	Validate() error
}

Parser interface defines methods for parsing Markdown content

func DefaultParser

func DefaultParser() Parser

DefaultParser returns the default parser implementation

func New

func New() Parser

New creates a new parser instance

type Text

type Text struct {
	Content string
}

Text represents a text node

func (*Text) String

func (n *Text) String() string

func (*Text) Type

func (n *Text) Type() NodeType

Type returns the node type for Text nodes.

type Walker

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

Walker provides a simple way to iterate over nodes

func NewWalker

func NewWalker(doc *Document) *Walker

NewWalker creates a new walker for the given document

func (*Walker) Next

func (w *Walker) Next() (Node, bool)

Next returns the next node in the walk

Jump to

Keyboard shortcuts

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