2023-12-17 09:53:22 +08:00
|
|
|
package v2
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
|
|
|
"github.com/usememos/memos/plugin/gomark/ast"
|
|
|
|
"github.com/usememos/memos/plugin/gomark/parser"
|
|
|
|
"github.com/usememos/memos/plugin/gomark/parser/tokenizer"
|
|
|
|
apiv2pb "github.com/usememos/memos/proto/gen/api/v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (*APIV2Service) ParseMarkdown(_ context.Context, request *apiv2pb.ParseMarkdownRequest) (*apiv2pb.ParseMarkdownResponse, error) {
|
|
|
|
rawNodes, err := parser.Parse(tokenizer.Tokenize(request.Markdown))
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to parse memo content")
|
|
|
|
}
|
|
|
|
nodes := convertFromASTNodes(rawNodes)
|
|
|
|
return &apiv2pb.ParseMarkdownResponse{
|
|
|
|
Nodes: nodes,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func convertFromASTNodes(rawNodes []ast.Node) []*apiv2pb.Node {
|
|
|
|
nodes := []*apiv2pb.Node{}
|
|
|
|
for _, rawNode := range rawNodes {
|
|
|
|
node := convertFromASTNode(rawNode)
|
|
|
|
nodes = append(nodes, node)
|
|
|
|
}
|
|
|
|
return nodes
|
|
|
|
}
|
|
|
|
|
|
|
|
func convertFromASTNode(rawNode ast.Node) *apiv2pb.Node {
|
|
|
|
node := &apiv2pb.Node{
|
|
|
|
Type: apiv2pb.NodeType(rawNode.Type()),
|
|
|
|
}
|
|
|
|
|
|
|
|
switch n := rawNode.(type) {
|
|
|
|
case *ast.LineBreak:
|
|
|
|
node.Node = &apiv2pb.Node_LineBreakNode{}
|
|
|
|
case *ast.Paragraph:
|
|
|
|
children := convertFromASTNodes(n.Children)
|
|
|
|
node.Node = &apiv2pb.Node_ParagraphNode{ParagraphNode: &apiv2pb.ParagraphNode{Children: children}}
|
|
|
|
case *ast.CodeBlock:
|
|
|
|
node.Node = &apiv2pb.Node_CodeBlockNode{CodeBlockNode: &apiv2pb.CodeBlockNode{Language: n.Language, Content: n.Content}}
|
|
|
|
case *ast.Heading:
|
|
|
|
children := convertFromASTNodes(n.Children)
|
|
|
|
node.Node = &apiv2pb.Node_HeadingNode{HeadingNode: &apiv2pb.HeadingNode{Level: int32(n.Level), Children: children}}
|
|
|
|
case *ast.HorizontalRule:
|
|
|
|
node.Node = &apiv2pb.Node_HorizontalRuleNode{HorizontalRuleNode: &apiv2pb.HorizontalRuleNode{Symbol: n.Symbol}}
|
|
|
|
case *ast.Blockquote:
|
|
|
|
children := convertFromASTNodes(n.Children)
|
|
|
|
node.Node = &apiv2pb.Node_BlockquoteNode{BlockquoteNode: &apiv2pb.BlockquoteNode{Children: children}}
|
|
|
|
case *ast.OrderedList:
|
|
|
|
children := convertFromASTNodes(n.Children)
|
2024-01-14 22:19:03 +08:00
|
|
|
node.Node = &apiv2pb.Node_OrderedListNode{OrderedListNode: &apiv2pb.OrderedListNode{Number: n.Number, Indent: int32(n.Indent), Children: children}}
|
2023-12-17 09:53:22 +08:00
|
|
|
case *ast.UnorderedList:
|
|
|
|
children := convertFromASTNodes(n.Children)
|
2024-01-14 22:19:03 +08:00
|
|
|
node.Node = &apiv2pb.Node_UnorderedListNode{UnorderedListNode: &apiv2pb.UnorderedListNode{Symbol: n.Symbol, Indent: int32(n.Indent), Children: children}}
|
2023-12-17 09:53:22 +08:00
|
|
|
case *ast.TaskList:
|
|
|
|
children := convertFromASTNodes(n.Children)
|
2024-01-14 22:19:03 +08:00
|
|
|
node.Node = &apiv2pb.Node_TaskListNode{TaskListNode: &apiv2pb.TaskListNode{Symbol: n.Symbol, Indent: int32(n.Indent), Complete: n.Complete, Children: children}}
|
2024-01-04 20:05:29 +08:00
|
|
|
case *ast.MathBlock:
|
|
|
|
node.Node = &apiv2pb.Node_MathBlockNode{MathBlockNode: &apiv2pb.MathBlockNode{Content: n.Content}}
|
2023-12-17 09:53:22 +08:00
|
|
|
case *ast.Text:
|
|
|
|
node.Node = &apiv2pb.Node_TextNode{TextNode: &apiv2pb.TextNode{Content: n.Content}}
|
|
|
|
case *ast.Bold:
|
|
|
|
children := convertFromASTNodes(n.Children)
|
|
|
|
node.Node = &apiv2pb.Node_BoldNode{BoldNode: &apiv2pb.BoldNode{Symbol: n.Symbol, Children: children}}
|
|
|
|
case *ast.Italic:
|
|
|
|
node.Node = &apiv2pb.Node_ItalicNode{ItalicNode: &apiv2pb.ItalicNode{Symbol: n.Symbol, Content: n.Content}}
|
|
|
|
case *ast.BoldItalic:
|
|
|
|
node.Node = &apiv2pb.Node_BoldItalicNode{BoldItalicNode: &apiv2pb.BoldItalicNode{Symbol: n.Symbol, Content: n.Content}}
|
|
|
|
case *ast.Code:
|
|
|
|
node.Node = &apiv2pb.Node_CodeNode{CodeNode: &apiv2pb.CodeNode{Content: n.Content}}
|
|
|
|
case *ast.Image:
|
|
|
|
node.Node = &apiv2pb.Node_ImageNode{ImageNode: &apiv2pb.ImageNode{AltText: n.AltText, Url: n.URL}}
|
|
|
|
case *ast.Link:
|
|
|
|
node.Node = &apiv2pb.Node_LinkNode{LinkNode: &apiv2pb.LinkNode{Text: n.Text, Url: n.URL}}
|
2023-12-27 08:44:51 +08:00
|
|
|
case *ast.AutoLink:
|
2024-01-08 20:57:44 +08:00
|
|
|
node.Node = &apiv2pb.Node_AutoLinkNode{AutoLinkNode: &apiv2pb.AutoLinkNode{Url: n.URL, IsRawText: n.IsRawText}}
|
2023-12-17 09:53:22 +08:00
|
|
|
case *ast.Tag:
|
|
|
|
node.Node = &apiv2pb.Node_TagNode{TagNode: &apiv2pb.TagNode{Content: n.Content}}
|
|
|
|
case *ast.Strikethrough:
|
|
|
|
node.Node = &apiv2pb.Node_StrikethroughNode{StrikethroughNode: &apiv2pb.StrikethroughNode{Content: n.Content}}
|
|
|
|
case *ast.EscapingCharacter:
|
|
|
|
node.Node = &apiv2pb.Node_EscapingCharacterNode{EscapingCharacterNode: &apiv2pb.EscapingCharacterNode{Symbol: n.Symbol}}
|
2024-01-04 20:05:29 +08:00
|
|
|
case *ast.Math:
|
|
|
|
node.Node = &apiv2pb.Node_MathNode{MathNode: &apiv2pb.MathNode{Content: n.Content}}
|
2024-01-15 22:54:18 +08:00
|
|
|
case *ast.Highlight:
|
|
|
|
node.Node = &apiv2pb.Node_HighlightNode{HighlightNode: &apiv2pb.HighlightNode{Content: n.Content}}
|
2023-12-17 09:53:22 +08:00
|
|
|
default:
|
|
|
|
node.Node = &apiv2pb.Node_TextNode{TextNode: &apiv2pb.TextNode{}}
|
|
|
|
}
|
|
|
|
|
|
|
|
return node
|
|
|
|
}
|
2024-01-02 08:56:30 +08:00
|
|
|
|
2024-01-05 08:40:16 +08:00
|
|
|
func convertToASTNodes(nodes []*apiv2pb.Node) []ast.Node {
|
|
|
|
rawNodes := []ast.Node{}
|
|
|
|
for _, node := range nodes {
|
|
|
|
rawNode := convertToASTNode(node)
|
|
|
|
rawNodes = append(rawNodes, rawNode)
|
|
|
|
}
|
|
|
|
return rawNodes
|
|
|
|
}
|
|
|
|
|
|
|
|
func convertToASTNode(node *apiv2pb.Node) ast.Node {
|
|
|
|
switch n := node.Node.(type) {
|
|
|
|
case *apiv2pb.Node_LineBreakNode:
|
|
|
|
return &ast.LineBreak{}
|
|
|
|
case *apiv2pb.Node_ParagraphNode:
|
|
|
|
children := convertToASTNodes(n.ParagraphNode.Children)
|
|
|
|
return &ast.Paragraph{Children: children}
|
|
|
|
case *apiv2pb.Node_CodeBlockNode:
|
|
|
|
return &ast.CodeBlock{Language: n.CodeBlockNode.Language, Content: n.CodeBlockNode.Content}
|
|
|
|
case *apiv2pb.Node_HeadingNode:
|
|
|
|
children := convertToASTNodes(n.HeadingNode.Children)
|
|
|
|
return &ast.Heading{Level: int(n.HeadingNode.Level), Children: children}
|
|
|
|
case *apiv2pb.Node_HorizontalRuleNode:
|
|
|
|
return &ast.HorizontalRule{Symbol: n.HorizontalRuleNode.Symbol}
|
|
|
|
case *apiv2pb.Node_BlockquoteNode:
|
|
|
|
children := convertToASTNodes(n.BlockquoteNode.Children)
|
|
|
|
return &ast.Blockquote{Children: children}
|
|
|
|
case *apiv2pb.Node_OrderedListNode:
|
|
|
|
children := convertToASTNodes(n.OrderedListNode.Children)
|
2024-01-14 22:19:03 +08:00
|
|
|
return &ast.OrderedList{Number: n.OrderedListNode.Number, Indent: int(n.OrderedListNode.Indent), Children: children}
|
2024-01-05 08:40:16 +08:00
|
|
|
case *apiv2pb.Node_UnorderedListNode:
|
|
|
|
children := convertToASTNodes(n.UnorderedListNode.Children)
|
2024-01-14 22:19:03 +08:00
|
|
|
return &ast.UnorderedList{Symbol: n.UnorderedListNode.Symbol, Indent: int(n.UnorderedListNode.Indent), Children: children}
|
2024-01-05 08:40:16 +08:00
|
|
|
case *apiv2pb.Node_TaskListNode:
|
|
|
|
children := convertToASTNodes(n.TaskListNode.Children)
|
2024-01-14 22:19:03 +08:00
|
|
|
return &ast.TaskList{Symbol: n.TaskListNode.Symbol, Indent: int(n.TaskListNode.Indent), Complete: n.TaskListNode.Complete, Children: children}
|
2024-01-05 08:40:16 +08:00
|
|
|
case *apiv2pb.Node_MathBlockNode:
|
|
|
|
return &ast.MathBlock{Content: n.MathBlockNode.Content}
|
|
|
|
case *apiv2pb.Node_TextNode:
|
|
|
|
return &ast.Text{Content: n.TextNode.Content}
|
|
|
|
case *apiv2pb.Node_BoldNode:
|
|
|
|
children := convertToASTNodes(n.BoldNode.Children)
|
|
|
|
return &ast.Bold{Symbol: n.BoldNode.Symbol, Children: children}
|
|
|
|
case *apiv2pb.Node_ItalicNode:
|
|
|
|
return &ast.Italic{Symbol: n.ItalicNode.Symbol, Content: n.ItalicNode.Content}
|
|
|
|
case *apiv2pb.Node_BoldItalicNode:
|
|
|
|
return &ast.BoldItalic{Symbol: n.BoldItalicNode.Symbol, Content: n.BoldItalicNode.Content}
|
|
|
|
case *apiv2pb.Node_CodeNode:
|
|
|
|
return &ast.Code{Content: n.CodeNode.Content}
|
|
|
|
case *apiv2pb.Node_ImageNode:
|
|
|
|
return &ast.Image{AltText: n.ImageNode.AltText, URL: n.ImageNode.Url}
|
|
|
|
case *apiv2pb.Node_LinkNode:
|
|
|
|
return &ast.Link{Text: n.LinkNode.Text, URL: n.LinkNode.Url}
|
|
|
|
case *apiv2pb.Node_AutoLinkNode:
|
2024-01-08 20:57:44 +08:00
|
|
|
return &ast.AutoLink{URL: n.AutoLinkNode.Url, IsRawText: n.AutoLinkNode.IsRawText}
|
2024-01-05 08:40:16 +08:00
|
|
|
case *apiv2pb.Node_TagNode:
|
|
|
|
return &ast.Tag{Content: n.TagNode.Content}
|
|
|
|
case *apiv2pb.Node_StrikethroughNode:
|
|
|
|
return &ast.Strikethrough{Content: n.StrikethroughNode.Content}
|
|
|
|
case *apiv2pb.Node_EscapingCharacterNode:
|
|
|
|
return &ast.EscapingCharacter{Symbol: n.EscapingCharacterNode.Symbol}
|
|
|
|
case *apiv2pb.Node_MathNode:
|
|
|
|
return &ast.Math{Content: n.MathNode.Content}
|
2024-01-15 22:54:18 +08:00
|
|
|
case *apiv2pb.Node_HighlightNode:
|
|
|
|
return &ast.Highlight{Content: n.HighlightNode.Content}
|
2024-01-05 08:40:16 +08:00
|
|
|
default:
|
|
|
|
return &ast.Text{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-02 08:56:30 +08:00
|
|
|
func traverseASTNodes(nodes []ast.Node, fn func(ast.Node)) {
|
|
|
|
for _, node := range nodes {
|
|
|
|
fn(node)
|
|
|
|
switch n := node.(type) {
|
|
|
|
case *ast.Paragraph:
|
|
|
|
traverseASTNodes(n.Children, fn)
|
|
|
|
case *ast.Heading:
|
|
|
|
traverseASTNodes(n.Children, fn)
|
|
|
|
case *ast.Blockquote:
|
|
|
|
traverseASTNodes(n.Children, fn)
|
|
|
|
case *ast.OrderedList:
|
|
|
|
traverseASTNodes(n.Children, fn)
|
|
|
|
case *ast.UnorderedList:
|
|
|
|
traverseASTNodes(n.Children, fn)
|
|
|
|
case *ast.TaskList:
|
|
|
|
traverseASTNodes(n.Children, fn)
|
|
|
|
case *ast.Bold:
|
|
|
|
traverseASTNodes(n.Children, fn)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|