forked from docker/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.go
More file actions
57 lines (53 loc) · 1.51 KB
/
Copy pathutils.go
File metadata and controls
57 lines (53 loc) · 1.51 KB
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 main
import (
"github.com/gdevillele/frontparser"
"io/ioutil"
"os"
"strings"
)
// isPublishedMarkdown returns wether a file is a published markdown or not
// as a convenience it also returns the markdown bytes to avoid reading files twice
func isPublishedMarkdown(path string) (bool, []byte, error) {
if strings.HasSuffix(path, ".md") {
fileBytes, err := ioutil.ReadFile(path)
if err != nil {
return false, nil, err
}
if frontparser.HasFrontmatterHeader(fileBytes) {
fm, _, err := frontparser.ParseFrontmatterAndContent(fileBytes)
if err != nil {
return false, nil, err
}
// skip markdowns that are not published
if published, exists := fm["published"]; exists {
if publishedBool, ok := published.(bool); ok {
if publishedBool {
// file is markdown, has frontmatter and is published
return true, fileBytes, nil
}
}
} else {
// if "published" field is missing, it means published == true
return true, fileBytes, nil
}
}
}
return false, nil, nil
}
// isHTML returns wether a file is an html file or not
// as a convenience it also returns the markdown bytes to avoid reading files twice
func isHTML(path string) (bool, []byte, error) {
if strings.HasSuffix(path, ".html") {
fileBytes, err := ioutil.ReadFile(path)
if err != nil {
return false, nil, err
}
return true, fileBytes, nil
}
return false, nil, nil
}
// fileExists returns true if the given file exists
func fileExists(name string) bool {
_, err := os.Stat(name)
return err == nil
}