mirror of https://github.com/usememos/memos
feat: implement math expression parser
parent
c842b921bc
commit
d12a2b0c38
@ -0,0 +1,56 @@
|
|||||||
|
package parser
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
|
||||||
|
"github.com/usememos/memos/plugin/gomark/ast"
|
||||||
|
"github.com/usememos/memos/plugin/gomark/parser/tokenizer"
|
||||||
|
)
|
||||||
|
|
||||||
|
type MathParser struct{}
|
||||||
|
|
||||||
|
func NewMathParser() *MathParser {
|
||||||
|
return &MathParser{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (*MathParser) Match(tokens []*tokenizer.Token) (int, bool) {
|
||||||
|
if len(tokens) < 3 {
|
||||||
|
return 0, false
|
||||||
|
}
|
||||||
|
|
||||||
|
if tokens[0].Type != tokenizer.DollarSign {
|
||||||
|
return 0, false
|
||||||
|
}
|
||||||
|
|
||||||
|
contentTokens := []*tokenizer.Token{}
|
||||||
|
for _, token := range tokens[1:] {
|
||||||
|
if token.Type == tokenizer.Newline {
|
||||||
|
return 0, false
|
||||||
|
}
|
||||||
|
if token.Type == tokenizer.DollarSign {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
contentTokens = append(contentTokens, token)
|
||||||
|
}
|
||||||
|
if len(contentTokens) == 0 {
|
||||||
|
return 0, false
|
||||||
|
}
|
||||||
|
if len(contentTokens)+2 > len(tokens) {
|
||||||
|
return 0, false
|
||||||
|
}
|
||||||
|
if tokens[len(contentTokens)+1].Type != tokenizer.DollarSign {
|
||||||
|
return 0, false
|
||||||
|
}
|
||||||
|
return len(contentTokens) + 2, true
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *MathParser) Parse(tokens []*tokenizer.Token) (ast.Node, error) {
|
||||||
|
size, ok := p.Match(tokens)
|
||||||
|
if size == 0 || !ok {
|
||||||
|
return nil, errors.New("not matched")
|
||||||
|
}
|
||||||
|
|
||||||
|
return &ast.Math{
|
||||||
|
Content: tokenizer.Stringify(tokens[1 : size-1]),
|
||||||
|
}, nil
|
||||||
|
}
|
@ -0,0 +1,56 @@
|
|||||||
|
package parser
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
|
||||||
|
"github.com/usememos/memos/plugin/gomark/ast"
|
||||||
|
"github.com/usememos/memos/plugin/gomark/parser/tokenizer"
|
||||||
|
)
|
||||||
|
|
||||||
|
type MathBlockParser struct{}
|
||||||
|
|
||||||
|
func NewMathBlockParser() *MathBlockParser {
|
||||||
|
return &MathBlockParser{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (*MathBlockParser) Match(tokens []*tokenizer.Token) (int, bool) {
|
||||||
|
if len(tokens) < 7 {
|
||||||
|
return 0, false
|
||||||
|
}
|
||||||
|
|
||||||
|
if tokens[0].Type != tokenizer.DollarSign && tokens[1].Type != tokenizer.DollarSign && tokens[2].Type != tokenizer.Newline {
|
||||||
|
return 0, false
|
||||||
|
}
|
||||||
|
|
||||||
|
cursor := 3
|
||||||
|
matched := false
|
||||||
|
for ; cursor < len(tokens)-2; cursor++ {
|
||||||
|
if tokens[cursor].Type == tokenizer.Newline && tokens[cursor+1].Type == tokenizer.DollarSign && tokens[cursor+2].Type == tokenizer.DollarSign {
|
||||||
|
if cursor+2 == len(tokens)-1 {
|
||||||
|
cursor += 3
|
||||||
|
matched = true
|
||||||
|
break
|
||||||
|
} else if tokens[cursor+3].Type == tokenizer.Newline {
|
||||||
|
cursor += 3
|
||||||
|
matched = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !matched {
|
||||||
|
return 0, false
|
||||||
|
}
|
||||||
|
|
||||||
|
return cursor, true
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *MathBlockParser) Parse(tokens []*tokenizer.Token) (ast.Node, error) {
|
||||||
|
size, ok := p.Match(tokens)
|
||||||
|
if size == 0 || !ok {
|
||||||
|
return nil, errors.New("not matched")
|
||||||
|
}
|
||||||
|
|
||||||
|
return &ast.MathBlock{
|
||||||
|
Content: tokenizer.Stringify(tokens[3 : size-3]),
|
||||||
|
}, nil
|
||||||
|
}
|
@ -0,0 +1,30 @@
|
|||||||
|
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 TestMathBlockParser(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
text string
|
||||||
|
link ast.Node
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
text: "$$\n(1+x)^2\n$$",
|
||||||
|
link: &ast.MathBlock{
|
||||||
|
Content: "(1+x)^2",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, test := range tests {
|
||||||
|
tokens := tokenizer.Tokenize(test.text)
|
||||||
|
node, _ := NewMathBlockParser().Parse(tokens)
|
||||||
|
require.Equal(t, restore.Restore([]ast.Node{test.link}), restore.Restore([]ast.Node{node}))
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,30 @@
|
|||||||
|
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 TestMathParser(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
text string
|
||||||
|
link ast.Node
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
text: "$\\sqrt{3x-1}+(1+x)^2$",
|
||||||
|
link: &ast.Math{
|
||||||
|
Content: "\\sqrt{3x-1}+(1+x)^2",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, test := range tests {
|
||||||
|
tokens := tokenizer.Tokenize(test.text)
|
||||||
|
node, _ := NewMathParser().Parse(tokens)
|
||||||
|
require.Equal(t, restore.Restore([]ast.Node{test.link}), restore.Restore([]ast.Node{node}))
|
||||||
|
}
|
||||||
|
}
|
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,13 @@
|
|||||||
|
import TeX from "@matejmazur/react-katex";
|
||||||
|
import "katex/dist/katex.min.css";
|
||||||
|
|
||||||
|
interface Props {
|
||||||
|
content: string;
|
||||||
|
block?: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
const Math: React.FC<Props> = ({ content, block }: Props) => {
|
||||||
|
return <TeX block={block} math={content}></TeX>;
|
||||||
|
};
|
||||||
|
|
||||||
|
export default Math;
|
Loading…
Reference in New Issue