-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
191 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,52 @@ | ||
package parser | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/usememos/memos/plugin/gomark/ast" | ||
"github.com/usememos/memos/plugin/gomark/parser/tokenizer" | ||
) | ||
|
||
type HeadingTokenizer struct { | ||
Level int | ||
ContentTokens []*tokenizer.Token | ||
} | ||
|
||
func NewHeadingTokenizer() *HeadingTokenizer { | ||
return &HeadingTokenizer{} | ||
} | ||
|
||
func (*HeadingTokenizer) Trigger() []byte { | ||
return []byte{'#'} | ||
} | ||
|
||
func (*HeadingTokenizer) Parse(parent *ast.Node, block string) *ast.Node { | ||
line := block | ||
level := 0 | ||
for _, c := range line { | ||
if c == '#' { | ||
level++ | ||
} else if c == ' ' { | ||
break | ||
func (*HeadingTokenizer) Match(tokens []*tokenizer.Token) *HeadingTokenizer { | ||
cursor := 0 | ||
for _, token := range tokens { | ||
if token.Type == tokenizer.Hash { | ||
cursor++ | ||
} else { | ||
return nil | ||
break | ||
} | ||
} | ||
if len(tokens) <= cursor+1 { | ||
return nil | ||
} | ||
if tokens[cursor].Type != tokenizer.Space { | ||
return nil | ||
} | ||
level := cursor | ||
if level == 0 || level > 6 { | ||
return nil | ||
} | ||
text := strings.TrimSpace(line[level+1:]) | ||
node := ast.NewNode("h1", text) | ||
if parent != nil { | ||
parent.AddChild(node) | ||
|
||
cursor++ | ||
contentTokens := []*tokenizer.Token{} | ||
for _, token := range tokens[cursor:] { | ||
if token.Type == tokenizer.Newline { | ||
break | ||
} | ||
contentTokens = append(contentTokens, token) | ||
} | ||
if len(contentTokens) == 0 { | ||
return nil | ||
} | ||
|
||
return &HeadingTokenizer{ | ||
Level: level, | ||
ContentTokens: contentTokens, | ||
} | ||
return node | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,95 @@ | ||
package parser | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
"github.com/usememos/memos/plugin/gomark/parser/tokenizer" | ||
) | ||
|
||
func TestHeadingParser(t *testing.T) { | ||
tests := []struct { | ||
text string | ||
heading *HeadingTokenizer | ||
}{ | ||
{ | ||
text: "*Hello world!", | ||
heading: nil, | ||
}, | ||
{ | ||
text: "## Hello World!", | ||
heading: &HeadingTokenizer{ | ||
Level: 2, | ||
ContentTokens: []*tokenizer.Token{ | ||
{ | ||
Type: tokenizer.Text, | ||
Value: "Hello", | ||
}, | ||
{ | ||
Type: tokenizer.Space, | ||
Value: " ", | ||
}, | ||
{ | ||
Type: tokenizer.Text, | ||
Value: "World!", | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
text: "# # Hello World", | ||
heading: &HeadingTokenizer{ | ||
Level: 1, | ||
ContentTokens: []*tokenizer.Token{ | ||
{ | ||
Type: tokenizer.Hash, | ||
Value: "#", | ||
}, | ||
{ | ||
Type: tokenizer.Space, | ||
Value: " ", | ||
}, | ||
{ | ||
Type: tokenizer.Text, | ||
Value: "Hello", | ||
}, | ||
{ | ||
Type: tokenizer.Space, | ||
Value: " ", | ||
}, | ||
{ | ||
Type: tokenizer.Text, | ||
Value: "World", | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
text: " # 123123 Hello World!", | ||
heading: nil, | ||
}, | ||
{ | ||
text: `# 123 | ||
Hello World!`, | ||
heading: &HeadingTokenizer{ | ||
Level: 1, | ||
ContentTokens: []*tokenizer.Token{ | ||
{ | ||
Type: tokenizer.Text, | ||
Value: "123", | ||
}, | ||
{ | ||
Type: tokenizer.Space, | ||
Value: " ", | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
tokens := tokenizer.Tokenize(test.text) | ||
headingTokenizer := NewHeadingTokenizer() | ||
require.Equal(t, test.heading, headingTokenizer.Match(tokens)) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters