Documentation ¶
Overview ¶
Package ast defines AST nodes that represent markdown elements.
Index ¶
- Constants
- Variables
- func DumpHelper(w io.Writer, v Node, source []byte, level int, kv map[string]string, ...)
- func IsParagraph(node Node) bool
- func MergeOrAppendTextSegment(parent Node, s textm.Segment)
- func MergeOrReplaceTextSegment(parent Node, n Node, s textm.Segment)
- func Walk(n Node, walker Walker) error
- type Attribute
- type AutoLink
- type AutoLinkType
- type BaseBlock
- func (b *BaseBlock) HasBlankPreviousLines() bool
- func (b *BaseBlock) IsRaw() bool
- func (b *BaseBlock) LeadingWhitespace() textm.Segment
- func (b *BaseBlock) Lines() *textm.Segments
- func (b *BaseBlock) SetBlankPreviousLines(v bool)
- func (b *BaseBlock) SetLeadingWhitespace(v textm.Segment)
- func (b *BaseBlock) SetLines(v *textm.Segments)
- func (b *BaseBlock) Type() NodeType
- type BaseInline
- func (b *BaseInline) HasBlankPreviousLines() bool
- func (b *BaseInline) IsRaw() bool
- func (b *BaseInline) LeadingWhitespace() textm.Segment
- func (b *BaseInline) Lines() *textm.Segments
- func (b *BaseInline) SetBlankPreviousLines(v bool)
- func (b *BaseInline) SetLeadingWhitespace(v textm.Segment)
- func (b *BaseInline) SetLines(v *textm.Segments)
- func (b *BaseInline) Type() NodeType
- type BaseNode
- func (n *BaseNode) AppendChild(self, v Node)
- func (n *BaseNode) Attribute(name []byte) (interface{}, bool)
- func (n *BaseNode) AttributeString(s string) (interface{}, bool)
- func (n *BaseNode) Attributes() []Attribute
- func (n *BaseNode) ChildCount() int
- func (n *BaseNode) FirstChild() Node
- func (n *BaseNode) HasChildren() bool
- func (n *BaseNode) InsertAfter(self, v1, insertee Node)
- func (n *BaseNode) InsertBefore(self, v1, insertee Node)
- func (n *BaseNode) LastChild() Node
- func (n *BaseNode) NextSibling() Node
- func (n *BaseNode) Parent() Node
- func (n *BaseNode) PreviousSibling() Node
- func (n *BaseNode) RemoveAttributes()
- func (n *BaseNode) RemoveChild(self, v Node)
- func (n *BaseNode) RemoveChildren(self Node)
- func (n *BaseNode) ReplaceChild(self, v1, insertee Node)
- func (n *BaseNode) SetAttribute(name []byte, value interface{})
- func (n *BaseNode) SetAttributeString(name string, value interface{})
- func (n *BaseNode) SetNextSibling(v Node)
- func (n *BaseNode) SetParent(v Node)
- func (n *BaseNode) SetPreviousSibling(v Node)
- func (n *BaseNode) SortChildren(comparator func(n1, n2 Node) int)
- func (n *BaseNode) Text(source []byte) []byte
- type Blockquote
- type CodeBlock
- type CodeSpan
- type Document
- type Emphasis
- type FencedCodeBlock
- type HTMLBlock
- type HTMLBlockType
- type Heading
- type Image
- type Link
- type LinkReferenceDefinition
- type LinkReferenceType
- type List
- type ListItem
- type Node
- type NodeKind
- type NodeType
- type Paragraph
- type RawHTML
- type String
- type Text
- func (n *Text) Dump(w io.Writer, source []byte, level int)
- func (n *Text) HardLineBreak() bool
- func (n *Text) Inline()
- func (n *Text) IsRaw() bool
- func (n *Text) Kind() NodeKind
- func (n *Text) Merge(node Node, source []byte) bool
- func (n *Text) SetHardLineBreak(v bool)
- func (n *Text) SetRaw(v bool)
- func (n *Text) SetSoftLineBreak(v bool)
- func (n *Text) SoftLineBreak() bool
- func (n *Text) Text(source []byte) []byte
- type TextBlock
- type ThematicBreak
- type WalkStatus
- type Walker
- type Whitespace
Constants ¶
const ( // LinkNoReference indicates that a link is not a reference. LinkNoReference = iota // LinkFullReference indicates a full reference. LinkFullReference // LinkCollapsedReference indicates a collapsed reference. LinkCollapsedReference // LinkShortcutReference indicates a shortcut reference. LinkShortcutReference )
Variables ¶
var KindAutoLink = NewNodeKind("AutoLink")
KindAutoLink is a NodeKind of the AutoLink node.
var KindBlockquote = NewNodeKind("Blockquote")
KindBlockquote is a NodeKind of the Blockquote node.
var KindCodeBlock = NewNodeKind("CodeBlock")
KindCodeBlock is a NodeKind of the CodeBlock node.
var KindCodeSpan = NewNodeKind("CodeSpan")
KindCodeSpan is a NodeKind of the CodeSpan node.
var KindDocument = NewNodeKind("Document")
KindDocument is a NodeKind of the Document node.
var KindEmphasis = NewNodeKind("Emphasis")
KindEmphasis is a NodeKind of the Emphasis node.
var KindFencedCodeBlock = NewNodeKind("FencedCodeBlock")
KindFencedCodeBlock is a NodeKind of the FencedCodeBlock node.
var KindHTMLBlock = NewNodeKind("HTMLBlock")
KindHTMLBlock is a NodeKind of the HTMLBlock node.
var KindHeading = NewNodeKind("Heading")
KindHeading is a NodeKind of the Heading node.
var KindImage = NewNodeKind("Image")
KindImage is a NodeKind of the Image node.
var KindLink = NewNodeKind("Link")
KindLink is a NodeKind of the Link node.
var KindLinkReferenceDefinition = NewNodeKind("LinkReferenceDefinition")
KindLinkReferenceDefinition is a NodeKind of the LinkReferenceDefinition node.
var KindList = NewNodeKind("List")
KindList is a NodeKind of the List node.
var KindListItem = NewNodeKind("ListItem")
KindListItem is a NodeKind of the ListItem node.
var KindParagraph = NewNodeKind("Paragraph")
KindParagraph is a NodeKind of the Paragraph node.
var KindRawHTML = NewNodeKind("RawHTML")
KindRawHTML is a NodeKind of the RawHTML node.
var KindString = NewNodeKind("String")
KindString is a NodeKind of the String node.
var KindText = NewNodeKind("Text")
KindText is a NodeKind of the Text node.
var KindTextBlock = NewNodeKind("TextBlock")
KindTextBlock is a NodeKind of the TextBlock node.
var KindThematicBreak = NewNodeKind("ThematicBreak")
KindThematicBreak is a NodeKind of the ThematicBreak node.
var KindWhitespace = NewNodeKind("Whitespace")
KindWhitespace is a NodeKind of the Whitespace node.
Functions ¶
func DumpHelper ¶
DumpHelper is a helper function to implement Node.Dump. kv is pairs of an attribute name and an attribute value. cb is a function called after wrote a name and attributes.
func IsParagraph ¶
IsParagraph returns true if the given node implements the Paragraph interface, otherwise false.
func MergeOrAppendTextSegment ¶
MergeOrAppendTextSegment merges a given s into the last child of the parent if it can be merged, otherwise creates a new Text node and appends it to after current last child.
func MergeOrReplaceTextSegment ¶
MergeOrReplaceTextSegment merges a given s into a previous sibling of the node n if a previous sibling of the node n is *Text, otherwise replaces Node n with s.
Types ¶
type Attribute ¶
type Attribute struct { Name []byte Value interface{} }
An Attribute is an attribute of the Node
type AutoLink ¶
type AutoLink struct { BaseInline // Type is a type of this autolink. AutoLinkType AutoLinkType // Protocol specified a protocol of the link. Protocol []byte // contains filtered or unexported fields }
An AutoLink struct represents an autolink of the Markdown text.
func NewAutoLink ¶
func NewAutoLink(typ AutoLinkType, value *Text) *AutoLink
NewAutoLink returns a new AutoLink node.
type AutoLinkType ¶
type AutoLinkType int
AutoLinkType defines kind of auto links.
const ( // AutoLinkEmail indicates that an autolink is an email address. AutoLinkEmail AutoLinkType = iota + 1 // AutoLinkURL indicates that an autolink is a generic URL. AutoLinkURL )
type BaseBlock ¶
type BaseBlock struct { BaseNode // contains filtered or unexported fields }
A BaseBlock struct implements the Node interface.
func (*BaseBlock) HasBlankPreviousLines ¶
HasBlankPreviousLines implements Node.HasBlankPreviousLines.
func (*BaseBlock) LeadingWhitespace ¶
LeadingWhitespace returns the leading whitespace for this block, if any.
func (*BaseBlock) SetBlankPreviousLines ¶
SetBlankPreviousLines implements Node.SetBlankPreviousLines.
func (*BaseBlock) SetLeadingWhitespace ¶
SetLeadingWhitespace sets the leading whitespace for this block.
type BaseInline ¶
type BaseInline struct {
BaseNode
}
A BaseInline struct implements the Node interface.
func (*BaseInline) HasBlankPreviousLines ¶
func (b *BaseInline) HasBlankPreviousLines() bool
HasBlankPreviousLines implements Node.HasBlankPreviousLines.
func (*BaseInline) LeadingWhitespace ¶
func (b *BaseInline) LeadingWhitespace() textm.Segment
LeadingWhitespace implements Node.LeadingWhitespace.
func (*BaseInline) SetBlankPreviousLines ¶
func (b *BaseInline) SetBlankPreviousLines(v bool)
SetBlankPreviousLines implements Node.SetBlankPreviousLines.
func (*BaseInline) SetLeadingWhitespace ¶
func (b *BaseInline) SetLeadingWhitespace(v textm.Segment)
SetLeadingWhitespace implements Node.SetLeadingWhitespace.
func (*BaseInline) SetLines ¶
func (b *BaseInline) SetLines(v *textm.Segments)
SetLines implements Node.SetLines
type BaseNode ¶
type BaseNode struct {
// contains filtered or unexported fields
}
A BaseNode struct implements the Node interface.
func (*BaseNode) AppendChild ¶
AppendChild implements Node.AppendChild .
func (*BaseNode) AttributeString ¶
AttributeString implements Node.AttributeString.
func (*BaseNode) Attributes ¶
Attributes implements Node.Attributes
func (*BaseNode) ChildCount ¶
ChildCount implements Node.ChildCount .
func (*BaseNode) FirstChild ¶
FirstChild implements Node.FirstChild .
func (*BaseNode) HasChildren ¶
HasChildren implements Node.HasChildren .
func (*BaseNode) InsertAfter ¶
InsertAfter implements Node.InsertAfter .
func (*BaseNode) InsertBefore ¶
InsertBefore implements Node.InsertBefore .
func (*BaseNode) NextSibling ¶
NextSibling implements Node.NextSibling .
func (*BaseNode) PreviousSibling ¶
PreviousSibling implements Node.PreviousSibling .
func (*BaseNode) RemoveAttributes ¶
func (n *BaseNode) RemoveAttributes()
RemoveAttributes implements Node.RemoveAttributes
func (*BaseNode) RemoveChild ¶
RemoveChild implements Node.RemoveChild .
func (*BaseNode) RemoveChildren ¶
RemoveChildren implements Node.RemoveChildren .
func (*BaseNode) ReplaceChild ¶
ReplaceChild implements Node.ReplaceChild .
func (*BaseNode) SetAttribute ¶
SetAttribute implements Node.SetAttribute.
func (*BaseNode) SetAttributeString ¶
SetAttributeString implements Node.SetAttributeString
func (*BaseNode) SetNextSibling ¶
SetNextSibling implements Node.SetNextSibling .
func (*BaseNode) SetPreviousSibling ¶
SetPreviousSibling implements Node.SetPreviousSibling .
func (*BaseNode) SortChildren ¶
SortChildren implements Node.SortChildren
type Blockquote ¶
type Blockquote struct {
BaseBlock
}
A Blockquote struct represents an blockquote block of Markdown text.
type CodeBlock ¶
type CodeBlock struct {
BaseBlock
}
A CodeBlock interface represents an indented code block of Markdown text.
func (*CodeBlock) SetLeadingWhitespace ¶
SetLeadingWhitespace implements Node.SetLeadingWhitespace.
type CodeSpan ¶
type CodeSpan struct { BaseInline // Backticks is the number of backticks used to open and close this code span. Backticks int }
A CodeSpan struct represents a code span of Markdown text.
func NewCodeSpan ¶
NewCodeSpan returns a new CodeSpan node.
type Document ¶
type Document struct {
BaseBlock
}
A Document struct is a root node of Markdown text.
type Emphasis ¶
type Emphasis struct { BaseInline // Marker is the character used to indicate emphasis. Marker byte // Level is a level of the emphasis. Level int }
An Emphasis struct represents an emphasis of Markdown text.
func NewEmphasis ¶
NewEmphasis returns a new Emphasis node with the given level.
type FencedCodeBlock ¶
type FencedCodeBlock struct { BaseBlock // Fence returns the fence used for this code block. Fence []byte // Info returns a info text of this fenced code block. Info *Text // contains filtered or unexported fields }
A FencedCodeBlock struct represents a fenced code block of Markdown text.
func NewFencedCodeBlock ¶
func NewFencedCodeBlock(fence []byte, info *Text) *FencedCodeBlock
NewFencedCodeBlock return a new FencedCodeBlock node.
func (*FencedCodeBlock) Dump ¶
func (n *FencedCodeBlock) Dump(w io.Writer, source []byte, level int)
Dump implements Node.Dump .
func (*FencedCodeBlock) Language ¶
func (n *FencedCodeBlock) Language(source []byte) []byte
Language returns an language in an info string. Language returns nil if this node does not have an info string.
type HTMLBlock ¶
type HTMLBlock struct { BaseBlock // Type is a type of this html block. HTMLBlockType HTMLBlockType // ClosureLine is a line that closes this html block. ClosureLine textm.Segment }
An HTMLBlock struct represents an html block of Markdown text.
func NewHTMLBlock ¶
func NewHTMLBlock(typ HTMLBlockType) *HTMLBlock
NewHTMLBlock returns a new HTMLBlock node.
func (*HTMLBlock) HasClosure ¶
HasClosure returns true if this html block has a closure line, otherwise false.
func (*HTMLBlock) SetLeadingWhitespace ¶
SetLeadingWhitespace implements Node.SetLeadingWhitespace.
type HTMLBlockType ¶
type HTMLBlockType int
HTMLBlockType represents kinds of an html blocks. See https://spec.commonmark.org/0.29/#html-blocks
const ( // HTMLBlockType1 represents type 1 html blocks HTMLBlockType1 HTMLBlockType = iota + 1 // HTMLBlockType2 represents type 2 html blocks HTMLBlockType2 // HTMLBlockType3 represents type 3 html blocks HTMLBlockType3 // HTMLBlockType4 represents type 4 html blocks HTMLBlockType4 // HTMLBlockType5 represents type 5 html blocks HTMLBlockType5 // HTMLBlockType6 represents type 6 html blocks HTMLBlockType6 // HTMLBlockType7 represents type 7 html blocks HTMLBlockType7 )
type Heading ¶
type Heading struct { BaseBlock // IsSetext is true if the heading is a setext heading. IsSetext bool // Level returns a level of this heading. // This value is between 1 and 6. Level int }
A Heading struct represents headings like SetextHeading and ATXHeading.
func NewHeading ¶
NewHeading returns a new Heading node.
type Image ¶
type Image struct {
// contains filtered or unexported fields
}
An Image struct represents an image of the Markdown text.
type Link ¶
type Link struct {
// contains filtered or unexported fields
}
A Link struct represents a link of the Markdown text.
type LinkReferenceDefinition ¶
type LinkReferenceDefinition struct { BaseBlock // Label is the label of this link reference definition. Label []byte // Destination is the destination(URL) of this link reference definition. Destination []byte // Title is the title of this link reference definition. Title []byte }
A LinkReferenceDefinition struct represents a link reference definition in the Markdown text.
func NewLinkReferenceDefinition ¶
func NewLinkReferenceDefinition() *LinkReferenceDefinition
NewLinkReferenceDefinition returns a new LinkReferenceDefinition node.
func (*LinkReferenceDefinition) Dump ¶
func (n *LinkReferenceDefinition) Dump(w io.Writer, source []byte, level int)
Dump implements Node.Dump.
func (*LinkReferenceDefinition) IsRaw ¶
func (n *LinkReferenceDefinition) IsRaw() bool
IsRaw implements Node.IsRaw.
func (*LinkReferenceDefinition) Kind ¶
func (n *LinkReferenceDefinition) Kind() NodeKind
Kind implements Node.Kind.
type LinkReferenceType ¶
type LinkReferenceType int
LinkReferenceType defines the kinds of references used by links.
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.29/#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 (*List) CanContinue ¶
CanContinue returns true if this list can continue with the given mark and a list type, otherwise false.
type ListItem ¶
A ListItem struct represents a list item of Markdown text.
type Node ¶
type Node interface { // Type returns a type of this node. Type() NodeType // Kind returns a kind of this node. Kind() NodeKind // 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 // 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(self, 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(self, child Node) // RemoveChildren removes all children from this node. RemoveChildren(self Node) // SortChildren sorts childrens by comparator. SortChildren(comparator func(n1, n2 Node) int) // ReplaceChild replace a node v1 with a node insertee. // If v1 is not children of this node, ReplaceChild append a insetee to the // tail of the children. ReplaceChild(self, v1, insertee Node) // InsertBefore inserts a node insertee before a node v1. // If v1 is not children of this node, InsertBefore append a insetee to the // tail of the children. InsertBefore(self, v1, insertee Node) // InsertAfterinserts a node insertee after a node v1. // If v1 is not children of this node, InsertBefore append a insetee to the // tail of the children. InsertAfter(self, v1, insertee Node) // Dump dumps an AST tree structure to stdout. // This function completely aimed for debugging. // level is a indent level. Implementer should indent informations with // 2 * level spaces. Dump(w io.Writer, source []byte, level int) // Text returns text values of this node. Text(source []byte) []byte // HasBlankPreviousLines returns true if the row before this node is blank, // otherwise false. // This method is valid only for block nodes. HasBlankPreviousLines() bool // SetBlankPreviousLines sets whether the row before this node is blank. // This method is valid only for block nodes. SetBlankPreviousLines(v bool) // LeadingWhitespace returns the leading whitespace for this node, if any. // This method is valid only for block nodes. LeadingWhitespace() textm.Segment // SetLeadingWhitespace sets the leading whitespace for this node. // This method is valid only for block nodes. SetLeadingWhitespace(v textm.Segment) // Lines returns text segments that hold positions in a source. // This method is valid only for block nodes. Lines() *textm.Segments // SetLines sets text segments that hold positions in a source. // This method is valid only for block nodes. SetLines(*textm.Segments) // IsRaw returns true if contents should be rendered as 'raw' contents. IsRaw() bool // SetAttribute sets the given value to the attributes. SetAttribute(name []byte, value interface{}) // SetAttributeString sets the given value to the attributes. SetAttributeString(name string, value interface{}) // Attribute returns a (attribute value, true) if an attribute // associated with the given name is found, otherwise // (nil, false) Attribute(name []byte) (interface{}, bool) // AttributeString returns a (attribute value, true) if an attribute // associated with the given name is found, otherwise // (nil, false) AttributeString(name string) (interface{}, 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.
type Paragraph ¶
type Paragraph struct {
BaseBlock
}
A Paragraph struct represents a paragraph of Markdown text.
type RawHTML ¶
type RawHTML struct { BaseInline Segments *textm.Segments }
A RawHTML struct represents an inline raw HTML of the Markdown text.
type String ¶
type String struct { BaseInline Value []byte // contains filtered or unexported fields }
A String struct is a textual content that has a concrete value
func (*String) IsCode ¶
IsCode returns true if this text should be rendered without any modifications.
func (*String) IsRaw ¶
IsRaw returns true if this text should be rendered without unescaping back slash escapes and resolving references.
func (*String) SetCode ¶
SetCode sets whether this text should be rendered without any modifications.
type Text ¶
type Text struct { BaseInline // Segment is a position in a source text. Segment textm.Segment // contains filtered or unexported fields }
A Text struct represents a textual content of the Markdown text.
func NewRawTextSegment ¶
NewRawTextSegment returns a new Text node with the given source position. The new node should be rendered as raw contents.
func NewTextSegment ¶
NewTextSegment returns a new Text node with the given source position.
func (*Text) HardLineBreak ¶
HardLineBreak returns true if this node ends with a hard line break. See https://spec.commonmark.org/0.29/#hard-line-breaks for details.
func (*Text) IsRaw ¶
IsRaw returns true if this text should be rendered without unescaping back slash escapes and resolving references.
func (*Text) Merge ¶
Merge merges a Node n into this node. Merge returns true if the given node has been merged, otherwise false.
func (*Text) SetHardLineBreak ¶
SetHardLineBreak sets whether this node ends with a hard line break.
func (*Text) SetSoftLineBreak ¶
SetSoftLineBreak sets whether this node ends with a new line.
func (*Text) SoftLineBreak ¶
SoftLineBreak returns true if this node ends with a new line, otherwise false.
type TextBlock ¶
type TextBlock struct {
BaseBlock
}
A TextBlock struct is a node whose lines should be rendered without any containers.
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.
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 wont walk on children of 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.
type Whitespace ¶
type Whitespace struct { BaseInline // Segment is the position of the whitespace in the source text. Segment textm.Segment }
Whitespace represents leading whitespace for lines inside a Markdown paragraph.
func NewWhitespace ¶
func NewWhitespace(v textm.Segment) *Whitespace
NewWhitespace returns a new Whitespace node.
func (*Whitespace) Dump ¶
func (n *Whitespace) Dump(w io.Writer, source []byte, level int)
Dump implements Node.Dump.
func (*Whitespace) Text ¶
func (n *Whitespace) Text(source []byte) []byte
Text implements Node.Text.