memos/plugin/gomark/ast/block.go

231 lines
3.7 KiB
Go
Raw Normal View History

2023-12-12 23:24:02 +08:00
package ast
2024-01-14 22:19:03 +08:00
import (
"fmt"
"strings"
)
2023-12-28 22:35:39 +08:00
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
2024-01-14 22:19:03 +08:00
// Number is the number of the list.
Number string
// Indent is the number of spaces.
Indent int
2023-12-16 08:51:29 +08:00
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()
}
2024-01-14 22:19:03 +08:00
return fmt.Sprintf("%s%s. %s", strings.Repeat(" ", n.Indent), n.Number, result)
2023-12-28 22:35:39 +08:00
}
2023-12-16 08:51:29 +08:00
type UnorderedList struct {
BaseBlock
// Symbol is "*" or "-" or "+".
2024-01-14 22:19:03 +08:00
Symbol string
// Indent is the number of spaces.
Indent int
2023-12-16 08:51:29 +08:00
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()
}
2024-01-14 22:19:03 +08:00
return fmt.Sprintf("%s%s %s", strings.Repeat(" ", n.Indent), n.Symbol, result)
2023-12-28 22:35:39 +08:00
}
2023-12-16 12:48:52 +08:00
type TaskList struct {
BaseBlock
// Symbol is "*" or "-" or "+".
2024-01-14 22:19:03 +08:00
Symbol string
// Indent is the number of spaces.
Indent int
2023-12-16 12:48:52 +08:00
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"
}
2024-01-14 22:19:03 +08:00
return fmt.Sprintf("%s%s [%s] %s", strings.Repeat(" ", n.Indent), n.Symbol, complete, result)
2023-12-28 22:35:39 +08:00
}
2024-01-04 20:05:29 +08:00
type MathBlock struct {
BaseBlock
Content string
}
func (*MathBlock) Type() NodeType {
return MathBlockNode
}
func (n *MathBlock) Restore() string {
return fmt.Sprintf("$$\n%s\n$$", n.Content)
}
2024-01-18 10:21:08 +08:00
type Table struct {
BaseBlock
2024-01-18 11:36:13 +08:00
Header []string
Delimiter []string
2024-01-18 10:21:08 +08:00
Rows [][]string
}
func (*Table) Type() NodeType {
return TableNode
}
func (n *Table) Restore() string {
var result string
for _, header := range n.Header {
result += fmt.Sprintf("| %s ", header)
}
result += "|\n"
for _, d := range n.Delimiter {
2024-01-18 11:36:13 +08:00
result += fmt.Sprintf("| %s ", d)
2024-01-18 10:21:08 +08:00
}
result += "|\n"
for index, row := range n.Rows {
for _, cell := range row {
result += fmt.Sprintf("| %s ", cell)
}
result += "|"
if index != len(n.Rows)-1 {
result += "\n"
}
}
return result
}