ast

package
v2.0.0 Latest Latest
Warning

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

Go to latest
Published: Aug 27, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package ast defines AST nodes that represent markdown elements.

Index

Constants

This section is empty.

Variables

View Source
var KindAutoLink = NewNodeKind("AutoLink")

KindAutoLink is a NodeKind of the AutoLink node.

View Source
var KindBlockquote = NewNodeKind("Blockquote")

KindBlockquote is a NodeKind of the Blockquote node.

View Source
var KindCodeBlock = NewNodeKind("CodeBlock")

KindCodeBlock is a NodeKind of the CodeBlock node.

View Source
var KindCodeSpan = NewNodeKind("CodeSpan")

KindCodeSpan is a NodeKind of the CodeSpan node.

View Source
var KindDocument = NewNodeKind("Document")

KindDocument is a NodeKind of the Document node.

View Source
var KindEmphasis = NewNodeKind("Emphasis")

KindEmphasis is a NodeKind of the Emphasis node.

View Source
var KindHTMLBlock = NewNodeKind("HTMLBlock")

KindHTMLBlock is a NodeKind of the HTMLBlock node.

View Source
var KindHeading = NewNodeKind("Heading")

KindHeading is a NodeKind of the Heading node.

View Source
var KindImage = NewNodeKind("Image")

KindImage is a NodeKind of the Image node.

View Source
var KindLink = NewNodeKind("Link")

KindLink is a NodeKind of the Link node.

View Source
var KindLinkReferenceDefinition = NewNodeKind("LinkReferenceDefinition")

KindLinkReferenceDefinition is a NodeKind of the LinkReferenceDefinition node.

View Source
var KindList = NewNodeKind("List")

KindList is a NodeKind of the List node.

View Source
var KindListItem = NewNodeKind("ListItem")

KindListItem is a NodeKind of the ListItem node.

View Source
var KindParagraph = NewNodeKind("Paragraph")

KindParagraph is a NodeKind of the Paragraph node.

View Source
var KindRawHTML = NewNodeKind("RawHTML")

KindRawHTML is a NodeKind of the RawHTML node.

View Source
var KindStrong = NewNodeKind("Strong")

KindStrong is a NodeKind of the Strong node.

View Source
var KindText = NewNodeKind("Text")

KindText is a NodeKind of the Text node.

View Source
var KindThematicBreak = NewNodeKind("ThematicBreak")

KindThematicBreak is a NodeKind of the ThematicBreak node.

Functions

func IsParagraph

func IsParagraph(node Node) bool

IsParagraph returns true if the given node is a Paragraph node, otherwise false.

func Walk

func Walk(n Node, walker Walker) error

Walk walks a AST tree by the depth first search algorithm.

func WithLinkTitle

func WithLinkTitle(title textm.MultiLineValue) interface {
	LinkOption
	LinkReferenceDefinitionOption
}

WithLinkTitle returns a LinkOption that sets the title of a link or image.

Types

type Attribute

type Attribute struct {
	Name  string
	Value textm.MultiLineValue
}

An Attribute is an attribute of the Node.

type AutoLink struct {
	BaseInline

	// Destination is the URL used for the href attribute.
	// For email autolinks, it includes the "mailto:" prefix.
	Destination textm.SingleLineValue

	// Label is the display text shown inside the link element.
	Label textm.SingleLineValue

	// Text is the original text as parsed from source, including any
	// surrounding syntax characters (e.g. "<" and ">" for CommonMark autolinks).
	Text textm.SingleLineValue
}

An AutoLink struct represents an autolink of the Markdown text.

func NewAutoLink(destination, label textm.SingleLineValue, opts ...AutoLinkOption) *AutoLink

NewAutoLink returns a new AutoLink node with the given destination, label, and options.

func (*AutoLink) Dump

func (n *AutoLink) Dump(_ []byte) *NodeDump

Dump implements Node.Dump.

func (*AutoLink) Kind

func (n *AutoLink) Kind() NodeKind

Kind implements Node.Kind.

type AutoLinkOption

type AutoLinkOption interface {
	// contains filtered or unexported methods
}

AutoLinkOption is an option for AutoLink nodes.

