From dd83782522b61ff265cecdaf10e62cd78f9ccef6 Mon Sep 17 00:00:00 2001 From: Steven Date: Tue, 12 Dec 2023 23:38:43 +0800 Subject: [PATCH] chore: add line break node --- plugin/gomark/ast/block.go | 14 ++++++++++++ plugin/gomark/parser/line_break.go | 33 +++++++++++++++++++++++++++++ plugin/gomark/parser/parser.go | 2 ++ plugin/gomark/parser/parser_test.go | 24 +++++++++++++++++++++ 4 files changed, 73 insertions(+) create mode 100644 plugin/gomark/parser/line_break.go diff --git a/plugin/gomark/ast/block.go b/plugin/gomark/ast/block.go index fcbe89c5..236db0e9 100644 --- a/plugin/gomark/ast/block.go +++ b/plugin/gomark/ast/block.go @@ -3,6 +3,20 @@ package ast type BaseBlock struct { } +type LineBreak struct { + BaseBlock +} + +var NodeTypeLineBreak = NewNodeType("LineBreak") + +func NewLineBreak() *LineBreak { + return &LineBreak{} +} + +func (*LineBreak) Type() NodeType { + return NodeTypeLineBreak +} + type Paragraph struct { BaseBlock diff --git a/plugin/gomark/parser/line_break.go b/plugin/gomark/parser/line_break.go new file mode 100644 index 00000000..46a833e5 --- /dev/null +++ b/plugin/gomark/parser/line_break.go @@ -0,0 +1,33 @@ +package parser + +import ( + "github.com/usememos/memos/plugin/gomark/ast" + "github.com/usememos/memos/plugin/gomark/parser/tokenizer" +) + +type LineBreakParser struct{} + +var defaultLineBreakParser = &LineBreakParser{} + +func NewLineBreakParser() *LineBreakParser { + return defaultLineBreakParser +} + +func (*LineBreakParser) Match(tokens []*tokenizer.Token) (int, bool) { + if len(tokens) == 0 { + return 0, false + } + if tokens[0].Type != tokenizer.Newline { + return 0, false + } + return 1, true +} + +func (p *LineBreakParser) Parse(tokens []*tokenizer.Token) ast.Node { + size, ok := p.Match(tokens) + if size == 0 || !ok { + return nil + } + + return ast.NewLineBreak() +} diff --git a/plugin/gomark/parser/parser.go b/plugin/gomark/parser/parser.go index 7024d3c1..c82fa50d 100644 --- a/plugin/gomark/parser/parser.go +++ b/plugin/gomark/parser/parser.go @@ -26,7 +26,9 @@ type BlockParser interface { func Parse(tokens []*tokenizer.Token) []ast.Node { nodes := []ast.Node{} blockParsers := []BlockParser{ + NewCodeBlockParser(), NewParagraphParser(), + NewLineBreakParser(), } for len(tokens) > 0 { for _, blockParser := range blockParsers { diff --git a/plugin/gomark/parser/parser_test.go b/plugin/gomark/parser/parser_test.go index 17a561bc..30460e3b 100644 --- a/plugin/gomark/parser/parser_test.go +++ b/plugin/gomark/parser/parser_test.go @@ -61,6 +61,30 @@ func TestParser(t *testing.T) { }, }, }, + { + text: "Hello **world**!\n```javascript\nconsole.log(\"Hello world!\");\n```", + nodes: []ast.Node{ + &ast.Paragraph{ + Children: []ast.Node{ + &ast.Text{ + Content: "Hello ", + }, + &ast.Bold{ + Symbol: "*", + Content: "world", + }, + &ast.Text{ + Content: "!", + }, + }, + }, + &ast.LineBreak{}, + &ast.CodeBlock{ + Language: "javascript", + Content: "console.log(\"Hello world!\");", + }, + }, + }, } for _, test := range tests {