-
Notifications
You must be signed in to change notification settings - Fork 1
/
markdown.go
65 lines (61 loc) · 1.42 KB
/
markdown.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package sqlmanager
import (
"errors"
"path/filepath"
"strings"
"github.com/gomarkdown/markdown"
"github.com/gomarkdown/markdown/ast"
"github.com/gomarkdown/markdown/parser"
)
func parseMarkdown(buf []byte, prefix string) ([]SqlTemple, error) {
var sqls []SqlTemple
buf = markdown.NormalizeNewlines(buf)
psr := parser.New()
node := markdown.Parse(buf, psr)
list := getAll(node)
i := 0
for {
// 1. text, code
// 2. text, text, code
if i >= len(list) {
break
}
name := filepath.Join(prefix, list[i].Content)
name = strings.ReplaceAll(name, "\\", "/")
var tpl SqlTemple
if list[i].Type == "text" && list[i+1].Type == "code" {
tpl.Name = name
tpl.Sql = list[i+1].Content
sqls = append(sqls, tpl)
i += 2
} else if list[i].Type == "text" && list[i+1].Type == "text" && list[i+2].Type == "code" {
tpl.Name = name
tpl.Description = list[i+1].Content
tpl.Sql = list[i+2].Content
sqls = append(sqls, tpl)
i += 3
} else {
return nil, errors.New("parse markdown failed")
}
}
return sqls, nil
}
func getAll(node ast.Node) []item {
var list []item
ast.WalkFunc(node, func(node ast.Node, entering bool) ast.WalkStatus {
switch v := node.(type) {
case *ast.Text:
list = append(list, item{
Type: "text",
Content: string(v.Literal),
})
case *ast.CodeBlock:
list = append(list, item{
Type: "code",
Content: string(v.Literal),
})
}
return 0
})
return list
}