0 package gmi
1
2 import (
3 "github.com/gomarkdown/markdown/ast"
4 "github.com/gomarkdown/markdown/parser"
5 )
6
7 func parseMarkdown(node ast.Node, isListItem bool) string {
8 s := ""
9 for _, v := range node.GetChildren() {
10 if v.AsContainer() != nil {
11 _, ok := v.(*ast.ListItem)
12 _, isList := v.(*ast.List)
13 s += parseMarkdown(v, ok || isListItem)
14 if isList { s += "\n" }
15 } else {
16 leaf := v.AsLeaf()
17 ptr := leaf.Literal
18 isLink := false
19 if ptr == nil { ptr = leaf.Content }
20 if ptr == nil { continue }
21 if i, ok := v.GetParent().(*ast.Image); ok {
22 s += "=>" + string(i.Destination) + " "
23 isLink = true
24 }
25 if l, ok := v.GetParent().(*ast.Link); ok {
26 s += "=>" + string(l.Destination) + " "
27 isLink = true
28 }
29 if _, ok := v.(*ast.Text); ok {
30 if string(ptr) == "" { continue }
31 h, ok := v.GetParent().(*ast.Heading)
32 if ok {
33 for i := 0; i < h.Level; i++ {
34 s += "#"
35 }
36 s += " "
37 } else if isListItem && !isLink {
38 s += "* "
39 }
40 s += string(ptr)
41 s += "\n"
42 if !isListItem {
43 s += "\n"
44 }
45 }
46 if _, ok := v.(*ast.CodeBlock); ok {
47 s += "```\n" + string(ptr) + "\n```\n"
48 }
49 }
50 }
51 return s
52 }
53
54 func fromMarkdownToGmi(data string) string {
55 extensions := parser.CommonExtensions
56 p := parser.NewWithExtensions(extensions)
57 doc := p.Parse([]byte(data))
58 return parseMarkdown(doc, false)
59 }
60