mirror of
https://github.com/usememos/memos.git
synced 2025-06-05 22:09:59 +02:00
feat: implement restore nodes
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
package ast
|
||||
|
||||
import "fmt"
|
||||
|
||||
type BaseBlock struct {
|
||||
BaseNode
|
||||
}
|
||||
@@ -12,6 +14,10 @@ func (*LineBreak) Type() NodeType {
|
||||
return LineBreakNode
|
||||
}
|
||||
|
||||
func (*LineBreak) Restore() string {
|
||||
return "\n"
|
||||
}
|
||||
|
||||
type Paragraph struct {
|
||||
BaseBlock
|
||||
|
||||
@@ -22,6 +28,14 @@ func (*Paragraph) Type() NodeType {
|
||||
return ParagraphNode
|
||||
}
|
||||
|
||||
func (n *Paragraph) Restore() string {
|
||||
var result string
|
||||
for _, child := range n.Children {
|
||||
result += child.Restore()
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
type CodeBlock struct {
|
||||
BaseBlock
|
||||
|
||||
@@ -33,6 +47,10 @@ func (*CodeBlock) Type() NodeType {
|
||||
return CodeBlockNode
|
||||
}
|
||||
|
||||
func (n *CodeBlock) Restore() string {
|
||||
return fmt.Sprintf("```%s\n%s\n```", n.Language, n.Content)
|
||||
}
|
||||
|
||||
type Heading struct {
|
||||
BaseBlock
|
||||
|
||||
@@ -44,6 +62,18 @@ func (*Heading) Type() NodeType {
|
||||
return HeadingNode
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
type HorizontalRule struct {
|
||||
BaseBlock
|
||||
|
||||
@@ -55,6 +85,10 @@ func (*HorizontalRule) Type() NodeType {
|
||||
return HorizontalRuleNode
|
||||
}
|
||||
|
||||
func (n *HorizontalRule) Restore() string {
|
||||
return n.Symbol + n.Symbol + n.Symbol
|
||||
}
|
||||
|
||||
type Blockquote struct {
|
||||
BaseBlock
|
||||
|
||||
@@ -65,6 +99,14 @@ func (*Blockquote) Type() NodeType {
|
||||
return BlockquoteNode
|
||||
}
|
||||
|
||||
func (n *Blockquote) Restore() string {
|
||||
var result string
|
||||
for _, child := range n.Children {
|
||||
result += child.Restore()
|
||||
}
|
||||
return fmt.Sprintf("> %s", result)
|
||||
}
|
||||
|
||||
type OrderedList struct {
|
||||
BaseBlock
|
||||
|
||||
@@ -76,6 +118,14 @@ func (*OrderedList) Type() NodeType {
|
||||
return OrderedListNode
|
||||
}
|
||||
|
||||
func (n *OrderedList) Restore() string {
|
||||
var result string
|
||||
for _, child := range n.Children {
|
||||
result += child.Restore()
|
||||
}
|
||||
return fmt.Sprintf("%s. %s", n.Number, result)
|
||||
}
|
||||
|
||||
type UnorderedList struct {
|
||||
BaseBlock
|
||||
|
||||
@@ -88,6 +138,14 @@ func (*UnorderedList) Type() NodeType {
|
||||
return UnorderedListNode
|
||||
}
|
||||
|
||||
func (n *UnorderedList) Restore() string {
|
||||
var result string
|
||||
for _, child := range n.Children {
|
||||
result += child.Restore()
|
||||
}
|
||||
return fmt.Sprintf("%s %s", n.Symbol, result)
|
||||
}
|
||||
|
||||
type TaskList struct {
|
||||
BaseBlock
|
||||
|
||||
@@ -100,3 +158,15 @@ type TaskList struct {
|
||||
func (*TaskList) Type() NodeType {
|
||||
return TaskListNode
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user