memos/plugin/gomark/ast/block.go

173 lines
2.6 KiB
Go
Raw Normal View History

2023-12-12 23:24:02 +08:00
package ast
2023-12-28 22:35:39 +08:00
import "fmt"
2023-12-12 23:24:02 +08:00
type BaseBlock struct {
2023-12-13 23:50:05 +08:00
BaseNode
2023-12-12 23:24:02 +08:00
}
2023-12-12 23:38:43 +08:00
type LineBreak struct {
BaseBlock
}
func (*LineBreak) Type() NodeType {
2023-12-14 22:21:23 +08:00
return LineBreakNode
2023-12-13 23:50:05 +08:00
}
2023-12-28 22:35:39 +08:00
func (*LineBreak) Restore() string {
return "\n"
}
2023-12-12 23:24:02 +08:00
type Paragraph struct {
BaseBlock
Children []Node
}
func (*Paragraph) Type() NodeType {
2023-12-14 22:21:23 +08:00
return ParagraphNode
2023-12-13 23:50:05 +08:00
}
2023-12-28 22:35:39 +08:00
func (n *Paragraph) Restore() string {
var result string
for _, child := range n.Children {
result += child.Restore()
}
return result
}
2023-12-12 23:24:02 +08:00
type CodeBlock struct {
BaseBlock
Language string
Content string
}
func (*CodeBlock) Type() NodeType {
2023-12-14 22:21:23 +08:00
return CodeBlockNode
2023-12-13 23:50:05 +08:00
}
2023-12-28 22:35:39 +08:00
func (n *CodeBlock) Restore() string {
return fmt.Sprintf("```%s\n%s\n```", n.Language, n.Content)
}
2023-12-13 09:06:47 +08:00
type Heading struct {
BaseBlock
Level int
Children []Node
}
func (*Heading) Type() NodeType {
2023-12-14 22:21:23 +08:00
return HeadingNode
2023-12-13 23:50:05 +08:00
}
2023-12-28 22:35:39 +08:00
func (n *Heading) Restore() string {
var result string
for _, child := range n.Children {
result += child.Restore()
}
symbol := ""
for i := 0; i < n.Level; i++ {
symbol += "#"
}
return fmt.Sprintf("%s %s", symbol, result)
}
2023-12-13 21:00:13 +08:00
type HorizontalRule struct {
BaseBlock
// Symbol is "*" or "-" or "_".
Symbol string
}
func (*HorizontalRule) Type() NodeType {
2023-12-14 22:21:23 +08:00
return HorizontalRuleNode
2023-12-13 23:50:05 +08:00
}
2023-12-28 22:35:39 +08:00
func (n *HorizontalRule) Restore() string {
return n.Symbol + n.Symbol + n.Symbol
}
2023-12-13 21:00:13 +08:00
type Blockquote struct {
BaseBlock
Children []Node
}
func (*Blockquote) Type() NodeType {
2023-12-14 22:21:23 +08:00
return BlockquoteNode
2023-12-13 23:50:05 +08:00
}
2023-12-16 08:51:29 +08:00
2023-12-28 22:35:39 +08:00
func (n *Blockquote) Restore() string {
var result string
for _, child := range n.Children {
result += child.Restore()
}
return fmt.Sprintf("> %s", result)
}
2023-12-16 08:51:29 +08:00
type OrderedList struct {
BaseBlock
Number string
Children []Node
}
func (*OrderedList) Type() NodeType {
return OrderedListNode
}
2023-12-28 22:35:39 +08:00
func (n *OrderedList) Restore() string {
var result string
for _, child := range n.Children {
result += child.Restore()
}
return fmt.Sprintf("%s. %s", n.Number, result)
}
2023-12-16 08:51:29 +08:00
type UnorderedList struct {
BaseBlock
// Symbol is "*" or "-" or "+".
Symbol string
Children []Node
}
func (*UnorderedList) Type() NodeType {
return UnorderedListNode
}
2023-12-16 12:48:52 +08:00
2023-12-28 22:35:39 +08:00
func (n *UnorderedList) Restore() string {
var result string
for _, child := range n.Children {
result += child.Restore()
}
return fmt.Sprintf("%s %s", n.Symbol, result)
}
2023-12-16 12:48:52 +08:00
type TaskList struct {
BaseBlock
// Symbol is "*" or "-" or "+".
Symbol string
Complete bool
Children []Node
}
func (*TaskList) Type() NodeType {
return TaskListNode
}
2023-12-28 22:35:39 +08:00
func (n *TaskList) Restore() string {
var result string
for _, child := range n.Children {
result += child.Restore()
}
complete := " "
if n.Complete {
complete = "x"
}
return fmt.Sprintf("%s [%s] %s", n.Symbol, complete, result)
}