func WithAutoLinkText

func WithAutoLinkText(text textm.SingleLineValue) AutoLinkOption

WithAutoLinkText returns an AutoLinkOption that sets the original source text of an autolink.

type BaseBlock

type BaseBlock struct {
	BaseNode
	// contains filtered or unexported fields
}

A BaseBlock struct implements the Node interface partially.

func (*BaseBlock) AppendSource

func (b *BaseBlock) AppendSource(seg textm.Segment)

AppendSource implements BlockNode.AppendSource.

func (*BaseBlock) HasBlankPreviousLines

func (b *BaseBlock) HasBlankPreviousLines() bool

HasBlankPreviousLines implements Node.HasBlankPreviousLines.

func (*BaseBlock) SetBlankPreviousLines

func (b *BaseBlock) SetBlankPreviousLines(v bool)

SetBlankPreviousLines implements Node.SetBlankPreviousLines.

func (*BaseBlock) SetSource

func (b *BaseBlock) SetSource(v []textm.Segment)

SetSource implements BlockNode.SetSource.

func (*BaseBlock) Source

func (b *BaseBlock) Source() []textm.Segment

Source implements BlockNode.Source.

type BaseInline

type BaseInline struct {
	BaseNode
}

A BaseInline struct implements the Node interface partially.

type BaseNode

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

A BaseNode struct implements the Node interface partially.

func (*BaseNode) AppendChild

func (n *BaseNode) AppendChild(v Node)

AppendChild implements Node.AppendChild .

func (*BaseNode) Attribute

func (n *BaseNode) Attribute(name string) (textm.MultiLineValue, bool)

Attribute implements Node.Attribute.

func (*BaseNode) Attributes

func (n *BaseNode) Attributes() []Attribute

Attributes implements Node.Attributes.

func (*BaseNode) ChildCount

func (n *BaseNode) ChildCount() int

ChildCount implements Node.ChildCount .

func (*BaseNode) Children

func (n *BaseNode) Children() iter.Seq[Node]

Children implements Node.Children .

func (*BaseNode) FirstChild

func (n *BaseNode) FirstChild() Node

FirstChild implements Node.FirstChild .

func (*BaseNode) HasChildren

func (n *BaseNode) HasChildren() bool

HasChildren implements Node.HasChildren .

func (*BaseNode) Init

func (n *BaseNode) Init(self Node)

Init initializes this node. Init must be called in each node's constructor.

func (*BaseNode) InsertAfter

func (n *BaseNode) InsertAfter(target, insertee Node)

InsertAfter implements Node.InsertAfter .

func (*BaseNode) InsertBefore

func (n *BaseNode) InsertBefore(target, insertee Node)

InsertBefore implements Node.InsertBefore .

func (*BaseNode) LastChild

func (n *BaseNode) LastChild() Node

LastChild implements Node.LastChild .

func (*BaseNode) NextSibling

func (n *BaseNode) NextSibling() Node

NextSibling implements Node.NextSibling .

func (*BaseNode) OwnerDocument

func (n *BaseNode) OwnerDocument() *Document

OwnerDocument implements Node.OwnerDocument.

func (*BaseNode) Parent

func (n *BaseNode) Parent() Node

Parent implements Node.Parent .

func (*BaseNode) Pos

func (n *BaseNode) Pos() int

Pos implements Node.Pos .

func (*BaseNode) PreviousSibling

func (n *BaseNode) PreviousSibling() Node

PreviousSibling implements Node.PreviousSibling .

func (*BaseNode) RemoveAttributes

func (n *BaseNode) RemoveAttributes()

RemoveAttributes implements Node.RemoveAttributes.

func (*BaseNode) RemoveChild

func (n *BaseNode) RemoveChild(v Node)

RemoveChild implements Node.RemoveChild .

func (*BaseNode) RemoveChildren

func (n *BaseNode) RemoveChildren()

RemoveChildren implements Node.RemoveChildren .

func (*BaseNode) ReplaceChild

func (n *BaseNode) ReplaceChild(target, insertee Node)

ReplaceChild implements Node.ReplaceChild .

