formatter

package
v0.2.5 Latest Latest
Warning

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

Go to latest
Published: Jun 17, 2025 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package formatter provides formatting functionality for markdown nodes.

Index

Constants

View Source
const (
	// HeadingFormatterPriority defines the priority for heading formatting (higher runs first)
	HeadingFormatterPriority = 100
	// ParagraphFormatterPriority defines the priority for paragraph formatting
	ParagraphFormatterPriority = 90
	// ListFormatterPriority defines the priority for list formatting
	ListFormatterPriority = 80
	// CodeFormatterPriority defines the priority for code block formatting
	CodeFormatterPriority = 70
	// WhitespaceFormatterPriority defines the priority for whitespace formatting (lowest)
	WhitespaceFormatterPriority = 10
	// InlineFormatterPriority defines the priority for inline formatting
	InlineFormatterPriority = 60

	// AtxHeadingStyle represents ATX-style heading format (# ## ###)
	AtxHeadingStyle = "atx"
	// SetextHeadingStyle represents setext-style heading format (underlined with = or -)
	SetextHeadingStyle = "setext"

	// MinHeadingLevel defines the minimum allowed heading level
	MinHeadingLevel = 1
	// MaxHeadingLevel defines the maximum allowed heading level
	MaxHeadingLevel = 6
	// SetextMaxLevel defines the maximum level for setext-style headings
	SetextMaxLevel = 2
)

Constants

Variables

This section is empty.

Functions

This section is empty.

Types

type BaseFormatter

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

BaseFormatter provides common functionality for formatters

func (*BaseFormatter) Name

func (f *BaseFormatter) Name() string

Name returns the formatter name

func (*BaseFormatter) Priority

func (f *BaseFormatter) Priority() int

Priority returns the formatter priority

type CodeBlockFormatter

type CodeBlockFormatter struct {
	BaseFormatter
}

CodeBlockFormatter formats code block nodes

func (*CodeBlockFormatter) CanFormat

func (f *CodeBlockFormatter) CanFormat(nodeType parser.NodeType) bool

CanFormat returns true if this formatter can handle code blocks

func (*CodeBlockFormatter) Format

func (f *CodeBlockFormatter) Format(node parser.Node, cfg *config.Config) error

Format formats code block nodes

type Engine

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

Engine represents the main formatting engine

func New

func New() *Engine

New creates a new formatting engine with default formatters

func (*Engine) Format

func (e *Engine) Format(doc *parser.Document, cfg *config.Config) error

Format formats the given AST according to configuration

func (*Engine) Register

func (e *Engine) Register(formatter NodeFormatter)

Register registers a new node formatter

func (*Engine) RegisterDefaults

func (e *Engine) RegisterDefaults()

RegisterDefaults registers the default formatters

type Formatter

type Formatter interface {
	// Format formats the given AST according to configuration
	Format(root parser.Node, cfg *config.Config) error
}

Formatter represents a markdown formatter interface

type HeadingFormatter

type HeadingFormatter struct {
	BaseFormatter
}

HeadingFormatter formats heading nodes

func NewHeadingFormatter

func NewHeadingFormatter() *HeadingFormatter

NewHeadingFormatter creates a new heading formatter

func (*HeadingFormatter) CanFormat

func (f *HeadingFormatter) CanFormat(nodeType parser.NodeType) bool

CanFormat returns true if this formatter can handle headings

func (*HeadingFormatter) Format

func (f *HeadingFormatter) Format(node parser.Node, cfg *config.Config) error

Format applies heading formatting rules.

type InlineFormatter

type InlineFormatter struct {
	BaseFormatter
}

InlineFormatter handles inline elements like links, emphasis, and inline code

func NewInlineFormatter

func NewInlineFormatter() *InlineFormatter

NewInlineFormatter creates a new inline formatter

func (*InlineFormatter) CanFormat

func (f *InlineFormatter) CanFormat(nodeType parser.NodeType) bool

CanFormat returns true if this formatter can handle text nodes (where inline elements are)

func (*InlineFormatter) Format

func (f *InlineFormatter) Format(node parser.Node, _ *config.Config) error

Format applies inline formatting rules

type ListFormatter

type ListFormatter struct {
	BaseFormatter
}

ListFormatter formats list nodes

func (*ListFormatter) CanFormat

func (f *ListFormatter) CanFormat(nodeType parser.NodeType) bool

CanFormat returns true if this formatter can handle lists

func (*ListFormatter) Format

func (f *ListFormatter) Format(node parser.Node, cfg *config.Config) error

Format applies list formatting rules.

type NodeFormatter

type NodeFormatter interface {
	// Name returns the name of the formatter
	Name() string
	// CanFormat returns true if this formatter can handle the given node type
	CanFormat(nodeType parser.NodeType) bool
	// Format formats a specific node
	Format(node parser.Node, cfg *config.Config) error
	// Priority returns the priority of this formatter (higher = earlier)
	Priority() int
}

NodeFormatter represents a formatter for specific node types

type ParagraphFormatter

type ParagraphFormatter struct {
	BaseFormatter
}

ParagraphFormatter formats paragraph nodes

func (*ParagraphFormatter) CanFormat

func (f *ParagraphFormatter) CanFormat(nodeType parser.NodeType) bool

CanFormat returns true if this formatter can handle paragraphs

func (*ParagraphFormatter) Format

func (f *ParagraphFormatter) Format(node parser.Node, cfg *config.Config) error

Format applies paragraph formatting rules with text reflow.

type WhitespaceFormatter

type WhitespaceFormatter struct {
	BaseFormatter
}

WhitespaceFormatter handles whitespace normalization

func (*WhitespaceFormatter) CanFormat

func (f *WhitespaceFormatter) CanFormat(_ parser.NodeType) bool

CanFormat returns true for all node types (whitespace affects everything)

func (*WhitespaceFormatter) Format

func (f *WhitespaceFormatter) Format(node parser.Node, cfg *config.Config) error

Format applies whitespace normalization rules.

Jump to

Keyboard shortcuts

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