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
932f636d
Commit
932f636d
authored
Jan 27, 2024
by
Steven
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
chore: update codeblock renderer
parent
ed32b20c
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
47 additions
and
42 deletions
+47
-42
code_block.go
plugin/gomark/parser/code_block.go
+36
-29
math_block.go
plugin/gomark/parser/math_block.go
+8
-5
CodeBlock.tsx
web/src/components/MemoContent/CodeBlock.tsx
+3
-8
No files found.
plugin/gomark/parser/code_block.go
View file @
932f636d
package
parser
package
parser
import
(
import
(
"slices"
"github.com/usememos/memos/plugin/gomark/ast"
"github.com/usememos/memos/plugin/gomark/ast"
"github.com/usememos/memos/plugin/gomark/parser/tokenizer"
"github.com/usememos/memos/plugin/gomark/parser/tokenizer"
)
)
...
@@ -15,51 +17,56 @@ func NewCodeBlockParser() *CodeBlockParser {
...
@@ -15,51 +17,56 @@ func NewCodeBlockParser() *CodeBlockParser {
}
}
func
(
*
CodeBlockParser
)
Match
(
tokens
[]
*
tokenizer
.
Token
)
(
ast
.
Node
,
int
)
{
func
(
*
CodeBlockParser
)
Match
(
tokens
[]
*
tokenizer
.
Token
)
(
ast
.
Node
,
int
)
{
if
len
(
tokens
)
<
9
{
rows
:=
tokenizer
.
Split
(
tokens
,
tokenizer
.
Newline
)
if
len
(
rows
)
<
3
{
return
nil
,
0
return
nil
,
0
}
}
if
tokens
[
0
]
.
Type
!=
tokenizer
.
Backtick
||
tokens
[
1
]
.
Type
!=
tokenizer
.
Backtick
||
tokens
[
2
]
.
Type
!=
tokenizer
.
Backtick
{
firstRow
:=
rows
[
0
]
if
len
(
firstRow
)
<
3
{
return
nil
,
0
}
if
firstRow
[
0
]
.
Type
!=
tokenizer
.
Backtick
||
firstRow
[
1
]
.
Type
!=
tokenizer
.
Backtick
||
firstRow
[
2
]
.
Type
!=
tokenizer
.
Backtick
{
return
nil
,
0
return
nil
,
0
}
}
if
tokens
[
3
]
.
Type
!=
tokenizer
.
Newline
&&
tokens
[
4
]
.
Type
!=
tokenizer
.
Newline
{
languageTokens
:=
[]
*
tokenizer
.
Token
{}
if
len
(
firstRow
)
>
3
{
languageTokens
=
firstRow
[
3
:
]
// Check if language is valid.
availableLanguageTokenTypes
:=
[]
tokenizer
.
TokenType
{
tokenizer
.
Text
,
tokenizer
.
Number
,
tokenizer
.
Underscore
}
for
_
,
token
:=
range
languageTokens
{
if
!
slices
.
Contains
(
availableLanguageTokenTypes
,
token
.
Type
)
{
return
nil
,
0
return
nil
,
0
}
}
cursor
:=
4
}
if
tokens
[
3
]
.
Type
!=
tokenizer
.
Newline
{
cursor
=
5
}
}
contentRows
:=
[][]
*
tokenizer
.
Token
{}
matched
:=
false
matched
:=
false
for
;
cursor
<
len
(
tokens
)
-
3
;
cursor
++
{
for
_
,
row
:=
range
rows
[
1
:
]
{
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
len
(
row
)
==
3
&&
row
[
0
]
.
Type
==
tokenizer
.
Backtick
&&
row
[
1
]
.
Type
==
tokenizer
.
Backtick
&&
row
[
2
]
.
Type
==
tokenizer
.
Backtick
{
if
cursor
+
3
==
len
(
tokens
)
-
1
{
cursor
+=
4
matched
=
true
break
}
else
if
tokens
[
cursor
+
4
]
.
Type
==
tokenizer
.
Newline
{
cursor
+=
4
matched
=
true
matched
=
true
break
break
}
}
}
contentRows
=
append
(
contentRows
,
row
)
}
}
if
!
matched
{
if
!
matched
{
return
nil
,
0
return
nil
,
0
}
}
languageToken
:=
tokens
[
3
]
contentTokens
:=
[]
*
tokenizer
.
Token
{}
contentStart
,
contentEnd
:=
5
,
cursor
-
4
for
index
,
row
:=
range
contentRows
{
if
languageToken
.
Type
==
tokenizer
.
Newline
{
contentTokens
=
append
(
contentTokens
,
row
...
)
languageToken
=
nil
if
index
!=
len
(
contentRows
)
-
1
{
contentStart
=
4
contentTokens
=
append
(
contentTokens
,
&
tokenizer
.
Token
{
Type
:
tokenizer
.
Newline
,
Value
:
"
\n
"
,
})
}
}
codeBlock
:=
&
ast
.
CodeBlock
{
Content
:
tokenizer
.
Stringify
(
tokens
[
contentStart
:
contentEnd
]),
}
}
if
languageToken
!=
nil
{
codeBlock
.
Language
=
languageToken
.
String
()
return
&
ast
.
CodeBlock
{
}
Content
:
tokenizer
.
Stringify
(
contentTokens
),
return
codeBlock
,
cursor
Language
:
tokenizer
.
Stringify
(
languageTokens
),
},
4
+
len
(
languageTokens
)
+
len
(
contentTokens
)
+
4
}
}
plugin/gomark/parser/math_block.go
View file @
932f636d
...
@@ -38,13 +38,16 @@ func (*MathBlockParser) Match(tokens []*tokenizer.Token) (ast.Node, int) {
...
@@ -38,13 +38,16 @@ func (*MathBlockParser) Match(tokens []*tokenizer.Token) (ast.Node, int) {
}
}
contentTokens
:=
[]
*
tokenizer
.
Token
{}
contentTokens
:=
[]
*
tokenizer
.
Token
{}
for
_
,
row
:=
range
contentRows
{
for
index
,
row
:=
range
contentRows
{
contentTokens
=
append
(
contentTokens
,
row
...
)
contentTokens
=
append
(
contentTokens
,
row
...
)
if
index
!=
len
(
contentRows
)
-
1
{
contentTokens
=
append
(
contentTokens
,
&
tokenizer
.
Token
{
contentTokens
=
append
(
contentTokens
,
&
tokenizer
.
Token
{
Type
:
tokenizer
.
Newline
,
Type
:
tokenizer
.
Newline
,
Value
:
"
\n
"
,
})
})
}
}
}
return
&
ast
.
MathBlock
{
return
&
ast
.
MathBlock
{
Content
:
tokenizer
.
Stringify
(
contentTokens
),
Content
:
tokenizer
.
Stringify
(
contentTokens
),
},
3
+
len
(
contentTokens
)
+
2
},
3
+
len
(
contentTokens
)
+
3
}
}
web/src/components/MemoContent/CodeBlock.tsx
View file @
932f636d
...
@@ -15,14 +15,9 @@ const CodeBlock: React.FC<Props> = ({ language, content }: Props) => {
...
@@ -15,14 +15,9 @@ const CodeBlock: React.FC<Props> = ({ language, content }: Props) => {
const
formatedLanguage
=
language
.
toLowerCase
()
||
"plaintext"
;
const
formatedLanguage
=
language
.
toLowerCase
()
||
"plaintext"
;
let
highlightedCode
=
hljs
.
highlightAuto
(
content
).
value
;
let
highlightedCode
=
hljs
.
highlightAuto
(
content
).
value
;
// Users can set Markdown code blocks as 'iframe'
// Users can set Markdown code blocks as `__html` to render HTML directly.
// to embed videos or external audio links from services like Apple Music or Spotify.
if
(
formatedLanguage
===
"__html"
)
{
if
(
formatedLanguage
===
"iframe"
)
{
return
<
div
className=
"w-full !my-2"
dangerouslySetInnerHTML=
{
{
__html
:
content
}
}
/>;
const
renderHTML
=
()
=>
{
return
{
__html
:
content
};
};
return
<
div
className=
"mx-auto w-11/12 !my-4"
dangerouslySetInnerHTML=
{
renderHTML
()
}
/>;
}
}
try
{
try
{
...
...
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