Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
canifa_note
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Vũ Hoàng Anh
canifa_note
Commits
fa53a255
Unverified
Commit
fa53a255
authored
May 23, 2023
by
boojack
Committed by
GitHub
May 23, 2023
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: add heading tokenizer (#1723)
parent
616b8b0e
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
191 additions
and
50 deletions
+191
-50
heading.go
plugin/gomark/parser/heading.go
+32
-21
heading_test.go
plugin/gomark/parser/heading_test.go
+94
-0
token.go
plugin/gomark/parser/tokenizer/token.go
+0
-27
tokenizer.go
plugin/gomark/parser/tokenizer/tokenizer.go
+29
-1
tokenizer_test.go
plugin/gomark/parser/tokenizer/tokenizer_test.go
+36
-1
No files found.
plugin/gomark/parser/heading.go
View file @
fa53a255
package
parser
package
parser
import
(
import
(
"strings"
"github.com/usememos/memos/plugin/gomark/parser/tokenizer"
"github.com/usememos/memos/plugin/gomark/ast"
)
)
type
HeadingTokenizer
struct
{
type
HeadingTokenizer
struct
{
Level
int
ContentTokens
[]
*
tokenizer
.
Token
}
}
func
NewHeadingTokenizer
()
*
HeadingTokenizer
{
func
NewHeadingTokenizer
()
*
HeadingTokenizer
{
return
&
HeadingTokenizer
{}
return
&
HeadingTokenizer
{}
}
}
func
(
*
HeadingTokenizer
)
Trigger
()
[]
byte
{
func
(
*
HeadingTokenizer
)
Match
(
tokens
[]
*
tokenizer
.
Token
)
*
HeadingTokenizer
{
return
[]
byte
{
'#'
}
cursor
:=
0
}
for
_
,
token
:=
range
tokens
{
if
token
.
Type
==
tokenizer
.
Hash
{
func
(
*
HeadingTokenizer
)
Parse
(
parent
*
ast
.
Node
,
block
string
)
*
ast
.
Node
{
cursor
++
line
:=
block
level
:=
0
for
_
,
c
:=
range
line
{
if
c
==
'#'
{
level
++
}
else
if
c
==
' '
{
break
}
else
{
}
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
{
if
level
==
0
||
level
>
6
{
return
nil
return
nil
}
}
text
:=
strings
.
TrimSpace
(
line
[
level
+
1
:
])
node
:=
ast
.
NewNode
(
"h1"
,
text
)
cursor
++
if
parent
!=
nil
{
contentTokens
:=
[]
*
tokenizer
.
Token
{}
parent
.
AddChild
(
node
)
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
}
}
plugin/gomark/parser/heading_test.go
View file @
fa53a255
package
parser
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
))
}
}
plugin/gomark/parser/tokenizer/token.go
deleted
100644 → 0
View file @
616b8b0e
package
tokenizer
type
TokenType
=
string
const
(
Underline
TokenType
=
"_"
Star
TokenType
=
"*"
Newline
TokenType
=
"
\n
"
Hash
TokenType
=
"#"
Space
TokenType
=
" "
)
const
(
Text
TokenType
=
""
)
type
Token
struct
{
Type
TokenType
Value
string
}
func
NewToken
(
tp
,
text
string
)
*
Token
{
return
&
Token
{
Type
:
tp
,
Value
:
text
,
}
}
plugin/gomark/parser/tokenizer/tokenizer.go
View file @
fa53a255
package
tokenizer
package
tokenizer
func
tokenize
(
text
string
)
[]
*
Token
{
type
TokenType
=
string
const
(
Underline
TokenType
=
"_"
Star
TokenType
=
"*"
Hash
TokenType
=
"#"
Newline
TokenType
=
"
\n
"
Space
TokenType
=
" "
)
const
(
Text
TokenType
=
""
)
type
Token
struct
{
Type
TokenType
Value
string
}
func
NewToken
(
tp
,
text
string
)
*
Token
{
return
&
Token
{
Type
:
tp
,
Value
:
text
,
}
}
func
Tokenize
(
text
string
)
[]
*
Token
{
tokens
:=
[]
*
Token
{}
tokens
:=
[]
*
Token
{}
for
_
,
c
:=
range
text
{
for
_
,
c
:=
range
text
{
switch
c
{
switch
c
{
...
@@ -8,6 +34,8 @@ func tokenize(text string) []*Token {
...
@@ -8,6 +34,8 @@ func tokenize(text string) []*Token {
tokens
=
append
(
tokens
,
NewToken
(
Underline
,
"_"
))
tokens
=
append
(
tokens
,
NewToken
(
Underline
,
"_"
))
case
'*'
:
case
'*'
:
tokens
=
append
(
tokens
,
NewToken
(
Star
,
"*"
))
tokens
=
append
(
tokens
,
NewToken
(
Star
,
"*"
))
case
'#'
:
tokens
=
append
(
tokens
,
NewToken
(
Hash
,
"#"
))
case
'\n'
:
case
'\n'
:
tokens
=
append
(
tokens
,
NewToken
(
Newline
,
"
\n
"
))
tokens
=
append
(
tokens
,
NewToken
(
Newline
,
"
\n
"
))
case
' '
:
case
' '
:
...
...
plugin/gomark/parser/tokenizer/tokenizer_test.go
View file @
fa53a255
...
@@ -32,9 +32,44 @@ func TestTokenize(t *testing.T) {
...
@@ -32,9 +32,44 @@ func TestTokenize(t *testing.T) {
},
},
},
},
},
},
{
text
:
`# hello
world`
,
tokens
:
[]
*
Token
{
{
Type
:
Hash
,
Value
:
"#"
,
},
{
Type
:
Space
,
Value
:
" "
,
},
{
Type
:
Text
,
Value
:
"hello"
,
},
{
Type
:
Space
,
Value
:
" "
,
},
{
Type
:
Newline
,
Value
:
"
\n
"
,
},
{
Type
:
Space
,
Value
:
" "
,
},
{
Type
:
Text
,
Value
:
"world"
,
},
},
},
}
}
for
_
,
test
:=
range
tests
{
for
_
,
test
:=
range
tests
{
result
:=
t
okenize
(
test
.
text
)
result
:=
T
okenize
(
test
.
text
)
require
.
Equal
(
t
,
test
.
tokens
,
result
)
require
.
Equal
(
t
,
test
.
tokens
,
result
)
}
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment