mirror of
https://github.com/usememos/memos.git
synced 2025-06-05 22:09:59 +02:00
chore: add line break node
This commit is contained in:
@ -3,6 +3,20 @@ package ast
|
|||||||
type BaseBlock struct {
|
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 {
|
type Paragraph struct {
|
||||||
BaseBlock
|
BaseBlock
|
||||||
|
|
||||||
|
33
plugin/gomark/parser/line_break.go
Normal file
33
plugin/gomark/parser/line_break.go
Normal file
@ -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()
|
||||||
|
}
|
@ -26,7 +26,9 @@ type BlockParser interface {
|
|||||||
func Parse(tokens []*tokenizer.Token) []ast.Node {
|
func Parse(tokens []*tokenizer.Token) []ast.Node {
|
||||||
nodes := []ast.Node{}
|
nodes := []ast.Node{}
|
||||||
blockParsers := []BlockParser{
|
blockParsers := []BlockParser{
|
||||||
|
NewCodeBlockParser(),
|
||||||
NewParagraphParser(),
|
NewParagraphParser(),
|
||||||
|
NewLineBreakParser(),
|
||||||
}
|
}
|
||||||
for len(tokens) > 0 {
|
for len(tokens) > 0 {
|
||||||
for _, blockParser := range blockParsers {
|
for _, blockParser := range blockParsers {
|
||||||
|
@ -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 {
|
for _, test := range tests {
|
||||||
|
Reference in New Issue
Block a user