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
| package doc
import ( "embed"
"github.com/gomarkdown/markdown" "github.com/pkg/errors" )
//go:embed * var FS embed.FS
func Content(path string) ([]byte, error) { if path == "embed.go" { return nil, errors.New("invalid asset") } data, err := FS.ReadFile(path) if err != nil { return nil, errors.Wrapf(err, "error reading doc asset at [%s]", path) }
return data, nil }
var ( titleCache = map[string]string{} htmlCache = map[string]string{} )
func HTML(key string, path string, f func(s string) (string, string, error)) (string, string, error) { if curr, ok := htmlCache[key]; ok { return titleCache[key], curr, nil } data, err := Content(path) if err != nil { return "", "", err } return HTMLString(key, data, f) }
func HTMLString(key string, data []byte, f func(s string) (string, string, error)) (string, string, error) { if curr, ok := htmlCache[key]; ok { return titleCache[key], curr, nil } var title string html := string(markdown.ToHTML(data, nil, nil)) if f != nil { var err error title, html, err = f(html) if err != nil { return "", "", err } } titleCache[key] = title htmlCache[key] = html return title, html, nil }
|