func (*BaseNode) SetAttribute

func (n *BaseNode) SetAttribute(name string, value textm.MultiLineValue)

SetAttribute implements Node.SetAttribute.

func (*BaseNode) SetNextSibling

func (n *BaseNode) SetNextSibling(v Node)

SetNextSibling implements Node.SetNextSibling .

func (*BaseNode) SetParent

func (n *BaseNode) SetParent(v Node)

SetParent implements Node.SetParent .

func (*BaseNode) SetPos

func (n *BaseNode) SetPos(v int)

SetPos implements Node.SetPos .

func (*BaseNode) SetPreviousSibling

func (n *BaseNode) SetPreviousSibling(v Node)

SetPreviousSibling implements Node.SetPreviousSibling .

type BlockNode

type BlockNode interface {
	Node

	// HasBlankPreviousLines returns true if the row before this node is blank,
	// otherwise false.
	HasBlankPreviousLines() bool

	// SetBlankPreviousLines sets whether the row before this node is blank.
	SetBlankPreviousLines(v bool)

	// Source returns text segments that hold positions in a source.
	// For nodes whose content is parsed into inline nodes, this holds the raw
	// source text used as the input for inline parsing. Nodes that do not parse
	// their content as inline elements return an empty slice.
	Source() []textm.Segment

	// SetSource sets text segments that hold positions in a source.
	SetSource([]textm.Segment)

	// AppendSource appends a text segment to the source.
	AppendSource(textm.Segment)
	// contains filtered or unexported methods
}

A BlockNode interface is a Node that represents a block element. Some block nodes (e.g. Paragraph, Heading) hold source text segments that are later parsed into inline nodes; others (e.g. CodeBlock, HTMLBlock) do not use this mechanism and leave Source empty.

type Blockquote

type Blockquote struct {
	BaseBlock
}

A Blockquote struct represents an blockquote block of Markdown text.

func NewBlockquote

func NewBlockquote() *Blockquote

NewBlockquote returns a new Blockquote node.

func (*Blockquote) Dump

func (n *Blockquote) Dump(_ []byte) *NodeDump

Dump implements Node.Dump .

func (*Blockquote) Kind

func (n *Blockquote) Kind() NodeKind

Kind implements Node.Kind.

type CodeBlock

type CodeBlock struct {
	BaseBlock

	// CodeBlockKind indicates whether this is an indented or fenced code block.
	CodeBlockKind CodeBlockKind

	// Info is the info string of a fenced code block (e.g. language identifier).
	// It is empty for indented code blocks.
	Info textm.SingleLineValue

	// Value holds the raw content of this code block for rendering.
	Value textm.Lines
}

A CodeBlock struct represents a code block of Markdown text.

func NewCodeBlock

func NewCodeBlock(kind CodeBlockKind, value textm.Lines, opts ...CodeBlockOption) *CodeBlock

NewCodeBlock returns a new CodeBlock node with the given kind and value.

func (*CodeBlock) Dump

func (n *CodeBlock) Dump(_ []byte) *NodeDump

Dump implements Node.Dump.

func (*CodeBlock) Kind

func (n *CodeBlock) Kind() NodeKind

Kind implements Node.Kind.

func (*CodeBlock) Language

func (n *CodeBlock) Language(source []byte) (string, bool)

Language returns the language extracted from the info string. Language returns false if there is no info string.

type CodeBlockKind

type CodeBlockKind int

CodeBlockKind represents the kind of a CodeBlock: indented or fenced.

const (
	// CodeBlockKindIndented indicates an indented code block (4-space or tab indent).
	CodeBlockKindIndented CodeBlockKind = iota + 1
	// CodeBlockKindFenced indicates a fenced code block (“` or ~~~).
	CodeBlockKindFenced
)

func (CodeBlockKind) String

func (k CodeBlockKind) String() string

String returns a human-readable name of the CodeBlockKind.

type CodeBlockOption

type CodeBlockOption interface {
	// contains filtered or unexported methods
}

CodeBlockOption is an option for CodeBlock nodes.

func WithCodeBlockInfo

func WithCodeBlockInfo(info textm.SingleLineValue) CodeBlockOption

