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
3f4b361f
Commit
3f4b361f
authored
Jan 15, 2024
by
Steven
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: implement highlight parser
parent
46bd4706
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
118 additions
and
0 deletions
+118
-0
ast.go
plugin/gomark/ast/ast.go
+1
-0
inline.go
plugin/gomark/ast/inline.go
+14
-0
highlight.go
plugin/gomark/parser/highlight.go
+58
-0
highlight_test.go
plugin/gomark/parser/highlight_test.go
+41
-0
parser.go
plugin/gomark/parser/parser.go
+1
-0
tokenizer.go
plugin/gomark/parser/tokenizer/tokenizer.go
+3
-0
No files found.
plugin/gomark/ast/ast.go
View file @
3f4b361f
...
...
@@ -28,6 +28,7 @@ const (
StrikethroughNode
EscapingCharacterNode
MathNode
HighlightNode
)
type
Node
interface
{
...
...
plugin/gomark/ast/inline.go
View file @
3f4b361f
...
...
@@ -191,3 +191,17 @@ func (*Math) Type() NodeType {
func
(
n
*
Math
)
Restore
()
string
{
return
fmt
.
Sprintf
(
"$%s$"
,
n
.
Content
)
}
type
Highlight
struct
{
BaseInline
Content
string
}
func
(
*
Highlight
)
Type
()
NodeType
{
return
HighlightNode
}
func
(
n
*
Highlight
)
Restore
()
string
{
return
fmt
.
Sprintf
(
"==%s=="
,
n
.
Content
)
}
plugin/gomark/parser/highlight.go
0 → 100644
View file @
3f4b361f
package
parser
import
(
"errors"
"github.com/usememos/memos/plugin/gomark/ast"
"github.com/usememos/memos/plugin/gomark/parser/tokenizer"
)
type
HighlightParser
struct
{}
func
NewHighlightParser
()
InlineParser
{
return
&
HighlightParser
{}
}
func
(
*
HighlightParser
)
Match
(
tokens
[]
*
tokenizer
.
Token
)
(
int
,
bool
)
{
if
len
(
tokens
)
<
5
{
return
0
,
false
}
prefixTokens
:=
tokens
[
:
2
]
if
prefixTokens
[
0
]
.
Type
!=
prefixTokens
[
1
]
.
Type
{
return
0
,
false
}
prefixTokenType
:=
prefixTokens
[
0
]
.
Type
if
prefixTokenType
!=
tokenizer
.
EqualSign
{
return
0
,
false
}
cursor
,
matched
:=
2
,
false
for
;
cursor
<
len
(
tokens
)
-
1
;
cursor
++
{
token
,
nextToken
:=
tokens
[
cursor
],
tokens
[
cursor
+
1
]
if
token
.
Type
==
tokenizer
.
Newline
||
nextToken
.
Type
==
tokenizer
.
Newline
{
return
0
,
false
}
if
token
.
Type
==
prefixTokenType
&&
nextToken
.
Type
==
prefixTokenType
{
matched
=
true
break
}
}
if
!
matched
{
return
0
,
false
}
return
cursor
+
2
,
true
}
func
(
p
*
HighlightParser
)
Parse
(
tokens
[]
*
tokenizer
.
Token
)
(
ast
.
Node
,
error
)
{
size
,
ok
:=
p
.
Match
(
tokens
)
if
size
==
0
||
!
ok
{
return
nil
,
errors
.
New
(
"not matched"
)
}
contentTokens
:=
tokens
[
2
:
size
-
2
]
return
&
ast
.
Highlight
{
Content
:
tokenizer
.
Stringify
(
contentTokens
),
},
nil
}
plugin/gomark/parser/highlight_test.go
0 → 100644
View file @
3f4b361f
package
parser
import
(
"testing"
"github.com/stretchr/testify/require"
"github.com/usememos/memos/plugin/gomark/ast"
"github.com/usememos/memos/plugin/gomark/parser/tokenizer"
"github.com/usememos/memos/plugin/gomark/restore"
)
func
TestHighlightParser
(
t
*
testing
.
T
)
{
tests
:=
[]
struct
{
text
string
bold
ast
.
Node
}{
{
text
:
"==Hello world!"
,
bold
:
nil
,
},
{
text
:
"==Hello=="
,
bold
:
&
ast
.
Highlight
{
Content
:
"Hello"
,
},
},
{
text
:
"==Hello world=="
,
bold
:
&
ast
.
Highlight
{
Content
:
"Hello world"
,
},
},
}
for
_
,
test
:=
range
tests
{
tokens
:=
tokenizer
.
Tokenize
(
test
.
text
)
node
,
_
:=
NewHighlightParser
()
.
Parse
(
tokens
)
require
.
Equal
(
t
,
restore
.
Restore
([]
ast
.
Node
{
test
.
bold
}),
restore
.
Restore
([]
ast
.
Node
{
node
}))
}
}
plugin/gomark/parser/parser.go
View file @
3f4b361f
...
...
@@ -80,6 +80,7 @@ var defaultInlineParsers = []InlineParser{
NewAutoLinkParser
(),
NewBoldParser
(),
NewItalicParser
(),
NewHighlightParser
(),
NewCodeParser
(),
NewMathParser
(),
NewTagParser
(),
...
...
plugin/gomark/parser/tokenizer/tokenizer.go
View file @
3f4b361f
...
...
@@ -19,6 +19,7 @@ const (
LessThan
TokenType
=
"<"
GreaterThan
TokenType
=
">"
DollarSign
TokenType
=
"$"
EqualSign
TokenType
=
"="
Backslash
TokenType
=
"
\\
"
Newline
TokenType
=
"
\n
"
Space
TokenType
=
" "
...
...
@@ -77,6 +78,8 @@ func Tokenize(text string) []*Token {
tokens
=
append
(
tokens
,
NewToken
(
Dot
,
"."
))
case
'$'
:
tokens
=
append
(
tokens
,
NewToken
(
DollarSign
,
"$"
))
case
'='
:
tokens
=
append
(
tokens
,
NewToken
(
EqualSign
,
"="
))
case
'\\'
:
tokens
=
append
(
tokens
,
NewToken
(
Backslash
,
`\`
))
case
'\n'
:
...
...
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