-
Notifications
You must be signed in to change notification settings - Fork 1
/
md.go
60 lines (55 loc) · 1.5 KB
/
md.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
package server
// md module
//
// Copyright (c) 2023 - Valentin Kuznetsov <vkuznet@gmail.com>
//
import (
"io"
"io/fs"
"log"
"github.com/gomarkdown/markdown"
mhtml "github.com/gomarkdown/markdown/html"
"github.com/gomarkdown/markdown/parser"
)
// MDToHTML function to generate HTML from given markdown file
func MDToHTML(fsys fs.FS, fname string) (string, error) {
/*
filesFS, err := fs.Sub(StaticFs, "static/markdown")
if err != nil {
log.Println("ERROR: unable to open static/markdown", err)
return "", err
}
log.Printf("### fileFS %+v", filesFS)
file, err := filesFS.Open(fname)
*/
file, err := fsys.Open(fname)
if err != nil {
log.Println("ERROR: unable to open", fname, err)
return "", err
}
/*
file, err := os.Open(fname)
if err != nil {
log.Println("ERROR: unable to open", fname, err)
return "", err
}
*/
defer file.Close()
var md []byte
md, err = io.ReadAll(file)
if err != nil {
return "", err
}
// create markdown parser with extensions
extensions := parser.CommonExtensions | parser.AutoHeadingIDs | parser.NoEmptyLineBeforeBlock
p := parser.NewWithExtensions(extensions)
doc := p.Parse(md)
// create HTML renderer with extensions
// htmlFlags := mhtml.CommonFlags | mhtml.HrefTargetBlank
htmlFlags := mhtml.CommonFlags
opts := mhtml.RendererOptions{Flags: htmlFlags}
renderer := mhtml.NewRenderer(opts)
content := markdown.Render(doc, renderer)
// return html.EscapeString(string(content)), nil
return string(content), nil
}