WithCodeBlockInfo returns a CodeBlockOption that sets the info string of a fenced code block.

type CodeSpan

type CodeSpan struct {
	BaseInline

	// Value holds the content of this code span.
	// The content is sourced from the raw Markdown text and may span multiple
	// source lines.
	//
	// This value is expected to be a single line; CommonMark requires that code span content is
	// normalized to a single line by replacing newlines with spaces.
	//
	// Code spans that span multiple lines and return multiple indices from [text.Value].Indices()
	// should return normalized single-line data from the Bytes, Str, and Value methods of [text.Value].
	Value textm.Value
}

A CodeSpan struct represents a code span of Markdown text.

func NewCodeSpan

func NewCodeSpan(value textm.Value) *CodeSpan

NewCodeSpan returns a new CodeSpan node with the given value.

Given value is expected to be a single line; CommonMark requires that code span content is normalized to a single line by replacing newlines with spaces.

You should use CodeBlock for code that you really want to treat as multiple lines.

Code spans that span multiple lines and return multiple indices from [text.Value].Indices() should return normalized single-line data from the Bytes, Str, and Value methods of [text.Value].

func (*CodeSpan) Dump

func (n *CodeSpan) Dump(_ []byte) *NodeDump

Dump implements Node.Dump.

func (*CodeSpan) IsBlank

func (n *CodeSpan) IsBlank(source []byte) bool

IsBlank returns true if this node consists of spaces, otherwise false.

func (*CodeSpan) Kind

func (n *CodeSpan) Kind() NodeKind

Kind implements Node.Kind.

type Document

type Document struct {
	BaseBlock
	// contains filtered or unexported fields
}

A Document struct is a root node of Markdown text.

func NewDocument

func NewDocument() *Document

NewDocument returns a new Document node.

func (*Document) AddMeta

func (n *Document) AddMeta(key string, value any)

AddMeta adds given metadata to this document.

func (*Document) Dump

func (n *Document) Dump(_ []byte) *NodeDump

Dump implements Node.Dump .

func (*Document) Kind

func (n *Document) Kind() NodeKind

Kind implements Node.Kind.

func (*Document) Metadata

func (n *Document) Metadata() map[string]any

Metadata returns metadata of this document.

func (*Document) OwnerDocument

func (n *Document) OwnerDocument() *Document

OwnerDocument implements Node.OwnerDocument.

func (*Document) Pos

func (n *Document) Pos() int

Pos implements Node.Pos.

func (*Document) SetMetadata

func (n *Document) SetMetadata(meta map[string]any)

SetMetadata sets given metadata to this document.

type Emphasis

type Emphasis struct {
	BaseInline
}

An Emphasis struct represents an emphasis of Markdown text (e.g. *text* or _text_).

func NewEmphasis

func NewEmphasis() *Emphasis

NewEmphasis returns a new Emphasis node.

func (*Emphasis) Dump

func (n *Emphasis) Dump(_ []byte) *NodeDump

Dump implements Node.Dump.

func (*Emphasis) Kind

func (n *Emphasis) Kind() NodeKind

Kind implements Node.Kind.

type HTMLBlock

type HTMLBlock struct {
	BaseBlock

	// HTMLBlockKind is the kind of this html block.
	HTMLBlockKind HTMLBlockKind

	// Value holds the raw HTML content of this block for rendering.
	Value textm.Lines
}

An HTMLBlock struct represents an html block of Markdown text.

func NewHTMLBlock

func NewHTMLBlock(kind HTMLBlockKind) *HTMLBlock

NewHTMLBlock returns a new HTMLBlock node.

func (*HTMLBlock) Dump

func (n *HTMLBlock) Dump(_ []byte) *NodeDump

Dump implements Node.Dump.

func (*HTMLBlock) Kind

func (n *HTMLBlock) Kind() NodeKind

Kind implements Node.Kind.

type HTMLBlockKind

type HTMLBlockKind int

HTMLBlockKind represents the kind of an HTMLBlock. See https://spec.commonmark.org/0.30/#html-blocks

