This repository has been archived by the owner on Feb 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.go
74 lines (62 loc) · 1.86 KB
/
service.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
package mark2web
import (
"bytes"
"crypto/sha1"
"fmt"
"os"
"github.com/gomarkdown/markdown"
"github.com/gomarkdown/markdown/parser"
"github.com/microcosm-cc/bluemonday"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/thealamu/mark2web-service/internal/pkg/db"
)
// Service implements core logic for converting markdown to URL
type Service struct {
Logger *log.Logger
DB db.DB
}
var markdownExtensions = parser.CommonExtensions | parser.AutoHeadingIDs
func NewService(opts ...func(*Service) error) (*Service, error) {
log.Traceln("creating a new service")
s := Service{}
// set sensible defaults
s.Logger = log.New()
s.DB = &db.FSDatabase{
BaseDir: os.TempDir(),
}
for _, opt := range opts {
if err := opt(&s); err != nil {
return nil, errors.Wrap(err, "could not create service")
}
}
return &s, nil
}
// HTMLFor returns the corresponding HTML for the ID
func (s *Service) HTMLFor(ID string) ([]byte, error) {
s.Logger.Tracef("in service, retrieving HTML for %s\n", ID)
return s.DB.GetHTMLFor(ID)
}
// MarkdownToURL generates a URL for the markdown,
// creates a mapping of the URL to the markdown and returns the URL
func (s *Service) MarkdownToURL(md []byte, host string) (string, error) {
psr := parser.NewWithExtensions(markdownExtensions)
HTMLEquiv := markdownToHTML(md, psr)
// Sanitize HTML
safeHTML := bluemonday.UGCPolicy().SanitizeBytes(HTMLEquiv)
path := shasumOf(safeHTML)
// Create mapping
err := s.DB.Save(path, safeHTML)
if err != nil {
return "", err
}
return fmt.Sprintf("%s/%s", host, path), nil
}
// shasumOf returns the sha1sum of data
func shasumOf(data []byte) string {
return fmt.Sprintf("%x", sha1.Sum(data))
}
// markdownToHTML returns the HTML equivalent of the passed in markdown
func markdownToHTML(md []byte, psr *parser.Parser) []byte {
return bytes.TrimSpace(markdown.ToHTML(md, psr, nil))
}