-
Notifications
You must be signed in to change notification settings - Fork 0
/
markdown.go
97 lines (83 loc) · 2.18 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package internal
import (
"fmt"
"os"
"strings"
)
type Markdown struct {
docsDir string
}
func NewMarkdown() *Markdown {
return &Markdown{}
}
func (client *Markdown) writeToFile(content string, destination string) (err error) {
path := destination[:strings.LastIndex(destination, "/")]
os.MkdirAll(path, 0770)
file, err := os.Create(destination)
if err != nil {
if !os.IsExist(err) {
return err
}
file, err = os.Open(destination)
if err != nil {
return err
}
}
defer file.Close()
_, err = file.WriteString(content)
if err != nil {
return err
}
return nil
}
func (client *Markdown) GenerateTitle(title string, level int) string {
return fmt.Sprintf("%s %s \n", strings.Repeat("#", level), title)
}
func (client *Markdown) GenerateLink(title string, link string) string {
return fmt.Sprintf("[%s](%s)", title, strings.ReplaceAll(link, " ", "%20"))
}
func (client *Markdown) GenerateListOfStringPointers(items []*string) (listText string) {
for _, item := range items {
listText += client.GenerateListItem(*item)
}
return listText
}
//func MarkdownGenerateListOfPointers(items []*any) (listText string) {
// for _, item := range items {
// listText += GenerateListItem(*item)
// }
// return listText
//}
func (client *Markdown) GenerateList(items []any) (listText string) {
for _, item := range items {
listText += client.GenerateListItem(item)
}
return listText
}
func (client *Markdown) GenerateListItem(itemText any) string {
return fmt.Sprintf("- %s \n", itemText)
}
//func MarkdownGenerateTable(itemText any) string {
// return fmt.Sprintf("- %s \n", itemText)
//}
func (client *Markdown) GenerateTableHeader(headers ...string) string {
markdown := client.GenerateTableRow(headers...)
separator := client.GenerateTableSeparator(len(headers))
return markdown + separator
}
func (client *Markdown) GenerateTableSeparator(columnCount int) string {
markdown := "|"
for i := 0; i < columnCount; i++ {
markdown += " --- |"
}
markdown += "\n"
return markdown
}
func (client *Markdown) GenerateTableRow(fields ...string) string {
markdown := "|"
for _, field := range fields {
markdown += fmt.Sprintf(" %s |", field)
}
markdown += "\n"
return markdown
}