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	blackfriday "github.com/russross/blackfriday/v2"
10)
11
12func check(err error) {
13	if err != nil {
14		color.Red(err.Error())
15	}
16}
17
18func readDir(dirpath string) []string {
19	var elems []string
20	_ = filepath.Walk(dirpath, func(path string, f os.FileInfo, err error) error {
21		elems = append(elems, path)
22		return nil
23	})
24	return elems
25}
26
27func readFile(path string) string {
28	dat, err := ioutil.ReadFile(path)
29	if err != nil {
30		color.Red(path)
31	}
32	check(err)
33	return string(dat)
34}
35
36func fileToHTML(path string) (string, error) {
37	mdcontent := readFile(path)
38	htmlcontent := string(blackfriday.Run([]byte(mdcontent)))
39	return htmlcontent, nil
40}