-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyMD.go
executable file
·66 lines (53 loc) · 1.23 KB
/
myMD.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
package main
import (
"fmt"
"html/template"
"io/ioutil"
"log"
"net/http"
"github.com/gin-contrib/static"
"github.com/gin-gonic/gin"
"github.com/russross/blackfriday"
)
type Post struct {
Title string
Content template.HTML
}
func main() {
r := gin.Default()
r.Use(gin.Logger())
r.Delims("{{", "}}")
r.Use(static.Serve("/assets", static.LocalFile("/assets", false)))
r.LoadHTMLGlob("./src/templates/*.gohtml")
r.GET("/", func(c *gin.Context) {
var posts []string
files, err := ioutil.ReadDir("./src/markdown/")
if err != nil {
log.Fatal(err)
}
for _, file := range files {
fmt.Println(file.Name())
posts = append(posts, file.Name())
}
c.HTML(http.StatusOK, "index.gohtml", gin.H{
"posts": posts,
})
})
r.GET("/:postName", func(c *gin.Context) {
postName := c.Param("postName")
mdfile, err := ioutil.ReadFile("./src/markdown/" + postName)
if err != nil {
fmt.Println(err)
c.HTML(http.StatusNotFound, "error.gohtml", nil)
c.Abort()
return
}
postHTML := template.HTML(blackfriday.MarkdownCommon([]byte(mdfile)))
post := Post{Title: postName, Content: postHTML}
c.HTML(http.StatusOK, "post.gohtml", gin.H{
"Title": post.Title,
"Content": post.Content,
})
})
r.Run()
}