mirror of
https://github.com/usememos/memos.git
synced 2024-12-23 03:02:10 +03:00
42 lines
669 B
Go
42 lines
669 B
Go
|
package parser
|
||
|
|
||
|
import (
|
||
|
"strings"
|
||
|
|
||
|
"github.com/usememos/memos/plugin/gomark/ast"
|
||
|
)
|
||
|
|
||
|
type HeadingTokenizer struct {
|
||
|
}
|
||
|
|
||
|
func NewHeadingTokenizer() *HeadingTokenizer {
|
||
|
return &HeadingTokenizer{}
|
||
|
}
|
||
|
|
||
|
func (*HeadingTokenizer) Trigger() []byte {
|
||
|
return []byte{'#'}
|
||
|
}
|
||
|
|
||
|
func (*HeadingTokenizer) Parse(parent *ast.Node, block string) *ast.Node {
|
||
|
line := block
|
||
|
level := 0
|
||
|
for _, c := range line {
|
||
|
if c == '#' {
|
||
|
level++
|
||
|
} else if c == ' ' {
|
||
|
break
|
||
|
} else {
|
||
|
return nil
|
||
|
}
|
||
|
}
|
||
|
if level == 0 || level > 6 {
|
||
|
return nil
|
||
|
}
|
||
|
text := strings.TrimSpace(line[level+1:])
|
||
|
node := ast.NewNode("h1", text)
|
||
|
if parent != nil {
|
||
|
parent.AddChild(node)
|
||
|
}
|
||
|
return node
|
||
|
}
|