prefetch main repo metadata
1 file changed, 48 insertions(+), 2 deletions(-)
changed files
M routes/util.go → routes/util.go
@@ -2,6 +2,7 @@ package routes import ( "bufio" + "fmt" "log" "os" "path"@@ -12,7 +13,14 @@ "alin.ovh/elgit/data" "alin.ovh/elgit/git" securejoin "github.com/cyphar/filepath-securejoin" + "github.com/microcosm-cc/bluemonday" + "github.com/russross/blackfriday/v2" "github.com/savsgio/atreugo/v11" +) + +const ( + RepoIndexCommitCountReadme = 3 + RepoIndexCommitCountNoReadme = 10 ) func isGoModule(gr *git.Repo) bool {@@ -93,10 +101,48 @@ Category: category, Path: fullPath, Slug: project, Description: getDescription(fullPath), + Repo: repo, + Gomod: isGoModule(repo), } - if cc, err := repo.LastCommit(); err == nil { - r.LastCommit = cc.Author.When + + r.MainBranch, err = r.FindMainBranch(d.c.Repo.MainBranch) + if err != nil { + log.Println(err) } + + for _, readme := range d.c.Repo.Readme { + ext := filepath.Ext(readme) + content, _ := r.FileContent(readme) + if len(content) > 0 { + switch ext { + case ".md", ".mkd", ".markdown": + unsafe := blackfriday.Run( + []byte(content), + blackfriday.WithExtensions(blackfriday.CommonExtensions), + ) + html := bluemonday.UGCPolicy().SanitizeBytes(unsafe) + r.ReadmeContent = string(html) + default: + safe := bluemonday.UGCPolicy().SanitizeBytes([]byte(content)) + r.ReadmeContent = fmt.Sprintf(`<pre>%s</pre>`, safe) + } + + break + } + } + + var baseComparison int + if r.ReadmeContent == "" { + baseComparison = RepoIndexCommitCountNoReadme + } else { + baseComparison = RepoIndexCommitCountReadme + } + + if commits, err := repo.Commits(); err == nil { + r.LastCommits = commits[:min(len(commits), baseComparison)] + r.LastCommit = commits[0].Author.When + } + entries.Add(&r) } }