web frontend for git repositories, written in Go git.pocka.jp/legit.git
3

Configure Feed

Select the types of activity you want to include in your feed.

Fix relative links in README markdown fails to resolve

Shota FUJI (Oct 23, 2025, 9:01 PM +0900) 9eb33dfd 4efb241d

+50 -8
+50 -8
routes/routes.go
··· 1 1 package routes 2 2 3 3 import ( 4 + "bytes" 4 5 "compress/gzip" 5 6 "fmt" 6 7 "html/template" 7 8 "log" 8 9 "net/http" 10 + "net/url" 9 11 "os" 10 12 "path/filepath" 11 13 "sort" ··· 115 117 return 116 118 } 117 119 120 + mainBranch, err := gr.FindMainBranch(d.c.Repo.MainBranch) 121 + if err != nil { 122 + d.Write500(w) 123 + log.Println(err) 124 + return 125 + } 126 + 118 127 var readmeContent template.HTML 119 128 for _, readme := range d.c.Repo.Readme { 120 129 ext := filepath.Ext(readme) ··· 122 131 if len(content) > 0 { 123 132 switch ext { 124 133 case ".md", ".mkd", ".markdown": 134 + parser := blackfriday.New(blackfriday.WithExtensions(blackfriday.CommonExtensions)) 135 + tree := parser.Parse([]byte(content)) 136 + tree.Walk(func(node *blackfriday.Node, entering bool) blackfriday.WalkStatus { 137 + if !entering { 138 + return blackfriday.GoToNext 139 + } 140 + 141 + if node.Type == blackfriday.Link { 142 + href := string(node.LinkData.Destination) 143 + parsedURL, err := url.Parse(href) 144 + if err == nil && parsedURL.Host != "" { 145 + // Full URL, skip. 146 + return blackfriday.GoToNext 147 + } 148 + 149 + if strings.IndexByte(href, '/') == 0 { 150 + // Repository index page is the "root" for repository's README file. 151 + href = "." + href 152 + } 153 + 154 + if strings.LastIndexByte(href, '/') == len(href)-1 { 155 + node.LinkData.Destination = fmt.Appendf([]byte{}, "/%s/tree/%s/%s", name, mainBranch, href) 156 + node.LinkData.Destination = node.LinkData.Destination[:len(node.LinkData.Destination)-1] 157 + } else { 158 + node.LinkData.Destination = fmt.Appendf([]byte{}, "/%s/blob/%s/%s", name, mainBranch, href) 159 + } 160 + 161 + } 162 + 163 + return blackfriday.GoToNext 164 + }) 165 + 166 + var writer bytes.Buffer 167 + renderer := blackfriday.NewHTMLRenderer(blackfriday.HTMLRendererParameters{}) 168 + renderer.RenderHeader(&writer, tree) 169 + tree.Walk(func(node *blackfriday.Node, entering bool) blackfriday.WalkStatus { 170 + return renderer.RenderNode(&writer, node, entering) 171 + }) 172 + renderer.RenderFooter(&writer, tree) 173 + 125 174 unsafe := blackfriday.Run( 126 - []byte(content), 175 + writer.Bytes(), 127 176 blackfriday.WithExtensions(blackfriday.CommonExtensions), 128 177 ) 129 178 html := bluemonday.UGCPolicy().SanitizeBytes(unsafe) ··· 140 189 141 190 if readmeContent == "" { 142 191 log.Printf("no readme found for %s", name) 143 - } 144 - 145 - mainBranch, err := gr.FindMainBranch(d.c.Repo.MainBranch) 146 - if err != nil { 147 - d.Write500(w) 148 - log.Println(err) 149 - return 150 192 } 151 193 152 194 tpath := filepath.Join(d.c.Dirs.Templates, "*")