const (
	// HTMLBlockKind1 represents type 1 html blocks.
	HTMLBlockKind1 HTMLBlockKind = iota + 1
	// HTMLBlockKind2 represents type 2 html blocks.
	HTMLBlockKind2
	// HTMLBlockKind3 represents type 3 html blocks.
	HTMLBlockKind3
	// HTMLBlockKind4 represents type 4 html blocks.
	HTMLBlockKind4
	// HTMLBlockKind5 represents type 5 html blocks.
	HTMLBlockKind5
	// HTMLBlockKind6 represents type 6 html blocks.
	HTMLBlockKind6
	// HTMLBlockKind7 represents type 7 html blocks.
	HTMLBlockKind7
)

func (HTMLBlockKind) String

func (k HTMLBlockKind) String() string

String returns a human-readable name of the HTMLBlockKind.

type Heading

type Heading struct {
	BaseBlock
	// Level is the level of this heading.
	// This value is between 1 and 6.
	Level int
	// HeadingKind indicates whether this is an ATX or Setext heading.
	HeadingKind HeadingKind
}

A Heading struct represents headings like SetextHeading and ATXHeading.

func NewHeading

func NewHeading(level int, kind HeadingKind) *Heading

NewHeading returns a new Heading node.

func (*Heading) Dump

func (n *Heading) Dump(_ []byte) *NodeDump

Dump implements Node.Dump .

func (*Heading) Kind

func (n *Heading) Kind() NodeKind

Kind implements Node.Kind.

type HeadingKind

type HeadingKind int

HeadingKind represents the kind of a Heading: ATX or Setext.

const (
	// HeadingKindATX represents an ATX heading (e.g. ## heading).
	HeadingKindATX HeadingKind = iota + 1
	// HeadingKindSetext represents a Setext heading (underline style).
	HeadingKindSetext
)

func (HeadingKind) String

func (k HeadingKind) String() string

String returns a human-readable name of the HeadingKind.

type Image

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

An Image struct represents an image of the Markdown text.

func NewImage

func NewImage(destination textm.SingleLineValue, opts ...LinkOption) *Image

NewImage returns a new Image node with the given destination and options.

func (*Image) Dump

func (n *Image) Dump(_ []byte) *NodeDump

Dump implements Node.Dump.

func (*Image) Kind

func (n *Image) Kind() NodeKind

Kind implements Node.Kind.

type InlineNode

type InlineNode interface {
	Node
	// contains filtered or unexported methods
}

An InlineNode interface is a Node that represents an inline element.

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

A Link struct represents a link of the Markdown text.

func NewLink(destination textm.SingleLineValue, opts ...LinkOption) *Link

NewLink returns a new Link node with the given destination and options.

func (*Link) Dump

func (n *Link) Dump(_ []byte) *NodeDump

Dump implements Node.Dump.

func (*Link) Kind

func (n *Link) Kind() NodeKind

Kind implements Node.Kind.

type LinkOption

type LinkOption interface {
	// contains filtered or unexported methods
}

LinkOption is an option for Link and Image nodes.

func WithLinkReference

func WithLinkReference(kind ReferenceLinkKind, value textm.MultiLineValue) LinkOption

WithLinkReference returns a LinkOption that sets the reference of a link or image.

type LinkReferenceDefinition

type LinkReferenceDefinition struct {
	BaseBlock

	// Label is a label of this link reference definition.
	Label textm.MultiLineValue

	// Destination is a destination of this link reference definition.
	Destination textm.SingleLineValue

	// Title is a title of this link reference definition.
	Title textm.MultiLineValue
}

A LinkReferenceDefinition struct represents a list of Markdown text.

func NewLinkReferenceDefinition

func NewLinkReferenceDefinition(
	label textm.MultiLineValue, destination textm.SingleLineValue,
	opts ...LinkReferenceDefinitionOption) *LinkReferenceDefinition

NewLinkReferenceDefinition returns a new LinkReferenceDefinition node.

func (*LinkReferenceDefinition) Dump

func (l *LinkReferenceDefinition) Dump(_ []byte) *NodeDump

Dump implements Node.Dump.

func (*LinkReferenceDefinition) Kind

Kind implements Node.Kind.

