74 lines
1.5 KiB
Go
74 lines
1.5 KiB
Go
package routes
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"html/template"
|
|
"os"
|
|
"path"
|
|
"time"
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
"github.com/russross/blackfriday/v2"
|
|
)
|
|
|
|
type Content struct {
|
|
ID string `json:"id"`
|
|
Name string `json:"name"`
|
|
Date string `json:"date"`
|
|
Author string `json:"author"`
|
|
Content template.HTML `json:"content"`
|
|
Time time.Time
|
|
}
|
|
|
|
func TimeFromString(date string) (time.Time, error) {
|
|
res, err := time.Parse("02/01/06", date)
|
|
if err == nil {
|
|
return res, nil
|
|
}
|
|
|
|
return time.Now(), nil
|
|
}
|
|
|
|
func GetContent(pth string, name string) (Content, error) {
|
|
var res Content
|
|
jsonfile := path.Join(pth, name+".json")
|
|
mdfile := path.Join(pth, name+".md")
|
|
|
|
jsoncontent, err := os.ReadFile(jsonfile)
|
|
if err != nil {
|
|
return res, errors.New("Cannot read JSON file: "+jsonfile)
|
|
}
|
|
|
|
mdcontent, err := os.ReadFile(mdfile)
|
|
if err != nil {
|
|
return res, errors.New("Cannot read markdown file: "+mdfile)
|
|
}
|
|
|
|
if(json.Unmarshal(jsoncontent, &res)!= nil) {
|
|
return res, errors.New("Cannot parse JSON: "+jsonfile)
|
|
}
|
|
|
|
ext := blackfriday.FencedCode
|
|
ext |= blackfriday.BackslashLineBreak
|
|
ext |= blackfriday.Strikethrough
|
|
|
|
md := string(blackfriday.Run(mdcontent, blackfriday.WithExtensions(ext)))
|
|
res.Content = template.HTML(md)
|
|
return res, nil
|
|
}
|
|
|
|
func RenderError(c *fiber.Ctx, code int) error{
|
|
var msg string = "Server Error"
|
|
c.Status(code)
|
|
|
|
switch code {
|
|
case 404:
|
|
msg = "Not Found"
|
|
}
|
|
|
|
return c.Render("error", fiber.Map{
|
|
"msg": msg,
|
|
})
|
|
}
|