-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrender.go
390 lines (325 loc) · 9.09 KB
/
render.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
package builder
import (
"bytes"
// embed file assets.
_ "embed"
"errors"
"fmt"
"net/url"
"os"
"path/filepath"
"strings"
"text/template"
"time"
"github.com/Masterminds/sprig/v3"
chromahtml "github.com/alecthomas/chroma/formatters/html"
"github.com/bmatcuk/doublestar/v4"
"github.com/gorilla/feeds"
"github.com/gosimple/slug"
"github.com/microcosm-cc/bluemonday"
cp "github.com/otiai10/copy"
"github.com/sabloger/sitemap-generator/smg"
"github.com/tdewolff/minify"
mHTML "github.com/tdewolff/minify/html"
"github.com/yuin/goldmark"
emoji "github.com/yuin/goldmark-emoji"
highlighting "github.com/yuin/goldmark-highlighting"
"github.com/yuin/goldmark/extension"
"github.com/yuin/goldmark/parser"
"github.com/yuin/goldmark/renderer/html"
"go.abhg.dev/goldmark/anchor"
"go.abhg.dev/goldmark/mermaid"
"golang.org/x/sync/errgroup"
)
type Render struct {
assetsPath string
baseURL string
buildPath string
converter goldmark.Markdown
layoutPath string
sourcePath string
}
var errMissingTitle = errors.New("missing title in metadata or h1")
func NewRender(
layoutPath string,
sourcePath string,
assetsPath string,
buildPath string,
baseURL string,
) *Render {
return &Render{
assetsPath: assetsPath,
baseURL: baseURL,
buildPath: buildPath,
layoutPath: layoutPath,
sourcePath: sourcePath,
converter: goldmark.New(
goldmark.WithRendererOptions(
html.WithXHTML(),
html.WithUnsafe(),
),
goldmark.WithExtensions(
extension.GFM,
emoji.Emoji,
&mermaid.Extender{},
highlighting.NewHighlighting(
highlighting.WithFormatOptions(
chromahtml.WithClasses(true),
chromahtml.WithLineNumbers(false),
),
),
extension.DefinitionList,
extension.Footnote,
extension.Typographer,
&anchor.Extender{
Texter: anchor.Text("#"),
Position: anchor.Before,
},
),
goldmark.WithParserOptions(
parser.WithAutoHeadingID(),
parser.WithAttribute(),
),
),
}
}
//nolint:funlen,cyclop
func (r *Render) Execute(
docsGlob string,
feedGlob string,
) error {
err := r.copyAssets()
if err != nil {
return fmt.Errorf("copying assets issue: %w", err)
}
docs, err := NewDocs(r.sourcePath, docsGlob, 0, false)
if err != nil {
return fmt.Errorf("could not glob markdown files: %w", err)
}
contents, err := readFile(r.layoutPath)
if err != nil {
return fmt.Errorf("could not read layout: %w", err)
}
funcMap := template.FuncMap{
"iterDocs": func(path string, limit int) (Docs, error) {
pattern := filepath.Join(r.sourcePath, path, "*.md")
docs, err := NewDocs(r.sourcePath, pattern, limit, true)
if err != nil {
return nil, fmt.Errorf("could not load docs: %w", err)
}
return docs, nil
},
}
layout, err := template.
New(r.layoutPath).
Funcs(funcMap).
Funcs(sprig.FuncMap()).
Parse(contents)
if err != nil {
return fmt.Errorf("could not parse layout template (%s): %w", r.layoutPath, err)
}
maxRenders := 10
group := &errgroup.Group{}
group.SetLimit(maxRenders)
for _, doc := range docs {
doc := doc
group.Go(func() error {
err := r.renderDocument(doc, funcMap, layout)
if err != nil {
return fmt.Errorf("rendering template issue: %w", err)
}
return nil
})
}
if r.baseURL != "" {
group.Go(func() error {
err = r.generateFeeds(docs, feedGlob, funcMap)
if err != nil {
return fmt.Errorf("could not render feeds: %w", err)
}
return nil
})
}
err = group.Wait()
if err != nil {
return fmt.Errorf("could not render: %w", err)
}
return nil
}
//nolint:funlen
func (r *Render) generateFeeds(
docs Docs,
feedGlob string,
funcMap template.FuncMap,
) error {
now := time.Now().UTC()
feed := &feeds.Feed{
Title: r.baseURL,
Link: &feeds.Link{Href: r.baseURL},
Description: fmt.Sprintf("feed for %s", r.baseURL),
Created: now,
}
sitemap := smg.NewSitemap(true)
sitemap.SetOutputPath(r.buildPath)
sitemap.SetLastMod(&now)
sitemap.SetCompress(false)
sitemap.SetHostname(r.baseURL)
sanitizer := bluemonday.UGCPolicy()
for _, doc := range docs {
if matched, _ := doublestar.Match(feedGlob, doc.Filename()); !matched {
continue
}
modifiedTime := doc.Timespec.ModTime().UTC()
createdTime := modifiedTime
if doc.Timespec.HasBirthTime() {
createdTime = doc.Timespec.BirthTime().UTC()
}
docURL, _ := url.JoinPath(r.baseURL, doc.Path())
err := sitemap.Add(&smg.SitemapLoc{
Loc: docURL,
LastMod: &modifiedTime,
ChangeFreq: smg.Always,
})
if err != nil {
return fmt.Errorf("could not add file %q to sitemap: %w", doc.Filename(), err)
}
contents, _ := r.renderMarkdownFromDoc(doc, funcMap)
feed.Items = append(feed.Items, &feeds.Item{
Id: docURL,
Title: doc.Title(),
Link: &feeds.Link{
Href: docURL,
},
Description: doc.Description(),
Updated: modifiedTime,
Created: createdTime,
Content: sanitizer.Sanitize(contents),
})
}
_, err := sitemap.Save()
if err != nil {
return fmt.Errorf("could not save sitemap: %w", err)
}
atomFeed, err := feed.ToAtom()
if err != nil {
return fmt.Errorf("could not generate atom feed: %w", err)
}
err = os.WriteFile(filepath.Join(r.buildPath, "atom.xml"), []byte(atomFeed), os.ModePerm)
if err != nil {
return fmt.Errorf("could not write atom feed: %w", err)
}
rssFeed, err := feed.ToRss()
if err != nil {
return fmt.Errorf("could not generate rss feed: %w", err)
}
err = os.WriteFile(filepath.Join(r.buildPath, "rss.xml"), []byte(rssFeed), os.ModePerm)
if err != nil {
return fmt.Errorf("could not write rss feed: %w", err)
}
return nil
}
func (r *Render) copyAssets() error {
err := os.RemoveAll(r.buildPath)
if err != nil {
return fmt.Errorf("could not remove build path (%s): %w", r.buildPath, err)
}
err = os.MkdirAll(r.buildPath, os.ModePerm)
if err != nil {
return fmt.Errorf("could not create build path (%s): %w", r.buildPath, err)
}
assetsPath := r.assetsPath
if _, err := os.Stat(assetsPath); !os.IsNotExist(err) {
opt := cp.Options{
Skip: func(info os.FileInfo, src, dest string) (bool, error) {
// skip dot files
return strings.HasPrefix(filepath.Base(src), "."), nil
},
}
err = cp.Copy(assetsPath, r.buildPath, opt)
if err != nil {
return fmt.Errorf("could not copy assets contents (%s): %w", assetsPath, err)
}
}
return nil
}
func (r *Render) renderMarkdownFromDoc(doc *Doc, funcMap template.FuncMap) (string, error) {
filename := doc.Filename()
if doc.Title() == "" {
return "", fmt.Errorf("could not determine title (%s): %w", filename, errMissingTitle)
}
markdown, err := template.
New(filename).
Funcs(funcMap).
Funcs(sprig.FuncMap()).
Parse(doc.Contents())
if err != nil {
return "", fmt.Errorf("could not parse markdown template (%s): %w", r.layoutPath, err)
}
markdownWriter, renderedWriter := &bytes.Buffer{}, &bytes.Buffer{}
err = markdown.Execute(markdownWriter, nil)
if err != nil {
return "", fmt.Errorf("could not render markdown template (%s): %w", filename, err)
}
err = r.converter.Convert(markdownWriter.Bytes(), renderedWriter)
if err != nil {
return "", fmt.Errorf("could not convert file (%s): %w", filename, err)
}
return renderedWriter.String(), nil
}
func (r *Render) renderDocument(doc *Doc, funcMap template.FuncMap, layout *template.Template) error {
renderedMarkdown, err := r.renderMarkdownFromDoc(doc, funcMap)
if err != nil {
return fmt.Errorf("could not render markdown for doc: %w", err)
}
layoutWriter := &bytes.Buffer{}
err = layout.Execute(layoutWriter,
map[string]any{
"Doc": doc,
"RenderedPage": renderedMarkdown,
},
)
if err != nil {
return fmt.Errorf("could not render layout template (%s): %w", doc.Filename(), err)
}
withoutSlugFilename := strings.Replace(doc.Filename(), r.sourcePath, r.buildPath, 1)
withoutSlugFilename = strings.Replace(withoutSlugFilename, ".md", ".html", 1)
filenames := []string{withoutSlugFilename}
if !strings.Contains(withoutSlugFilename, "index.html") {
withSlugFilename := strings.Replace(withoutSlugFilename, ".html", "-"+slug.Make(doc.Title())+".html", 1)
filenames = append(filenames, withSlugFilename)
}
err = writeHTMLFiles(filenames, layoutWriter.String())
if err != nil {
return fmt.Errorf("could write new template: %w", err)
}
return nil
}
func readFile(filename string) (string, error) {
contents, err := os.ReadFile(filename)
if err != nil {
return "", fmt.Errorf("could not read file (%s): %w", filename, err)
}
return string(contents), nil
}
func writeHTMLFiles(filenames []string, contents string) error {
dirPath := filepath.Dir(filenames[0])
err := os.MkdirAll(dirPath, os.ModePerm)
if err != nil {
return fmt.Errorf("could not create path (%s): %w", dirPath, err)
}
writer := &strings.Builder{}
minifier := &mHTML.Minifier{
KeepDocumentTags: true,
}
err = minifier.Minify(&minify.M{}, writer, strings.NewReader(contents), nil)
if err != nil {
return fmt.Errorf("could not minify: %w", err)
}
for _, filename := range filenames {
err = os.WriteFile(filename, []byte(writer.String()), os.ModePerm)
if err != nil {
return fmt.Errorf("could not write path (%s): %w", filename, err)
}
}
return nil
}