Memos is an open-source, self-hosted note-taking app that brings your everyday thoughts together in a personal timeline. Your data stays yours, and you choose what to keep private or share.
Go dockerfossgomarkdownmemomicroblognote-takingnotesopen-sourceown-your-datareactself-hostedsqlitetimeline
feat(gomark): add bold parser (#1724)
boojack8c34be92a661
diff --git a/plugin/gomark/parser/bold.go b/plugin/gomark/parser/bold.gonew file mode 100644index 000000000..a47dc018b--- /dev/null+++ b/plugin/gomark/parser/bold.go@@ -0,0 +1,46 @@+package parser++import (+ "github.com/usememos/memos/plugin/gomark/parser/tokenizer"+)++type BoldParser struct {+ ContentTokens []*tokenizer.Token+}++func NewBoldParser() *BoldParser {+ return &BoldParser{}+}++func (*BoldParser) Match(tokens []*tokenizer.Token) *BoldParser {+ if len(tokens) < 5 {+ return nil+ }++ prefixTokens := tokens[:2]+ if len(prefixTokens) != 2 || prefixTokens[0].Type != prefixTokens[1].Type {+ return nil+ }+ prefixTokenType := prefixTokens[0].Type++ contentTokens := []*tokenizer.Token{}+ cursor := 2+ for ; cursor < len(tokens)-1; cursor++ {+ token, nextToken := tokens[cursor], tokens[cursor+1]++ if token.Type == tokenizer.Newline || nextToken.Type == tokenizer.Newline {+ break+ }+ if token.Type == prefixTokenType && nextToken.Type == prefixTokenType {+ break+ }+ contentTokens = append(contentTokens, token)+ }+ if cursor != len(tokens)-2 {+ return nil+ }++ return &BoldParser{+ ContentTokens: contentTokens,+ }+}diff --git a/plugin/gomark/parser/bold_test.go b/plugin/gomark/parser/bold_test.gonew file mode 100644index 000000000..adeabef25--- /dev/null+++ b/plugin/gomark/parser/bold_test.go@@ -0,0 +1,88 @@+package parser++import (+ "testing"++ "github.com/stretchr/testify/require"+ "github.com/usememos/memos/plugin/gomark/parser/tokenizer"+)++func TestBoldParser(t *testing.T) {+ tests := []struct {+ text string+ bold *BoldParser+ }{+ {+ text: "*Hello world!",+ bold: nil,+ },+ {+ text: "**Hello**",+ bold: &BoldParser{+ ContentTokens: []*tokenizer.Token{+ {+ Type: tokenizer.Text,+ Value: "Hello",+ },+ },+ },+ },+ {+ text: "** Hello **",+ bold: &BoldParser{+ ContentTokens: []*tokenizer.Token{+ {+ Type: tokenizer.Space,+ Value: " ",+ },+ {+ Type: tokenizer.Text,+ Value: "Hello",+ },+ {+ Type: tokenizer.Space,+ Value: " ",+ },+ },+ },+ },+ {+ text: "** Hello * *",+ bold: nil,+ },+ {+ text: "* * Hello **",+ bold: nil,+ },+ {+ text: `** Hello+**`,+ bold: nil,+ },+ {+ text: `**Hello \n**`,+ bold: &BoldParser{+ ContentTokens: []*tokenizer.Token{+ {+ Type: tokenizer.Text,+ Value: "Hello",+ },+ {+ Type: tokenizer.Space,+ Value: " ",+ },+ {+ Type: tokenizer.Text,+ Value: `\n`,+ },+ },+ },+ },+ }++ for _, test := range tests {+ tokens := tokenizer.Tokenize(test.text)+ bold := NewBoldParser()+ require.Equal(t, test.bold, bold.Match(tokens))+ }+}diff --git a/plugin/gomark/parser/heading.go b/plugin/gomark/parser/heading.goindex bd366c851..c9c26323f 100644--- a/plugin/gomark/parser/heading.go+++ b/plugin/gomark/parser/heading.go@@ -4,16 +4,16 @@ import ("github.com/usememos/memos/plugin/gomark/parser/tokenizer")-type HeadingTokenizer struct {+type HeadingParser struct {Level intContentTokens []*tokenizer.Token}-func NewHeadingTokenizer() *HeadingTokenizer {- return &HeadingTokenizer{}+func NewHeadingParser() *HeadingParser {+ return &HeadingParser{}}-func (*HeadingTokenizer) Match(tokens []*tokenizer.Token) *HeadingTokenizer {+func (*HeadingParser) Match(tokens []*tokenizer.Token) *HeadingParser {cursor := 0for _, token := range tokens {if token.Type == tokenizer.Hash {@@ -40,12 +40,13 @@ func (*HeadingTokenizer) Match(tokens []*tokenizer.Token) *HeadingTokenizer {break}contentTokens = append(contentTokens, token)+ cursor++}if len(contentTokens) == 0 {return nil}- return &HeadingTokenizer{+ return &HeadingParser{Level: level,ContentTokens: contentTokens,}diff --git a/plugin/gomark/parser/heading_test.go b/plugin/gomark/parser/heading_test.goindex d4a18c13c..7a8f99a46 100644--- a/plugin/gomark/parser/heading_test.go+++ b/plugin/gomark/parser/heading_test.go@@ -10,7 +10,7 @@ import (func TestHeadingParser(t *testing.T) {tests := []struct {text string- heading *HeadingTokenizer+ heading *HeadingParser}{{text: "*Hello world!",@@ -18,7 +18,7 @@ func TestHeadingParser(t *testing.T) {},{text: "## Hello World!",- heading: &HeadingTokenizer{+ heading: &HeadingParser{Level: 2,ContentTokens: []*tokenizer.Token{{@@ -38,7 +38,7 @@ func TestHeadingParser(t *testing.T) {},{text: "# # Hello World",- heading: &HeadingTokenizer{+ heading: &HeadingParser{Level: 1,ContentTokens: []*tokenizer.Token{{@@ -71,7 +71,7 @@ func TestHeadingParser(t *testing.T) {{text: `# 123Hello World!`,- heading: &HeadingTokenizer{+ heading: &HeadingParser{Level: 1,ContentTokens: []*tokenizer.Token{{@@ -89,7 +89,7 @@ Hello World!`,for _, test := range tests {tokens := tokenizer.Tokenize(test.text)- headingTokenizer := NewHeadingTokenizer()- require.Equal(t, test.heading, headingTokenizer.Match(tokens))+ heading := NewHeadingParser()+ require.Equal(t, test.heading, heading.Match(tokens))}}