Skip to content

Commit

Permalink
chore: add node type field
Browse files Browse the repository at this point in the history
  • Loading branch information
boojack committed Jan 31, 2024
1 parent 1302420 commit c97c878
Show file tree
Hide file tree
Showing 33 changed files with 169 additions and 125 deletions.
68 changes: 36 additions & 32 deletions ast/ast.go
Original file line number Diff line number Diff line change
@@ -1,39 +1,41 @@
package ast

type NodeType uint32
type NodeType string

// Block nodes.
const (
UnknownNode NodeType = iota
// Block nodes.
LineBreakNode
ParagraphNode
CodeBlockNode
HeadingNode
HorizontalRuleNode
BlockquoteNode
OrderedListNode
UnorderedListNode
TaskListNode
MathBlockNode
TableNode
EmbeddedContentNode
// Inline nodes.
TextNode
BoldNode
ItalicNode
BoldItalicNode
CodeNode
ImageNode
LinkNode
AutoLinkNode
TagNode
StrikethroughNode
EscapingCharacterNode
MathNode
HighlightNode
SubscriptNode
SuperscriptNode
ReferencedContentNode
LineBreakNode NodeType = "LineBreak"
ParagraphNode NodeType = "Paragraph"
CodeBlockNode NodeType = "CodeBlock"
HeadingNode NodeType = "Heading"
HorizontalRuleNode NodeType = "HorizontalRule"
BlockquoteNode NodeType = "Blockquote"
OrderedListNode NodeType = "OrderedList"
UnorderedListNode NodeType = "UnorderedList"
TaskListNode NodeType = "TaskList"
MathBlockNode NodeType = "MathBlock"
TableNode NodeType = "Table"
EmbeddedContentNode NodeType = "EmbeddedContent"
)

// Inline nodes.
const (
TextNode NodeType = "Text"
BoldNode NodeType = "Bold"
ItalicNode NodeType = "Italic"
BoldItalicNode NodeType = "BoldItalic"
CodeNode NodeType = "Code"
ImageNode NodeType = "Image"
LinkNode NodeType = "Link"
AutoLinkNode NodeType = "AutoLink"
TagNode NodeType = "Tag"
StrikethroughNode NodeType = "Strikethrough"
EscapingCharacterNode NodeType = "EscapingCharacter"
MathNode NodeType = "Math"
HighlightNode NodeType = "Highlight"
SubscriptNode NodeType = "Subscript"
SuperscriptNode NodeType = "Superscript"
ReferencedContentNode NodeType = "ReferencedContent"
)

type Node interface {
Expand All @@ -57,6 +59,8 @@ type Node interface {
}

type BaseNode struct {
Type NodeType `json:"type"`

prevSibling Node

nextSibling Node
Expand Down
54 changes: 31 additions & 23 deletions ast/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ type BaseBlock struct {
BaseNode
}

func NewBaseBlock(nodeType NodeType) BaseBlock {
return BaseBlock{
BaseNode: BaseNode{
Type: nodeType,
},
}
}

type LineBreak struct {
BaseBlock
}
Expand All @@ -24,7 +32,7 @@ func (*LineBreak) Restore() string {
type Paragraph struct {
BaseBlock

Children []Node
Children []Node `json:"children"`
}

func (*Paragraph) Type() NodeType {
Expand All @@ -42,8 +50,8 @@ func (n *Paragraph) Restore() string {
type CodeBlock struct {
BaseBlock

Language string
Content string
Language string `json:"language"`
Content string `json:"content"`
}

func (*CodeBlock) Type() NodeType {
Expand All @@ -57,8 +65,8 @@ func (n *CodeBlock) Restore() string {
type Heading struct {
BaseBlock

Level int
Children []Node
Level int `json:"level"`
Children []Node `json:"children"`
}

func (*Heading) Type() NodeType {
Expand All @@ -81,7 +89,7 @@ type HorizontalRule struct {
BaseBlock

// Symbol is "*" or "-" or "_".
Symbol string
Symbol string `json:"symbol"`
}

func (*HorizontalRule) Type() NodeType {
Expand All @@ -95,7 +103,7 @@ func (n *HorizontalRule) Restore() string {
type Blockquote struct {
BaseBlock

Children []Node
Children []Node `json:"children"`
}

func (*Blockquote) Type() NodeType {
Expand All @@ -117,10 +125,10 @@ type OrderedList struct {
BaseBlock

// Number is the number of the list.
Number string
Number string `json:"number"`
// Indent is the number of spaces.
Indent int
Children []Node
Indent int `json:"indent"`
Children []Node `json:"children"`
}

func (*OrderedList) Type() NodeType {
Expand All @@ -139,10 +147,10 @@ type UnorderedList struct {
BaseBlock

// Symbol is "*" or "-" or "+".
Symbol string
Symbol string `json:"symbol"`
// Indent is the number of spaces.
Indent int
Children []Node
Indent int `json:"indent"`
Children []Node `json:"children"`
}

func (*UnorderedList) Type() NodeType {
Expand All @@ -161,11 +169,11 @@ type TaskList struct {
BaseBlock

// Symbol is "*" or "-" or "+".
Symbol string
Symbol string `json:"symbol"`
// Indent is the number of spaces.
Indent int
Complete bool
Children []Node
Indent int `json:"indent"`
Complete bool `json:"complete"`
Children []Node `json:"children"`
}

func (*TaskList) Type() NodeType {
Expand All @@ -187,7 +195,7 @@ func (n *TaskList) Restore() string {
type MathBlock struct {
BaseBlock

Content string
Content string `json:"content"`
}

func (*MathBlock) Type() NodeType {
Expand All @@ -201,9 +209,9 @@ func (n *MathBlock) Restore() string {
type Table struct {
BaseBlock

Header []string
Delimiter []string
Rows [][]string
Header []string `json:"header"`
Delimiter []string `json:"delimiter"`
Rows [][]string `json:"rows"`
}

func (*Table) Type() NodeType {
Expand Down Expand Up @@ -235,8 +243,8 @@ func (n *Table) Restore() string {
type EmbeddedContent struct {
BaseBlock

ResourceName string
Params string
ResourceName string `json:"resourceName"`
Params string `json:"params"`
}

func (*EmbeddedContent) Type() NodeType {
Expand Down
54 changes: 31 additions & 23 deletions ast/inline.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,18 @@ type BaseInline struct {
BaseNode
}

func NewBaseInline(nodeType NodeType) BaseInline {
return BaseInline{
BaseNode: BaseNode{
Type: nodeType,
},
}
}

type Text struct {
BaseInline

Content string
Content string `json:"content"`
}

func (*Text) Type() NodeType {
Expand All @@ -24,8 +32,8 @@ type Bold struct {
BaseInline

// Symbol is "*" or "_".
Symbol string
Children []Node
Symbol string `json:"symbol"`
Children []Node `json:"children"`
}

func (*Bold) Type() NodeType {
Expand All @@ -45,8 +53,8 @@ type Italic struct {
BaseInline

// Symbol is "*" or "_".
Symbol string
Content string
Symbol string `json:"symbol"`
Content string `json:"content"`
}

func (*Italic) Type() NodeType {
Expand All @@ -61,8 +69,8 @@ type BoldItalic struct {
BaseInline

// Symbol is "*" or "_".
Symbol string
Content string
Symbol string `json:"symbol"`
Content string `json:"content"`
}

func (*BoldItalic) Type() NodeType {
Expand All @@ -77,7 +85,7 @@ func (n *BoldItalic) Restore() string {
type Code struct {
BaseInline

Content string
Content string `json:"content"`
}

func (*Code) Type() NodeType {
Expand All @@ -91,8 +99,8 @@ func (n *Code) Restore() string {
type Image struct {
BaseInline

AltText string
URL string
AltText string `json:"altText"`
URL string `json:"url"`
}

func (*Image) Type() NodeType {
Expand All @@ -106,8 +114,8 @@ func (n *Image) Restore() string {
type Link struct {
BaseInline

Text string
URL string
Text string `json:"text"`
URL string `json:"url"`
}

func (*Link) Type() NodeType {
Expand All @@ -121,8 +129,8 @@ func (n *Link) Restore() string {
type AutoLink struct {
BaseInline

URL string
IsRawText bool
URL string `json:"url"`
IsRawText bool `json:"isRawText"`
}

func (*AutoLink) Type() NodeType {
Expand All @@ -139,7 +147,7 @@ func (n *AutoLink) Restore() string {
type Tag struct {
BaseInline

Content string
Content string `json:"content"`
}

func (*Tag) Type() NodeType {
Expand All @@ -153,7 +161,7 @@ func (n *Tag) Restore() string {
type Strikethrough struct {
BaseInline

Content string
Content string `json:"content"`
}

func (*Strikethrough) Type() NodeType {
Expand All @@ -167,7 +175,7 @@ func (n *Strikethrough) Restore() string {
type EscapingCharacter struct {
BaseInline

Symbol string
Symbol string `json:"symbol"`
}

func (*EscapingCharacter) Type() NodeType {
Expand All @@ -181,7 +189,7 @@ func (n *EscapingCharacter) Restore() string {
type Math struct {
BaseInline

Content string
Content string `json:"content"`
}

func (*Math) Type() NodeType {
Expand All @@ -195,7 +203,7 @@ func (n *Math) Restore() string {
type Highlight struct {
BaseInline

Content string
Content string `json:"content"`
}

func (*Highlight) Type() NodeType {
Expand All @@ -209,7 +217,7 @@ func (n *Highlight) Restore() string {
type Subscript struct {
BaseInline

Content string
Content string `json:"content"`
}

func (*Subscript) Type() NodeType {
Expand All @@ -223,7 +231,7 @@ func (n *Subscript) Restore() string {
type Superscript struct {
BaseInline

Content string
Content string `json:"content"`
}

func (*Superscript) Type() NodeType {
Expand All @@ -237,8 +245,8 @@ func (n *Superscript) Restore() string {
type ReferencedContent struct {
BaseInline

ResourceName string
Params string
ResourceName string `json:"resourceName"`
Params string `json:"params"`
}

func (*ReferencedContent) Type() NodeType {
Expand Down
Loading

0 comments on commit c97c878

Please sign in to comment.