This repository has been archived by the owner on Sep 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 33
/
render.go
64 lines (55 loc) · 1.68 KB
/
render.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
package main
import (
"encoding/base64"
"errors"
"html/template"
"io/ioutil"
"net/http"
"regexp"
"strings"
"github.com/golang-commonmark/markdown"
"github.com/labstack/echo"
)
var (
statuses = map[int]string{
400: "Bad request",
401: "Unauthorized",
403: "Forbidden",
404: "Not found",
412: "Precondition failed",
503: "Service unavailable",
}
rexpNewLine = regexp.MustCompile("[\n\r]")
rexpNonAlphaNum = regexp.MustCompile("[`~!@#$%^&*_|+=?;:'\",.<>{}\\/]")
rexpNoScriptIframe = regexp.MustCompile("(<.*?script.*?>.*?<.*?/.*?script.*?>|<.*?iframe.*?>|</.*?iframe.*?>)")
errorUnathorised = errors.New("password is wrong")
errorBadRequest = errors.New("password is empty")
)
func (n *Note) prepare() {
fstLine := rexpNewLine.Split(n.Text, -1)[0]
maxLength := 25
if len(fstLine) < 25 {
maxLength = len(fstLine)
}
n.Text = rexpNoScriptIframe.ReplaceAllString(n.Text, "")
n.Title = strings.TrimSpace(rexpNonAlphaNum.ReplaceAllString(fstLine[:maxLength], ""))
n.Content = mdTmplHTML([]byte(n.Text))
if n.Fraud() {
n.Encoded = base64.StdEncoding.EncodeToString([]byte(n.Content))
}
}
var mdRenderer = markdown.New(markdown.HTML(true))
func mdTmplHTML(content []byte) template.HTML {
return template.HTML(mdRenderer.RenderToString(content))
}
func md2html(c echo.Context, name string) (*Note, int) {
path := "assets/markdown/" + name + ".md"
mdContent, err := ioutil.ReadFile(path)
if err != nil {
c.Logger().Errorf("couldn't open markdown page %s: %v", path, err)
code := http.StatusServiceUnavailable
return nil, code
}
c.Logger().Debugf("rendering markdown page %s", name)
return &Note{Title: name, Content: mdTmplHTML(mdContent)}, http.StatusOK
}