2023-05-18 21:33:18 +08:00
|
|
|
package ast
|
2023-12-17 09:53:22 +08:00
|
|
|
|
|
|
|
type NodeType uint32
|
|
|
|
|
|
|
|
const (
|
|
|
|
UnknownNode NodeType = iota
|
|
|
|
// Block nodes.
|
|
|
|
LineBreakNode
|
|
|
|
ParagraphNode
|
|
|
|
CodeBlockNode
|
|
|
|
HeadingNode
|
|
|
|
HorizontalRuleNode
|
|
|
|
BlockquoteNode
|
|
|
|
OrderedListNode
|
|
|
|
UnorderedListNode
|
|
|
|
TaskListNode
|
|
|
|
// Inline nodes.
|
|
|
|
TextNode
|
|
|
|
BoldNode
|
|
|
|
ItalicNode
|
|
|
|
BoldItalicNode
|
|
|
|
CodeNode
|
|
|
|
ImageNode
|
|
|
|
LinkNode
|
2023-12-27 08:44:51 +08:00
|
|
|
AutoLinkNode
|
2023-12-17 09:53:22 +08:00
|
|
|
TagNode
|
|
|
|
StrikethroughNode
|
|
|
|
EscapingCharacterNode
|
|
|
|
)
|
|
|
|
|
|
|
|
func (t NodeType) String() string {
|
|
|
|
switch t {
|
|
|
|
case LineBreakNode:
|
|
|
|
return "LineBreakNode"
|
|
|
|
case ParagraphNode:
|
|
|
|
return "ParagraphNode"
|
|
|
|
case CodeBlockNode:
|
|
|
|
return "CodeBlockNode"
|
|
|
|
case HeadingNode:
|
|
|
|
return "HeadingNode"
|
|
|
|
case HorizontalRuleNode:
|
|
|
|
return "HorizontalRuleNode"
|
|
|
|
case BlockquoteNode:
|
|
|
|
return "BlockquoteNode"
|
|
|
|
case OrderedListNode:
|
|
|
|
return "OrderedListNode"
|
|
|
|
case UnorderedListNode:
|
|
|
|
return "UnorderedListNode"
|
|
|
|
case TaskListNode:
|
|
|
|
return "TaskListNode"
|
|
|
|
case TextNode:
|
|
|
|
return "TextNode"
|
|
|
|
case BoldNode:
|
|
|
|
return "BoldNode"
|
|
|
|
case ItalicNode:
|
|
|
|
return "ItalicNode"
|
|
|
|
case BoldItalicNode:
|
|
|
|
return "BoldItalicNode"
|
|
|
|
case CodeNode:
|
|
|
|
return "CodeNode"
|
|
|
|
case ImageNode:
|
|
|
|
return "ImageNode"
|
|
|
|
case LinkNode:
|
|
|
|
return "LinkNode"
|
2023-12-27 08:44:51 +08:00
|
|
|
case AutoLinkNode:
|
|
|
|
return "AutoLinkNode"
|
2023-12-17 09:53:22 +08:00
|
|
|
case TagNode:
|
|
|
|
return "TagNode"
|
|
|
|
case StrikethroughNode:
|
|
|
|
return "StrikethroughNode"
|
|
|
|
case EscapingCharacterNode:
|
|
|
|
return "EscapingCharacterNode"
|
|
|
|
default:
|
|
|
|
return "UnknownNode"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type Node interface {
|
|
|
|
// Type returns a node type.
|
|
|
|
Type() NodeType
|
|
|
|
|
|
|
|
// PrevSibling returns a previous sibling node of this node.
|
|
|
|
PrevSibling() Node
|
|
|
|
|
|
|
|
// NextSibling returns a next sibling node of this node.
|
|
|
|
NextSibling() Node
|
|
|
|
|
|
|
|
// SetPrevSibling sets a previous sibling node to this node.
|
|
|
|
SetPrevSibling(Node)
|
|
|
|
|
|
|
|
// SetNextSibling sets a next sibling node to this node.
|
|
|
|
SetNextSibling(Node)
|
|
|
|
}
|
|
|
|
|
|
|
|
type BaseNode struct {
|
|
|
|
prevSibling Node
|
|
|
|
|
|
|
|
nextSibling Node
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *BaseNode) PrevSibling() Node {
|
|
|
|
return n.prevSibling
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *BaseNode) NextSibling() Node {
|
|
|
|
return n.nextSibling
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *BaseNode) SetPrevSibling(node Node) {
|
|
|
|
n.prevSibling = node
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *BaseNode) SetNextSibling(node Node) {
|
|
|
|
n.nextSibling = node
|
|
|
|
}
|