internal/httpd

namelast commitupdated
account.go web: Settings gets a labelled rail row; tidy the account cell 1 month ago
accounts.go web: the login page tells a new visitor how to register 1 month ago
activity.go web: the owner page dispatches profile show 1 month ago
api.go help: filter by prefix, and document every command's arguments 1 month ago
apilimit.go httpd: rate limit the JSON API 1 month ago
apilimit_test.go httpd: rate limit the JSON API 1 month ago
apiread.go httpd: GET /api/v1/read for conditional reads 1 month ago
badge.go web: a dashboard that answers "what needs me", badges, and author names 1 month ago
buildpages_test.go build: render tests for both build pages 1 month ago
builds.go build: a jobs command, and build pages that dispatch it 1 month ago
control.go web: the owner page dispatches profile show 1 month ago
diff.go web: keep an added line's leading dash in the diff 1 month ago
diff_test.go web: keep an added line's leading dash in the diff 1 month ago
facts.go Collapse a contributor's several addresses into one row 1 month ago
feed.go web: a dashboard that answers "what needs me", badges, and author names 1 month ago
issueactions.go web: triage issues from the browser 1 month ago
langs.go web: repository facts on the code page 1 month ago
lfs.go Git LFS: batch API, basic transfers, SSH-minted tokens 1 month ago
mractions.go mr: retarget an open merge request onto another branch 1 month ago
mrpage_test.go Merge requests say when things happened, and who resolved them 1 month ago
orgrender_test.go bodies: choose markdown or org per body 1 month ago
orgweb.go web: run an organization from its page 1 month ago
page.go web: the login page names the host, not the display title 1 month ago
pages.go Custom domains for pages 1 month ago
palette_test.go web: chroma line numbers use the site gutter colour 1 month ago
readme.go web: the owner page dispatches profile show 1 month ago
releaseactions.go web: create and edit releases, trigger builds 1 month ago
routes.go mr: retarget an open merge request onto another branch 1 month ago
routes_test.go Git LFS: batch API, basic transfers, SSH-minted tokens 1 month ago
settings.go Report dependencies that have fallen behind upstream 1 month ago
smart.go httpd: rate limit the JSON API 1 month ago
web.go Merge requests say when things happened, and who resolved them 1 month ago
wiki.go control: wiki list and wiki show 1 month ago
package httpd

import (
	"html/template"
	"path"
	"strings"

	"golang.org/x/net/html"
	"golang.org/x/net/html/atom"

	"gitbay.org/gitbay/internal/gitutil"
)

// rewriteRelativeLinks makes relative hrefs and srcs in rendered repo
// content resolve on the forge: links go to blob pages, images to raw.
// go-org exports .org links as .html, so an .html target whose .org (or
// .md) source exists in the tree maps back to the source file.
func rewriteRelativeLinks(rendered template.HTML, p repoPage, baseDir string) template.HTML {
	ctx := &html.Node{Type: html.ElementNode, Data: "div", DataAtom: atom.Div}
	nodes, err := html.ParseFragment(strings.NewReader(string(rendered)), ctx)
	if err != nil {
		return rendered
	}
	var walk func(*html.Node)
	walk = func(n *html.Node) {
		if n.Type == html.ElementNode {
			for i, a := range n.Attr {
				isHref := a.Key == "href" && n.Data == "a"
				isSrc := a.Key == "src" && (n.Data == "img" || n.Data == "video" || n.Data == "source")
				if !isHref && !isSrc {
					continue
				}
				v := a.Val
				if v == "" || strings.Contains(v, "://") || strings.HasPrefix(v, "/") ||
					strings.HasPrefix(v, "#") || strings.HasPrefix(v, "mailto:") ||
					strings.HasPrefix(v, "data:") {
					continue
				}
				target := path.Clean(path.Join(baseDir, v))
				if strings.HasPrefix(target, "..") {
					continue
				}
				if isSrc {
					n.Attr[i].Val = "/" + p.Repo.Path() + "/raw/" + p.Ref + "/" + target
					continue
				}
				// .html from org/markdown exports maps back to the source.
				if strings.HasSuffix(target, ".html") {
					stem := strings.TrimSuffix(target, ".html")
					for _, ext := range []string{".org", ".md"} {
						if _, err := gitutil.ReadBlob(p.Dir, p.Ref, stem+ext, 1); err == nil {
							target = stem + ext
							break
						}
					}
				}
				n.Attr[i].Val = "/" + p.Repo.Path() + "/blob/" + p.Ref + "/" + target
			}
		}
		for c := n.FirstChild; c != nil; c = c.NextSibling {
			walk(c)
		}
	}
	var out strings.Builder
	for _, n := range nodes {
		walk(n)
		if err := html.Render(&out, n); err != nil {
			return rendered
		}
	}
	return template.HTML(out.String())
}