all repos — elgit @ effa6151ae97398a7adcbf0b74f04ba5f800be42

fork of legit: web frontend for git, written in go

render markdown files in file viewer

Alan Pearce
commit

effa6151ae97398a7adcbf0b74f04ba5f800be42

parent

4cb034adf2fd34dbaeae280c30d88601865dc7d8

3 files changed, 71 insertions(+), 43 deletions(-)

changed files
M routes/template.goroutes/template.go
@@ -2,13 +2,17 @@ package routes
import ( "bytes" + "fmt" "io" "log" "net/http" + "path/filepath" "strings" "alin.ovh/elgit/git" "alin.ovh/elgit/templates" + "github.com/microcosm-cc/bluemonday" + "github.com/russross/blackfriday/v2" ) func (d *deps) Write404(w http.ResponseWriter) {
@@ -90,6 +94,22 @@ }
} func (d *deps) showFile(content string, data map[string]any, w http.ResponseWriter) { + var renderedContent string + if len(content) > 0 { + switch filepath.Ext(data["path"].(string)) { + case ".md": + unsafe := blackfriday.Run( + []byte(content), + blackfriday.WithExtensions(blackfriday.CommonExtensions), + ) + html := bluemonday.UGCPolicy().SanitizeBytes(unsafe) + renderedContent = string(html) + default: + safe := bluemonday.UGCPolicy().SanitizeBytes([]byte(content)) + renderedContent = fmt.Sprintf(`<pre>%s</pre>`, safe) + } + } + lc, err := countLines(strings.NewReader(content)) if err != nil { // Non-fatal, we'll just skip showing line numbers in the template.
@@ -104,14 +124,15 @@ }
} pageData := templates.PageData{ - Meta: d.c.Meta, - DisplayName: getDisplayName(data["name"].(string)), - LineCount: lines, - Content: content, - Name: data["name"].(string), - Ref: data["ref"].(string), - Description: data["desc"].(string), - Path: data["path"].(string), + Meta: d.c.Meta, + DisplayName: getDisplayName(data["name"].(string)), + LineCount: lines, + Content: content, + RenderedContent: renderedContent, + Name: data["name"].(string), + Ref: data["ref"].(string), + Description: data["desc"].(string), + Path: data["path"].(string), } if err := templates.FilePage(pageData).Render(w); err != nil {
M templates/file.gotemplates/file.go
@@ -15,22 +15,28 @@ RenderNav(data),
Main( P(g.Text(data.Path), g.Text(" ("), A(Href("?raw=true"), g.Text("view raw")), g.Text(")")), Div(Class("file-wrapper"), - Table( - TBody( - Tr( - Td(Class("line-numbers"), - Pre(g.Map(data.LineCount, func(line int) g.Node { - return g.Group([]g.Node{ - g.Text("\n"), - A( - ID(fmt.Sprintf("L%d", line)), - Href(fmt.Sprintf("#L%d", line)), - g.Text(fmt.Sprintf("%d", line)), - )}) - })), - ), - Td(Class("file-content"), - Pre(g.Text(data.Content)), + g.If(data.RenderedContent != "", + Article( + Class("readme"), + g.Raw(data.RenderedContent), + ), + Table( + TBody( + Tr( + Td(Class("line-numbers"), + Pre(g.Map(data.LineCount, func(line int) g.Node { + return g.Group([]g.Node{ + g.Text("\n"), + A( + ID(fmt.Sprintf("L%d", line)), + Href(fmt.Sprintf("#L%d", line)), + g.Text(fmt.Sprintf("%d", line)), + )}) + })), + ), + Td(Class("file-content"), + Pre(g.Text(data.Content)), + ), ), ), ),
M templates/page.gotemplates/page.go
@@ -12,25 +12,26 @@ . "alin.ovh/gomponents/html"
) type PageData struct { - Meta config.Meta - DisplayName string - Name string - Ref string - Readme string - Description string - Path string - Parent string - Files any - Diff []git.Diff - Stat git.Stat - Commit *git.Commit - Commits []*git.Commit - LineCount []int - Content string - Branches any - Log bool - Servername string - Gomod bool + Meta config.Meta + DisplayName string + Name string + Ref string + Readme string + Description string + Path string + Parent string + Files any + Diff []git.Diff + Stat git.Stat + Commit *git.Commit + Commits []*git.Commit + LineCount []int + Content string + RenderedContent string + Branches any + Log bool + Servername string + Gomod bool } func RenderHead(data PageData) []g.Node {