website/lib/util.go

56 lines
1.1 KiB
Go
Raw Permalink Normal View History

package lib
import (
2024-05-05 18:22:45 +00:00
"fmt"
"strings"
"time"
"github.com/gofiber/fiber/v2"
"github.com/russross/blackfriday/v2"
)
2024-05-05 18:22:45 +00:00
// python3 -c "import string; print(string.ascii_letters+string.digits+\" /,_-?!'\\\"\")"
var valid string = `abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 /,_-?!'"`
2024-08-13 21:32:48 +00:00
func IsStringValid(s string) bool {
for _, c := range s {
if !strings.Contains(valid, string(c)) {
fmt.Printf("%c\n", c)
return false
}
}
return true
2024-05-05 18:22:45 +00:00
}
2024-08-13 21:32:48 +00:00
func RenderError(c *fiber.Ctx, code int) error {
var msg string = "Server Error"
c.Status(code)
2024-08-13 21:32:48 +00:00
switch code {
case 404:
msg = "Not Found"
}
2024-08-13 21:32:48 +00:00
return c.Render("error", fiber.Map{
"msg": msg,
})
}
func TimeFromString(date string) (time.Time, error) {
2024-08-13 21:32:48 +00:00
res, err := time.Parse("02/01/06", date)
if err == nil {
return res, nil
}
return time.Now(), nil
}
func ParseMarkdown(md string) string {
2024-08-13 21:32:48 +00:00
ext := blackfriday.FencedCode
ext |= blackfriday.BackslashLineBreak
ext |= blackfriday.Strikethrough
ext |= blackfriday.Tables
ext |= blackfriday.NoEmptyLineBeforeBlock
2024-08-13 21:32:48 +00:00
return string(blackfriday.Run([]byte(md), blackfriday.WithExtensions(ext)))
}