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
65890bc2
Unverified
Commit
65890bc2
authored
May 23, 2023
by
boojack
Committed by
GitHub
May 23, 2023
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: implement code block parser (#1727)
parent
42c653e1
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
191 additions
and
0 deletions
+191
-0
code.go
plugin/gomark/parser/code.go
+38
-0
code_block.go
plugin/gomark/parser/code_block.go
+52
-0
code_block_test.go
plugin/gomark/parser/code_block_test.go
+62
-0
code_test.go
plugin/gomark/parser/code_test.go
+36
-0
tokenizer.go
plugin/gomark/parser/tokenizer/tokenizer.go
+3
-0
No files found.
plugin/gomark/parser/code.go
0 → 100644
View file @
65890bc2
package
parser
import
"github.com/usememos/memos/plugin/gomark/parser/tokenizer"
type
CodeParser
struct
{
Content
string
}
func
NewCodeParser
()
*
CodeParser
{
return
&
CodeParser
{}
}
func
(
*
CodeParser
)
Match
(
tokens
[]
*
tokenizer
.
Token
)
*
CodeParser
{
if
len
(
tokens
)
<
3
{
return
nil
}
if
tokens
[
0
]
.
Type
!=
tokenizer
.
Backtick
{
return
nil
}
content
,
matched
:=
""
,
false
for
_
,
token
:=
range
tokens
[
1
:
]
{
if
token
.
Type
==
tokenizer
.
Newline
{
return
nil
}
if
token
.
Type
==
tokenizer
.
Backtick
{
matched
=
true
break
}
content
+=
token
.
Value
}
if
!
matched
||
len
(
content
)
==
0
{
return
nil
}
return
&
CodeParser
{
Content
:
content
,
}
}
plugin/gomark/parser/code_block.go
0 → 100644
View file @
65890bc2
package
parser
import
"github.com/usememos/memos/plugin/gomark/parser/tokenizer"
type
CodeBlockParser
struct
{
Language
string
Content
string
}
func
NewCodeBlockParser
()
*
CodeBlockParser
{
return
&
CodeBlockParser
{}
}
func
(
*
CodeBlockParser
)
Match
(
tokens
[]
*
tokenizer
.
Token
)
*
CodeBlockParser
{
if
len
(
tokens
)
<
9
{
return
nil
}
if
tokens
[
0
]
.
Type
!=
tokenizer
.
Backtick
||
tokens
[
1
]
.
Type
!=
tokenizer
.
Backtick
||
tokens
[
2
]
.
Type
!=
tokenizer
.
Backtick
{
return
nil
}
if
tokens
[
3
]
.
Type
!=
tokenizer
.
Newline
&&
tokens
[
4
]
.
Type
!=
tokenizer
.
Newline
{
return
nil
}
cursor
,
language
:=
4
,
""
if
tokens
[
3
]
.
Type
!=
tokenizer
.
Newline
{
language
=
tokens
[
3
]
.
Value
cursor
=
5
}
content
,
matched
:=
""
,
false
for
;
cursor
<
len
(
tokens
)
-
3
;
cursor
++
{
if
tokens
[
cursor
]
.
Type
==
tokenizer
.
Newline
&&
tokens
[
cursor
+
1
]
.
Type
==
tokenizer
.
Backtick
&&
tokens
[
cursor
+
2
]
.
Type
==
tokenizer
.
Backtick
&&
tokens
[
cursor
+
3
]
.
Type
==
tokenizer
.
Backtick
{
if
cursor
+
3
==
len
(
tokens
)
-
1
{
matched
=
true
break
}
else
if
tokens
[
cursor
+
4
]
.
Type
==
tokenizer
.
Newline
{
matched
=
true
break
}
}
content
+=
tokens
[
cursor
]
.
Value
}
if
!
matched
{
return
nil
}
return
&
CodeBlockParser
{
Language
:
language
,
Content
:
content
,
}
}
plugin/gomark/parser/code_block_test.go
0 → 100644
View file @
65890bc2
package
parser
import
(
"testing"
"github.com/stretchr/testify/require"
"github.com/usememos/memos/plugin/gomark/parser/tokenizer"
)
func
TestCodeBlockParser
(
t
*
testing
.
T
)
{
tests
:=
[]
struct
{
text
string
codeBlock
*
CodeBlockParser
}{
{
text
:
"```Hello world!```"
,
codeBlock
:
nil
,
},
{
text
:
"```
\n
Hello
\n
```"
,
codeBlock
:
&
CodeBlockParser
{
Language
:
""
,
Content
:
"Hello"
,
},
},
{
text
:
"```
\n
Hello world!
\n
```"
,
codeBlock
:
&
CodeBlockParser
{
Language
:
""
,
Content
:
"Hello world!"
,
},
},
{
text
:
"```java
\n
Hello
\n
world!
\n
```"
,
codeBlock
:
&
CodeBlockParser
{
Language
:
"java"
,
Content
:
"Hello
\n
world!"
,
},
},
{
text
:
"```java
\n
Hello
\n
world!
\n
```111"
,
codeBlock
:
nil
,
},
{
text
:
"```java
\n
Hello
\n
world!
\n
``` 111"
,
codeBlock
:
nil
,
},
{
text
:
"```java
\n
Hello
\n
world!
\n
```
\n
123123"
,
codeBlock
:
&
CodeBlockParser
{
Language
:
"java"
,
Content
:
"Hello
\n
world!"
,
},
},
}
for
_
,
test
:=
range
tests
{
tokens
:=
tokenizer
.
Tokenize
(
test
.
text
)
codeBlock
:=
NewCodeBlockParser
()
require
.
Equal
(
t
,
test
.
codeBlock
,
codeBlock
.
Match
(
tokens
))
}
}
plugin/gomark/parser/code_test.go
0 → 100644
View file @
65890bc2
package
parser
import
(
"testing"
"github.com/stretchr/testify/require"
"github.com/usememos/memos/plugin/gomark/parser/tokenizer"
)
func
TestCodeParser
(
t
*
testing
.
T
)
{
tests
:=
[]
struct
{
text
string
code
*
CodeParser
}{
{
text
:
"`Hello world!"
,
code
:
nil
,
},
{
text
:
"`Hello world!`"
,
code
:
&
CodeParser
{
Content
:
"Hello world!"
,
},
},
{
text
:
"`Hello
\n
world!`"
,
code
:
nil
,
},
}
for
_
,
test
:=
range
tests
{
tokens
:=
tokenizer
.
Tokenize
(
test
.
text
)
code
:=
NewCodeParser
()
require
.
Equal
(
t
,
test
.
code
,
code
.
Match
(
tokens
))
}
}
plugin/gomark/parser/tokenizer/tokenizer.go
View file @
65890bc2
...
...
@@ -6,6 +6,7 @@ const (
Underline
TokenType
=
"_"
Star
TokenType
=
"*"
Hash
TokenType
=
"#"
Backtick
TokenType
=
"`"
Newline
TokenType
=
"
\n
"
Space
TokenType
=
" "
)
...
...
@@ -38,6 +39,8 @@ func Tokenize(text string) []*Token {
tokens
=
append
(
tokens
,
NewToken
(
Hash
,
"#"
))
case
'\n'
:
tokens
=
append
(
tokens
,
NewToken
(
Newline
,
"
\n
"
))
case
'`'
:
tokens
=
append
(
tokens
,
NewToken
(
Backtick
,
"`"
))
case
' '
:
tokens
=
append
(
tokens
,
NewToken
(
Space
,
" "
))
default
:
...
...
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