type LinkReferenceDefinitionOption

type LinkReferenceDefinitionOption interface {
	// contains filtered or unexported methods
}

LinkReferenceDefinitionOption is an option for LinkReferenceDefinition nodes.

type List

type List struct {
	BaseBlock

	// Marker is a marker character like '-', '+', ')' and '.'.
	Marker byte

	// IsTight is a true if this list is a 'tight' list.
	// See https://spec.commonmark.org/0.30/#loose for details.
	IsTight bool

	// Start is an initial number of this ordered list.
	// If this list is not an ordered list, Start is 0.
	Start int
}

A List struct represents a list of Markdown text.

func NewList

func NewList(marker byte) *List

NewList returns a new List node.

func (*List) CanContinue

func (l *List) CanContinue(marker byte, isOrdered bool) bool

CanContinue returns true if this list can continue with the given marker character and orderedness, otherwise false.

func (*List) Dump

func (l *List) Dump(_ []byte) *NodeDump

Dump implements Node.Dump.

func (*List) IsOrdered

func (l *List) IsOrdered() bool

IsOrdered returns true if this list is an ordered list, otherwise false.

func (*List) Kind

func (l *List) Kind() NodeKind

Kind implements Node.Kind.

type ListItem

type ListItem struct {
	BaseBlock
	// contains filtered or unexported fields
}

A ListItem struct represents a list item of Markdown text.

func NewListItem

func NewListItem() *ListItem

NewListItem returns a new ListItem node.

func (*ListItem) Dump

func (n *ListItem) Dump(_ []byte) *NodeDump

Dump implements Node.Dump.

func (*ListItem) Kind

func (n *ListItem) Kind() NodeKind

Kind implements Node.Kind.

func (*ListItem) Offset

func (n *ListItem) Offset() int

Offset returns the content indentation offset used during parsing. This is set by the list parser and is not meaningful for programmatic construction.

func (*ListItem) SetOffset

func (n *ListItem) SetOffset(v int)

SetOffset sets the content indentation offset. This is called by the list parser during parsing.

type Node

type Node interface {
	// Kind returns a kind of this node.
	Kind() NodeKind

	// Pos returns a position of this node in a source.
	// If this node position is not defined, Pos returns -1.
	Pos() int

	// SetPos sets a position of this node in a source.
	// Some node may ignore this method. For example, Paragraph node ignores this method because
	// it calculates its position from its lines.
	SetPos(v int)

	// NextSibling returns a next sibling node of this node.
	NextSibling() Node

	// PreviousSibling returns a previous sibling node of this node.
	PreviousSibling() Node

	// Parent returns a parent node of this node.
	Parent() Node

	// SetParent sets a parent node to this node.
	SetParent(Node)

	// SetPreviousSibling sets a previous sibling node to this node.
	SetPreviousSibling(Node)

	// SetNextSibling sets a next sibling node to this node.
	SetNextSibling(Node)

	// HasChildren returns true if this node has any children, otherwise false.
	HasChildren() bool

	// ChildCount returns a total number of children.
	ChildCount() int

	// Children returns an iterator of children of this node.
	Children() iter.Seq[Node]

	// FirstChild returns a first child of this node.
	FirstChild() Node

	// LastChild returns a last child of this node.
	LastChild() Node

	// AppendChild append a node child to the tail of the children.
	AppendChild(child Node)

	// RemoveChild removes a node child from this node.
	// If a node child is not children of this node, RemoveChild nothing to do.
	RemoveChild(child Node)

	// RemoveChildren removes all children from this node.
	RemoveChildren()

	// ReplaceChild replaces a node target with a node insertee.
	// If target is not a child of this node, ReplaceChild appends insertee to the
	// tail of the children.
	ReplaceChild(target, insertee Node)

	// InsertBefore inserts a node insertee before a node target.
	// If target is not a child of this node, InsertBefore appends insertee to the
	// tail of the children.
	InsertBefore(target, insertee Node)

	// InsertAfter inserts a node insertee after a node target.
	// If target is not a child of this node, InsertAfter appends insertee to the
	// tail of the children.
	InsertAfter(target, insertee Node)

	// OwnerDocument returns this node's owner document.
	// If this node is not a child of the Document node, OwnerDocument
	// returns nil.
	OwnerDocument() *Document

	// Dump dumps an AST node.
	Dump(source []byte) *NodeDump

	// SetAttribute sets the given value to the attributes.
	SetAttribute(name string, value textm.MultiLineValue)

	// Attribute returns a (attribute value, true) if an attribute
	// associated with the given name is found, otherwise
	// (zero MultiLineValue, false)
	Attribute(name string) (textm.MultiLineValue, bool)

	// Attributes returns a list of attributes.
	// This may be a nil if there are no attributes.
	Attributes() []Attribute

	// RemoveAttributes removes all attributes from this node.
	RemoveAttributes()
}

