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
a316e239
Commit
a316e239
authored
Jan 23, 2024
by
Steven
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
chore: implement referenced content node
parent
d7f02b94
Changes
7
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
132 additions
and
3 deletions
+132
-3
ast.go
plugin/gomark/ast/ast.go
+1
-0
block.go
plugin/gomark/ast/block.go
+3
-2
inline.go
plugin/gomark/ast/inline.go
+20
-0
embedded_content.go
plugin/gomark/parser/embedded_content.go
+1
-1
parser.go
plugin/gomark/parser/parser.go
+1
-0
referenced_content.go
plugin/gomark/parser/referenced_content.go
+45
-0
referenced_content_test.go
plugin/gomark/parser/referenced_content_test.go
+61
-0
No files found.
plugin/gomark/ast/ast.go
View file @
a316e239
...
@@ -33,6 +33,7 @@ const (
...
@@ -33,6 +33,7 @@ const (
HighlightNode
HighlightNode
SubscriptNode
SubscriptNode
SuperscriptNode
SuperscriptNode
ReferencedContentNode
)
)
type
Node
interface
{
type
Node
interface
{
...
...
plugin/gomark/ast/block.go
View file @
a316e239
...
@@ -241,9 +241,10 @@ func (*EmbeddedContent) Type() NodeType {
...
@@ -241,9 +241,10 @@ func (*EmbeddedContent) Type() NodeType {
}
}
func
(
n
*
EmbeddedContent
)
Restore
()
string
{
func
(
n
*
EmbeddedContent
)
Restore
()
string
{
result
:=
fmt
.
Sprintf
(
"![[%s]]"
,
n
.
ResourceName
)
params
:=
""
if
n
.
Params
!=
""
{
if
n
.
Params
!=
""
{
result
+
=
fmt
.
Sprintf
(
"?%s"
,
n
.
Params
)
params
=
fmt
.
Sprintf
(
"?%s"
,
n
.
Params
)
}
}
result
:=
fmt
.
Sprintf
(
"![[%s%s]]"
,
n
.
ResourceName
,
params
)
return
result
return
result
}
}
plugin/gomark/ast/inline.go
View file @
a316e239
...
@@ -233,3 +233,23 @@ func (*Superscript) Type() NodeType {
...
@@ -233,3 +233,23 @@ func (*Superscript) Type() NodeType {
func
(
n
*
Superscript
)
Restore
()
string
{
func
(
n
*
Superscript
)
Restore
()
string
{
return
fmt
.
Sprintf
(
"^%s^"
,
n
.
Content
)
return
fmt
.
Sprintf
(
"^%s^"
,
n
.
Content
)
}
}
type
ReferencedContent
struct
{
BaseInline
ResourceName
string
Params
string
}
func
(
*
ReferencedContent
)
Type
()
NodeType
{
return
ReferencedContentNode
}
func
(
n
*
ReferencedContent
)
Restore
()
string
{
params
:=
""
if
n
.
Params
!=
""
{
params
=
fmt
.
Sprintf
(
"?%s"
,
n
.
Params
)
}
result
:=
fmt
.
Sprintf
(
"[[%s%s]]"
,
n
.
ResourceName
,
params
)
return
result
}
plugin/gomark/parser/embedded_content.go
View file @
a316e239
...
@@ -13,7 +13,7 @@ func NewEmbeddedContentParser() *EmbeddedContentParser {
...
@@ -13,7 +13,7 @@ func NewEmbeddedContentParser() *EmbeddedContentParser {
func
(
*
EmbeddedContentParser
)
Match
(
tokens
[]
*
tokenizer
.
Token
)
(
ast
.
Node
,
int
)
{
func
(
*
EmbeddedContentParser
)
Match
(
tokens
[]
*
tokenizer
.
Token
)
(
ast
.
Node
,
int
)
{
matchedTokens
:=
tokenizer
.
GetFirstLine
(
tokens
)
matchedTokens
:=
tokenizer
.
GetFirstLine
(
tokens
)
if
len
(
matchedTokens
)
<
5
{
if
len
(
matchedTokens
)
<
6
{
return
nil
,
0
return
nil
,
0
}
}
if
matchedTokens
[
0
]
.
Type
!=
tokenizer
.
ExclamationMark
||
matchedTokens
[
1
]
.
Type
!=
tokenizer
.
LeftSquareBracket
||
matchedTokens
[
2
]
.
Type
!=
tokenizer
.
LeftSquareBracket
{
if
matchedTokens
[
0
]
.
Type
!=
tokenizer
.
ExclamationMark
||
matchedTokens
[
1
]
.
Type
!=
tokenizer
.
LeftSquareBracket
||
matchedTokens
[
2
]
.
Type
!=
tokenizer
.
LeftSquareBracket
{
...
...
plugin/gomark/parser/parser.go
View file @
a316e239
...
@@ -80,6 +80,7 @@ var defaultInlineParsers = []InlineParser{
...
@@ -80,6 +80,7 @@ var defaultInlineParsers = []InlineParser{
NewSubscriptParser
(),
NewSubscriptParser
(),
NewSuperscriptParser
(),
NewSuperscriptParser
(),
NewMathParser
(),
NewMathParser
(),
NewReferencedContentParser
(),
NewTagParser
(),
NewTagParser
(),
NewStrikethroughParser
(),
NewStrikethroughParser
(),
NewLineBreakParser
(),
NewLineBreakParser
(),
...
...
plugin/gomark/parser/referenced_content.go
0 → 100644
View file @
a316e239
package
parser
import
(
"github.com/usememos/memos/plugin/gomark/ast"
"github.com/usememos/memos/plugin/gomark/parser/tokenizer"
)
type
ReferencedContentParser
struct
{}
func
NewReferencedContentParser
()
*
ReferencedContentParser
{
return
&
ReferencedContentParser
{}
}
func
(
*
ReferencedContentParser
)
Match
(
tokens
[]
*
tokenizer
.
Token
)
(
ast
.
Node
,
int
)
{
matchedTokens
:=
tokenizer
.
GetFirstLine
(
tokens
)
if
len
(
matchedTokens
)
<
5
{
return
nil
,
0
}
if
matchedTokens
[
0
]
.
Type
!=
tokenizer
.
LeftSquareBracket
||
matchedTokens
[
1
]
.
Type
!=
tokenizer
.
LeftSquareBracket
{
return
nil
,
0
}
contentTokens
:=
[]
*
tokenizer
.
Token
{}
matched
:=
false
for
index
,
token
:=
range
matchedTokens
[
2
:
len
(
matchedTokens
)
-
1
]
{
if
token
.
Type
==
tokenizer
.
RightSquareBracket
&&
matchedTokens
[
2
+
index
+
1
]
.
Type
==
tokenizer
.
RightSquareBracket
{
matched
=
true
break
}
contentTokens
=
append
(
contentTokens
,
token
)
}
if
!
matched
{
return
nil
,
0
}
resourceName
,
params
:=
tokenizer
.
Stringify
(
contentTokens
),
""
questionMarkIndex
:=
tokenizer
.
FindUnescaped
(
contentTokens
,
tokenizer
.
QuestionMark
)
if
questionMarkIndex
>
0
{
resourceName
,
params
=
tokenizer
.
Stringify
(
contentTokens
[
:
questionMarkIndex
]),
tokenizer
.
Stringify
(
contentTokens
[
questionMarkIndex
+
1
:
])
}
return
&
ast
.
ReferencedContent
{
ResourceName
:
resourceName
,
Params
:
params
,
},
len
(
contentTokens
)
+
4
}
plugin/gomark/parser/referenced_content_test.go
0 → 100644
View file @
a316e239
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
TestReferencedContentParser
(
t
*
testing
.
T
)
{
tests
:=
[]
struct
{
text
string
referencedContent
ast
.
Node
}{
{
text
:
"[[Hello world]"
,
referencedContent
:
nil
,
},
{
text
:
"[[Hello world]]"
,
referencedContent
:
&
ast
.
ReferencedContent
{
ResourceName
:
"Hello world"
,
},
},
{
text
:
"[[memos/1]]"
,
referencedContent
:
&
ast
.
ReferencedContent
{
ResourceName
:
"memos/1"
,
},
},
{
text
:
"[[resources/101]]111
\n
123"
,
referencedContent
:
&
ast
.
ReferencedContent
{
ResourceName
:
"resources/101"
,
},
},
{
text
:
"[[resources/101?align=center]]"
,
referencedContent
:
&
ast
.
ReferencedContent
{
ResourceName
:
"resources/101"
,
Params
:
"align=center"
,
},
},
{
text
:
"[[resources/6uxnhT98q8vN8anBbUbRGu?align=center]]"
,
referencedContent
:
&
ast
.
ReferencedContent
{
ResourceName
:
"resources/6uxnhT98q8vN8anBbUbRGu"
,
Params
:
"align=center"
,
},
},
}
for
_
,
test
:=
range
tests
{
tokens
:=
tokenizer
.
Tokenize
(
test
.
text
)
node
,
_
:=
NewReferencedContentParser
()
.
Match
(
tokens
)
require
.
Equal
(
t
,
restore
.
Restore
([]
ast
.
Node
{
test
.
referencedContent
}),
restore
.
Restore
([]
ast
.
Node
{
node
}))
}
}
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