arnaucube
arnaucube/md-live-serverPublic Self-hosted repository
md-live-server/utils.go
utils.goRaw
 1package main
 2
 3import (
 4	"io/ioutil"
 5	"os"
 6	"path/filepath"
 7
 8	"github.com/fatih/color"
 9	"github.com/gomarkdown/markdown"
10	"github.com/gomarkdown/markdown/parser"
11)
12
13func check(err error) {
14	if err != nil {
15		color.Red(err.Error())
16	}
17}
18
19func readDir(dirpath string) []string {
20	var elems []string
21	_ = filepath.Walk(dirpath, func(path string, f os.FileInfo, err error) error {
22		elems = append(elems, path)
23		return nil
24	})
25	return elems
26}
27
28func readFile(path string) string {
29	dat, err := ioutil.ReadFile(path)
30	if err != nil {
31		color.Red(path)
32	}
33	check(err)
34	return string(dat)
35}
36
37func fileToHTML(path string) (string, error) {
38	mdExtensions := parser.NoIntraEmphasis | parser.Tables | parser.FencedCode |
39		parser.Autolink | parser.Strikethrough | parser.SpaceHeadings | parser.HeadingIDs |
40		parser.BackslashLineBreak | parser.DefinitionLists | parser.MathJax
41
42	mdcontent := readFile(path)
43	mdParser := parser.NewWithExtensions(mdExtensions)
44	htmlcontent := markdown.ToHTML([]byte(mdcontent), mdParser, nil)
45
46	// htmlcontent := string(blackfriday.Run([]byte(mdcontent)))
47	return string(htmlcontent), nil
48}