/
docs.go
37 lines (32 loc) · 984 Bytes
/
docs.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
package actions
import (
"fmt"
"strings"
"github.com/gobuffalo/buffalo"
"github.com/pkg/errors"
)
const docsRepoBase = "https://github.com/gobuffalo/docs/edit/master/templates/"
// Docs serves documentation pages
func Docs(c buffalo.Context) error {
languages, ok := c.Value("languages").([]string)
if !ok {
return errors.New("could not get user languages")
}
// try to get the docs for language in URL, then fallback to default one (en)
name := strings.TrimRight(c.Param("name"), "/")
for i, lang := range []string{languages[0], "en"} {
for _, ext := range []string{"md", "html"} {
f := fmt.Sprintf("%s/docs/%s.%s", lang, name, ext)
if r.TemplatesBox.Has(f) {
c.Set("sourceRoot", docsRepoBase)
l := "docs-layout.html"
if c.Param("_indexing") == "true" {
l = "search-layout.html"
}
c.Set("is_fallback_version", i > 0)
return c.Render(200, r.HTML(f, l))
}
}
}
return c.Error(404, errors.Errorf("could not find %s", name))
}