A Node interface defines basic AST node functionalities.

func N

func N(node Node, children ...any) Node

N is a helper function to create a new node with children.

Children must be a Node or a string. If a child is a string, N creates a Text node with the string and append it to the children. Given string must be a decoded string:

- Good: `N(NewParagraph(), "Hello, World!")` - Bad: `N(NewParagraph(), "Hello, World&#33;")`

N panics if a child is not a Node or a string.

type NodeDump

type NodeDump struct {
	// Node is the node to be dumped.
	Node Node

	// Properties is a map of additional properties to be dumped.
	// Property names should be PascalCase.
	Properties map[string]any
}

NodeDump is a struct that holds information for dumping a node.

func NewNodeDump

func NewNodeDump(node Node, properties map[string]any) *NodeDump

NewNodeDump returns a new NodeDump.

func (*NodeDump) Children

func (d *NodeDump) Children(source []byte) iter.Seq[*NodeDump]

Children returns an iterator of children of this node dump.

func (*NodeDump) PrettyPrint

func (d *NodeDump) PrettyPrint(w io.Writer, source []byte, opts ...PrettyPrintOption) error

PrettyPrint pretty prints this node dump to the given writer.

PrettyPrint format may change in the future. Do not rely on the format. This method is intended for debugging purpose only. The returned error is from the writer. If the writer never returns an error, the returned error is always nil.

type NodeKind

type NodeKind int

NodeKind identifies the concrete type of a Node.

var CurrentKindValue NodeKind

CurrentKindValue is the current value of NodeKind.

You **MUST NOT** use this value. This value is public for the goldmark internal package to use.

func NewNodeKind

func NewNodeKind(name string) NodeKind

NewNodeKind returns a new Kind value.

func (NodeKind) String

func (k NodeKind) String() string

type Paragraph

type Paragraph struct {
	BaseBlock
}

A Paragraph struct represents a paragraph of Markdown text.

func NewParagraph

func NewParagraph() *Paragraph

NewParagraph returns a new Paragraph node.

func (*Paragraph) Dump

func (n *Paragraph) Dump(_ []byte) *NodeDump

Dump implements Node.Dump .

func (*Paragraph) Kind

func (n *Paragraph) Kind() NodeKind

Kind implements Node.Kind.

func (*Paragraph) Pos

func (n *Paragraph) Pos() int

Pos implements Node.Pos.

type PrettyPrintOption

type PrettyPrintOption func(*ppCfg)

PrettyPrintOption is a function that modifies the configuration of PrettyPrint.

func WithLevel

func WithLevel(level int) PrettyPrintOption

WithLevel returns an option that sets the indentation level for PrettyPrint.

func WithSource

func WithSource(include bool) PrettyPrintOption

WithSource returns an option that sets whether to include the source text in the PrettyPrint output.

type RawHTML

type RawHTML struct {
	BaseInline

	// Value holds the raw HTML content.
	Value textm.MultiLineValue
}

A RawHTML struct represents an inline raw HTML of the Markdown text.

func NewRawHTML

func NewRawHTML(value textm.MultiLineValue) *RawHTML

NewRawHTML returns a new RawHTML node with the given value.

func (*RawHTML) Dump

func (n *RawHTML) Dump(_ []byte) *NodeDump

Dump implements Node.Dump.

func (*RawHTML) Kind

func (n *RawHTML) Kind() NodeKind

Kind implements Node.Kind.

