-
Notifications
You must be signed in to change notification settings - Fork 0
/
render.go
40 lines (37 loc) · 852 Bytes
/
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
package main
import (
"bytes"
"os"
"path/filepath"
"strings"
gmt "github.com/mdigger/goldmark-toc"
"github.com/yuin/goldmark"
"github.com/yuin/goldmark/extension"
"github.com/yuin/goldmark/renderer/html"
"go.xrfang.cn/yal"
)
func RenderMD(fn string) (res map[string]interface{}, err error) {
defer yal.Catch(&err)
src, err := os.ReadFile(fn)
yal.Assert(err)
base := filepath.Base(fn)
ext := filepath.Ext(base)
base = base[:len(base)-len(ext)]
render := gmt.New(
goldmark.WithExtensions(
extension.DefinitionList,
extension.Footnote,
extension.GFM,
extension.Typographer,
),
goldmark.WithRendererOptions(html.WithUnsafe()),
)
var buf bytes.Buffer
toc, err := render(src, &buf)
yal.Assert(err)
return map[string]interface{}{
"toc": toc,
"title": base,
"doc": strings.TrimSpace(buf.String()),
}, nil
}