feat: implement restore nodes

This commit is contained in:
Steven
2023-12-28 22:35:39 +08:00
parent 2a6f054876
commit 46f7cffc7b
34 changed files with 264 additions and 154 deletions

View File

@ -1,13 +1,13 @@
package parser
import (
"strconv"
"testing"
"github.com/stretchr/testify/require"
"github.com/usememos/memos/plugin/gomark/ast"
"github.com/usememos/memos/plugin/gomark/parser/tokenizer"
"github.com/usememos/memos/plugin/gomark/restore"
)
func TestParser(t *testing.T) {
@ -202,62 +202,6 @@ func TestParser(t *testing.T) {
for _, test := range tests {
tokens := tokenizer.Tokenize(test.text)
nodes, _ := Parse(tokens)
require.Equal(t, StringifyNodes(test.nodes), StringifyNodes(nodes))
require.Equal(t, restore.Restore(test.nodes), restore.Restore(nodes))
}
}
func StringifyNodes(nodes []ast.Node) string {
var result string
for _, node := range nodes {
if node != nil {
result += StringifyNode(node)
}
}
return result
}
func StringifyNode(node ast.Node) string {
switch n := node.(type) {
case *ast.LineBreak:
return "LineBreak()"
case *ast.CodeBlock:
return "CodeBlock(" + n.Language + ", " + n.Content + ")"
case *ast.Paragraph:
return "Paragraph(" + StringifyNodes(n.Children) + ")"
case *ast.Heading:
return "Heading(" + StringifyNodes(n.Children) + ")"
case *ast.HorizontalRule:
return "HorizontalRule(" + n.Symbol + ")"
case *ast.Blockquote:
return "Blockquote(" + StringifyNodes(n.Children) + ")"
case *ast.OrderedList:
return "OrderedList(" + n.Number + ", " + StringifyNodes(n.Children) + ")"
case *ast.UnorderedList:
return "UnorderedList(" + n.Symbol + ", " + StringifyNodes(n.Children) + ")"
case *ast.TaskList:
return "TaskList(" + n.Symbol + ", " + strconv.FormatBool(n.Complete) + ", " + StringifyNodes(n.Children) + ")"
case *ast.Text:
return "Text(" + n.Content + ")"
case *ast.Bold:
return "Bold(" + n.Symbol + StringifyNodes(n.Children) + n.Symbol + ")"
case *ast.Italic:
return "Italic(" + n.Symbol + n.Content + n.Symbol + ")"
case *ast.BoldItalic:
return "BoldItalic(" + n.Symbol + n.Content + n.Symbol + ")"
case *ast.Code:
return "Code(" + n.Content + ")"
case *ast.Image:
return "Image(" + n.URL + ", " + n.AltText + ")"
case *ast.Link:
return "Link(" + n.Text + ", " + n.URL + ")"
case *ast.AutoLink:
return "AutoLink(" + n.URL + ")"
case *ast.Tag:
return "Tag(" + n.Content + ")"
case *ast.Strikethrough:
return "Strikethrough(" + n.Content + ")"
case *ast.EscapingCharacter:
return "EscapingCharacter(" + n.Symbol + ")"
}
return ""
}