type ReferenceLink struct {
	// ReferenceLinkKind is the kind of this reference link.
	ReferenceLinkKind ReferenceLinkKind

	// Value is a value of this reference link.
	Value textm.MultiLineValue
}

ReferenceLink struct represents a reference link of the Markdown text.

func NewReferenceLink(kind ReferenceLinkKind, value textm.MultiLineValue) *ReferenceLink

NewReferenceLink returns a new ReferenceLink with the given kind and value.

type ReferenceLinkKind

type ReferenceLinkKind int

ReferenceLinkKind represents the kind of a reference link.

const (
	// ReferenceLinkKindFull indicates that a reference link has a full reference like [foo][bar].
	ReferenceLinkKindFull ReferenceLinkKind = iota + 1
	// ReferenceLinkKindCollapsed indicates that a reference link has a collapsed reference like [foo][].
	ReferenceLinkKindCollapsed
	// ReferenceLinkKindShortcut indicates that a reference link has a shortcut reference like [foo].
	ReferenceLinkKindShortcut
)

func (ReferenceLinkKind) String

func (t ReferenceLinkKind) String() string

String returns a string representation of this reference link kind.

type Strong

type Strong struct {
	BaseInline
}

A Strong struct represents strong importance of Markdown text (e.g. **text** or __text__).

func NewStrong

func NewStrong() *Strong

NewStrong returns a new Strong node.

func (*Strong) Dump

func (n *Strong) Dump(_ []byte) *NodeDump

Dump implements Node.Dump.

func (*Strong) Kind

func (n *Strong) Kind() NodeKind

Kind implements Node.Kind.

type Text

type Text struct {
	BaseInline
	// Value is the text value. It is either a source position or an owned string.
	Value textm.SingleLineValue
	// contains filtered or unexported fields
}

A Text struct represents a textual content of the Markdown text.

func NewText

func NewText(value textm.SingleLineValue) *Text

NewText returns a new Text node.

func (*Text) Dump

func (n *Text) Dump(_ []byte) *NodeDump

Dump implements Node.Dump.

func (*Text) HardLineBreak

func (n *Text) HardLineBreak() bool

HardLineBreak returns true if this node ends with a hard line break. See https://spec.commonmark.org/0.30/#hard-line-breaks for details.

func (*Text) Kind

func (n *Text) Kind() NodeKind

Kind implements Node.Kind.

func (*Text) Pos

func (n *Text) Pos() int

Pos implements Node.Pos.

func (*Text) SetHardLineBreak

func (n *Text) SetHardLineBreak(v bool)

SetHardLineBreak sets whether this node ends with a hard line break.

func (*Text) SetSoftLineBreak

func (n *Text) SetSoftLineBreak(v bool)

SetSoftLineBreak sets whether this node ends with a new line.

func (*Text) SoftLineBreak

func (n *Text) SoftLineBreak() bool

SoftLineBreak returns true if this node ends with a new line, otherwise false.

type ThematicBreak

type ThematicBreak struct {
	BaseBlock
}

A ThematicBreak struct represents a thematic break of Markdown text.

func NewThematicBreak

func NewThematicBreak() *ThematicBreak

NewThematicBreak returns a new ThematicBreak node.

func (*ThematicBreak) Dump

func (n *ThematicBreak) Dump(_ []byte) *NodeDump

Dump implements Node.Dump .

func (*ThematicBreak) Kind

func (n *ThematicBreak) Kind() NodeKind

Kind implements Node.Kind.

type WalkStatus

type WalkStatus int

WalkStatus represents a current status of the Walk function.

const (
	// WalkStop indicates no more walking needed.
	WalkStop WalkStatus = iota + 1

	// WalkSkipChildren indicates that Walk will not walk on children of the current
	// node.
	WalkSkipChildren

	// WalkContinue indicates that Walk can continue to walk.
	WalkContinue
)

type Walker

type Walker func(n Node, entering bool) (WalkStatus, error)

Walker is a function that will be called when Walk find a new node. entering is set true before walks children, false after walked children. If Walker returns error, Walk function immediately stop walking.

Jump to

Keyboard shortcuts

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