Skip to content

Commit d0aac74

Browse files
authored
docsite: allow content to be removed from the search index if it matches a pattern in the docsite config (#83)
1 parent 43a39ef commit d0aac74

4 files changed

Lines changed: 20 additions & 0 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ The site data describes the location of its templates, assets, and content. It i
5151
- `assetsBaseURLPath`: the URL path where the assets are available (such as `/assets/`).
5252
- `redirects`: an object mapping URL paths (such as `/my/old/page`) to redirect destination URLs (such as `/my/new/page`).
5353
- `check` (optional): an object containing a single property `ignoreURLPattern`, which is a [RE2 regexp](https://golang.org/pkg/regexp/syntax/) of URLs to ignore when checking for broken URLs with `docsite check`.
54+
- `search` (optional): an object containing a single proprety `skipIndexURLPattern`, which is a [RE2 regexp](https://golang.org/pkg/regexp/syntax/) pattern that if matching any content file URL will remove that file from the search index.
5455

5556
The possible values for VFS URLs are:
5657

cmd/docsite/site.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ type docsiteConfig struct {
6363
Check struct {
6464
IgnoreURLPattern string
6565
}
66+
Search struct {
67+
SkipIndexURLPattern string
68+
}
6669
}
6770

6871
func partialSiteFromConfig(config docsiteConfig) (*docsite.Site, error) {
@@ -100,6 +103,13 @@ func partialSiteFromConfig(config docsiteConfig) (*docsite.Site, error) {
100103
if config.AssetsBaseURLPath != "" {
101104
site.AssetsBase = &url.URL{Path: config.AssetsBaseURLPath}
102105
}
106+
if config.Search.SkipIndexURLPattern != "" {
107+
var err error
108+
site.SkipIndexURLPattern, err = regexp.Compile(config.Search.SkipIndexURLPattern)
109+
if err != nil {
110+
return nil, err
111+
}
112+
}
103113

104114
for fromPath, toURLStr := range config.Redirects {
105115
if err := addSiteRedirect(&site, fromPath, toURLStr); err != nil {

search.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ func (s *Site) Search(ctx context.Context, contentVersion string, queryStr strin
2828
return nil, err
2929
}
3030
for _, page := range pages {
31+
if s.SkipIndexURLPattern != nil && s.SkipIndexURLPattern.MatchString(page.Path) {
32+
// this URL matches a pattern that we do not want to index for search, so we will skip it
33+
continue
34+
}
35+
3136
ast := markdown.NewParser(nil).Parse(page.Data)
3237
data, err := s.renderTextContent(ctx, page, ast, contentVersion)
3338
if err != nil {

site.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ type Site struct {
5656

5757
// CheckIgnoreURLPattern is a regexp matching URLs to ignore in the Check method.
5858
CheckIgnoreURLPattern *regexp.Regexp
59+
60+
// SkipIndexURLPattern is a regexp matching URLs to ignore when searching. Any files that have a URL that match this
61+
// pattern will be ignored from the search index.
62+
SkipIndexURLPattern *regexp.Regexp
5963
}
6064

6165
// newContentPage creates a new ContentPage in the site.

0 commit comments

Comments
 (0)