-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathsite.go
213 lines (183 loc) · 6.27 KB
/
site.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
package docsite
import (
"bytes"
"context"
"fmt"
"html/template"
"net/http"
"net/url"
"os"
pathpkg "path"
"path/filepath"
"regexp"
"strings"
"github.com/pkg/errors"
"github.com/sourcegraph/docsite/markdown"
)
// VersionedFileSystem represents multiple versions of an http.FileSystem.
type VersionedFileSystem interface {
OpenVersion(ctx context.Context, version string) (http.FileSystem, error)
}
type subdirFileSystem struct {
fs http.FileSystem
path string
}
func (sd *subdirFileSystem) Open(path string) (http.File, error) {
return sd.fs.Open(filepath.Join(sd.path, path))
}
// Site represents a documentation site, including all of its templates, assets, and content.
type Site struct {
// Content is the versioned file system containing the Markdown files and assets (e.g., images)
// embedded in them.
Content VersionedFileSystem
// ContentExcludePattern is a regexp matching file paths to exclude in the content file system.
ContentExcludePattern *regexp.Regexp
// Base is the base URL (typically including only the path, such as "/" or "/help/") where the
// site is available.
Base *url.URL
// Root is the root URL that is only used for specific cases where an absolute URL is mandatory,
// such as for opengraph tags in the headers for example. It must include both the scheme and
// host.
Root *url.URL
// AssetsBase is the base URL (sometimes only including the path, such as "/assets/") where the
// assets are available.
AssetsBase *url.URL
// Redirects contains a mapping from URL path to redirect destination URL.
Redirects map[string]*url.URL
// CheckIgnoreURLPattern is a regexp matching URLs to ignore in the Check method.
CheckIgnoreURLPattern *regexp.Regexp
// SkipIndexURLPattern is a regexp matching URLs to ignore when searching. Any files that have a URL that match this
// pattern will be ignored from the search index.
SkipIndexURLPattern *regexp.Regexp
}
func (s *Site) GetResources(dir, version string) (http.FileSystem, error) {
c, err := s.Content.OpenVersion(context.Background(), version)
if err != nil {
// if template dir doesn't exist, use the default one from main
if errors.Is(err, os.ErrNotExist) {
c, err = s.Content.OpenVersion(context.Background(), "")
if err != nil {
return nil, err
}
} else {
return nil, err
}
}
return &subdirFileSystem{fs: c, path: "_resources/" + dir}, nil
}
// newContentPage creates a new ContentPage in the site.
func (s *Site) newContentPage(filePath string, data []byte, contentVersion string) (*ContentPage, error) {
path := contentFilePathToPath(filePath)
doc, err := markdown.Run(data, s.markdownOptions(filePath, contentVersion))
if err != nil {
return nil, errors.WithMessage(err, fmt.Sprintf("run Markdown for %s", filePath))
}
return &ContentPage{
Path: path,
FilePath: filePath,
Data: data,
Doc: *doc,
Breadcrumbs: makeBreadcrumbEntries(path),
}, nil
}
func (s *Site) markdownOptions(filePath, contentVersion string) markdown.Options {
var urlPathPrefix string
if contentVersion != "" {
urlPathPrefix = "/@" + contentVersion + "/"
}
urlPathPrefix = pathpkg.Join(urlPathPrefix, strings.TrimPrefix(pathpkg.Dir(filePath)+"/", "/"))
if urlPathPrefix != "" {
urlPathPrefix += "/"
}
base := s.Base
if base == nil {
base = &url.URL{Path: "/"}
}
return markdown.Options{
Base: base.ResolveReference(&url.URL{Path: urlPathPrefix}),
ContentFilePathToLinkPath: contentFilePathToPath,
Funcs: createMarkdownFuncs(s),
FuncInfo: markdown.FuncInfo{Version: contentVersion},
}
}
// AllContentPages returns a list of all content pages in the site.
func (s *Site) AllContentPages(ctx context.Context, contentVersion string) ([]*ContentPage, error) {
content, err := s.Content.OpenVersion(ctx, contentVersion)
if err != nil {
return nil, err
}
filter := func(path string) bool {
if !isContentPage(path) {
return false
}
return s.checkIsValidPath(path) == nil
}
var pages []*ContentPage
err = WalkFileSystem(content, filter, func(path string) error {
data, err := ReadFile(content, path)
if err != nil {
return err
}
page, err := s.newContentPage(path, data, contentVersion)
if err != nil {
return err
}
pages = append(pages, page)
return nil
})
return pages, err
}
// ResolveContentPage looks up the content page at the given version and path (which generally comes
// from a URL). The path may omit the ".md" file extension and the "/index" or "/index.md" suffix.
//
// If the resulting ContentPage differs from the path argument, the caller should (if possible)
// communicate a redirect.
func (s *Site) ResolveContentPage(ctx context.Context, contentVersion, path string) (*ContentPage, error) {
if err := s.checkIsValidPath(path); err != nil {
return nil, err
}
content, err := s.Content.OpenVersion(ctx, contentVersion)
if err != nil {
return nil, err
}
filePath, data, err := resolveAndReadAll(content, path)
if err != nil {
return nil, err
}
return s.newContentPage(filePath, data, contentVersion)
}
func (s *Site) checkIsValidPath(path string) error {
if s.ContentExcludePattern == nil || !s.ContentExcludePattern.MatchString(path) {
return nil
}
return &os.PathError{Op: "open", Path: path, Err: errors.New("path is excluded")}
}
// PageData is the data available to the HTML template used to render a page.
type PageData struct {
ContentVersion string // content version string requested
ContentPagePath string // content page path requested
ContentVersionNotFoundError bool // whether the requested version was not found
ContentPageNotFoundError bool // whether the requested content page was not found
// Content is the content page, when it is found.
Content *ContentPage
}
// RenderContentPage renders a content page using the template.
func (s *Site) RenderContentPage(page *PageData) ([]byte, error) {
templates, err := s.GetResources("templates", page.ContentVersion)
if err != nil {
return nil, err
}
tmpl, err := s.getTemplate(templates, documentTemplateName, template.FuncMap{
"markdown": func(page ContentPage) template.HTML {
return template.HTML(page.Doc.HTML)
},
})
if err != nil {
return nil, err
}
var buf bytes.Buffer
if err := tmpl.Execute(&buf, page); err != nil {
return nil, err
}
return buf.Bytes(), nil
}