Migration This is my new public git source viewer. Source coming soon. Please contact with concerns/bugs. Contact me

internal/modules/readme.go

3a50036d96eb14462e96598fd169d1c79264ac4b · 1.1 KB · 52 lines raw

1 package modules
2
3 import (
4 "html/template"
5 "strings"
6
7 "gsg/internal/core"
8 )
9
10 type Readme struct {
11 Name string
12 HTML template.HTML
13 }
14
15 var readmeNames = []string{"README.md", "readme.md", "README.markdown", "README", "readme"}
16
17 func init() { core.Register("readme", readmeMod) }
18
19 // readme: first README found at Ctx.Path, rendered.
20 // Returns nil (not an error) when absent, so `with` in a template just skips it.
21 func readmeMod(c *core.Ctx, args ...string) (any, error) {
22 rev := c.Commit
23 if rev == "" {
24 rev = c.Ref
25 }
26 entries, err := c.Repo.Tree(rev, c.Path)
27 if err != nil {
28 return nil, nil
29 }
30 present := map[string]bool{}
31 for _, e := range entries {
32 if e.Type == "blob" {
33 present[e.Name] = true
34 }
35 }
36 for _, name := range readmeNames {
37 if !present[name] {
38 continue
39 }
40 full := strings.TrimPrefix(c.Path+"/"+name, "/")
41 src, err := c.Repo.Blob(rev, full)
42 if err != nil {
43 continue
44 }
45 out, err := Markdown(src, "/"+c.Info.Slug, c.Ref, c.Path)
46 if err != nil {
47 return nil, err
48 }
49 return Readme{Name: name, HTML: out}, nil
50 }
51 return nil, nil
52 }