/
pipeline.go
175 lines (144 loc) · 3.83 KB
/
pipeline.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package pipeline
import (
"bytes"
"fmt"
"io/ioutil"
"log"
"os"
"path"
"path/filepath"
"strings"
mathjax "github.com/athul/goldmark-mathjax"
"github.com/yourbasic/graph"
"github.com/yuin/goldmark"
highlighting "github.com/yuin/goldmark-highlighting"
"github.com/yuin/goldmark/extension"
"github.com/yuin/goldmark/renderer/html"
"gopkg.in/yaml.v3"
)
// ReadFiles reads the given directory and appends into given posts
func ReadFiles(directory string) ([]Post, error) {
posts := []Post{}
err := filepath.Walk(directory, func(pth string, info os.FileInfo, err error) error {
// Return if there is an error
if err != nil {
return err
}
// Skip all directories
if info.IsDir() {
return nil
}
// Skip any files which aren't markdown.
if !strings.HasSuffix(path.Base(pth), ".md") {
return nil
}
post := NewPost(pth)
// Read the file
data, err := ioutil.ReadFile(path.Clean(pth))
if err != nil {
return err
}
// Extract metadata
splits := bytes.Split(data, []byte("---\n"))
frontMatter := splits[1]
if err = yaml.Unmarshal(frontMatter, &post.Meta); err != nil {
log.Printf("error while yaml unmarshal: %v", err)
return err
}
// If the post is a draft, skip the file
if post.Meta.Draft {
return nil
}
// Join the body back ignoring the frontmatter
post.Body = string(bytes.Join(splits[2:], []byte("---\n")))
// Append the post to the posts
posts = append(posts, post)
return nil
})
if err != nil {
return []Post{}, err
}
return posts, nil
}
// ReplaceLinks replaces all the `[[]]` links in the body with the link to the HTML page.
func ReplaceLinks(posts []Post, sitePrefix string, stripHTML bool) error {
// First we need a map of all the files, so that we know
// if there's a link to any non existent file and also
// to get the metadata of the file
slugs := make(map[string]Metadata)
for _, p := range posts {
pth := path.Base(p.FilePath)
slug := strings.TrimSuffix(pth, ".md")
slugs[slug] = p.Meta
}
// Loop over and replace the `[[ ]]` link.
for i, p := range posts {
// Find all the matches first
matches := findLinks(p.Body)
for _, m := range matches {
sg := strings.TrimPrefix(m, "[[")
sg = strings.TrimSuffix(sg, "]]")
// Find if the slug exists
meta, ok := slugs[sg]
if !ok {
return fmt.Errorf("link to an invalid slug: %s", sg)
}
var link string
if stripHTML {
link = fmt.Sprintf(`[%s](%s/posts/%s/)`, meta.Title, sitePrefix, sg)
} else {
link = fmt.Sprintf(`[%s](%s/posts/%s.html)`, meta.Title, sitePrefix, sg)
}
// Replace the slug with a link
posts[i].Body = strings.ReplaceAll(posts[i].Body, m, link)
l := Link{
Slug: sg,
Title: meta.Title,
}
posts[i].Links = append(posts[i].Links, l)
}
}
return nil
}
// ConvertMarkdownToHTML converts post's body into HTML
func ConvertMarkdownToHTML(posts []Post, syntaxStyle string) error {
md := goldmark.New(
goldmark.WithExtensions(
extension.GFM,
mathjax.MathJax,
highlighting.NewHighlighting(
highlighting.WithStyle(syntaxStyle),
),
),
goldmark.WithRendererOptions(
html.WithUnsafe(),
),
)
for i, p := range posts {
html := bytes.NewBuffer([]byte{})
if err := md.Convert([]byte(p.Body), html); err != nil {
return err
}
posts[i].Body = html.String()
}
return nil
}
// MakeGraph returns a graph for a given list of `Posts`.
func MakeGraph(posts []Post) (*graph.Mutable, error) {
// Make a map of all posts with their index
// so that it would be easy to lookup the index using the slug
slugs := make(map[string]int)
for i, p := range posts {
pth := path.Base(p.FilePath)
slug := strings.TrimSuffix(pth, ".md")
slugs[slug] = i
}
g := graph.New(len(posts))
for i, p := range posts {
for _, link := range p.Links {
linkIndex := slugs[link.Slug]
g.AddBoth(i, linkIndex)
}
}
return g, nil
}