-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
99 lines (84 loc) · 2.24 KB
/
main.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
98
99
package main
import (
"bytes"
"github.com/chasefleming/elem-go"
"github.com/chasefleming/elem-go/attrs"
"github.com/yuin/goldmark"
"log"
"os"
"path/filepath"
"strings"
)
func layout(title string, content elem.Node) string {
htmlPage := elem.Html(nil,
elem.Head(nil,
elem.Title(nil, elem.Text(title)),
),
elem.Body(nil,
elem.Header(nil, elem.H1(nil, elem.Text(title))),
elem.Main(nil, content),
elem.Footer(nil, elem.Text("Footer content here")),
),
)
return htmlPage.Render()
}
func createHTMLPage(title, content string) string {
htmlOutput := layout(title, elem.Raw(content))
postFilename := title + ".html"
filepath := filepath.Join("public", postFilename)
os.WriteFile(filepath, []byte(htmlOutput), 0644)
return postFilename
}
func markdownToHTML(content string) string {
var buf bytes.Buffer
md := goldmark.New()
if err := md.Convert([]byte(content), &buf); err != nil {
log.Fatal(err)
}
return buf.String()
}
func readMarkdownPosts(dir string) []string {
var posts []string
err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if !info.IsDir() {
content, err := os.ReadFile(path)
if err != nil {
return err
}
htmlContent := markdownToHTML(string(content))
title := strings.TrimSuffix(info.Name(), filepath.Ext(info.Name()))
postFilename := createHTMLPage(title, htmlContent)
posts = append(posts, postFilename)
}
return nil
})
if err != nil {
log.Fatal(err)
}
return posts
}
func createIndexPage(postFilenames []string) {
listItems := make([]elem.Node, len(postFilenames))
for i, filename := range postFilenames {
link := elem.A(attrs.Props{attrs.Href: "./" + filename}, elem.Text(filename))
listItems[i] = elem.Li(nil, link)
}
indexContent := elem.Ul(nil, listItems...)
htmlOutput := layout("Home", indexContent)
filepath := filepath.Join("public", "index.html")
os.WriteFile(filepath, []byte(htmlOutput), 0644)
}
func createDirIfNotExist(dir string) {
if _, err := os.Stat(dir); os.IsNotExist(err) {
err := os.Mkdir(dir, 0755) // or 0700 if you need it to be private
if err != nil {
log.Fatal(err)
}
}
}
func main() {
createDirIfNotExist("posts")
createDirIfNotExist("public")
posts := readMarkdownPosts("posts")
createIndexPage(posts)
}