-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathchuno.go
78 lines (66 loc) · 1.35 KB
/
chuno.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
// Package chuno is instant preview server written in Go
package chuno
import (
"fmt"
"log"
"net/http"
"os"
// "path/filepath"
)
// shareing variable
var content []byte
var (
// Path is markdown file path to preview.
Path string
isDark = false
)
func handler(w http.ResponseWriter, r *http.Request) {
content, err := load(Path)
if err != nil {
log.Fatal(err)
os.Exit(1)
}
rendred, err := render(content, isDark)
if err != nil {
log.Fatal(err)
os.Exit(1)
}
w.Write(rendred)
}
func strContains(arr []string, str string) bool {
for _, v := range arr {
if v == str {
return true
}
}
return false
}
// LaunchPreviewServer is launch preview server.
func LaunchPreviewServer(path string, port int, darkmode bool) error {
Path = path
isDark = darkmode
strPort := fmt.Sprintf(":%d", port)
// Start setver
go watch(path)
http.HandleFunc("/", handler)
fmt.Println("🌸 Server is starting at http://localhost" + strPort)
err := http.ListenAndServe(strPort, nil)
if err != nil {
log.Fatal(err)
return err
}
return nil
}
// Build is rendoring MarkDown to HTML
func Build(path string, isDark bool) ([]byte, error) {
content, err := load(path)
if err != nil {
log.Fatal("Can't open file", err)
}
html, err := render(content, isDark)
if err != nil {
log.Fatal("Error in rendering.")
return []byte(""), err
}
return html, nil
}