Skip to content

Commit 647423e

Browse files
committed
add feed glob
1 parent 54a4b78 commit 647423e

File tree

3 files changed

+69
-15
lines changed

3 files changed

+69
-15
lines changed

builder_suite_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,5 +289,29 @@ title: required
289289
Expect(contents).To(gbytes.Say(`https://example.com/other.html`))
290290
Expect(contents).To(gbytes.Say(`https://example.com/dir/index.html`))
291291
})
292+
293+
It("creates feeds for a certain glob", func() {
294+
createLayout()
295+
createFile("dir/index.md", "# some text")
296+
createFile("other.md", "# some text")
297+
createFile("dir/test.html", "some other text")
298+
299+
cli.BaseURL = "https://example.com"
300+
cli.FeedGlob = "dir/**/*.md"
301+
err := cli.Run()
302+
Expect(err).NotTo(HaveOccurred())
303+
304+
contents := string(readFile("rss.xml").Contents())
305+
Expect(contents).ToNot(ContainSubstring(`https://example.com/other.html`))
306+
Expect(contents).To(ContainSubstring(`https://example.com/dir/index.html`))
307+
308+
contents = string(readFile("atom.xml").Contents())
309+
Expect(contents).ToNot(ContainSubstring(`https://example.com/other.html`))
310+
Expect(contents).To(ContainSubstring(`https://example.com/dir/index.html`))
311+
312+
contents = string(readFile("sitemap.xml").Contents())
313+
Expect(contents).ToNot(ContainSubstring(`https://example.com/other.html`))
314+
Expect(contents).To(ContainSubstring(`https://example.com/dir/index.html`))
315+
})
292316
})
293317
})

cli.go

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@ import (
1212
)
1313

1414
type CLI struct {
15+
AssetsPath string `help:"path to static assets (default with be source-path/public)"`
16+
BaseURL string `help:"the URL which the contents will be served from, this is only used for generating feeds"`
1517
BuildPath string `help:"where generated content should go" required:"" type:"path"`
18+
FeedGlob string `help:"glob patterns for documents to feature in feeds"`
1619
LayoutFilename string `default:"layout.html" help:"layout file to render" required:""`
1720
Serve bool `help:"serve when done building"`
1821
SourcePath string `help:"source of all files" required:"" type:"path"`
19-
AssetsPath string `help:"path to static assets (default with be source-path/public)"`
20-
BaseURL string `help:"the URL which the contents will be served from, this is only used for generating feeds"`
2122
}
2223

2324
func (c *CLI) Run() error {
@@ -35,15 +36,24 @@ func (c *CLI) Run() error {
3536

3637
markdownGlob := filepath.Join(c.SourcePath, "**", "*.md")
3738

38-
err := renderer.Execute(markdownGlob)
39+
if c.FeedGlob == "" {
40+
c.FeedGlob = markdownGlob
41+
} else {
42+
c.FeedGlob = filepath.Join(c.SourcePath, c.FeedGlob)
43+
}
44+
45+
err := renderer.Execute(
46+
markdownGlob,
47+
c.FeedGlob,
48+
)
3949
if err != nil {
4050
return fmt.Errorf("could not execute render: %w", err)
4151
}
4252

4353
if c.Serve {
4454
watcher := NewWatcher(c.SourcePath)
4555

46-
go c.startWatcher(watcher, renderer, markdownGlob)
56+
go c.startWatcher(watcher, renderer, markdownGlob, c.FeedGlob)
4757

4858
e := echo.New()
4959
e.Use(middleware.Logger())
@@ -62,6 +72,7 @@ func (c *CLI) startWatcher(
6272
watcher *Watcher,
6373
renderer *Render,
6474
markdownGlob string,
75+
feedGlob string,
6576
) {
6677
allGlob := filepath.Join(c.SourcePath, "**", "{*.md,*.html,*.js,*.css}")
6778

@@ -71,7 +82,10 @@ func (c *CLI) startWatcher(
7182
if matched {
7283
slog.Info("rebuilding markdown files", slog.String("filename", filename))
7384

74-
err := renderer.Execute(markdownGlob)
85+
err := renderer.Execute(
86+
markdownGlob,
87+
feedGlob,
88+
)
7589
if err != nil {
7690
slog.Error("could not rebuild markdown files", slog.String("error", err.Error()))
7791
}

render.go

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"time"
1515

1616
"github.com/Masterminds/sprig/v3"
17+
"github.com/bmatcuk/doublestar/v4"
1718
"github.com/gorilla/feeds"
1819
"github.com/gosimple/slug"
1920
"github.com/microcosm-cc/bluemonday"
@@ -83,13 +84,16 @@ func NewRender(
8384
}
8485

8586
//nolint:funlen,cyclop
86-
func (r *Render) Execute(pattern string) error {
87+
func (r *Render) Execute(
88+
docsGlob string,
89+
feedGlob string,
90+
) error {
8791
err := r.copyAssets()
8892
if err != nil {
8993
return fmt.Errorf("copying assets issue: %w", err)
9094
}
9195

92-
docs, err := NewDocs(r.sourcePath, pattern, 0, false)
96+
docs, err := NewDocs(r.sourcePath, docsGlob, 0, false)
9397
if err != nil {
9498
return fmt.Errorf("could not glob markdown files: %w", err)
9599
}
@@ -134,23 +138,31 @@ func (r *Render) Execute(pattern string) error {
134138
})
135139
}
136140

141+
if r.baseURL != "" {
142+
group.Go(func() error {
143+
err = r.generateFeeds(docs, feedGlob, funcMap)
144+
if err != nil {
145+
return fmt.Errorf("could not render feeds: %w", err)
146+
}
147+
148+
return nil
149+
})
150+
}
151+
137152
err = group.Wait()
138153
if err != nil {
139154
return fmt.Errorf("could not render: %w", err)
140155
}
141156

142-
if r.baseURL != "" {
143-
err = r.generateFeeds(docs, funcMap)
144-
if err != nil {
145-
return fmt.Errorf("could not render feeds: %w", err)
146-
}
147-
}
148-
149157
return nil
150158
}
151159

152160
//nolint:funlen
153-
func (r *Render) generateFeeds(docs Docs, funcMap template.FuncMap) error {
161+
func (r *Render) generateFeeds(
162+
docs Docs,
163+
feedGlob string,
164+
funcMap template.FuncMap,
165+
) error {
154166
now := time.Now().UTC()
155167

156168
feed := &feeds.Feed{
@@ -169,6 +181,10 @@ func (r *Render) generateFeeds(docs Docs, funcMap template.FuncMap) error {
169181
sanitizer := bluemonday.UGCPolicy()
170182

171183
for _, doc := range docs {
184+
if matched, _ := doublestar.Match(feedGlob, doc.Filename()); !matched {
185+
continue
186+
}
187+
172188
modifiedTime := doc.Timespec.ModTime().UTC()
173189
createdTime := modifiedTime
174190

0 commit comments

Comments
 (0)