memos/plugin/gomark/ast/ast.go

88 lines
1.6 KiB
Go
Raw Normal View History

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
2024-01-04 20:05:29 +08:00
MathBlockNode
2024-01-18 10:21:08 +08:00
TableNode
2024-01-20 01:56:10 +08:00
EmbeddedContentNode
2023-12-17 09:53:22 +08:00
// 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
2024-01-04 20:05:29 +08:00
MathNode
2024-01-15 22:30:06 +08:00
HighlightNode
SubscriptNode
SuperscriptNode
2023-12-17 09:53:22 +08:00
)
type Node interface {
// Type returns a node type.
Type() NodeType
2023-12-28 22:35:39 +08:00
// Restore returns a string representation of this node.
Restore() string
2023-12-17 09:53:22 +08:00
// 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
}
2023-12-28 22:35:39 +08:00
func IsBlockNode(node Node) bool {
switch node.Type() {
2024-01-23 21:23:40 +08:00
case ParagraphNode, CodeBlockNode, HeadingNode, HorizontalRuleNode, BlockquoteNode, OrderedListNode, UnorderedListNode, TaskListNode, MathBlockNode, TableNode, EmbeddedContentNode:
2023-12-28 22:35:39 +08:00
return true
default:
return false
}
}